Skip to content

Commit 7678a1f

Browse files
authored
[feature] - Add keyimport to import automatically from autokey (#42)
* 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
1 parent e67769c commit 7678a1f

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

quikey/importer.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import logging
3+
import json
4+
5+
def PhraseFind(location):
6+
filedict = {}
7+
for r, d, f in os.walk(location):
8+
for file in f:
9+
if ".txt" in file:
10+
filepath = os.path.join(r, file)
11+
filejson = os.path.join(r, "." + file[:-4] + ".json")
12+
#if filejson doesn't exist, skip that file on import
13+
if not os.path.isfile(filejson):
14+
logging.error("json config file does not exist for %s ... Skipping import on this key!" % filepath)
15+
continue
16+
else:
17+
with open(filejson) as openfile:
18+
try:
19+
filedata = json.load(openfile)
20+
except json.JSONDecodeError:
21+
logging.error("Invalid json detected on %s. Skipping import on this key." % filejson)
22+
continue
23+
#check if the 'type' setting is a phrase. If it isn't then skip it
24+
if (filedata.get('type') == "phrase"):
25+
modes = filedata.get('modes')
26+
#modes in autohotkey are 1 for abbreviation, 3 for hotkey. Use this to check what we want to message
27+
if sum(modes) == 1:
28+
abbreviation = filedata.get('abbreviation', {}).get('abbreviations')
29+
logging.info('Importing %s.' % (filepath))
30+
elif sum(modes) == 4:
31+
abbreviation = filedata.get('abbreviation', {}).get('abbreviations')
32+
logging.warning('Modes for %s are both abbreviation and hotkey. Using abbreviation for import' % filepath)
33+
elif sum(modes) == 3:
34+
#there are too many invalid hotkeys, such as F keys. Skip these for now
35+
logging.warning('Mode for %s is hotkey. Please manually add this phrase with an abbreviation, or change from hotkey to abbreviation.' % filepath)
36+
continue
37+
else:
38+
logging.error('Could not auto-detect mode for %s - please manually import this phrase.' % filepath)
39+
continue
40+
with open(filepath) as phrasefile:
41+
value = phrasefile.read()
42+
if len(abbreviation) > 1:
43+
print("Multiple abbreviations were found. Please select which number you would like to use and hit enter.\n")
44+
for entry in abbreviation:
45+
print(1 + abbreviation.index(entry), end=" ")
46+
print(entry)
47+
while True:
48+
try:
49+
abbreviationselection = int(input("Your selection: ")) - 1
50+
key = abbreviation[abbreviationselection]
51+
except IndexError:
52+
print("%s is not a valid selection, please pick a valid entry number." % abbreviationselection)
53+
continue
54+
except ValueError:
55+
print("Selection must be a number, please try again.")
56+
continue
57+
else:
58+
break
59+
else:
60+
key = abbreviation[0]
61+
filedict[key] = value
62+
else:
63+
print("%s is not a 'phrase' type. Skipping import on this key!" % filepath)
64+
continue
65+
return filedict

quikey/quikey.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import humanize
66
import signal
77
import os
8+
import logging
89

910
from quikey.models import Database
1011
from quikey.directories import AppDirectories
1112
from quikey.version import __version__
1213
from quikey.autostart import enableAutostart, disableAutostart
14+
from quikey.importer import PhraseFind
15+
from xdg import BaseDirectory
1316
import subprocess
1417

1518
MARKER = '''
@@ -95,6 +98,35 @@ def ls(ctx, show_all):
9598
output = AsciiTable(table)
9699
click.echo(output.table)
97100

101+
@cli.command()
102+
@click.option('--location', '-l', default=BaseDirectory.xdg_config_home+"/autokey/data/", show_default=True, help='Location of top level directory to import from autokey')
103+
@click.pass_context
104+
def keyimport(ctx,location):
105+
tags = ['autokey-imports']
106+
db = ctx.obj['database']
107+
importfiles = PhraseFind(location)
108+
for key in importfiles:
109+
contents = None
110+
if importfiles[key] is not None:
111+
if db.get(key) is not None:
112+
click.echo('quikey phrase with key of %s already exists' % key)
113+
continue
114+
else:
115+
contents = importfiles[key]
116+
else:
117+
if db.get(key) is not None:
118+
click.echo('quikey phrase with key of %s already exists' % key)
119+
continue
120+
else:
121+
contents = click.edit('\n\n'+MARKER)
122+
if contents is not None:
123+
contents = contents.split(MARKER, 1)[0].rstrip('\n')
124+
else:
125+
click.echo('quikey phrase with key of %s not added' % key)
126+
continue
127+
db.put(key, contents, tags)
128+
click.echo('quikey phrase with key of %s added.' % key)
129+
98130
@cli.command()
99131
def version():
100132
click.echo("quikey %s" % __version__)

0 commit comments

Comments
 (0)