Definitions of construction statements.
| 295 | |
| 296 | |
| 297 | class Definition: |
| 298 | """Definitions of construction statements.""" |
| 299 | |
| 300 | @classmethod |
| 301 | def from_txt_file(cls, fname: str, to_dict: bool = False) -> Definition: |
| 302 | with open(fname, 'r') as f: |
| 303 | lines = f.read() |
| 304 | return cls.from_string(lines, to_dict) |
| 305 | |
| 306 | @classmethod |
| 307 | def from_string(cls, string: str, to_dict: bool = False) -> Definition: |
| 308 | lines = string.split('\n') |
| 309 | data = [cls.from_txt('\n'.join(group)) for group in reshape(lines, 6)] |
| 310 | if to_dict: |
| 311 | return cls.to_dict(data) |
| 312 | return data |
| 313 | |
| 314 | @classmethod |
| 315 | def to_dict(cls, data: list[Definition]) -> dict[str, Definition]: |
| 316 | return {d.construction.name: d for d in data} |
| 317 | |
| 318 | @classmethod |
| 319 | def from_txt(cls, data: str) -> Definition: |
| 320 | """Load definitions from a str object.""" |
| 321 | construction, rely, deps, basics, numerics, _ = data.split('\n') |
| 322 | basics = [] if not basics else [b.strip() for b in basics.split(';')] |
| 323 | |
| 324 | levels = [] |
| 325 | for bs in basics: |
| 326 | if ':' in bs: |
| 327 | points, bs = bs.split(':') |
| 328 | points = points.strip().split() |
| 329 | else: |
| 330 | points = [] |
| 331 | if bs.strip(): |
| 332 | bs = [Construction.from_txt(b.strip()) for b in bs.strip().split(',')] |
| 333 | else: |
| 334 | bs = [] |
| 335 | levels.append((points, bs)) |
| 336 | |
| 337 | numerics = [] if not numerics else numerics.split(', ') |
| 338 | |
| 339 | return Definition( |
| 340 | construction=Construction.from_txt(construction), |
| 341 | rely=parse_rely(rely), |
| 342 | deps=Clause.from_txt(deps), |
| 343 | basics=levels, |
| 344 | numerics=[Construction.from_txt(c) for c in numerics], |
| 345 | ) |
| 346 | |
| 347 | def __init__( |
| 348 | self, |
| 349 | construction: Construction, |
| 350 | rely: dict[str, str], |
| 351 | deps: Clause, |
| 352 | basics: list[tuple[list[str], list[Construction]]], |
| 353 | numerics: list[Construction], |
| 354 | ): |