<commit_before>#!/usr/bin/python

import asynchat
import asyncore
import socket

from et_facade import EyeTrackerFacade


class MonkeyFeeder(object):

    _TCP_IP = '127.0.0.1'
    _TCP_PORT = 1080
    _WIDTH = 480
    _HEIGHT = 800

    # Debug option for printing commands without doing anything.
    _DRY_RUN = False

    def __init__(self):
        self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._entered = False
        self._lastx = None
        self._lasty = None

    def __enter__(self):
        if not MonkeyFeeder._DRY_RUN:
            self._s.connect((MonkeyFeeder._TCP_IP, MonkeyFeeder._TCP_PORT))
        EventPubSub.subscribe('data', self.move)
        return self

    def __exit__(self, type, value, traceback):
        self._s.close()
        return False

    def _send_command(self, command):
        if not MonkeyFeeder._DRY_RUN:
            self._s.send(command + '\n')
        print "Sent: ", command

    def move(self, x, y):
        width = MonkeyFeeder._WIDTH
        height = MonkeyFeeder._HEIGHT

        # Mouse is not moved.
        if (x == self._lastx and y == self._lasty):
            return False

        if (x > 0 and x < 1 and y > 0 and y < 1):
            action = 'move' if self._entered else 'enter'
            self._send_command('hover %s %d %d' % (
                               action, x * width, y * height))
            self._entered = True

        elif (self._entered):
            self._send_command('hover move %d %d' % (x * width, y * height))
            self._send_command('hover exit %d %d' % (x * width, y * height))
            self._entered = False

        self._lastx = x
        self._lasty = y

        return True


class MonkeyServer(asyncore.dispatcher):

    _TCP_IP = 'localhost' # Use socket.gethostname() for real device.
    _TCP_PORT = 10800

    def __init__(self):
        asyncore.dispatcher.__init__(self)
        self._handler = None
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)

    def __enter__(self):
        self.bind((MonkeyServer._TCP_IP, MonkeyServer._TCP_PORT))
        self.listen(1)
        print "Listening to", MonkeyServer._TCP_IP, MonkeyServer._TCP_PORT<commit_msg>Changed parameters for use on actual device.<commit_after>#!/usr/bin/python

import asynchat
import asyncore
import socket

from et_facade import EyeTrackerFacade


class MonkeyFeeder(object):

    _TCP_IP = '127.0.0.1'
    _TCP_PORT = 1080
    _WIDTH = 800
    _HEIGHT = 1205

    # Debug option for printing commands without doing anything.
    _DRY_RUN = False

    def __init__(self):
        self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._entered = False
        self._lastx = None
        self._lasty = None

    def __enter__(self):
        if not MonkeyFeeder._DRY_RUN:
            self._s.connect((MonkeyFeeder._TCP_IP, MonkeyFeeder._TCP_PORT))
        EventPubSub.subscribe('data', self.move)
        return self

    def __exit__(self, type, value, traceback):
        self._s.close()
        return False

    def _send_command(self, command):
        if not MonkeyFeeder._DRY_RUN:
            self._s.send(command + '\n')
        print "Sent: ", command

    def move(self, x, y):
        width = MonkeyFeeder._WIDTH
        height = MonkeyFeeder._HEIGHT

        # Mouse is not moved.
        if (x == self._lastx and y == self._lasty):
            return False

        if (x > 0 and x < 1 and y > 0 and y < 1):
            action = 'move' if self._entered else 'enter'
            self._send_command('hover %s %d %d' % (
                               action, x * width, y * height))
            self._entered = True

        elif (self._entered):
            self._send_command('hover move %d %d' % (x * width, y * height))
            self._send_command('hover exit %d %d' % (x * width, y * height))
            self._entered = False

        self._lastx = x
        self._lasty = y

        return True


class MonkeyServer(asyncore.dispatcher):

    _TCP_IP = socket.gethostname() # Use 'localhost' for emulators.
    _TCP_PORT = 10800

    def __init__(self):
        asyncore.dispatcher.__init__(self)
        self._handler = None
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)

    def __enter__(self):
        self.bind((MonkeyServer._TCP_IP, MonkeyServer._TCP_PORT))
        self.listen(1)
        print "Listening to", MonkeyServer._TCP_IP, MonkeyServer._TCP_PORT
{'fraction_non_alphanumeric': 0.10081190798376184, 'fraction_numerical': 0.011953089760938205, 'mean_word_length': 2.1610833927298647, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3359246', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1365, 'n_words': 354}