def load_settings(settings_dir=None, silent=False): if settings_dir is None: settings_dir = get_root_dir() / 'settings' / 'settings.json' try: with open(settings_dir, 'r') as file: try: SettingsParser.settings = load(file) except JSONDecodeError: if not silent: print("settings.json is not readable by the program. \n" + "Make sure settings.json follows the correct standard.") exit(2) except FileNotFoundError: if not silent: print("settings.json not found in settings sub-folder") exit(1) for key, instance in SettingsParser.settings.items(): if not key in SettingsParser.DEFAULT_SETTINGS_STANDARD.keys() or \ not type(instance) is SettingsParser.DEFAULT_SETTINGS_STANDARD[key]: if not silent: print("settings.json has the wrong format. \n" + "Make sure settings.json follows the correct standard.") exit(3)
def save_settings(settings_dir=None, silent=False): if settings_dir is None: settings_dir = get_root_dir() / 'settings' / 'settings.json' try: with open(settings_dir, 'w') as file: dump(SettingsParser.settings, file) except FileNotFoundError: if not silent: print("settings.json not found in settings sub-folder") exit(1) except UnsupportedOperation: if not silent: print("Unable to write instance to file") exit(1)
def load_dictionary(language, language_dir=None, silent=False): if language_dir is None: language_dir = get_root_dir() / 'dicts' / language language_meta_dir = language_dir / 'meta.json' language_list_dir = language_dir / 'dict.txt' try: with open(language_meta_dir) as file: try: DictionaryParser.meta = load(file) except JSONDecodeError: if not silent: print("meta.json is not readable by the program. \n" + "Make sure meta.json follows the correct standard.") exit(5) except FileNotFoundError: if not silent: print(f"meta.json not found in dicts/{language} sub-folder") exit(4) for key, instance in DictionaryParser.meta.items(): if not key in DictionaryParser.DEFAULT_DICT_STANDARD.keys() or \ not type(instance) is DictionaryParser.DEFAULT_DICT_STANDARD[key]: if not silent: print("meta.json has the wrong format. \n" + "Make sure meta.json follows the correct standard.") exit(6) try: with open(language_list_dir) as dictionary: DictionaryParser.wordlist = [word.rstrip().upper() for word in dictionary] except FileNotFoundError: if not silent: print(f"dict.txt not found in dicts/{language} sub-folder") exit(4) tmp = DictionaryParser.wordlist if len(DictionaryParser.wordlist) == 0: if not silent: print("dict.txt is empty") exit(6)
def test_dictionary_incorrect_files_format(self): path = get_root_dir() / 'tests' / 'test_dict_format_incorrect' try: DictionaryParser.load_dictionary(path, silent=True) except SystemExit as cm: self.assertEqual(cm.code, 6)
def test_incorrect_dict_exists(self): path = get_root_dir() / 'tests' / 'test_dict_format_incorrect' / 'dict.txt' self.assertTrue(isfile(path))
def test_settings_incorrect_format(self): path = get_root_dir() / 'tests' / 'test_settings_incorrect_format.json' try: SettingsParser.load_settings(path, silent=True) except SystemExit as cm: self.assertEqual(cm.code, 3)
def test_incorrect_dict_meta_exists(self): path = get_root_dir() / 'tests' / 'test_dict_format_incorrect' / 'meta.json' self.assertTrue(isfile(path))
def test_incorrect_settings_exists(self): path = get_root_dir() / 'tests' / 'test_settings_incorrect_format.json' self.assertTrue(isfile(path))
def test_incorrect_json_exists(self): path = get_root_dir() / 'tests' / 'test_json_incorrect.json' self.assertTrue(isfile(path))
def test_root_dir(self): self.assertTrue(isdir(get_root_dir()))
def load_minimal_language(): path = get_root_dir() / 'tests' / 'test_dict_minimal_lang' DictionaryParser.load_dictionary('complete lang', language_dir=path, silent=True)