-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilities.py
More file actions
243 lines (206 loc) · 10.7 KB
/
utilities.py
File metadata and controls
243 lines (206 loc) · 10.7 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python3
import argparse
from pathlib import Path
import sys
import cellml
import logging
root_logger = logging.getLogger("libcellml_python_utils")
root_logger.setLevel(logging.INFO)
terminal_handler = logging.StreamHandler(sys.stderr)
terminal_handler.setLevel(logging.INFO)
root_logger.addHandler(terminal_handler)
def validate_model(file_path, strict_mode):
model = cellml.parse_model(file_path, strict_mode)
if model:
cellml.validate_model(model)
# del model
def generate_code(file_path, strict_mode, print_code):
flat_model = flatten_model(file_path, strict_mode, False, None)
if flat_model:
if cellml.validate_model(flat_model) > 0:
root_logger.warning('Validation issues found in flattened model')
return
root_logger.info('Model was flattened without any issues.')
# this will report any issues that come up in analysing the model to prepare for code generation
analysed_model = cellml.analyse_model(flat_model)
cellml.generate_code(analysed_model, print_code)
def flatten_model(file_path, strict_mode, add_ids, output=None):
model = cellml.parse_model(file_path, strict_mode)
if model is None:
return None
if cellml.validate_model(model) > 0:
return None
importer = cellml.resolve_imports(model, str(file_path.parent), strict_mode)
if model.hasUnresolvedImports():
return None
if cellml.validate_model(model) > 0:
return None
root_logger.info('Model was parsed, resolved, and validated without any issues.')
# need a flattened model for analysing
flat_model = cellml.flatten_model(model, importer)
fms = cellml.print_model(flat_model, add_ids)
if output:
with output.open("w") as f:
f.write(fms)
return flat_model
def _insert_source_comment(xml_string: str, url: str) -> str:
"""Insert a source comment into an XML string, respecting any XML declaration."""
comment = f"<!--\n This is a flattened model originally retrieved from:\n {url}\n-->"
if xml_string.lstrip().startswith("<?xml"):
# Insert after the end of the declaration line
decl_end = xml_string.index("?>") + 2
return xml_string[:decl_end] + "\n" + comment + xml_string[decl_end:]
return comment + xml_string
def fetch_flat_model(url, strict_mode, add_ids, output=None):
model, version = cellml.parse_remote_model(url, strict_mode)
if model is None:
return None
if cellml.validate_model(model) > 0:
return None
importer = None
if model.hasUnresolvedImports():
importer = cellml.resolve_remote_imports(model, url, strict_mode)
if model.hasUnresolvedImports():
root_logger.debug(f'Model has unresolved imports after attempting to resolve remote imports')
return None
if cellml.validate_model(model) > 0:
root_logger.warning('Validation issues found in model after resolving remote imports')
return None
root_logger.info('Model was parsed, resolved, and validated without any issues.')
# # need a flattened model for analysing
flat_model = cellml.flatten_model(model, importer)
if output:
fms = cellml.print_model(flat_model, add_ids)
fms = _insert_source_comment(fms, url)
with output.open("w") as f:
f.write(fms)
return flat_model
def convert_model(file_path, add_ids, output):
model = cellml.parse_model(file_path, False)
if model:
model_string = cellml.print_model(model, add_ids)
if output:
with output.open("w") as f:
f.write(model_string)
else:
print(model_string)
def summarize(file_path, verbose):
size = Path(file_path).stat().st_size
print(f"File: {file_path}")
print(f"Size: {size} bytes")
if verbose:
print(f"Absolute path: {Path(file_path).resolve()}")
def main():
parser = argparse.ArgumentParser(description="Perform various actions on a CellML file.")
# --- Logging options ---
parser.add_argument(
"--log-level",
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
metavar="LEVEL",
help="Log verbosity: DEBUG (most verbose) → INFO → WARNING → ERROR (least verbose). ",
)
parser.add_argument(
"--debug",
action="store_true",
help="Enable debug logging. Equivalent to --log-level DEBUG.",
)
subparsers = parser.add_subparsers(dest="command", required=True)
# Subcommand: validate
parser_validate = subparsers.add_parser("validate", help="Validate the model")
parser_validate.add_argument("input_file", help='Path to the input CellML file')
parser_validate.add_argument('-s', '--strict', action='store_true',
help='Validate in strict mode.')
# Subcommand: executable
parser_executable = subparsers.add_parser("executable", help="Check if code can be generated for the model")
parser_executable.add_argument("input_file", help='Path to the input CellML file')
parser_executable.add_argument('-s', '--strict', action='store_true',
help='Parse and import models in strict mode.')
parser_executable.add_argument('-p', '--print', action='store_true',
help='Print out the generated code')
# Subcommand: flatten
parser_flatten = subparsers.add_parser("flatten", help="Flatten the provided model, if possible")
parser_flatten.add_argument("input_file", help='Path to the input CellML file')
parser_flatten.add_argument('-s', '--strict', action='store_true',
help='Parse and import models in strict mode.')
parser_flatten.add_argument('-i', '--add-ids', action='store_true',
help='If set, make sure all CellML elements have an ID attribute')
parser_flatten.add_argument('-o', '--output', required=True, type=Path,
help='Path to output flattened model')
parser_flatten.add_argument('-f', '--force', action='store_true',
help='Overwrite any existing file at the output location')
# Subcommand: fetch-flat-model
parser_fetch_flat_model = subparsers.add_parser("fetch-flat-model", help="Fetch the flattened version of the provided remote model")
parser_fetch_flat_model.add_argument("url", help='URL of the remote CellML model to fetch and flatten')
parser_fetch_flat_model.add_argument('-s', '--strict', action='store_true',
help='Parse and import models in strict mode.')
parser_fetch_flat_model.add_argument('-i', '--add-ids', action='store_true',
help='If set, make sure all CellML elements int he flattened model have an ID attribute')
parser_fetch_flat_model.add_argument('-o', '--output', required=True, type=Path,
help='Path to output flattened model')
parser_fetch_flat_model.add_argument('-f', '--force', action='store_true',
help='Overwrite any existing file at the output location')
# Subcommand: convert
parser_convert = subparsers.add_parser("convert", help="Convert input CellML model to CellML 2")
parser_convert.add_argument("input_file", help='Path to the input CellML file')
parser_convert.add_argument("-n", "--num-lines", type=int, default=5, help="Number of lines to show (default: 5)")
parser_convert.add_argument('-i', '--add-ids', action='store_true',
help='If set, make sure all CellML elements have an ID attribute')
parser_convert.add_argument('-f', '--force', action='store_true',
help='Overwrite any existing file at the output location')
parser_convert.add_argument("-o", "--output", type=Path,
help="Path to output file; fallback to stdout if not provided")
# Subcommand: summarize
parser_summary = subparsers.add_parser("summarize", help="Print file summary")
parser_summary.add_argument("input_file", help='Path to the input file')
parser_summary.add_argument("-v", "--verbose", action="store_true", help="Show additional file details")
args = parser.parse_args()
root_logger.info(f'Command: {args.command}')
if args.debug:
root_logger.info('Debug logging enabled via --debug flag')
root_logger.setLevel(logging.DEBUG)
terminal_handler.setLevel(logging.DEBUG)
elif args.log_level:
root_logger.setLevel(getattr(logging, args.log_level))
terminal_handler.setLevel(getattr(logging, args.log_level))
file_path = None
if args.command != 'fetch-flat-model':
file_path = Path(args.input_file)
if not file_path.is_file():
print(f"Error: File '{file_path}' not found.", file=sys.stderr)
sys.exit(1)
if args.command == "validate":
validate_model(file_path, args.strict)
elif args.command == "convert":
if args.output:
if args.output.exists():
if args.force:
root_logger.warning(f'Warning: forcing overwrite of existing file {args.output}')
else:
root_logger.error(f"Error: Output file '{args.output}' already exists. Use a different name or delete it.")
sys.exit(1)
convert_model(file_path, args.add_ids, args.output)
elif args.command == "flatten":
if args.output.exists():
if args.force:
root_logger.warning(f'Warning: forcing overwrite of existing file {args.output}')
else:
root_logger.error(f"Error: Output file '{args.output}' already exists. Use a different name or delete it or force "
f"overwrite.")
sys.exit(1)
flatten_model(file_path, args.strict, args.add_ids, args.output)
elif args.command == "fetch-flat-model":
root_logger.debug(f'Fetching and flattening model from {args.url} with strict_mode={args.strict} and add_ids={args.add_ids}')
if args.output.exists():
if args.force:
root_logger.warning(f'Warning: forcing overwrite of existing file {args.output}')
else:
root_logger.error(f"Error: Output file '{args.output}' already exists. Use a different name or delete it or force "
f"overwrite.")
sys.exit(1)
fetch_flat_model(args.url, args.strict, args.add_ids, args.output)
elif args.command == "summarize":
summarize(file_path, args.verbose)
elif args.command == "executable":
generate_code(file_path, args.strict, args.print)
if __name__ == "__main__":
main()