Skip to content

Commit f9afbc8

Browse files
authored
Merge pull request #1897 from MTG/analysis_file_load
Don't check if a file exists before opening it
2 parents 068fa2a + 138df83 commit f9afbc8

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

sounds/models.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,11 +1955,11 @@ def value_is_valid(value):
19551955
return not math.isinf(value) and not math.isnan(value)
19561956
return True
19571957

1958-
if self.analysis_status == "OK" and \
1959-
'descriptors_map' in settings.ANALYZERS_CONFIGURATION.get(self.analyzer, {}):
1958+
analysis_configuration = settings.ANALYZERS_CONFIGURATION.get(self.analyzer, {})
1959+
if self.analysis_status == "OK" and 'descriptors_map' in analysis_configuration:
19601960
analysis_results = self.get_analysis_data_from_file()
19611961
if analysis_results:
1962-
descriptors_map = settings.ANALYZERS_CONFIGURATION[self.analyzer]['descriptors_map']
1962+
descriptors_map = analysis_configuration['descriptors_map']
19631963
analysis_data_for_db = {}
19641964
for file_descriptor_key_path, db_descriptor_key, _ in descriptors_map:
19651965
# TODO: here we could implement support for nested keys in the analysis file, maybe by accessing
@@ -1976,18 +1976,16 @@ def value_is_valid(value):
19761976
def get_analysis_data_from_file(self):
19771977
"""Returns the analysis data as stored in file or returns empty dict if no file exists. It tries
19781978
extensions .json and .yaml as these are the supported formats for analysis results"""
1979-
if os.path.exists(self.analysis_filepath_base + '.json'):
1980-
try:
1981-
with open(self.analysis_filepath_base + '.json') as f:
1982-
return json.load(f)
1983-
except Exception:
1984-
pass
1985-
if os.path.exists(self.analysis_filepath_base + '.yaml'):
1986-
try:
1987-
with open(self.analysis_filepath_base + '.yaml') as f:
1988-
return yaml.load(f, Loader=yaml.cyaml.CSafeLoader)
1989-
except Exception:
1990-
pass
1979+
try:
1980+
with open(self.analysis_filepath_base + '.json') as f:
1981+
return json.load(f)
1982+
except Exception:
1983+
pass
1984+
try:
1985+
with open(self.analysis_filepath_base + '.yaml') as f:
1986+
return yaml.load(f, Loader=yaml.cyaml.CSafeLoader)
1987+
except Exception:
1988+
pass
19911989
return {}
19921990

19931991
def get_analysis_data(self):

utils/search/backends/solr555pysolr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,14 @@ def convert_post_to_search_engine_document(self, post):
326326
"has_posts": False if post.thread.num_posts == 0 else True
327327
}
328328
return document
329-
329+
330330
def get_dynamic_fields_map(self):
331331
if hasattr(self, '_dynamic_fields_map'):
332332
return self._dynamic_fields_map
333333
dynamic_fields_map = {}
334334
for analyzer, analyzer_data in settings.ANALYZERS_CONFIGURATION.items():
335335
if 'descriptors_map' in analyzer_data:
336-
descriptors_map = settings.ANALYZERS_CONFIGURATION[analyzer]['descriptors_map']
336+
descriptors_map = analyzer_data['descriptors_map']
337337
for _, db_descriptor_key, descriptor_type in descriptors_map:
338338
if descriptor_type is not None:
339339
dynamic_fields_map[db_descriptor_key] = '{}{}'.format(

0 commit comments

Comments
 (0)