commonlib-reader 1.0.9__tar.gz → 1.1.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: commonlib-reader
3
- Version: 1.0.9
3
+ Version: 1.1.0
4
4
  Summary: Reader for Equinor commonlib api.
5
5
  License: MIT
6
6
  Author: Åsmund Våge Fannemel
@@ -38,27 +38,64 @@ class Facility:
38
38
  self._data = Facility._get_facility_data(code=code)
39
39
  self._attributes = attributes_list_to_dict(self._data["attributes"])
40
40
 
41
- self.identity = self._data["identity"]
42
- self.name = self._data["name"]
43
- self.description = self._data["description"]
44
-
45
- self.SAPPlant = ""
46
- self.Field = ""
47
- self.ParentFacility = ""
48
- self.NPDID = ""
49
- self.GovernmentalName = ""
50
-
51
- for key, val in self._attributes.items():
52
- if key in self.__getattribute__("__dict__").keys():
53
- self.__setattr__(key, val)
54
-
55
- self.STID = self.identity # Alias for identity
41
+ if self.is_stid:
42
+ self.STID = self.identity
56
43
  self.instCode = self.identity # Alias for identity
57
44
  try:
58
- self.SAP = int(self.SAPPlant) # Alias for SAPPlant as integer
45
+ self.SAP = int(self.sap) # Alias for SAPPlant as integer
59
46
  except (TypeError, ValueError):
60
47
  pass
61
48
 
49
+ @property
50
+ def identity(self) -> str:
51
+ return self._data.get("identity", "")
52
+
53
+ @property
54
+ def name(self) -> str:
55
+ return self._data.get("name", "")
56
+
57
+ @property
58
+ def description(self) -> str:
59
+ return self._data.get("description", "")
60
+
61
+ @property
62
+ def stid(self) -> str:
63
+ return self.inst_code
64
+
65
+ @property
66
+ def inst_code(self) -> str:
67
+ if self.is_stid:
68
+ return self.identity
69
+ return ""
70
+
71
+ @property
72
+ def is_stid(self) -> bool:
73
+ return self.isSTID()
74
+
75
+ @property
76
+ def sap(self) -> str:
77
+ return self._attributes.get("SAPPlant", "")
78
+
79
+ @property
80
+ def field(self) -> str:
81
+ return self._attributes.get("Field", "")
82
+
83
+ @property
84
+ def parent_facility(self) -> str:
85
+ return self._attributes.get("ParentFacility", "")
86
+
87
+ @property
88
+ def npd_id(self):
89
+ return self._attributes.get("NPDID", "")
90
+
91
+ @property
92
+ def governmental_name(self) -> str:
93
+ return self._attributes.get("GovernmentalName", "")
94
+
95
+ @property
96
+ def ioc_plant(self) -> str:
97
+ return self._attributes.get("IOCPlant", "")
98
+
62
99
  def isSTID(self):
63
100
  """
64
101
  Checks if the facility is marked for STID.
@@ -72,11 +109,51 @@ class Facility:
72
109
 
73
110
  return False
74
111
 
75
- def __str__(self):
76
- return f"Facility: {self.identity}, Name: {self.name}, Description: {self.description}"
112
+ def resolve_gov_facility_name(self) -> str:
113
+ """
114
+ Retrieves the governmental facility name.
115
+
116
+ Returns:
117
+ str: The governmental facility name if available, otherwise an empty string.
118
+ """
119
+ if len(self.governmental_name) > 0:
120
+ return self.governmental_name
121
+
122
+ if self.parent_facility != "":
123
+ parent = Facility(self.parent_facility)
124
+ return parent.resolve_gov_facility_name()
125
+
126
+ raise ValueError(
127
+ f"Cannot resolve governmental facility name for facility {self.name}"
128
+ )
129
+
130
+ def resolve_inst_code(self) -> str:
131
+ if self.stid != "":
132
+ return self.stid
133
+ elif len(self.parent_facility) > 0:
134
+ f = Facility(self.parent_facility)
135
+ return f.resolve_inst_code()
136
+
137
+ raise ValueError(f"Cannot resolve installation code for facility {self.name}")
77
138
 
78
- @staticmethod
79
- def get_inst_code(facility: Union[str, int]) -> str:
139
+ def resolve_ioc_plant_name(self) -> str:
140
+ """
141
+ Retrieves the IOC plant name.
142
+
143
+ Returns:
144
+ str: The IOC plant name if available, otherwise an empty string.
145
+ """
146
+ if len(self.ioc_plant) > 0:
147
+ return self.ioc_plant
148
+
149
+ if self.parent_facility != "":
150
+ parent = Facility(self.parent_facility)
151
+ return parent.resolve_ioc_plant_name()
152
+
153
+ raise ValueError(f"Cannot resolve IOC plant name for facility {self.name}")
154
+
155
+ @classmethod
156
+ def get_inst_code(cls, facility: Union[str, int]) -> str:
80
157
  """
81
158
  Retrieves the STID installation code for the given facility identifier.
82
159
  If the facility is not a STID facility, it recursively retrieves the installation code of its parent facility.
@@ -89,15 +166,47 @@ class Facility:
89
166
  ValueError: If no facility is found for the provided identifier.
90
167
  """
91
168
 
92
- f = Facility(facility)
93
- if isinstance(f, Facility):
94
- if f.isSTID():
95
- return f.STID
96
- else:
97
- return Facility.get_inst_code(f.ParentFacility)
169
+ f = cls(facility)
170
+ if isinstance(f, cls):
171
+ return f.stid
98
172
 
99
173
  raise ValueError(f"No facility found for {facility}")
100
174
 
175
+ @classmethod
176
+ def get_child_facilities(
177
+ cls, parent_facility: Union[str, "Facility"], recurse=True
178
+ ) -> List["Facility"]:
179
+ """
180
+ Retrieves all child facilities of the given parent facility.
181
+
182
+ Parameters:
183
+ parent_facility (str or Facility): The parent identifier of the parent facility.
184
+ recurse (bool): Whether to recursively retrieve child
185
+ facilities. Default is True.
186
+ Returns:
187
+ List[Facility]: A list of child
188
+ """
189
+
190
+ if isinstance(parent_facility, Facility):
191
+ parent_facility = parent_facility.identity
192
+
193
+ children = []
194
+ for f in cls.get_all_facilities():
195
+ if f.parent_facility == parent_facility:
196
+ children.append(f)
197
+
198
+ if recurse and len(children) > 0:
199
+ grandkids = []
200
+ for c in children:
201
+ gc = cls.get_child_facilities(c)
202
+ if len(gc) > 0:
203
+ grandkids.append(gc)
204
+
205
+ if len(grandkids) > 0:
206
+ [children.append(gc) for gc in grandkids]
207
+
208
+ return children
209
+
101
210
  @classmethod
102
211
  def get_all_facilities(cls) -> List["Facility"]:
103
212
  """
@@ -118,7 +227,11 @@ class Facility:
118
227
  List[dict]: A list of dictionaries, each representing facility data.
119
228
  """
120
229
  if cls._cache is None:
121
- cls._cache = get_code("Facility")
230
+ code = get_code("Facility")
231
+ if isinstance(code, list):
232
+ cls._cache = code
233
+ else:
234
+ raise ValueError("Failed to retrieve facility data")
122
235
 
123
236
  return cls._cache
124
237
 
@@ -34,14 +34,29 @@ class IMS:
34
34
  self._attributes = attributes_list_to_dict(self._data["attributes"])
35
35
 
36
36
  self.isValid = data["isValid"]
37
-
38
- self.Facility = ""
39
- self.IMSType = ""
40
- self.Alias = ""
41
-
42
37
  for attr in self._data["attributes"]:
43
38
  self.__setattr__(attr["definitionName"], attr["displayValue"])
44
39
 
40
+ @property
41
+ def alias(self) -> str:
42
+ return getattr(self, "Alias", "")
43
+
44
+ @property
45
+ def type(self) -> str:
46
+ return getattr(self, "IMSType", "")
47
+
48
+ @property
49
+ def facility(self) -> str:
50
+ return getattr(self, "Facility", "")
51
+
52
+ @property
53
+ def is_valid(self) -> bool:
54
+ return getattr(self, "isValid", False)
55
+
56
+ @property
57
+ def seeq_datasource(self) -> str:
58
+ return getattr(self, "IMSSeeqDatasource", "")
59
+
45
60
  @staticmethod
46
61
  def from_facility(facility: Union[Facility, str, int]) -> "IMS":
47
62
  """Get IMS instance from STID code.
