<commit_before>#! /usr/bin/env python3

from scancodes import *
import subprocess
from vboxapi import VirtualBoxManager
from v4l2 import *
import numpy as np
import fcntl

from time import sleep
import threading

import os,sys
import traceback

class V4l2Capture(threading.Thread):

    def __init__(self, device, format, display, bmp_fmt):
        self.device = device
        self.format = format
        self.display = display
        self.bmp_fmt = bmp_fmt
        super(V4l2Capture,self).__init__()

    def run(self):

        while True:
            try:
                im = self.display.takeScreenShotToArray(0, self.format.fmt.pix.width, self.format.fmt.pix.height, self.bmp_fmt)
                img=np.fromstring(im)
                self.device.write(img)
                sleep(1./60.)
            except Exception as e:
                sleep(0.1)
                

class VMHandler:
    def __init__(self, vm_name, vidDevName = None):
        
        self.vm_name = vm_name

        # This is a VirtualBox COM/XPCOM API client, no data needed.
        self.wrapper = VirtualBoxManager(None, None)

        # Get the VirtualBox manager
        mgr  = self.wrapper.mgr
        # Get the global VirtualBox object
        vbox = self.wrapper.vbox

        print ("Running VirtualBox version %s" %(vbox.version))

        # Get all constants through the Python wrapper code
        self.vboxConstants = self.wrapper.constants

        self.mach = vbox.findMachine(vm_name)

        # Get the session object
        self.session = mgr.getSessionObject(vbox)

        progress = self.mach.launchVMProcess(self.session, "headless", "")
        progress.waitForCompletion(-1)

        # Our session object is ready at this point, the machine is already locked
        # since we launched it ourselves
        
        if self.mach.state == self.vboxConstants.MachineState_PoweredOff:
            self.session.console.powerUp()

        while self.mach.state != self.vboxConstants.MachineState_Running:
            sleep(0.1)
        
        console = self.session.console

        self.mouse = console.mouse
        self.keyboard = console.keyboard
        self.display = console.display

        # Check if we have a v4l2 device to write to
        if vidDevName != None:
            
            self.device = open(vidDevName, 'w', 0) # Do not forget the 0 for unbuffered mode RGB(A) won't work otherwise

            fmt = V4L2_PIX_FMT_RGB32

            width = 640
            height = 480
        
            self.format = v4l2_format()
            self.format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT
            self.format.fmt.pix.pixelformat = fmt
            self.format.fmt.pix.width = width
            self.format.fmt.pix.height = height
            self.format.fmt.pix.field = V4L2_FIELD_NONE
            self.format.fmt.pix.bytesperline = width * 4
            self.format.fmt.pix.sizeimage = width * height * 4
            self.format.fmt.pix.colorspace = V4L2_COLORSPACE_SRGB

            fcntl.ioctl(self.device, VIDIOC_S_FMT, self.format)

            self.vmDesktopCapture = V4l2Capture(self.device, self.format, self.display, self.vboxConstants.BitmapFormat_RGBA)
            self.vmDesktopCapture.daemon = True
            self.vmDesktopCapture.start() # Run the capturing thread


    def __del__(self):

        if self.mach.state == self.vboxConstants.MachineState_Running:
            # We're done -- don't forget to unlock the machine!
            self.session.unlockMachine()
        
            # Call destructor and delete wrapper
            del self.wrapper
        
        
    def handle_command(self, c):
        
        tks = c.split(" ",1)
        
        if tks[0] == 'TYPE':
            self.handle_type_command(tks[1])

        if tks[0] == 'MOUSEMOVE':
            self.handle_mousemove_command(tks[1])

        if tks[0] == 'MOUSESET':
            self.handle_mouseset_command(tks[1])

        if tks[0] == 'LC':
            self.handle_mouselc_command()

        if tks[0] == 'RC':
            self.handle_mouserc_command()

        if tks[0] == 'MC':
            self.handle_mousemc_command()

        if tks[0] == 'DC':
            self.handle_mousedc_command()

        if tks[0] == 'ML':
            self.handle_ml_command()

        if tks[0] == 'MR':
            self.handle_mr_command()

        if tks[0] == 'MU':
            self.handle_mu_command()

        if tks[0] == 'MD':
            self.handle_md_command()

        if tks[0] == 'MLL':
            self.handle_mll_command()

        if tks[0] == 'MRR':
            self.handle_mrr_command()

        if tks[0] == 'MUU':
            self.handle_muu_command()

        if tks[0] == 'MDD':
            self.handle_mdd_command()

                
    def handle_type_command(self, c):

        for i, tk in enumerate(c):
            try:
                self.keyboard.putScancodes(type_in(tk,qwerty))
                
            except KeyError as ke:
                print("No corresponding scancode(s) to type'" + tk +"'.")

                
    def handle_mousemove_command(self, c):

        tks = c.split(" ")
        if len(tks) != 2:
            print("Wrong number of parameters for MOUSEMOVE.")
            return

        try:
            
            dx = int(tks[0])
            dy = int(tks[1])

            self.mouse.putMouseEvent(dx, dy, 0, 0, 0x00)
            
        except ValueError:
            print("Cannot parse MOUSEMOVE parameters as integers.")


    def handle_mouseset_command(self, c):

        tks = c.split(" ")
        if len(tks) != 2:
            print("Wrong number of parameters for MOUSEMOVE.")
            return

        try:
            
            dx = int(tks[0])
            dy = int(tks[1])

            self.mouse.putMouseEventAbsolute(dx, dy, 0, 0, 0x00)
            self.mouse.putMouseEvent(0, 0, 0, 0, 0x00) # need this so the mouse stays visible
            
        except ValueError:
            print("Cannot parse MOUSESET parameters as integers.")


    def handle_mouselc_command(self):

        self.mouse.putMouseEvent(0, 0, 0, 0, 0x01)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)


    def handle_mouserc_command(self):

        self.mouse.putMouseEvent(0, 0, 0, 0, 0x02)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)

        
    def handle_mousemc_command(self):

        self.mouse.putMouseEvent(0, 0, 0, 0, 0x04)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)


    def handle_mousedc_command(self):

        self.mouse.putMouseEvent(0, 0, 0, 0, 0x01)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)
        sleep(0.05)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x01)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)

        
    def handle_ml_command(self):

        self.mouse.putMouseEvent(-3, 0, 0, 0, 0x00)
        

    def handle_mr_command(self):

        self.mouse.putMouseEvent(3, 0, 0, 0, 0x00)
        

    def handle_mu_command(self):

        self.mouse.putMouseEvent(0, -3, 0, 0, 0x00)
        

    def handle_md_command(self):
        
        self.mouse.putMouseEvent(0, 3, 0, 0, 0x00)


    def handle_mll_command(self):

        self.mouse.putMouseEvent(-15, 0, 0, 0, 0x00)
        

    def handle_mrr_command(self):

        self.mouse.putMouseEvent(15, 0, 0, 0, 0x00)
        

    def handle_muu_command(self):

        self.mouse.putMouseEvent(0, -15, 0, 0, 0x00)
        

    def handle_mdd_command(self):
        
        self.mouse.putMouseEvent(0, 15, 0, 0, 0x00)
