labfreed 0.0.5__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of labfreed might be problematic. Click here for more details.
- labfreed/__init__.py +4 -1
- labfreed/labfreed_infrastructure.py +276 -0
- labfreed/pac_cat/__init__.py +17 -0
- labfreed/pac_cat/category_base.py +51 -0
- labfreed/pac_cat/pac_cat.py +159 -0
- labfreed/pac_cat/predefined_categories.py +190 -0
- labfreed/pac_id/__init__.py +19 -0
- labfreed/pac_id/extension.py +48 -0
- labfreed/pac_id/id_segment.py +90 -0
- labfreed/pac_id/pac_id.py +140 -0
- labfreed/pac_id/url_parser.py +154 -0
- labfreed/pac_id/url_serializer.py +80 -0
- labfreed/pac_id_resolver/__init__.py +2 -0
- labfreed/pac_id_resolver/cit_v1.py +149 -0
- labfreed/pac_id_resolver/cit_v2.py +303 -0
- labfreed/pac_id_resolver/resolver.py +81 -0
- labfreed/pac_id_resolver/services.py +80 -0
- labfreed/qr/__init__.py +1 -0
- labfreed/qr/generate_qr.py +422 -0
- labfreed/trex/__init__.py +16 -0
- labfreed/trex/python_convenience/__init__.py +3 -0
- labfreed/trex/python_convenience/data_table.py +45 -0
- labfreed/trex/python_convenience/pyTREX.py +242 -0
- labfreed/trex/python_convenience/quantity.py +46 -0
- labfreed/trex/table_segment.py +227 -0
- labfreed/trex/trex.py +69 -0
- labfreed/trex/trex_base_models.py +336 -0
- labfreed/trex/value_segments.py +111 -0
- labfreed/{DisplayNameExtension → utilities}/base36.py +29 -13
- labfreed/well_known_extensions/__init__.py +5 -0
- labfreed/well_known_extensions/default_extension_interpreters.py +7 -0
- labfreed/well_known_extensions/display_name_extension.py +40 -0
- labfreed/well_known_extensions/trex_extension.py +31 -0
- labfreed/well_known_keys/gs1/__init__.py +6 -0
- labfreed/well_known_keys/gs1/gs1.py +4 -0
- labfreed/well_known_keys/gs1/gs1_ai_enum_sorted.py +57 -0
- labfreed/{PAC_ID/well_known_segment_keys.py → well_known_keys/labfreed/well_known_keys.py} +1 -1
- labfreed/well_known_keys/unece/UneceUnits.json +33730 -0
- labfreed/well_known_keys/unece/__init__.py +4 -0
- labfreed/well_known_keys/unece/unece_units.py +68 -0
- labfreed-0.2.0.dist-info/METADATA +357 -0
- labfreed-0.2.0.dist-info/RECORD +44 -0
- {labfreed-0.0.5.dist-info → labfreed-0.2.0.dist-info}/WHEEL +1 -1
- labfreed/DisplayNameExtension/DisplayNameExtension.py +0 -34
- labfreed/PAC_CAT/__init__.py +0 -1
- labfreed/PAC_CAT/data_model.py +0 -109
- labfreed/PAC_ID/__init__.py +0 -0
- labfreed/PAC_ID/data_model.py +0 -215
- labfreed/PAC_ID/parse.py +0 -142
- labfreed/PAC_ID/serialize.py +0 -60
- labfreed/TREXExtension/data_model.py +0 -239
- labfreed/TREXExtension/parse.py +0 -46
- labfreed/TREXExtension/uncertainty.py +0 -32
- labfreed/TREXExtension/unit_utilities.py +0 -143
- labfreed/validation.py +0 -71
- labfreed-0.0.5.dist-info/METADATA +0 -34
- labfreed-0.0.5.dist-info/RECORD +0 -19
- {labfreed-0.0.5.dist-info → labfreed-0.2.0.dist-info}/licenses/LICENSE +0 -0
labfreed/PAC_ID/data_model.py
DELETED
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import re
|
|
2
|
-
from typing import Optional
|
|
3
|
-
from typing_extensions import Self
|
|
4
|
-
from pydantic import Field, ValidationInfo, computed_field, conlist, model_validator, field_validator
|
|
5
|
-
from ..validation import BaseModelWithWarnings, ValidationWarning, hsegment_pattern, domain_name_pattern
|
|
6
|
-
from abc import ABC, abstractproperty, abstractstaticmethod
|
|
7
|
-
from .well_known_segment_keys import WellKnownSegmentKeys
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class IDSegment(BaseModelWithWarnings):
|
|
11
|
-
key:str|None = None
|
|
12
|
-
value:str
|
|
13
|
-
@model_validator(mode="after")
|
|
14
|
-
def validate_segment(cls, model):
|
|
15
|
-
key = model.key or ""
|
|
16
|
-
value = model.value
|
|
17
|
-
|
|
18
|
-
# MUST be a valid hsegment according to RFC 1738, but without * (see PAC-ID Extension)
|
|
19
|
-
# This means it must be true for both, key and value
|
|
20
|
-
if not_allowed_chars := set(re.sub(hsegment_pattern, '', key)):
|
|
21
|
-
raise ValueError(f"id segment key {key} contains invalid characters {' '.join(not_allowed_chars)}.")
|
|
22
|
-
|
|
23
|
-
if not_allowed_chars := set(re.sub(hsegment_pattern, '', value)):
|
|
24
|
-
raise ValueError(f"id segment key {value} contains invalid characters {' '.join(not_allowed_chars)}.")
|
|
25
|
-
|
|
26
|
-
# Segment key SHOULD be limited to A-Z, 0-9, and -+..
|
|
27
|
-
if not_recommended_chars := set(re.sub(r'[A-Z0-9-:+]', '', key)):
|
|
28
|
-
model.add_warning(
|
|
29
|
-
source=f"id segment key {key}",
|
|
30
|
-
type="Recommendation",
|
|
31
|
-
msg=f"{' '.join(not_recommended_chars)} should not be used.",
|
|
32
|
-
recommendation = "SHOULD be limited to A-Z, 0-9, and -+",
|
|
33
|
-
highlight_pattern = key,
|
|
34
|
-
highlight_sub = not_recommended_chars
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
# Segment key should be in Well know keys
|
|
38
|
-
if key and key not in [k.value for k in WellKnownSegmentKeys]:
|
|
39
|
-
model.add_warning(
|
|
40
|
-
source=f"id segment key {key}",
|
|
41
|
-
type="Recommendation",
|
|
42
|
-
msg=f"{key} is not a well known segment key.",
|
|
43
|
-
recommendation = "RECOMMENDED to be a well-known id segment key.",
|
|
44
|
-
highlight_pattern = key
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
# Segment value SHOULD be limited to A-Z, 0-9, and -+..
|
|
49
|
-
if not_recommended_chars := set(re.sub(r'[A-Z0-9-:+]', '', value)):
|
|
50
|
-
model.add_warning(
|
|
51
|
-
source=f"id segment value {value}",
|
|
52
|
-
type="Recommendation",
|
|
53
|
-
msg=f"Characters {' '.join(not_recommended_chars)} should not be used.",
|
|
54
|
-
recommendation = "SHOULD be limited to A-Z, 0-9, and -+",
|
|
55
|
-
highlight_pattern = value,
|
|
56
|
-
highlight_sub = not_recommended_chars
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
# Segment value SHOULD be limited to A-Z, 0-9, and :-+ for new designs.
|
|
60
|
-
# this means that ":" in key or value is problematic
|
|
61
|
-
if ':' in key:
|
|
62
|
-
model.add_warning(
|
|
63
|
-
source=f"id segment key {key}",
|
|
64
|
-
type="Recommendation",
|
|
65
|
-
msg=f"Character ':' should not be used in segment key, since this character is used to separate key and value this can lead to undefined behaviour.",
|
|
66
|
-
highlight_pattern = key
|
|
67
|
-
)
|
|
68
|
-
if ':' in value:
|
|
69
|
-
model.add_warning(
|
|
70
|
-
source=f"id segment value {value}",
|
|
71
|
-
type="Recommendation",
|
|
72
|
-
msg=f"Character ':' should not be used in segment value, since this character is used to separate key and value this can lead to undefined behaviour.",
|
|
73
|
-
highlight_pattern = value
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
return model
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
class Category(BaseModelWithWarnings):
|
|
82
|
-
key:str|None = None
|
|
83
|
-
segments: list[IDSegment]
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
class Identifier(BaseModelWithWarnings):
|
|
87
|
-
segments: conlist(IDSegment, min_length=1) = Field(..., exclude=True) # type: ignore # exclude=True prevents this from being serialized by Pydantic
|
|
88
|
-
|
|
89
|
-
@computed_field
|
|
90
|
-
@property
|
|
91
|
-
def categories(self) -> list[Category]:
|
|
92
|
-
categories = list()
|
|
93
|
-
c = Category(segments=[])
|
|
94
|
-
categories.append(c)
|
|
95
|
-
for s in self.segments:
|
|
96
|
-
# new category starts with "-"
|
|
97
|
-
if s.value[0] == '-':
|
|
98
|
-
cat_key = s.value
|
|
99
|
-
c = Category(key=cat_key, segments=[])
|
|
100
|
-
categories.append(c)
|
|
101
|
-
else:
|
|
102
|
-
c.segments.append(s)
|
|
103
|
-
|
|
104
|
-
# the first category might have no segments. remove categories without segments
|
|
105
|
-
if not categories[0].segments:
|
|
106
|
-
categories = categories[1:]
|
|
107
|
-
|
|
108
|
-
return categories
|
|
109
|
-
|
|
110
|
-
@model_validator(mode='after')
|
|
111
|
-
def check_keys_are_unique_in_each_category(self) -> Self:
|
|
112
|
-
for c in self.categories:
|
|
113
|
-
keys = [s.key for s in c.segments if s.key]
|
|
114
|
-
duplicate_keys = [k for k in set(keys) if keys.count(k) > 1]
|
|
115
|
-
if duplicate_keys:
|
|
116
|
-
raise ValueError(f'Duplicate keys {",".join(duplicate_keys)} in category {c.key}')
|
|
117
|
-
return self
|
|
118
|
-
|
|
119
|
-
@model_validator(mode='after')
|
|
120
|
-
def check_length(self) -> Self:
|
|
121
|
-
l = 0
|
|
122
|
-
for s in self.segments:
|
|
123
|
-
if s.key:
|
|
124
|
-
l += len(s.key)
|
|
125
|
-
l += 1 # for ":"
|
|
126
|
-
l += len(s.value)
|
|
127
|
-
l += len(self.segments) - 1 # account for "/" separating the segments
|
|
128
|
-
|
|
129
|
-
if l > 256:
|
|
130
|
-
raise ValueError(f'Identifier is {l} characters long, Identifier must not exceed 256 characters.')
|
|
131
|
-
return self
|
|
132
|
-
|
|
133
|
-
@staticmethod
|
|
134
|
-
def from_categories(categories:list[Category]) :
|
|
135
|
-
segments = list()
|
|
136
|
-
for c in categories:
|
|
137
|
-
if c.key:
|
|
138
|
-
segments.append(IDSegment(value=c.key))
|
|
139
|
-
segments.extend(c.segments)
|
|
140
|
-
return Identifier(segments=segments)
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
class Extension(ABC, BaseModelWithWarnings):
|
|
145
|
-
|
|
146
|
-
@abstractproperty
|
|
147
|
-
def name(self)->str:
|
|
148
|
-
pass
|
|
149
|
-
|
|
150
|
-
@abstractproperty
|
|
151
|
-
def type(self)->str:
|
|
152
|
-
pass
|
|
153
|
-
|
|
154
|
-
@abstractproperty
|
|
155
|
-
def data(self)->str:
|
|
156
|
-
pass
|
|
157
|
-
|
|
158
|
-
@abstractstaticmethod
|
|
159
|
-
def from_spec_fields(name, type, data):
|
|
160
|
-
pass
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
class UnknownExtension(Extension):
|
|
164
|
-
name_:str
|
|
165
|
-
type_:str
|
|
166
|
-
data_:str
|
|
167
|
-
|
|
168
|
-
@property
|
|
169
|
-
def name(self)->str:
|
|
170
|
-
return self.name_
|
|
171
|
-
|
|
172
|
-
@property
|
|
173
|
-
def type(self)->str:
|
|
174
|
-
return self.type_
|
|
175
|
-
|
|
176
|
-
@property
|
|
177
|
-
def data(self)->str:
|
|
178
|
-
return self.data_
|
|
179
|
-
|
|
180
|
-
@staticmethod
|
|
181
|
-
def from_spec_fields(name, type, data):
|
|
182
|
-
return UnknownExtension(name_=name, type_=type, data_=data)
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
class PACID(BaseModelWithWarnings):
|
|
187
|
-
issuer:str
|
|
188
|
-
identifier: Identifier
|
|
189
|
-
|
|
190
|
-
@model_validator(mode="after")
|
|
191
|
-
def validate_issuer(cls, model):
|
|
192
|
-
if not re.fullmatch(domain_name_pattern, model.issuer):
|
|
193
|
-
raise ValueError("Issuer must be a valid domain name.")
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
# recommendation that A-Z, 0-9, -, and . should be used
|
|
197
|
-
if not_recommended_chars := set(re.sub(r'[A-Z0-9\.-]', '', model.issuer)):
|
|
198
|
-
model.add_warning(
|
|
199
|
-
source="PAC-ID",
|
|
200
|
-
type="Recommendation",
|
|
201
|
-
highlight_pattern=model.issuer,
|
|
202
|
-
highlight_sub=not_recommended_chars,
|
|
203
|
-
msg=f"Characters {' '.join(not_recommended_chars)} should not be used. Issuer SHOULD contain only the characters A-Z, 0-9, -, and . "
|
|
204
|
-
)
|
|
205
|
-
return model
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
class PACID_With_Extensions(BaseModelWithWarnings):
|
|
209
|
-
pac_id: PACID
|
|
210
|
-
extensions: list[Extension] = Field(default_factory=list)
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
labfreed/PAC_ID/parse.py
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import re
|
|
4
|
-
from types import MappingProxyType
|
|
5
|
-
from .data_model import *
|
|
6
|
-
|
|
7
|
-
from ..validation import extract_warnings, ValidationWarning
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
category_conventions = MappingProxyType(
|
|
11
|
-
{
|
|
12
|
-
'-MD': ['240', '21'],
|
|
13
|
-
'-MS': ['240', '10', '20', '21', '250'],
|
|
14
|
-
'-MC': ['240', '10', '20', '21', '250'],
|
|
15
|
-
'-MM': ['240', '10', '20', '21', '250']
|
|
16
|
-
}
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
extension_convention = MappingProxyType(
|
|
21
|
-
{
|
|
22
|
-
0: { 'name': 'N', 'type': 'N'},
|
|
23
|
-
1: { 'name': 'SUM', 'type': 'TREX'}
|
|
24
|
-
}
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
class PAC_Parser():
|
|
30
|
-
|
|
31
|
-
def __init__(self, extension_interpreters:dict[str, Extension]=None):
|
|
32
|
-
self.extension_interpreters = extension_interpreters or {}
|
|
33
|
-
|
|
34
|
-
def parse_pac_url(self, pac_url:str) -> tuple[PACID_With_Extensions, list[ValidationWarning] ]:
|
|
35
|
-
if '*' in pac_url:
|
|
36
|
-
id_str, ext_str = pac_url.split('*', 1)
|
|
37
|
-
else:
|
|
38
|
-
id_str = pac_url
|
|
39
|
-
ext_str = ""
|
|
40
|
-
|
|
41
|
-
pac_id = self.parse_pac_id(id_str)
|
|
42
|
-
extensions = self.parse_extensions(ext_str)
|
|
43
|
-
|
|
44
|
-
pac_with_extension = PACID_With_Extensions(pac_id=pac_id, extensions=extensions)
|
|
45
|
-
warnings = extract_warnings(pac_with_extension)
|
|
46
|
-
|
|
47
|
-
return pac_with_extension, warnings
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
def parse_id_segments(self, identifier:str):
|
|
51
|
-
if not identifier:
|
|
52
|
-
return []
|
|
53
|
-
|
|
54
|
-
id_segments = list()
|
|
55
|
-
if len(identifier) > 0 and identifier[0] == '/':
|
|
56
|
-
identifier = identifier[1:]
|
|
57
|
-
for s in identifier.split('/'):
|
|
58
|
-
tmp = s.split(':')
|
|
59
|
-
|
|
60
|
-
if len(tmp) == 1:
|
|
61
|
-
segment = IDSegment(value=tmp[0])
|
|
62
|
-
elif len(tmp) == 2:
|
|
63
|
-
segment = IDSegment(key=tmp[0], value=tmp[1])
|
|
64
|
-
else:
|
|
65
|
-
raise ValueError(f'invalid segment: {s}')
|
|
66
|
-
|
|
67
|
-
id_segments.append(segment)
|
|
68
|
-
return id_segments
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
def _apply_category_defaults(self, segments_in: list[IDSegment]):
|
|
72
|
-
|
|
73
|
-
segments = segments_in.copy()
|
|
74
|
-
default_keys = None
|
|
75
|
-
for s in segments:
|
|
76
|
-
if not s.key and default_keys:
|
|
77
|
-
s.key = default_keys.pop(0)
|
|
78
|
-
else:
|
|
79
|
-
default_keys = None
|
|
80
|
-
|
|
81
|
-
# category starts: start with new defaults.
|
|
82
|
-
if s.value in category_conventions.keys():
|
|
83
|
-
default_keys = category_conventions.get(s.value).copy() #copy, so the entries can be popped when used
|
|
84
|
-
return segments
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
def parse_pac_id(self,id_str:str) -> PACID:
|
|
89
|
-
m = re.match(f'(HTTPS://)?(PAC.)?(?P<issuer>.+?\..+?)/(?P<identifier>.*)', id_str)
|
|
90
|
-
d = m.groupdict()
|
|
91
|
-
|
|
92
|
-
id_segments = list()
|
|
93
|
-
default_keys = None
|
|
94
|
-
id_segments = self.parse_id_segments(d.get('identifier'))
|
|
95
|
-
id_segments = self._apply_category_defaults(id_segments)
|
|
96
|
-
|
|
97
|
-
pac = PACID(issuer= d.get('issuer'),
|
|
98
|
-
identifier=Identifier(segments=id_segments)
|
|
99
|
-
)
|
|
100
|
-
return pac
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
def parse_extensions(self, extensions_str:str|None) -> list[Extension]:
|
|
104
|
-
extensions = list()
|
|
105
|
-
|
|
106
|
-
if not extensions_str:
|
|
107
|
-
return extensions
|
|
108
|
-
|
|
109
|
-
defaults = extension_convention
|
|
110
|
-
for i, e in enumerate(extensions_str.split('*')):
|
|
111
|
-
if e == '': #this will happen if first extension starts with *
|
|
112
|
-
continue
|
|
113
|
-
d = re.match('((?P<name>.+)\$(?P<type>.+)/)?(?P<data>.+)', e).groupdict()
|
|
114
|
-
|
|
115
|
-
name = d.get('name')
|
|
116
|
-
type = d.get('type')
|
|
117
|
-
data = d.get('data')
|
|
118
|
-
|
|
119
|
-
if name:
|
|
120
|
-
defaults = None # once a name was specified no longer assign defaults
|
|
121
|
-
else:
|
|
122
|
-
if defaults:
|
|
123
|
-
name = defaults.get(i).get('name')
|
|
124
|
-
type = defaults.get(i).get('type')
|
|
125
|
-
else:
|
|
126
|
-
raise ValueError('extension number {i}, must have name and type')
|
|
127
|
-
|
|
128
|
-
#convert to subtype if they were given
|
|
129
|
-
subtype = self.extension_interpreters.get(type) or UnknownExtension
|
|
130
|
-
e = subtype.from_spec_fields(name=name, type=type, data=data)
|
|
131
|
-
extensions.append(e)
|
|
132
|
-
|
|
133
|
-
return extensions
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if __name__ == "__main__":
|
|
140
|
-
pacid_str = 'HTTPS://PAC.METTORIUS.COM/-DR/AB378/-MD/B-500/1235/-MS/AB/X:88/WWW/-MS/240:11/BB*ABCFD*A$HUR:25+B$CEL:99*BLUBB$TREX/A$HUR:25+B$CEL:99'
|
|
141
|
-
|
|
142
|
-
pac = PAC_Parser().parse_pac(pacid_str)
|
labfreed/PAC_ID/serialize.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
from .data_model import *
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class PAC_Serializer():
|
|
7
|
-
def to_url(self, pac:PACID|PACID_With_Extensions, extensions:list[Extension]=None, use_short_notation_for_extensions=False, uppercase_only=False) -> str:
|
|
8
|
-
if isinstance(pac, PACID_With_Extensions):
|
|
9
|
-
if extensions:
|
|
10
|
-
raise ValueError('Extensions were given twice, as part of PACID_With_Extension and as method parameter.')
|
|
11
|
-
extensions = pac.extensions
|
|
12
|
-
pac = pac.pac_id
|
|
13
|
-
issuer = pac.issuer
|
|
14
|
-
extensions_str = self._serialize_extensions(extensions, use_short_notation_for_extensions)
|
|
15
|
-
id_segments = self._serialize_id_segments(pac.identifier.segments)
|
|
16
|
-
out = f"HTTPS://PAC.{issuer}{id_segments}{extensions_str}"
|
|
17
|
-
if uppercase_only:
|
|
18
|
-
out = out.upper()
|
|
19
|
-
return out
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def _serialize_id_segments(self, segments):
|
|
23
|
-
out = ''
|
|
24
|
-
for s in segments:
|
|
25
|
-
if s.key:
|
|
26
|
-
out += f'/{s.key}:{s.value}'
|
|
27
|
-
else:
|
|
28
|
-
out += f'/{s.value}'
|
|
29
|
-
return out
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def _serialize_extensions(self, extensions:list[Extension], use_short_notation_for_extensions):
|
|
33
|
-
out = ''
|
|
34
|
-
short_notation = use_short_notation_for_extensions
|
|
35
|
-
for i, e in enumerate(extensions):
|
|
36
|
-
|
|
37
|
-
if short_notation and i==0:
|
|
38
|
-
if e.name=='N':
|
|
39
|
-
out += f'*{e.data}'
|
|
40
|
-
continue
|
|
41
|
-
else:
|
|
42
|
-
short_notation = False
|
|
43
|
-
if short_notation and i==1:
|
|
44
|
-
if e.name=='SUM':
|
|
45
|
-
out += f'*{e.data}'
|
|
46
|
-
continue
|
|
47
|
-
else:
|
|
48
|
-
short_notation = False
|
|
49
|
-
|
|
50
|
-
out += f'*{e.name}${e.type}/{e.data}'
|
|
51
|
-
return out
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def main():
|
|
56
|
-
pass
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if __name__ == "__main__":
|
|
60
|
-
main()
|
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
from datetime import datetime
|
|
2
|
-
from enum import Enum
|
|
3
|
-
import logging
|
|
4
|
-
import re
|
|
5
|
-
|
|
6
|
-
from pydantic import BaseModel, ValidationError, field_validator
|
|
7
|
-
from abc import ABC
|
|
8
|
-
|
|
9
|
-
from .unit_utilities import *
|
|
10
|
-
from ..PAC_ID.data_model import Extension
|
|
11
|
-
|
|
12
|
-
re_table_pattern = re.compile(f"(?P<tablename>[\w\.-]*?)\$\$(?P<header>[\w\.,\$:]*?)::(?P<body>.*)")
|
|
13
|
-
re_col_head_pattern = re.compile(f"(?P<name>[\w\.-]*?)\$(?P<unit>[\w\.]*)")
|
|
14
|
-
re_scalar_pattern = re.compile(f"(?P<name>[\w\.-]*?)\$(?P<unit>[\w\.]*?):(?P<value>.*)")
|
|
15
|
-
|
|
16
|
-
TREX_DATEFORMAT = '%Y%m%dT%H%M%S'
|
|
17
|
-
TREX_TIMEFORMAT = '%Y%m%d'
|
|
18
|
-
|
|
19
|
-
class TREX_types(Enum):
|
|
20
|
-
BOOL = 'T.B'
|
|
21
|
-
DATE = 'T.D'
|
|
22
|
-
TEXT = 'T.A'
|
|
23
|
-
ERROR = 'E'
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class T_REX_Segment_ParseError(BaseException):
|
|
28
|
-
pass
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
class TREX_Segment(BaseModel, ABC):
|
|
32
|
-
segment_name: str = None
|
|
33
|
-
|
|
34
|
-
def as_trex_segment_str(self, segment_name):
|
|
35
|
-
pass
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
class TREX_SimpleSegment(TREX_Segment):
|
|
39
|
-
type: str
|
|
40
|
-
value: str
|
|
41
|
-
|
|
42
|
-
@field_validator('type', mode='before')
|
|
43
|
-
def validate_type(t):
|
|
44
|
-
if isinstance(t, TREX_types):
|
|
45
|
-
t = t.value
|
|
46
|
-
return t
|
|
47
|
-
|
|
48
|
-
@staticmethod
|
|
49
|
-
def from_trex_segmentstring(segment_str):
|
|
50
|
-
|
|
51
|
-
matches = re_scalar_pattern.match(segment_str)
|
|
52
|
-
if not matches:
|
|
53
|
-
raise T_REX_Segment_ParseError("Segment is not a valid TREX Scalar")
|
|
54
|
-
|
|
55
|
-
name, type_, value = matches.groups()
|
|
56
|
-
|
|
57
|
-
out = TREX_SimpleSegment(type=type_, value=value, segment_name=name)
|
|
58
|
-
return out
|
|
59
|
-
|
|
60
|
-
@property
|
|
61
|
-
def value_as_builtin_or_quantity_type(self) -> datetime|bool|str|PydanticUncertainQuantity:
|
|
62
|
-
return _value_as_builtin_or_quantity(self.value, self.type)
|
|
63
|
-
|
|
64
|
-
def as_trex_segment_str(self, segment_name) -> str:
|
|
65
|
-
return f'{segment_name}${self.type}:{self.value}'
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
class TREX_Table(TREX_Segment):
|
|
69
|
-
col_names: list[str]
|
|
70
|
-
col_types: list[str]
|
|
71
|
-
data: list[list[str]]
|
|
72
|
-
|
|
73
|
-
@staticmethod
|
|
74
|
-
def from_trex_segmentstring( segment_str:str):
|
|
75
|
-
matches = re_table_pattern.match(segment_str)
|
|
76
|
-
if not matches:
|
|
77
|
-
raise T_REX_Segment_ParseError(f"Segment is not a valid TREX table: {segment_str}")
|
|
78
|
-
name, header, body = matches.groups()
|
|
79
|
-
|
|
80
|
-
column_heads = [re_col_head_pattern.match(colhead).groups() for colhead in header.split(':')]
|
|
81
|
-
col_names = [ch[0] for ch in column_heads]
|
|
82
|
-
col_types = [ch[1] for ch in column_heads]
|
|
83
|
-
|
|
84
|
-
data = [row.split(':') for row in body.split('::') ]
|
|
85
|
-
|
|
86
|
-
out = TREX_Table(col_names=col_names, col_types=col_types, data=data, segment_name=name)
|
|
87
|
-
return out
|
|
88
|
-
|
|
89
|
-
def n_rows(self) -> int:
|
|
90
|
-
return len(self.data)
|
|
91
|
-
|
|
92
|
-
def n_cols(self) -> int:
|
|
93
|
-
return len(self.col_names)
|
|
94
|
-
|
|
95
|
-
def row_data(self, row:int) -> list:
|
|
96
|
-
out = [_value_as_builtin_or_quantity(element, self.col_types[i]) for i, element in enumerate(self.data)]
|
|
97
|
-
return out
|
|
98
|
-
|
|
99
|
-
def col_data(self, col:str|int) -> list:
|
|
100
|
-
col_index = self._get_col_index(col)
|
|
101
|
-
type = self.col_types[col_index]
|
|
102
|
-
out = [_value_as_builtin_or_quantity(row[col_index],type) for row in self.data]
|
|
103
|
-
return out
|
|
104
|
-
|
|
105
|
-
def cell_data(self, row:int, col:str|int):
|
|
106
|
-
try:
|
|
107
|
-
col_index = self._get_col_index(col)
|
|
108
|
-
value = self.data[row][col_index]
|
|
109
|
-
type = self.col_types[col_index]
|
|
110
|
-
except ValueError:
|
|
111
|
-
logging.warning(f"row {row}, column {col} not found")
|
|
112
|
-
return None
|
|
113
|
-
|
|
114
|
-
return _value_as_builtin_or_quantity(value, type)
|
|
115
|
-
|
|
116
|
-
def _get_col_index(self, col:str|int):
|
|
117
|
-
if isinstance(col, str):
|
|
118
|
-
col_index = self.col_names.index(col)
|
|
119
|
-
elif isinstance(col, int):
|
|
120
|
-
col_index = col
|
|
121
|
-
else:
|
|
122
|
-
raise TypeError(f"Column must be specified as string or int: {col.__name__}")
|
|
123
|
-
return col_index
|
|
124
|
-
|
|
125
|
-
def as_trex_segment_str(self, name):
|
|
126
|
-
header = ':'.join([f'{el[0]}${el[1]}' for el in zip(self.col_names, self.col_types)])
|
|
127
|
-
date_rows = list()
|
|
128
|
-
for r in self.data:
|
|
129
|
-
row = ':'.join([str(cell) for cell in r])
|
|
130
|
-
date_rows.append(row)
|
|
131
|
-
data = '::'.join(date_rows)
|
|
132
|
-
s = f'{name}$${header}::{data}'
|
|
133
|
-
return s
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
class TREX(Extension, BaseModel):
|
|
138
|
-
name_:str
|
|
139
|
-
segments: dict[str,TREX_Segment]
|
|
140
|
-
|
|
141
|
-
@property
|
|
142
|
-
def name(self)->str:
|
|
143
|
-
return self.name_
|
|
144
|
-
|
|
145
|
-
@property
|
|
146
|
-
def type(self)->str:
|
|
147
|
-
return 'TREX'
|
|
148
|
-
|
|
149
|
-
@property
|
|
150
|
-
def data(self)->str:
|
|
151
|
-
seg_strings = list()
|
|
152
|
-
for s_name, s in self.segments.items():
|
|
153
|
-
seg_strings.append(s.as_trex_segment_str(s_name))
|
|
154
|
-
s_out = '+'.join(seg_strings)
|
|
155
|
-
return s_out
|
|
156
|
-
|
|
157
|
-
@staticmethod
|
|
158
|
-
def from_spec_fields(name, type, data):
|
|
159
|
-
if type != 'TREX':
|
|
160
|
-
logging.warning(f'Type {name} was given, but this extension should only be used with type "TREX". Will try to parse data as TREX')
|
|
161
|
-
|
|
162
|
-
if not data:
|
|
163
|
-
raise ValueError(f'T-REX must be a string of non zero length')
|
|
164
|
-
|
|
165
|
-
trex_str = data
|
|
166
|
-
|
|
167
|
-
# remove extension indicator. Precaution in case it is not done yet
|
|
168
|
-
if trex_str[0]=="*":
|
|
169
|
-
trex_str=trex_str[1:-1]
|
|
170
|
-
# remove line breaks. for editing T-REXes it's more convenient to have them in, so one never knows
|
|
171
|
-
trex_str = re.sub(r"\s+", "", trex_str)
|
|
172
|
-
|
|
173
|
-
segment_strings = trex_str.split('+')
|
|
174
|
-
out_segments = dict()
|
|
175
|
-
for s in segment_strings:
|
|
176
|
-
# there are only two valid options. The segment is a scalar or a table.
|
|
177
|
-
# Constructors do the parsing anyways and raise exceptions if invalid data
|
|
178
|
-
# try both options and then let it fail
|
|
179
|
-
try:
|
|
180
|
-
segment = TREX_SimpleSegment.from_trex_segmentstring(s)
|
|
181
|
-
except T_REX_Segment_ParseError:
|
|
182
|
-
segment = TREX_Table.from_trex_segmentstring(s)
|
|
183
|
-
out_segments[segment.segment_name] = segment
|
|
184
|
-
|
|
185
|
-
return TREX(name_=name, segments=out_segments)
|
|
186
|
-
|
|
187
|
-
def get_segment(self, segment_id:str) -> TREX_Segment:
|
|
188
|
-
return self.segments.get(segment_id)
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
class TREX_Struct(TREX_Segment):
|
|
194
|
-
"""Struct is a special interpretation of a T-REX Table with one row"""
|
|
195
|
-
wrapped_table:TREX_Table
|
|
196
|
-
|
|
197
|
-
@property
|
|
198
|
-
def segment_name_(self):
|
|
199
|
-
return self.wrapped_table.segment_name
|
|
200
|
-
|
|
201
|
-
@field_validator('wrapped_table')
|
|
202
|
-
def validate_table(table):
|
|
203
|
-
if len(table.data) != 1:
|
|
204
|
-
raise ValidationError("Too many input rows. Struct can only have one row")
|
|
205
|
-
return table
|
|
206
|
-
|
|
207
|
-
def get(self, key):
|
|
208
|
-
return self.wrapped_table.cell_data(0, key)
|
|
209
|
-
|
|
210
|
-
def keys(self):
|
|
211
|
-
return self.wrapped_table.col_names
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
def _to_datetime(trex_datetime):
|
|
217
|
-
try:
|
|
218
|
-
# return datetime.fromisoformat(trex_datetime) # should work with python 3.11
|
|
219
|
-
return datetime.strptime(trex_datetime, TREX_DATEFORMAT)
|
|
220
|
-
except (ValueError , TypeError) as e:
|
|
221
|
-
try:
|
|
222
|
-
return datetime.strptime(trex_datetime, TREX_TIMEFORMAT)
|
|
223
|
-
except (ValueError, TypeError):
|
|
224
|
-
return None
|
|
225
|
-
|
|
226
|
-
def _value_as_builtin_or_quantity(v:str|list[str], type:str) -> datetime|bool|str|PydanticUncertainQuantity:
|
|
227
|
-
match type:
|
|
228
|
-
case 'T.D':
|
|
229
|
-
return _to_datetime(v)
|
|
230
|
-
case 'T.B':
|
|
231
|
-
return v == 'T' or bool(v)
|
|
232
|
-
case 'T.A':
|
|
233
|
-
return v
|
|
234
|
-
case 'T.X':
|
|
235
|
-
raise NotImplementedError("Base36 encoded T-REX segment not implemented")
|
|
236
|
-
case 'E':
|
|
237
|
-
return v
|
|
238
|
-
case _:
|
|
239
|
-
return quantity_from_UN_CEFACT(v, type)
|
labfreed/TREXExtension/parse.py
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
import re
|
|
3
|
-
|
|
4
|
-
from .data_model import TREX, T_REX_Segment_ParseError, TREX_SimpleSegment, TREX_Table
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def from_trex_string(trex_str, enforce_type=True) -> TREX:
|
|
8
|
-
if not trex_str:
|
|
9
|
-
raise ValueError(f'T-REX must be a string of non zero length')
|
|
10
|
-
|
|
11
|
-
# remove extension indicator. Precaution in case it is not done yet
|
|
12
|
-
if trex_str[0]=="*":
|
|
13
|
-
trex_str=trex_str[1:-1]
|
|
14
|
-
# remove line breaks. for editing T-REXes it's more convenient to have them in, so one never knows
|
|
15
|
-
trex_str = trex_str.replace('\n','')
|
|
16
|
-
|
|
17
|
-
d = re.match('((?P<name>.+)\$(?P<type>.+)/)?(?P<data>.+)', trex_str).groupdict()
|
|
18
|
-
if not d:
|
|
19
|
-
raise ValueError('TREX is invalid.')
|
|
20
|
-
type = d.get('type')
|
|
21
|
-
name = d.get('name')
|
|
22
|
-
data = d.get('data')
|
|
23
|
-
|
|
24
|
-
if not type:
|
|
25
|
-
logging.warning('No type given. Assume its trex')
|
|
26
|
-
elif type != 'TREX' and enforce_type:
|
|
27
|
-
logging.error(f'Extension type {type} is not TREX. Aborting')
|
|
28
|
-
raise ValueError(f'Extension type {type} is not TREX.')
|
|
29
|
-
else:
|
|
30
|
-
logging.warning('Extension type {type} is not TREX. Try anyways')
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
segment_strings = data.split('+')
|
|
34
|
-
out_segments = dict()
|
|
35
|
-
for s in segment_strings:
|
|
36
|
-
# there are only two valid options. The segment is a scalar or a table.
|
|
37
|
-
# Constructors do the parsing anyways and raise exceptions if invalid data
|
|
38
|
-
# try both options and then let it fail
|
|
39
|
-
try:
|
|
40
|
-
segment = TREX_SimpleSegment.from_trex_segmentstring(s)
|
|
41
|
-
except T_REX_Segment_ParseError:
|
|
42
|
-
segment = TREX_Table.from_trex_segmentstring(s)
|
|
43
|
-
out_segments[segment.segment_name] = segment
|
|
44
|
-
trex = TREX(name_= name, segments=out_segments)
|
|
45
|
-
trex._trex_str = trex_str
|
|
46
|
-
return trex
|