@@ -56,7 +71,7 @@ class IMS:
56
71
  IMS: IMS instance
57
72
  """
58
73
  if isinstance(facility, int):
59
- facility = Facility(facility).identity
74
+ facility = Facility(facility)
60
75
 
61
76
  if isinstance(facility, Facility):
62
77
  facility = facility.identity
@@ -94,12 +109,12 @@ class IMS:
94
109
  """
95
110
  i = IMS._get_ims_codes()
96
111
  if len(facility) > 0:
97
- return [x for x in i if x.Facility == facility]
112
+ return [x for x in i if x.facility == facility]
98
113
 
99
114
  return i
100
115
 
101
- @staticmethod
102
- def get_source(facility: str) -> str:
116
+ @classmethod
117
+ def get_source(cls, facility: str) -> str:
103
118
  """Get IMS name for a facility.
104
119
 
105
120
  Args:
@@ -112,11 +127,11 @@ class IMS:
112
127
  str: Name of IMS
113
128
  """
114
129
 
115
- l_ims = IMS.from_facility(facility=facility)
116
- return l_ims.Alias
130
+ l_ims = cls.from_facility(facility=facility)
131
+ return l_ims.alias
117
132
 
118
- @staticmethod
119
- def get_type(facility: str) -> str:
133
+ @classmethod
134
+ def get_type(cls, facility: str) -> str:
120
135
  """Get IMS type for a facility.
121
136
 
122
137
  Args:
@@ -128,5 +143,5 @@ class IMS:
128
143
  Returns:
129
144
  str: Type of IMS
130
145
  """
131
- l_ims = IMS.from_facility(facility=facility)
132
- return l_ims.IMSType
146
+ l_ims = cls.from_facility(facility=facility)
147
+ return l_ims.type
@@ -1,5 +1,10 @@
1
1
  from typing import List, Optional, Union
2
- from commonlib_reader.utils import get_code, get_code_param, query_sql
2
+ from commonlib_reader.utils import (
3
+ attributes_list_to_dict,
4
+ get_code,
5
+ get_code_param,
6
+ query_sql,
7
+ )
3
8
  import re
4
9
 
5
10
  # from commonlib_reader.utils import attributes_list_to_dict
@@ -31,12 +36,30 @@ class TagFormatElement:
31
36
  """
32
37
  self._data = data
33
38
 
34
- self.description = data["Description"]
35
- self.name = data["Name"]
36
39
  self.IsValid = bool(data["IsValid"])
37
40
  self.IsRequired = bool(data["Required"])
38
41
  self.IsSelected = bool(data["SelectFlag"])
39
42
 
43
+ @property
44
+ def name(self) -> str:
45
+ return self._data["Name"]
46
+
47
+ @property
48
+ def description(self) -> str:
49
+ return self._data["Description"]
50
+
51
+ @property
52
+ def is_valid(self) -> bool:
53
+ return self.IsValid
54
+
55
+ @property
56
+ def is_required(self) -> bool:
57
+ return self.IsRequired
58
+
59
+ @property
60
+ def is_selected(self) -> bool:
61
+ return self.IsSelected
62
+
40
63
  def get_TagFormat(self) -> "TagFormat":
41
64
  tag_format_id = self._data["TagFormat_ID"]
42
65
  res = query_sql(f"Select * from tagformat where Id = '{tag_format_id}'")
@@ -86,25 +109,33 @@ class TagFormat:
86
109
  # Cache tagformatelements
87
110
  self._tfe = None
88
111
 
89
- # self._attributes = attributes_list_to_dict(self._data["attributes"])
112
+ self._attributes = attributes_list_to_dict(self._data["attributes"])
90
113
 
91
- self.inst_code = inst_code
92
- self.description = data["description"]
114
+ self._inst_code = inst_code
93
115
 
94
- self.tag_category = ""
95
- self.syntax = ""
96
- self.regex = ""
97
- self.regex_grouped = ""
116
+ @property
117
+ def inst_code(self) -> str:
118
+ return self._inst_code
98
119
 
99
- for attr in self._data["attributes"]:
100
- if attr["definitionName"] == "TagCategory":
101
- self.tag_category = attr["displayValue"]
102
- elif attr["definitionName"] == "Syntax":
103
- self.syntax = attr["displayValue"]
104
- elif attr["definitionName"] == "Regex":
105
- self.regex = attr["displayValue"]
106
- elif attr["definitionName"] == "RegexGrouped":
107
- self.regex_grouped = attr["displayValue"]
120
+ @property
121
+ def description(self) -> str:
122
+ return self._data["description"]
123
+
124
+ @property
125
+ def tag_category(self) -> str:
126
+ return self._attributes.get("TagCategory", "")
127
+
128
+ @property
129
+ def syntax(self) -> str:
130
+ return self._attributes.get("Syntax", "")
131
+
132
+ @property
133
+ def regex(self) -> str:
134
+ return self._attributes.get("Regex", "")
135
+
136
+ @property
137
+ def regex_grouped(self) -> str:
138
+ return self._attributes.get("RegexGrouped", "")
108
139
 
109
140
  def __str__(self):
110
141
  return f"{self.description} - {self.tag_category}"
@@ -237,18 +268,36 @@ class TagType:
237
268
  inst_code (str): Installation code for which this tag type applies.
238
269
  """
