-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters