diff --git a/quikey/importer.py b/quikey/importer.py new file mode 100644 index 0000000..a2dccc7 --- /dev/null +++ b/quikey/importer.py @@ -0,0 +1,65 @@ +import os +import logging +import json + +def PhraseFind(location): + filedict = {} + for r, d, f in os.walk(location): + for file in f: + if ".txt" in file: + filepath = os.path.join(r, file) + filejson = os.path.join(r, "." + file[:-4] + ".json") + #if filejson doesn't exist, skip that file on import + if not os.path.isfile(filejson): + logging.error("json config file does not exist for %s ... Skipping import on this key!" % filepath) + continue + else: + with open(filejson) as openfile: + try: + filedata = json.load(openfile) + except json.JSONDecodeError: + logging.error("Invalid json detected on %s. Skipping import on this key." % filejson) + continue + #check if the 'type' setting is a phrase. If it isn't then skip it + if (filedata.get('type') == "phrase"): + modes = filedata.get('modes') + #modes in autohotkey are 1 for abbreviation, 3 for hotkey. Use this to check what we want to message + if sum(modes) == 1: + abbreviation = filedata.get('abbreviation', {}).get('abbreviations') + logging.info('Importing %s.' % (filepath)) + elif sum(modes) == 4: + abbreviation = filedata.get('abbreviation', {}).get('abbreviations') + logging.warning('Modes for %s are both abbreviation and hotkey. Using abbreviation for import' % filepath) + elif sum(modes) == 3: + #there are too many invalid hotkeys, such as F keys. Skip these for now + logging.warning('Mode for %s is hotkey. Please manually add this phrase with an abbreviation, or change from hotkey to abbreviation.' % filepath) + continue + else: + logging.error('Could not auto-detect mode for %s - please manually import this phrase.' % filepath) + continue + with open(filepath) as phrasefile: + value = phrasefile.read() + if len(abbreviation) > 1: + print("Multiple abbreviations were found. Please select which number you would like to use and hit enter.\n") + for entry in abbreviation: + print(1 + abbreviation.index(entry), end=" ") + print(entry) + while True: + try: + abbreviationselection = int(input("Your selection: ")) - 1 + key = abbreviation[abbreviationselection] + except IndexError: + print("%s is not a valid selection, please pick a valid entry number." % abbreviationselection) + continue + except ValueError: + print("Selection must be a number, please try again.") + continue + else: + break + else: + key = abbreviation[0] + filedict[key] = value + else: + print("%s is not a 'phrase' type. Skipping import on this key!" % filepath) + continue + return filedict diff --git a/quikey/quikey.py b/quikey/quikey.py index 2697735..8b8d21e 100755 --- a/quikey/quikey.py +++ b/quikey/quikey.py @@ -5,11 +5,14 @@ import humanize import signal import os +import logging from quikey.models import Database from quikey.directories import AppDirectories from quikey.version import __version__ from quikey.autostart import enableAutostart, disableAutostart +from quikey.importer import PhraseFind +from xdg import BaseDirectory import subprocess MARKER = ''' @@ -95,6 +98,35 @@ def ls(ctx, show_all): output = AsciiTable(table) click.echo(output.table) +@cli.command() +@click.option('--location', '-l', default=BaseDirectory.xdg_config_home+"/autokey/data/", show_default=True, help='Location of top level directory to import from autokey') +@click.pass_context +def keyimport(ctx,location): + tags = ['autokey-imports'] + db = ctx.obj['database'] + importfiles = PhraseFind(location) + for key in importfiles: + contents = None + if importfiles[key] is not None: + if db.get(key) is not None: + click.echo('quikey phrase with key of %s already exists' % key) + continue + else: + contents = importfiles[key] + else: + if db.get(key) is not None: + click.echo('quikey phrase with key of %s already exists' % key) + continue + else: + contents = click.edit('\n\n'+MARKER) + if contents is not None: + contents = contents.split(MARKER, 1)[0].rstrip('\n') + else: + click.echo('quikey phrase with key of %s not added' % key) + continue + db.put(key, contents, tags) + click.echo('quikey phrase with key of %s added.' % key) + @cli.command() def version(): click.echo("quikey %s" % __version__)