Ejemplo n.º 1
0
    def _get_dictionary():
        """Load dictionary from file.

        Returns:
            str: the loaded dictionary as string.

        """
        def _truncate_dictionary(mydict):
            """Trim dictionary's last 'legal' section."""
            END_OF_DICT = "End of Project Gutenberg's"
            mydict = mydict[0:mydict.index(END_OF_DICT)].strip()
            return mydict

        def _add_last_dummy_entry(mydict):
            """Add a last dummy entry to the dictionary."""
            LAST_DUMMY_ENTRY = '\n' + 100 * 'Z'
            return mydict + LAST_DUMMY_ENTRY

        getdict_logger = vim_dictionary.instantiate_logger('get_dictionary')
        getdict_logger.debug("Was called.")
        with open(DICTIONARY_PATH, 'rt', encoding='utf8') as f:
            dictionary = f.read()
        dictionary = _truncate_dictionary(dictionary)
        dictionary = _add_last_dummy_entry(dictionary)
        return dictionary
Ejemplo n.º 2
0
def main():
    """Invoke logging and download the dictionary.

    Returns:
        None.

    """
    setup_logging()
    main_logger = instantiate_logger('main_logger')
    main_logger.debug('Started logger.')
    download_dictionary()
Ejemplo n.º 3
0
    def _get_entries(self):
        """Get all entries in the dictionary.

        Arguments:
            dictionary (str): the dictionary full text.

        Returns:
            tuple: a tuple of all the entries in the dictionary. In this case
            their order is important as one entry ends where another starts.

        """
        getentries_logger = vim_dictionary.instantiate_logger('get_entries')
        getentries_logger.debug("Was called.")
        return tuple(self.DICT_ENTRY_REGEX.findall(self._RAW_DICTIONARY))
Ejemplo n.º 4
0
def download_dictionary():
    """Download dictionary to download/ folder.

    Returns:
        None.

    """
    download_logger = instantiate_logger('download_dictionary')
    download_logger.debug("Opening URL '{0}'.".format(DICT_URL))
    response = urllib.request.urlopen(DICT_URL)
    download_logger.debug("Downloading '{0}'.".format(DICT_URL))
    dictionary_data = response.read()
    with open(DICTIONARY_PATH, 'wb') as f:
        f.write(dictionary_data)
        download_logger.debug("Saved to file '{0}'.".format(DICTIONARY_PATH))
Ejemplo n.º 5
0
    def _get_next_different_entry(self, entry):
        """Get next different entry.

        Get next different entry (an entry may have multiple definitions).

        """
        diffentry_logger = vim_dictionary.instantiate_logger(
            '_get_next_different_entry')
        diffentry_logger.debug("Got '{0}'.".format(entry))
        try:
            first_occurrence = self.entries.index(entry)
        except ValueError as error:
            diffentry_logger.debug(
                "'{0}' is not in the dictionary.".format(entry))
            return ''
        i = first_occurrence + 1
        while self.entries[i] == entry:
            i += 1
        return self.entries[i]
def check_server_is_on():
    """Check if server is online."""
    csio_logger = vim_dictionary.instantiate_logger('check_server_is_on')
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        while True:
            try:
                csio_logger.debug('Trying to connect to socket.')
                sock.connect((HOST, PORT))
                break
            except ConnectionRefusedError:
                csio_logger.debug('Got a ConnectionRefusedError exception.')
                return False
        csio_logger.debug('Connected.')
        message = json.dumps(['-1', '!is_alive'])
        sock.sendall(bytes(message, 'utf8'))
        response = str(sock.recv(1024), 'ascii')
        if response == 'TRUE':
            csio_logger.debug('Server is alive.')
            return True
        else:
            csio_logger.debug('Server is not alive.')
            return False
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
    """Server that handles dictionary requests by vim and lookups."""

    tcpreqhan_logger = vim_dictionary.instantiate_logger('tcpreqhan')
    tcpreqhan_logger.debug("Initializing 'ThreadedTCPRequestHandler' class.")

    def handle(self):
        """Receive and send data from vim."""

        self.dictionary = vim_dictionary.get_dictionary()

        self.tcpreqhan_logger.debug("Started log in 'handle'.")
        while True:
            # Data receiving part.
            try:
                data = self.request.recv(4096).decode('utf-8')
                self.tcpreqhan_logger.debug(
                    "Got data: '{0}'.".format(repr(data)))
            except socket.error:
                self.tcpreqhan_logger.debug(
                    "Socket error: 'socket.error'.")
                break
            except IOError:
                self.tcpreqhan_logger.debug(
                    "Socket error: 'IOError'.")
                break
            if data == '':
                self.tcpreqhan_logger.debug("Socket error: 'empty data'.")
                break
            self.tcpreqhan_logger.debug(
                "Socket received: '{0}'.".format(repr(data)))
            try:
                decoded = map(lambda x: (int(x[0]), x[1]),
                              map(json.loads, data.splitlines()))
            except ValueError:
                self.tcpreqhan_logger.debug(
                    "Json decoding error: 'ValueError'.")
                break

            for code, content in decoded:
                self.tcpreqhan_logger.debug(
                    "Correctly decoded json. Code: '{0}'. Content: '{1}'.".format(
                        code, content))

                # Send a response if the sequence number is positive.
                # Negative numbers are used for "eval" responses.
                if content == '!close':
                    self.tcpreqhan_logger.debug(
                        "Terminating ThreadedTCPRequestHandler.")
                    self.server.shutdown()
                    self.server.server_close()
                    return 1
                elif content == '!is_alive':
                    self.tcpreqhan_logger.debug("Send '!is_alive' signal.")
                    self.request.sendall('TRUE'.encode('utf-8'))
                elif code >= 0:
                    response = self.dictionary.lookup(content)
                    encoded = json.dumps([code, response])
                    # self.tcpreqhan_logger.info("Sleeping for 2 seconds.")
                    # time.sleep(2)
                    try:
                        self.request.sendall(encoded.encode('utf-8'))
                    except BrokenPipeError as exp:
                        print(exp)
                    self.tcpreqhan_logger.info("Sending: '{0}'.".format(encoded))
        message = json.dumps(['-1', '!is_alive'])
        sock.sendall(bytes(message, 'utf8'))
        response = str(sock.recv(1024), 'ascii')
        if response == 'TRUE':
            csio_logger.debug('Server is alive.')
            return True
        else:
            csio_logger.debug('Server is not alive.')
            return False


if __name__ == '__main__':

    # Start logging.
    vim_dictionary.setup_logging()
    server_logger = vim_dictionary.instantiate_logger(
        os.path.basename(__file__).strip('.py'))
    server_logger.debug('Is inside main.')
    server_logger.debug(
        "Was initialized with arguments: '{0}'".format(' '.join(sys.argv)))

    # Instantiate server.
    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    ip, port = server.server_address
    server_logger.debug("Listening on port {0}".format(PORT))

    # Start server.
    server.serve_forever()

    # Finish server gracefully.
    server_logger.debug("Finished '{0}'.".format(__file__))
    server.shutdown()
Ejemplo n.º 9
0
 def lookup(self, entry):
     lookup_logger = vim_dictionary.instantiate_logger('Lookup')
     lookup_logger.debug("Looking up: '{0}'".format(entry))
     return self._lookup(entry)