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

import argparse
import colorama
import datetime
import pydbus as dbus
import queue
import cmd
import threading
import functools
import logging
import yaml
import os
import sys
from gi.repository import GLib
from asteroid.module import *
from asteroid import *


def in_glib(fn):
    def wrapper(*args, **kwargs):
        retq = queue.Queue()

        def glibfn():
            retval = fn(*args, **kwargs)
            retq.put(retval)
            return False

        GLib.timeout_add(0, glibfn)
        # If something kills the event loop at this point, we are fucked
        return retq.get()
    return wrapper


class AsteroidCmd(cmd.Cmd):

    prompt = "[" + colorama.Fore.LIGHTBLUE_EX + "asteroid" + \
        colorama.Style.RESET_ALL + "] "

    def __init__(self, asteroid, loop):
        super(AsteroidCmd, self).__init__()
        self.asteroid = asteroid
        self.asteroid.dev.properties_changed.connect(self._changed_callback)
        self.loop = loop
        self.exiting = False
        self.logger = logging.getLogger()

    def _changed_callback(self, name, changed, lst):
        for k, v in changed.items():
            self.logger.debug("Changed %s = %s" % (k, v))

    @in_glib
    def do_battery(self, line):
        """ Fetches and prints the battery level """
        logger.info("Battery = %d" % self.asteroid.battery_level())

    @in_glib
    def do_update_time(self, line):
        """ Updates time on the watch """
        dt = None
        if line:
            dt = datetime.datetime.strptime(line, "%Y-%m-%d %T")
        self.asteroid.update_time(dt)
        if dt:
            logger.info("Set time to " + dt.isoformat(" "))
        else:
            logger.info("Set time local time")

    def do_ipython(self, line):
        import IPython
        IPython.embed()

    def do_exit(self, line):
        self.loop.quit()
        self.exiting = True

    do_EOF = do_exit

    def postcmd(self, stop, line):
        return self.exiting


class CmdThread(threading.Thread):

    def __init__(self, cmd):
        super(CmdThread, self).__init__()
        self.cmd = cmd

    def run(self):
        self.cmd.cmdloop("")<commit_msg>AsteroidCmd: Don't repeat last command on empty line<commit_after>#! /usr/bin/env python3

import argparse
import colorama
import datetime
import pydbus as dbus
import queue
import cmd
import threading
import functools
import logging
import yaml
import os
import sys
from gi.repository import GLib
from asteroid.module import *
from asteroid import *


def in_glib(fn):
    def wrapper(*args, **kwargs):
        retq = queue.Queue()

        def glibfn():
            retval = fn(*args, **kwargs)
            retq.put(retval)
            return False

        GLib.timeout_add(0, glibfn)
        # If something kills the event loop at this point, we are fucked
        return retq.get()
    return wrapper


class AsteroidCmd(cmd.Cmd):

    prompt = "[" + colorama.Fore.LIGHTBLUE_EX + "asteroid" + \
        colorama.Style.RESET_ALL + "] "

    def __init__(self, asteroid, loop):
        super(AsteroidCmd, self).__init__()
        self.asteroid = asteroid
        self.asteroid.dev.properties_changed.connect(self._changed_callback)
        self.loop = loop
        self.exiting = False
        self.logger = logging.getLogger()

    def _changed_callback(self, name, changed, lst):
        for k, v in changed.items():
            self.logger.debug("Changed %s = %s" % (k, v))

    @in_glib
    def do_battery(self, line):
        """ Fetches and prints the battery level """
        logger.info("Battery = %d" % self.asteroid.battery_level())

    @in_glib
    def do_update_time(self, line):
        """ Updates time on the watch """
        dt = None
        if line:
            dt = datetime.datetime.strptime(line, "%Y-%m-%d %T")
        self.asteroid.update_time(dt)
        if dt:
            logger.info("Set time to " + dt.isoformat(" "))
        else:
            logger.info("Set time local time")

    def do_ipython(self, line):
        import IPython
        IPython.embed()

    def do_exit(self, line):
        self.loop.quit()
        self.exiting = True

    do_EOF = do_exit

    def emptyline(self):
        pass

    def postcmd(self, stop, line):
        return self.exiting


class CmdThread(threading.Thread):

    def __init__(self, cmd):
        super(CmdThread, self).__init__()
        self.cmd = cmd

    def run(self):
        self.cmd.cmdloop("")
{'fraction_non_alphanumeric': 0.10994647636039251, 'fraction_numerical': 0.0008920606601248885, 'mean_word_length': 2.327151335311573, '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': '4767599', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1379, 'n_words': 373}