239
270
  self._data = data
240
- self.inst_code = inst_code
241
-
242
- self.name = self._data["name"]
243
- self.description = self._data["description"]
244
- self.is_valid = self._data["isValid"]
245
- self.tag_category = ""
271
+ self._attributes = attributes_list_to_dict(self._data["attributes"])
272
+ self._inst_code = inst_code
246
273
 
247
274
  for attr in self._data["attributes"]:
248
275
  if attr["definitionName"] == "STIDTagCount":
249
276
  self.STID_tag_Count = int(attr["displayValue"])
250
- elif attr["definitionName"] == "TagCategory":
251
- self.tag_category = attr["displayValue"]
277
+
278
+ @property
279
+ def inst_code(self) -> str:
280
+ return self._inst_code
281
+
282
+ @property
283
+ def name(self) -> str:
284
+ return self._data["name"]
285
+
286
+ @property
287
+ def description(self) -> str:
288
+ return self._data["description"]
289
+
290
+ @property
291
+ def tag_category(self) -> str:
292
+ return self._attributes.get("TagCategory", "")
293
+
294
+ @property
295
+ def is_valid(self) -> bool:
296
+ return self._data["isValid"]
297
+
298
+ @property
299
+ def stid_tag_count(self) -> int:
300
+ return self.STID_tag_Count
252
301
 
253
302
  def __str__(self):
254
303
  return f"{self.name} - {self.description}"
@@ -425,11 +474,8 @@ class TagCategory:
425
474
  inst_code (str): Installation code for which this tag category applies.
426
475
  """
427
476
  self._data = data
428
- self.inst_code = inst_code
429
-
430
- self.name = self._data["name"]
431
- self.description = self._data["description"]
432
- self.is_valid = self._data["isValid"]
477
+ self._inst_code = inst_code
478
+ self._attributes = attributes_list_to_dict(self._data["attributes"])
433
479
 
434
480
  for attr in self._data["attributes"]:
435
481
  if attr["definitionName"] == "TagCategoryId":
@@ -444,6 +490,42 @@ class TagCategory:
444
490
  self._types = []
445
491
  self._formats = []
446
492
 
493
+ @property
494
+ def inst_code(self) -> str:
495
+ return self._inst_code
496
+
497
+ @property
498
+ def name(self) -> str:
499
+ return self._data["name"]
500
+
501
+ @property
502
+ def description(self) -> str:
503
+ return self._data["description"]
504
+
505
+ @property
506
+ def is_valid(self) -> bool:
507
+ return self._data["isValid"]
508
+
509
+ @property
510
+ def tag_category_id(self) -> int:
511
+ id = self._attributes.get("TagCategoryId")
512
+ if id is None:
513
+ raise ValueError("TagCategoryId attribute not found")
514
+
515
+ return int(id)
516
+
517
+ @property
518
+ def tag_category_dlg(self) -> str:
519
+ return self._attributes.get("TagCategoryDlg", "")
520
+
521
+ @property
522
+ def individ_flag(self) -> str:
523
+ return self._attributes.get("IndividFlag", "")
524
+
525
+ @property
526
+ def sap_fl_category(self) -> str:
527
+ return self._attributes.get("SapFlCategory", "")
528
+
447
529
  def get_formats(self) -> List[TagFormat]:
448
530
  """Get tag formats for this specific tag category.
449
531
 
@@ -506,7 +588,7 @@ class TagCategory:
506
588
  )
507
589
 
508
590
  def __str__(self):
509
- return f"{self.TagCategoryId}:{self.name} - {self.description}"
591
+ return f"{self.tag_category_id}:{self.name} - {self.description}"
510
592
 
511
593
  @staticmethod
512
594
  def get_category(inst_code: str, name="") -> List["TagCategory"]:
@@ -522,7 +604,7 @@ class TagCategory:
522
604
 
523
605
  categories = _get_category_data(inst_code=inst_code)
524
606
  tc = [TagCategory(x, inst_code=inst_code) for x in categories]
525
- tc.sort(key=lambda x: x.TagCategoryId)
607
+ tc.sort(key=lambda x: x.tag_category_id)
526
608
  if name is None:
527
609
  pass
528
610
  elif isinstance(name, str) and len(name) > 0:
@@ -1,18 +1,12 @@
1
- from commonlib_reader.utils import get_code
1
+ from typing import Union
2
2
 
3
- # from commonlib_reader.utils import attributes_list_to_dict
3
+ from commonlib_reader.utils import get_code
4
4
 
5
5
 
6
6
  class Unit:
7
7
  _units = []
8
8
 
9
- def __init__(self, data: dict):
10
- self.name = ""
11
- self.description = ""
12
-
13
- self.identity = ""
14
- self.quantity = ""
15
-
9
+ def __init__(self, data: Union[dict, str]):
16
10
  if isinstance(data, str):
17
11
  s = [x for x in Unit.get_units() if x.name == data]
18
12
  if len(s) > 0:
@@ -22,14 +16,42 @@ class Unit:
22
16
 
23
17
  if isinstance(data, dict):
24
18
  self._data = data
25
- # self._attributes = attributes_list_to_dict(self._data["attributes"])
26
-
27
19
  for key in data.keys():
28
20
  if key == "attributes":
29
21
  for attr in data["attributes"]:
30
22
  self.__setattr__(attr["definitionName"], attr["displayValue"])
31
23
  else:
32
24
  self.__setattr__(key, data[key])
25
+ else:
26
+ raise ValueError("Input data must be a dict from codetable")
27
+
28
+ @property
29
+ def name(self):
30
+ return getattr(self, "_name", "")
31
+
32
+ @name.setter
33
+ def name(self, value):
34
+ self._name = value
35
+
36
+ @property
37
+ def description(self):
38
+ return getattr(self, "_description", "")
39
+
40
+ @description.setter
41
+ def description(self, value):
42
+ self._description = value
43
+
44
+ @property
45
+ def identity(self):
46
+ return getattr(self, "_identity", "")
47
+
48
+ @identity.setter
49
+ def identity(self, value):
50
+ self._identity = value
51
+
52
+ @property
53
+ def quantity(self):
54
+ return getattr(self, "Quantity", "")
33
55
 
34
56
  @classmethod
35
57
  def get_units(cls):
@@ -9,7 +9,8 @@ from commonlib_reader.connector import get_connector
9
9
 
10
10
  def get_library_names():
11
11
  library_names = get_connector().get_json(url="/api/Library/NameList")
12
- library_names.sort()
12
+ if isinstance(library_names, list):
13
+ library_names.sort()
13
14
  return library_names
14
15
 
15
16
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "commonlib-reader"
3
- version = "1.0.9"
3
+ version = "1.1.0"
4
4
  description = "Reader for Equinor commonlib api."
5
5
  authors = [
6
6
  { name = "Åsmund Våge Fannemel", email = "asmf@equinor.com" }