<commit_before>import os
import socket
import threading
import Queue
import SocketServer
import time
from robot.libraries.Process import Process
from robot.libraries.Remote import Remote
from robot.running import EXECUTION_CONTEXTS
from robot.running.namespace import IMPORTER
from robot.running.testlibraries import TestLibrary
from robot.libraries.BuiltIn import BuiltIn
REMOTE_AGENTS = Queue.Queue()

class SimpleServer(SocketServer.BaseRequestHandler):

    def handle(self):
        data = b''.join(iter(self.read_socket, b''))
        REMOTE_AGENTS.put(data.decode())
        self.request.sendall(data)

    def read_socket(self):
        return self.request.recv(1)

class InvalidURLException(Exception):
    pass


class _RobotImporterWrapper(object):
    def remove_library(self, name, args):
        lib = TestLibrary(name, args, None, create_handlers=False)
        key = (name, lib.positional_args, lib.named_args)
        self._remove_library(key)

    def _remove_library(self, key):
        raise NotImplementedError()


class Robot26ImporterWrapper(_RobotImporterWrapper):
    def _remove_library(self, key):
        if key in IMPORTER._library_cache:
            index = IMPORTER._library_cache._keys.index(key)
            IMPORTER._library_cache._keys.pop(index)
            IMPORTER._library_cache._items.pop(index)


class OldRobotImporterWrapper(_RobotImporterWrapper):
    def _remove_library(self, key):
        if IMPORTER._libraries.has_key(key):  # key in dict doesn't work here
            index = IMPORTER._libraries._keys.index(key)
            IMPORTER._libraries._keys.pop(index)
            IMPORTER._libraries._libs.pop(index)


class RobotLibraryImporter(object):
    """Class for manipulating Robot Framework library imports during runtime"""

    def re_import_rappio(self):
        name = 'Rappio'
        self._remove_lib_from_current_namespace(name)
        self._import_wrapper().remove_library(name, [])
        BuiltIn().import_library(name)

    def _import_wrapper(self):
        if hasattr(IMPORTER, '_library_cache'):
            return Robot26ImporterWrapper()
        return OldRobotImporterWrapper()

    def _remove_lib_from_current_namespace(self, name):
        testlibs = EXECUTION_CONTEXTS.current.namespace._testlibs
        if testlibs.has_key(name):
            del(testlibs[name])


