|
15 | 15 | class ValidationError(Exception): |
16 | 16 | pass |
17 | 17 |
|
18 | | - |
19 | | -HEADER_FIELDS = { |
20 | | - "file_description": [ |
21 | | - "description", |
22 | | - "implementation_level", |
23 | | - ], |
24 | | - "file_name": [ |
25 | | - "name", |
26 | | - "time_stamp", |
27 | | - "author", |
28 | | - "organization", |
29 | | - "preprocessor_version", |
30 | | - "originating_system", |
31 | | - "authorization", |
32 | | - ], |
33 | | - "file_schema": [ |
34 | | - "schema_identifiers", |
35 | | - ], |
36 | | -} |
37 | | - |
38 | | -class HeaderWrapper: |
39 | | - def __init__(self, field_names, values): |
40 | | - self._fields = field_names |
41 | | - self._values = values |
42 | | - for k, v in zip(field_names, values): |
43 | | - setattr(self, k, v) |
44 | | - |
45 | | - def __getitem__(self, index): |
46 | | - """ |
47 | | - Allows for the object to be subscriptable, avoiding conflicts with the validation service. Can be removed after service is fully compatible. |
48 | | - e.g. |
49 | | - >>> f.header.file_name[0] |
50 | | - 'Header example2.ifc' |
51 | | - >>> f.header.file_name.name |
52 | | - 'Header example2.ifc' |
53 | | - """ |
54 | | - return self._values[index] |
55 | | - |
56 | | - def __len__(self): |
57 | | - return len(self._values) |
58 | | - |
59 | | - def __iter__(self): |
60 | | - return iter(self._values) |
61 | | - |
62 | | - def __repr__(self): |
63 | | - """ |
64 | | - Representing custom header object when debugging, e.g. |
65 | | - >>> f.header.file_name |
66 | | - <HeaderWrapper name='Header example2.ifc', time_stamp='2022-09-16T10:35:07', |
67 | | - author=('Evandro Alfieri',), organization=('buildingSMART Int.',), |
68 | | - preprocessor_version='IFC Motor 1.0', originating_system='Company - Application - 26.0.0.0', |
69 | | - authorization='none'> |
70 | | - """ |
71 | | - return f"<{self.__class__.__name__} " + ", ".join( |
72 | | - f"{k}={repr(getattr(self, k))}" for k in self._fields |
73 | | - ) + ">" |
74 | | - |
| 18 | +from collections import namedtuple |
75 | 19 |
|
76 | 20 | class SyntaxError(ValidationError): |
77 | 21 | def __init__(self, filecontent, exception): |
@@ -490,26 +434,21 @@ def schema_version(self) -> tuple[int, int, int, int]: |
490 | 434 | version.append(int(number.group(1)) if number else 0) |
491 | 435 | return tuple(version) |
492 | 436 |
|
493 | | - # @property |
494 | | - # def header(self): |
495 | | - # mapping = { |
496 | | - # "file_description": file_description, |
497 | | - # "file_name": file_name, |
498 | | - # "file_schema": file_schema, |
499 | | - # } |
500 | | - |
501 | | - # return types.SimpleNamespace(**{ |
502 | | - # k.lower(): mapping[k.lower()](v) |
503 | | - # for k, v in self.header_.items() |
504 | | - # if k.lower() in mapping |
505 | | - # }) |
| 437 | + |
506 | 438 | @property |
507 | 439 | def header(self): |
508 | | - return types.SimpleNamespace(**{ |
509 | | - name: HeaderWrapper(fields, self.header_[name.upper()]) |
510 | | - for name, fields in HEADER_FIELDS.items() |
511 | | - if name.upper() in self.header_ |
512 | | - }) |
| 440 | + HEADER_FIELDS = { |
| 441 | + "file_description": namedtuple('file_description', ['description', 'implementation_level']), |
| 442 | + "file_name": namedtuple('file_name', ['name', 'time_stamp', 'author', 'organization', 'preprocessor_version', 'originating_system', 'authorization']), |
| 443 | + "file_schema": namedtuple('file_schema', ['schema_identifiers']), |
| 444 | + } |
| 445 | + header = {} |
| 446 | + |
| 447 | + for field_name, namedtuple_class in HEADER_FIELDS.items(): |
| 448 | + field_data = self.header_.get(field_name.upper(), []) |
| 449 | + header[field_name.lower()] = namedtuple_class(*field_data) |
| 450 | + |
| 451 | + return types.SimpleNamespace(**header) |
513 | 452 |
|
514 | 453 | def __getitem__(self, key: numbers.Integral) -> entity_instance: |
515 | 454 | return self.by_id(key) |
@@ -546,22 +485,3 @@ def by_type(self, type: str) -> list[entity_instance]: |
546 | 485 |
|
547 | 486 | def open(fn) -> file: |
548 | 487 | return file(parse(filename=fn, with_tree=True, with_header=True)) |
549 | | - |
550 | | -class file_description: |
551 | | - def __init__(self, values): |
552 | | - self.description = values[0] |
553 | | - self.implementation_level = values[1] |
554 | | - |
555 | | -class file_name: |
556 | | - def __init__(self, values): |
557 | | - self.name = values[0] |
558 | | - self.time_stamp = values[1] |
559 | | - self.author = values[2] |
560 | | - self.organization = values[3] |
561 | | - self.preprocessor_version = values[4] |
562 | | - self.originating_system = values[5] |
563 | | - self.authorization = values[6] |
564 | | - |
565 | | -class file_schema: |
566 | | - def __init__(self, values): |
567 | | - self.schema_identifiers = values[0] |
0 commit comments