forked from sys-bio/temp-biomodels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_manual_corrections.py
62 lines (47 loc) · 2.11 KB
/
fix_manual_corrections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import enum
import os
import yaml
with open(__file__.replace('.py', '.yml'), 'r') as file:
FIXES = yaml.load(file, Loader=yaml.Loader)
class FixType(str, enum.Enum):
""" Type of manual fix """
replace_text = 'replace_text'
rename_file = 'rename_file'
remove_file = 'remove_file'
def run(id, working_dir):
""" Fix the namespaces in a SED-ML file and save the file in place
* Remove duplicate SED-ML namespace declarations -- automatically fixed by LXML
* Add namespaces for models
Args:
id (:obj:`str`): id of BioModels entry
working_dir (:obj:`str`): directory of entries to change
Returns:
:obj:`list` of :obj:`str`: list of corrected files
"""
fixes = FIXES.get(id, None)
if not fixes:
return []
for fix in fixes:
filename = os.path.join(working_dir, fix['filename'])
if fix['type'] == FixType.replace_text.value:
with open(filename, 'rb') as file:
contents = file.read()
try:
old = fix['old'].replace('\\t', "\t")
new = fix['new'].replace('\\t', "\t")
assert old.encode() in contents, "Text could not be replaced in `{}`\n\n{}".format(filename, old)
contents = contents.replace(old.encode(), new.encode())
except Exception:
old = fix['old'].replace('\\t', "\t").replace('\n', '\r\n')
new = fix['new'].replace('\\t', "\t").replace('\n', '\r\n')
assert old.encode() in contents, "Text could not be replaced in `{}`\n\n{}".format(filename, old)
contents = contents.replace(old.encode(), new.encode())
with open(filename, 'wb') as file:
file.write(contents)
elif fix['type'] == FixType.rename_file.value:
os.rename(filename, os.path.join(working_dir, fix['new']))
elif fix['type'] == FixType.remove_file.value:
os.remove(filename)
else:
raise NotImplementedError('Fix type `{}` is not supported.'.format(fix['type']))
return sorted(set(fix['filename'] for fix in fixes))