コード例 #1
0
ファイル: dictionary.py プロジェクト: junhg0211/ibus-hiragana
    def __init__(self, path):
        logger.info("Dictionary(%s)", path)

        self._dict_base = {}
        self._dict = {}

        self._yomi = ''
        self._no = 0
        self._cand = []
        self._numeric = ''
        self._dirty = False

        self._orders_path = ''

        try:
            # Load Katakana dictionary first so that Katakana words come after Kanji words.
            katakana_path = os.path.join(package.get_datadir(), 'katakana.dic')
            self._load_dict(self._dict_base, katakana_path)

            # Load system dictionary
            self._load_dict(self._dict_base, path)
        except Exception as error:
            logger.error(error)

        # Load private dictionary
        self._dict = self._dict_base.copy()

        my_path = os.path.join(package.get_user_datadir(), 'my.dic')
        self._load_dict(self._dict, my_path, 'a+')

        base = os.path.basename(path)
        if base:
            self._orders_path = os.path.join(package.get_user_datadir(), base)
            self._load_dict(self._dict, self._orders_path, 'a+', version_checked=False)
コード例 #2
0
ファイル: main.py プロジェクト: esrille/ibus-hiragana
 def on_edit(self, *args):
     path = self._user_dictionary.get_text().strip()
     path = os.path.join(package.get_user_datadir(), path)
     with open(path, 'a+') as f:
         pass
     try:
         Gtk.show_uri_on_window(None, 'file://' + path, Gdk.CURRENT_TIME)
     except Exception:
         pass
コード例 #3
0
ファイル: dictionary.py プロジェクト: esrille/ibus-hiragana
    def __init__(self, path, user, clear_history=False):
        logger.info(f'Dictionary("{path}", "{user}")')

        self._dict_base = {}
        self._dict = {}

        self._yomi = ''
        self._no = 0
        self._cand = []
        self._order = []
        self._completed = []
        self._numeric = ''
        self._dirty = False
        self._strdcmp = self.strcmp

        self._orders_path = ''

        try:
            # Load Katakana dictionary first so that Katakana words come after Kanji words.
            katakana_path = os.path.join(package.get_datadir(), 'katakana.dic')
            self._load_dict(self._dict_base, katakana_path)
            # Load system dictionary
            self._load_dict(self._dict_base, path)
        except Exception as error:
            logger.error(error)

        # Load private dictionary
        self._dict = self._dict_base.copy()
        if user:
            my_path = os.path.join(package.get_user_datadir(), user)
            self._load_dict(self._dict, my_path, 'a+')

        base = os.path.basename(path)
        if base:
            self._orders_path = os.path.join(package.get_user_datadir(), base)
            if clear_history:
                logger.info('clear_history')
                with open(self._orders_path, 'w') as file:
                    file.write("; " + DICTIONARY_VERSION + "\n")
            self._load_dict(self._dict, self._orders_path, 'a+', version_checked=False)
コード例 #4
0
 def __init__(self):
     datadir = package.get_user_datadir()
     os.makedirs(datadir, 0o700, True)
     os.chmod(datadir, 0o700)
     self.filename = os.path.join(datadir, 'stats.txt')
     stats = dict()
     today = date.today()
     self._reset_stats()
     try:
         with open(self.filename, 'r') as f:
             for line in f:
                 record = line.strip().split(',')
                 try:
                     t = datetime.strptime(record[0], '%Y-%m-%d %H:%M:%S')
                     if MAX_STATS_DAYS <= (datetime.today() - t).days:
                         continue
                     ms = record[2].split(':')
                     if len(ms) != 2:
                         continue
                     duration = int(ms[0]) * 60 + float(ms[1])
                     correct_count = int(record[3])
                     touch_count = int(record[4])
                     if touch_count < correct_count:
                         touch_count = correct_count
                     key = t.strftime('%Y-%m-%d')
                     if key not in stats:
                         stats[key] = (duration, correct_count, touch_count)
                     else:
                         stats[key] = list(
                             map(operator.add, stats[key],
                                 (duration, correct_count, touch_count)))
                 except ValueError:
                     continue
             logger.info(stats)
             for item in sorted(stats.items()):
                 logger.info(item)
                 t = datetime.strptime(item[0], '%Y-%m-%d').date()
                 self._update(t, item[1][0], item[1][1], item[1][2])
                 if t == today:
                     self.today_duration = item[1][0]
                     self.today_correct_count = item[1][1]
                     self.today_touch_count = item[1][2]
             logger.info(self.stats)
     except FileNotFoundError:
         # TODO: Print the version number of the file format
         pass
     self.file = open(self.filename, mode='a')
コード例 #5
0
ファイル: main.py プロジェクト: esrille/ibus-hiragana
def main():
    os.umask(0o077)

    # Create user specific data directory
    user_datadir = package.get_user_datadir()
    os.makedirs(user_datadir, 0o700, True)
    os.chmod(user_datadir, 0o700)  # For logfile created by v0.2.0 or earlier

    if __debug__:
        logging.basicConfig(level=logging.DEBUG)
    else:
        # Create a debug log file
        logfile = os.path.join(user_datadir, package.get_name() + '.log')
        logging.basicConfig(filename=logfile,
                            filemode='w',
                            level=logging.WARNING)

    exec_by_ibus = False
    daemonize = False

    shortopt = "ihd"
    longopt = ["ibus", "help", "daemonize"]

    try:
        opts, args = getopt.getopt(sys.argv[1:], shortopt, longopt)
    except getopt.GetoptError as err:
        print_help(1)

    for o, a in opts:
        if o in ("-h", "--help"):
            print_help(0)
        elif o in ("-d", "--daemonize"):
            daemonize = True
        elif o in ("-i", "--ibus"):
            exec_by_ibus = True
        else:
            sys.stderr.write("Unknown argument: %s\n" % o)
            print_help(1)

    if daemonize:
        if os.fork():
            sys.exit()

    IMApp(exec_by_ibus).run()