|
| 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 |
0 commit comments