Skip to content

Commit

Permalink
[feature] - Add keyimport to import automatically from autokey (#42)
Browse files Browse the repository at this point in the history
* Add framwork for autokey importing functionality. Issue #10 from bostrt/quikey

* add some TODO

* Add filtering to the file list so that we only get files with valid .json configs, and files that are a phrase type

* change from regex to json parsing on autokey import.

* - Change to mode checking on import to see if it's a hotkey or abbreviation. Import with abbreviation for now, since there could be invalid hotkeys we skip hotkey import
- change filefind to phrasefind, since we're actually returning the name+phrase instead of a list of files
- phrasefind now returns a dict of name:phrase that can be imported to quikey

* Added and confirmed autokey import functionality.

* Make requested changes for PR#42

* add check on keyimport to see if a key already exists.

* fix references to 'name' in keyimport
  • Loading branch information
rdmullett authored Aug 3, 2020
1 parent e67769c commit 7678a1f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
65 changes: 65 additions & 0 deletions quikey/importer.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions quikey/quikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''
Expand Down Expand Up @@ -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__)
Expand Down

0 comments on commit 7678a1f

Please sign in to comment.