class Rappio(object):

    ROBOT_LIBRARY_SCOPE = 'SUITE'
    KEYWORDS = ['start_application', 'application_started', 'switch_to_application', 'stop_application']
    REMOTES = {}
    CURRENT = None
    PROCESS = Process()
    ROBOT_NAMESPACE_BRIDGE = RobotLibraryImporter()
    TIMEOUT = 60
    PORT = None

    def __init__(self, port=None):
        if Rappio.PORT is None:
            if port is None:
                Rappio.PORT = self.get_open_port()
            else:
                Rappio.PORT = int(port)            
                address = ('127.0.0.1', Rappio.PORT)
                server = SocketServer.TCPServer(address, SimpleServer)
                server.allow_reuse_address = True
                t = threading.Thread(target=server.serve_forever)
                t.daemon = True # don't hang on exit
                t.start()            
        self.set_env()


    def get_open_port(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(("",0))
        s.listen(1)
        port = s.getsockname()[1]
        s.close()
        return port       

    @property
    def current(self):
        if not self.CURRENT:
            return None
        return self.REMOTES[self.CURRENT]

    def set_env(self):
        if not Rappio.PORT:
            raise Exception("Port is not defined!")
        os.environ['JAVA_TOOL_OPTIONS'] = \
            '-javaagent:robotframework-rappio-1.0-SNAPSHOT-jar-with-dependencies.jar=%s' % Rappio.PORT

    def start_application(self, alias, command, timeout=60):
        self.PROCESS.start_process(command, alias=alias, shell=True)
        self.application_started(alias, timeout=timeout)

    def application_started(self, alias, timeout=60):
        self.TIMEOUT = int(timeout)
        port = REMOTE_AGENTS.get(True, self.TIMEOUT);<commit_msg>Fixed bug when port parameter is not passed to library<commit_after>import os
import socket
import threading
import Queue
import SocketServer
import time
from robot.libraries.Process import Process
from robot.libraries.Remote import Remote
from robot.running import EXECUTION_CONTEXTS
from robot.running.namespace import IMPORTER
from robot.running.testlibraries import TestLibrary
from robot.libraries.BuiltIn import BuiltIn
REMOTE_AGENTS = Queue.Queue()

class SimpleServer(SocketServer.BaseRequestHandler):

    def handle(self):
        data = b''.join(iter(self.read_socket, b''))
        REMOTE_AGENTS.put(data.decode())
        self.request.sendall(data)

    def read_socket(self):
        return self.request.recv(1)

class InvalidURLException(Exception):
    pass


class _RobotImporterWrapper(object):
    def remove_library(self, name, args):
        lib = TestLibrary(name, args, None, create_handlers=False)
        key = (name, lib.positional_args, lib.named_args)
        self._remove_library(key)

    def _remove_library(self, key):
        raise NotImplementedError()


class Robot26ImporterWrapper(_RobotImporterWrapper):
    def _remove_library(self, key):
        if key in IMPORTER._library_cache:
            index = IMPORTER._library_cache._keys.index(key)
            IMPORTER._library_cache._keys.pop(index)
            IMPORTER._library_cache._items.pop(index)


class OldRobotImporterWrapper(_RobotImporterWrapper):
    def _remove_library(self, key):
        if IMPORTER._libraries.has_key(key):  # key in dict doesn't work here
            index = IMPORTER._libraries._keys.index(key)
            IMPORTER._libraries._keys.pop(index)
            IMPORTER._libraries._libs.pop(index)


class RobotLibraryImporter(object):
    """Class for manipulating Robot Framework library imports during runtime"""

    def re_import_rappio(self):
        name = 'Rappio'
        self._remove_lib_from_current_namespace(name)
        self._import_wrapper().remove_library(name, [])
        BuiltIn().import_library(name)

    def _import_wrapper(self):
        if hasattr(IMPORTER, '_library_cache'):
            return Robot26ImporterWrapper()
        return OldRobotImporterWrapper()

    def _remove_lib_from_current_namespace(self, name):
        testlibs = EXECUTION_CONTEXTS.current.namespace._testlibs
        if testlibs.has_key(name):
            del(testlibs[name])


class Rappio(object):

    ROBOT_LIBRARY_SCOPE = 'SUITE'
    KEYWORDS = ['start_application', 'application_started', 'switch_to_application', 'stop_application']
    REMOTES = {}
    CURRENT = None
    PROCESS = Process()
    ROBOT_NAMESPACE_BRIDGE = RobotLibraryImporter()
    TIMEOUT = 60
    PORT = None

    def __init__(self, port=None):
        if Rappio.PORT is None:
            if port is None:
                Rappio.PORT = self.get_open_port()
            else:
                Rappio.PORT = int(port)            
            address = ('127.0.0.1', Rappio.PORT)
            server = SocketServer.TCPServer(address, SimpleServer)
            server.allow_reuse_address = True
            t = threading.Thread(target=server.serve_forever)
            t.daemon = True # don't hang on exit
            t.start()            
        self.set_env()


    def get_open_port(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(("",0))
        s.listen(1)
        port = s.getsockname()[1]
        s.close()
        return port       

    @property
    def current(self):
        if not self.CURRENT:
            return None
        return self.REMOTES[self.CURRENT]

    def set_env(self):
        if not Rappio.PORT:
            raise Exception("Port is not defined!")
        os.environ['JAVA_TOOL_OPTIONS'] = \
            '-javaagent:robotframework-rappio-1.0-SNAPSHOT-jar-with-dependencies.jar=%s' % Rappio.PORT

    def start_application(self, alias, command, timeout=60):
        self.PROCESS.start_process(command, alias=alias, shell=True)
        self.application_started(alias, timeout=timeout)

    def application_started(self, alias, timeout=60):
        self.TIMEOUT = int(timeout)
        port = REMOTE_AGENTS.get(True, self.TIMEOUT);
{'fraction_non_alphanumeric': 0.09514679448771719, 'fraction_numerical': 0.005272618334331935, 'mean_word_length': 2.831955922865014, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3537217', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2487, 'n_words': 504}