<commit_msg>Fixed some print typo<commit_after>#! /usr/bin/env python3

from scancodes import *
import subprocess
from vboxapi import VirtualBoxManager
from v4l2 import *
import numpy as np
import fcntl

from time import sleep
import threading

import os,sys
import traceback

class V4l2Capture(threading.Thread):

    def __init__(self, device, format, display, bmp_fmt):
        self.device = device
        self.format = format
        self.display = display
        self.bmp_fmt = bmp_fmt
        super(V4l2Capture,self).__init__()

    def run(self):

        while True:
            try:
                im = self.display.takeScreenShotToArray(0, self.format.fmt.pix.width, self.format.fmt.pix.height, self.bmp_fmt)
                img=np.fromstring(im)
                self.device.write(img)
                sleep(1./60.)
            except Exception as e:
                sleep(0.1)
                

class VMHandler:
    def __init__(self, vm_name, vidDevName = None):
        
        self.vm_name = vm_name

        # This is a VirtualBox COM/XPCOM API client, no data needed.
        self.wrapper = VirtualBoxManager(None, None)

        # Get the VirtualBox manager
        mgr  = self.wrapper.mgr
        # Get the global VirtualBox object
        vbox = self.wrapper.vbox

        print ("Running VirtualBox version %s" %(vbox.version))

        # Get all constants through the Python wrapper code
        self.vboxConstants = self.wrapper.constants

        self.mach = vbox.findMachine(vm_name)

        # Get the session object
        self.session = mgr.getSessionObject(vbox)

        progress = self.mach.launchVMProcess(self.session, "headless", "")
        progress.waitForCompletion(-1)

        # Our session object is ready at this point, the machine is already locked
        # since we launched it ourselves
        
        if self.mach.state == self.vboxConstants.MachineState_PoweredOff:
            self.session.console.powerUp()

        while self.mach.state != self.vboxConstants.MachineState_Running:
            sleep(0.1)
        
        console = self.session.console

        self.mouse = console.mouse
        self.keyboard = console.keyboard
        self.display = console.display

        # Check if we have a v4l2 device to write to
        if vidDevName != None:
            
            self.device = open(vidDevName, 'w', 0) # Do not forget the 0 for unbuffered mode RGB(A) won't work otherwise

            fmt = V4L2_PIX_FMT_RGB32

            width = 640
            height = 480
        
            self.format = v4l2_format()
            self.format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT
            self.format.fmt.pix.pixelformat = fmt
            self.format.fmt.pix.width = width
            self.format.fmt.pix.height = height
            self.format.fmt.pix.field = V4L2_FIELD_NONE
            self.format.fmt.pix.bytesperline = width * 4
            self.format.fmt.pix.sizeimage = width * height * 4
            self.format.fmt.pix.colorspace = V4L2_COLORSPACE_SRGB

            fcntl.ioctl(self.device, VIDIOC_S_FMT, self.format)

            self.vmDesktopCapture = V4l2Capture(self.device, self.format, self.display, self.vboxConstants.BitmapFormat_RGBA)
            self.vmDesktopCapture.daemon = True
            self.vmDesktopCapture.start() # Run the capturing thread


    def __del__(self):

        if self.mach.state == self.vboxConstants.MachineState_Running:
            # We're done -- don't forget to unlock the machine!
            self.session.unlockMachine()
        
            # Call destructor and delete wrapper
            del self.wrapper
        
        
    def handle_command(self, c):
        
        tks = c.split(" ",1)
        
        if tks[0] == 'TYPE':
            self.handle_type_command(tks[1])

        if tks[0] == 'MOUSEMOVE':
            self.handle_mousemove_command(tks[1])

        if tks[0] == 'MOUSESET':
            self.handle_mouseset_command(tks[1])

        if tks[0] == 'LC':
            self.handle_mouselc_command()

        if tks[0] == 'RC':
            self.handle_mouserc_command()

        if tks[0] == 'MC':
            self.handle_mousemc_command()

        if tks[0] == 'DC':
            self.handle_mousedc_command()

        if tks[0] == 'ML':
            self.handle_ml_command()

        if tks[0] == 'MR':
            self.handle_mr_command()

        if tks[0] == 'MU':
            self.handle_mu_command()

        if tks[0] == 'MD':
            self.handle_md_command()

        if tks[0] == 'MLL':
            self.handle_mll_command()

        if tks[0] == 'MRR':
            self.handle_mrr_command()

        if tks[0] == 'MUU':
            self.handle_muu_command()

        if tks[0] == 'MDD':
            self.handle_mdd_command()

                
    def handle_type_command(self, c):

        for i, tk in enumerate(c):
            try:
                self.keyboard.putScancodes(type_in(tk,qwerty))
                
            except KeyError as ke:
                print("No corresponding scancode(s) to type'" + tk +"'.")

                
    def handle_mousemove_command(self, c):

        tks = c.split(" ")
        if len(tks) != 2:
            print("Wrong number of parameters for MOUSEMOVE.")
            return

        try:
            
            dx = int(tks[0])
            dy = int(tks[1])

            self.mouse.putMouseEvent(dx, dy, 0, 0, 0x00)
            
        except ValueError:
            print("Cannot parse MOUSEMOVE parameters as integers.")


    def handle_mouseset_command(self, c):

        tks = c.split(" ")
        if len(tks) != 2:
            print("Wrong number of parameters for MOUSESET.")
            return

        try:
            
            dx = int(tks[0])
            dy = int(tks[1])

            self.mouse.putMouseEventAbsolute(dx, dy, 0, 0, 0x00)
            self.mouse.putMouseEvent(0, 0, 0, 0, 0x00) # need this so the mouse stays visible
            
        except ValueError:
            print("Cannot parse MOUSESET parameters as integers.")


    def handle_mouselc_command(self):

        self.mouse.putMouseEvent(0, 0, 0, 0, 0x01)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)


    def handle_mouserc_command(self):

        self.mouse.putMouseEvent(0, 0, 0, 0, 0x02)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)

        
    def handle_mousemc_command(self):

        self.mouse.putMouseEvent(0, 0, 0, 0, 0x04)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)


    def handle_mousedc_command(self):

        self.mouse.putMouseEvent(0, 0, 0, 0, 0x01)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)
        sleep(0.05)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x01)
        self.mouse.putMouseEvent(0, 0, 0, 0, 0x00)

        
    def handle_ml_command(self):

        self.mouse.putMouseEvent(-3, 0, 0, 0, 0x00)
        

    def handle_mr_command(self):

        self.mouse.putMouseEvent(3, 0, 0, 0, 0x00)
        

    def handle_mu_command(self):

        self.mouse.putMouseEvent(0, -3, 0, 0, 0x00)
        

    def handle_md_command(self):
        
        self.mouse.putMouseEvent(0, 3, 0, 0, 0x00)


    def handle_mll_command(self):

        self.mouse.putMouseEvent(-15, 0, 0, 0, 0x00)
        

    def handle_mrr_command(self):

        self.mouse.putMouseEvent(15, 0, 0, 0, 0x00)
        

    def handle_muu_command(self):

        self.mouse.putMouseEvent(0, -15, 0, 0, 0x00)
        

    def handle_mdd_command(self):
        
        self.mouse.putMouseEvent(0, 15, 0, 0, 0x00)

{'fraction_non_alphanumeric': 0.10314766753177614, 'fraction_numerical': 0.028881346908897317, 'mean_word_length': 1.8280015054572827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4747511', 'n_tokens_mistral': 5149, 'n_tokens_neox': 4736, 'n_words': 1071}