Commit 1ced3f8f authored by Mikaël Salson's avatar Mikaël Salson Committed by Vidjil Team

vidjil_utils: Move regex search in vidjil_utils

parent ee8ad6a6
......@@ -198,23 +198,12 @@ def stats():
d['stats'] = keys
for row in d['query']:
results_f = defs.DIR_RESULTS + row.results_file.data_file
try:
results = open(results_f).readlines(STATS_READLINES)
except IOError:
results = []
results_f = row.results_file.data_file
row_result = vidjil_utils.search_first_regex_in_file(regex, defs.DIR_RESULTS + results_f, STATS_READLINES)
for key in keys:
row[key] = ''
for r in regex:
for line in results:
m = r.search(line)
if m:
for (key, val) in m.groupdict().items():
row[key] = val.replace('\\', '')
break
log.debug("patient/stats (%.3fs)" % (time.time()-start))
return d
......
......@@ -116,6 +116,26 @@ def filter(str, filter_str):
return True
#### Utilities on regex
def search_first_regex_in_file(regex, filename, max_nb_line=None):
try:
if max_nb_line is None:
results = open(filename).readlines()
else:
results = open(filename).readlines(max_nb_line)
except IOError as e:
results = []
matched_keys = {}
for r in regex:
for line in results:
m = r.search(line)
if m:
for (key, val) in m.groupdict().items():
matched_keys[key] = val.replace('\\', '')
break
return matched_keys
log_patient = re.compile('\((\d+)\)')
log_config = re.compile(' c(\d+)')
log_task = re.compile('\[(\d+)\]')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment