From 4aa7eb8608272f26a8f786e921dc7922d4e87745 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 12 Jul 2021 15:32:30 +0200 Subject: [PATCH] feat(config): config folder on home .config --- .gitignore | 2 -- README.md | 14 +++++++++++--- SyntaxAutoFix/syntaxautofix.py | 27 ++++++++++++++++++++------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 146ad0c..cf58fea 100644 --- a/.gitignore +++ b/.gitignore @@ -131,5 +131,3 @@ pip-selfcheck.json # End of https://www.gitignore.io/api/python - -stats.json diff --git a/README.md b/README.md index 684ea76..b8f42d3 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,15 @@ For the UI is required PyQt5. Doesn't create conflicts with [Espanso](https://espanso.org/). -## How to use with Target Language(s) +### Settings and assets files (dictionaries) + +Automatically will create a folder inside '~/.config/SyntaxAutoFix` that will include the stats.json files. +Also will look for the dictionaries and settings also on this folder so this let you customize it without define commandline parameters. + +## Usage + +### How to use with Target Language(s) + - For English only ``` syntaxautofix -words ./words/en.json @@ -30,7 +38,7 @@ syntaxautofix -words ./words/en.json -words2 ./words/it.json syntaxautofix -configini /path/config.ini ``` -## Adding New Terms +### Adding New Terms This require the git repo or the right path to the pip package folder. @@ -40,7 +48,7 @@ manageterms.py -wrong="wdiget" -right="widget" -lang=en You can use also an ui for that: `manageterms-gui.py` -## Adding New Terms using CSV File +### Adding New Terms using a CSV File This require the git repo or the right path to the pip package folder. diff --git a/SyntaxAutoFix/syntaxautofix.py b/SyntaxAutoFix/syntaxautofix.py index 765de66..fd9bd89 100755 --- a/SyntaxAutoFix/syntaxautofix.py +++ b/SyntaxAutoFix/syntaxautofix.py @@ -11,8 +11,21 @@ import json script_path = os.path.dirname(os.path.realpath(__file__)) -config_parser = ConfigParser() +def getHomePath(): + user = os.getenv("SUDO_USER") or os.getenv("USER") + home = os.path.join(os.path.expanduser('~' + user), '.config/SyntaxAutoFix') + if not os.path.exists(home): + os.mkdir(home) + return home + +def getAssetPath(path): + home = getHomePath() + complete_path = os.path.join(home, path) + if not os.path.exists(complete_path): + complete_path = os.path.join(script_path, path) + return complete_path +config_parser = ConfigParser() # Load words def loadWord(filename): @@ -23,14 +36,14 @@ def loadWord(filename): # Parse argument parser = argparse.ArgumentParser(description='Scan your digited letter for wrong words and alert you!') -parser.add_argument('-config', dest='configini', nargs='?', default=os.path.join(script_path, 'config.ini'), type=str) -parser.add_argument('-words', dest='words_file', nargs='?', default=os.path.join(script_path, 'words/en.json'), type=str) -parser.add_argument('-words2', dest='words_file2', nargs='?', default=os.path.join(script_path, 'words/it.json'), type=str) +parser.add_argument('-config', dest='configini', nargs='?', default=getAssetPath('config.ini'), type=str) +parser.add_argument('-words', dest='words_file', nargs='?', default=getAssetPath('words/en.json'), type=str) +parser.add_argument('-words2', dest='words_file2', nargs='?', default=getAssetPath('words/it.json'), type=str) args = parser.parse_args() config_parser.read(args.configini) LIST_OF_FILES = json.loads(config_parser.get('DEFAULT', 'words_file')) -WORDS_FILE_DEFAULT_LOCATION = [os.path.join(script_path, file_path) for file_path in LIST_OF_FILES] +WORDS_FILE_DEFAULT_LOCATION = [getAssetPath(file_path) for file_path in LIST_OF_FILES] # it holds the files name passed and the stat os file files = {} @@ -43,7 +56,8 @@ def mispell_callback(): list_splitted = recorded_words_list[0].split() if len(list_splitted) > 0: wrong_word = list_splitted[-1] - save_stats_file(os.path.join(script_path, "stats.json"), wrong_word, 1) + print("Word '" + wrong_word + "' detected and tracked") + save_stats_file(os.path.join(getHomePath(), "stats.json"), wrong_word, 1) keyboard.start_recording() @@ -70,7 +84,6 @@ def loadJSON(): for (correct, wrongs) in words.items(): for wrong in wrongs: if wrong != '': - print('Loaded ' + wrong + ' with as: ' + correct) keyboard.add_abbreviation(wrong, ' ' + correct + ' ') keyboard.add_word_listener(wrong, mispell_callback)