Skip to content

Commit

Permalink
fix: failed to add dict when using ~
Browse files Browse the repository at this point in the history
  • Loading branch information
Crissium committed Jan 30, 2024
1 parent 1f60eda commit 935860e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions server/app/dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def __init__(self, app: 'Flask') -> 'None':
logger.info('Dictionaries loaded.')

def add_dictionary(self, dictionary_info: 'dict') -> 'None':
dictionary_info['dictionary_filename'] =\
self.settings.parse_path_with_env_variables(dictionary_info['dictionary_filename'])
self._load_dictionary(dictionary_info)
self.settings.add_dictionary(dictionary_info)
logger.info('Added dictionary %s' % dictionary_info['dictionary_name'])
Expand Down
23 changes: 17 additions & 6 deletions server/app/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import os
import sys
import shutil
Expand Down Expand Up @@ -156,7 +157,7 @@ def _dictionary_format(self, filename: 'str') -> 'str | None':
return None

@staticmethod
def _parse_path_with_env_variables(path: 'str') -> 'str':
def parse_path_with_env_variables(path: 'str') -> 'str':
"""
Converts environment variables in a path to their values.
"""
Expand All @@ -178,7 +179,15 @@ def _save_dictionary_list(self) -> 'None':
if dictionary_info['dictionary_format'] == 'DSL (.dsl/.dsl.dz)'\
and not dictionary_info['dictionary_filename'].endswith('.dz'):
dictionary_info['dictionary_filename'] += '.dz'
self._save_settings_to_file(self.dictionaries_list, self.DICTIONARIES_LIST_FILE)
if sys.platform != 'win32': # FIXME: ensure other platforms do use ~
dictionaries_list = copy.deepcopy(self.dictionaries_list)
for dictionary_info in dictionaries_list:
if dictionary_info['dictionary_filename'].startswith(self.HOMEDIR):
dictionary_info['dictionary_filename'] =\
dictionary_info['dictionary_filename'].replace(self.HOMEDIR, '~')
self._save_settings_to_file(dictionaries_list, self.DICTIONARIES_LIST_FILE)
else:
self._save_settings_to_file(self.dictionaries_list, self.DICTIONARIES_LIST_FILE)

def _save_dictionary_metadata(self) -> 'None':
self._save_settings_to_file(self.dictionary_metadata, self.DICTIONARY_METADATA_FILE)
Expand Down Expand Up @@ -242,7 +251,7 @@ def __init__(self) -> 'None':
or '~' in dictionary_info['dictionary_filename']\
or '%' in dictionary_info['dictionary_filename']:
dictionary_info['dictionary_filename'] =\
self._parse_path_with_env_variables(dictionary_info['dictionary_filename'])
self.parse_path_with_env_variables(dictionary_info['dictionary_filename'])
else:
self.dictionaries_list: 'list[dict[str, str]]' = []
self._save_dictionary_list()
Expand Down Expand Up @@ -302,15 +311,17 @@ def __init__(self) -> 'None':
self._scan_lock = threading.Lock()

def dictionary_info_valid(self, dictionary_info: 'dict') -> 'bool':
filename = dictionary_info['dictionary_filename']
filename = self.parse_path_with_env_variables(filename)
return all(key in dictionary_info.keys()
for key in ['dictionary_display_name',
'dictionary_name',
'dictionary_format',
'dictionary_filename'])\
and dictionary_info['dictionary_format'] in self.SUPPORTED_DICTIONARY_FORMATS.keys()\
and os.access(dictionary_info['dictionary_filename'], os.R_OK)\
and os.path.isfile(dictionary_info['dictionary_filename'])\
and os.path.splitext(dictionary_info['dictionary_filename'])[1] in\
and os.access(filename, os.R_OK)\
and os.path.isfile(filename)\
and os.path.splitext(filename)[1] in\
self.SUPPORTED_DICTIONARY_FORMATS[dictionary_info['dictionary_format']]\
and not any(dictionary_info['dictionary_name'] == dictionary['dictionary_name']
for dictionary in self.dictionaries_list)
Expand Down

0 comments on commit 935860e

Please sign in to comment.