commonlib-reader 1.0.3__tar.gz → 1.0.6__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,26 +1,34 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: commonlib-reader
3
- Version: 1.0.3
4
- Summary: Reader for Equinor commonlibapi.
5
- Home-page: https://github.com/equinor/commonlib-reader.git
3
+ Version: 1.0.6
4
+ Summary: Reader for Equinor commonlib api.
5
+ License: MIT
6
6
  Author: Åsmund Våge Fannemel
7
7
  Author-email: asmf@equinor.com
8
- Requires-Python: >=3.9,<4.0
8
+ Requires-Python: >=3.9.2,<4.0.0
9
+ Classifier: License :: OSI Approved :: MIT License
9
10
  Classifier: Programming Language :: Python :: 3
10
- Classifier: Programming Language :: Python :: 3.9
11
11
  Classifier: Programming Language :: Python :: 3.10
12
12
  Classifier: Programming Language :: Python :: 3.11
13
13
  Classifier: Programming Language :: Python :: 3.12
14
- Requires-Dist: msal_bearer (>0.2.1,<2.0.0)
15
- Requires-Dist: pandas (>=2.1.4,<3.0.0)
16
- Requires-Dist: requests (>=2.31.0,<3.0.0)
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Requires-Dist: msal_bearer (>=1.3.3,<2.0.0)
16
+ Requires-Dist: pandas (>=2.1.4)
17
+ Requires-Dist: requests (>=2.32.4)
18
+ Requires-Dist: urllib3 (>=2.5.0)
17
19
  Project-URL: Repository, https://github.com/equinor/commonlib-reader.git
18
20
  Description-Content-Type: text/markdown
19
21
 
20
22
  # commonlib-reader [![SNYK dependency check](https://github.com/equinor/commonlib-reader/actions/workflows/snyk.yml/badge.svg)](https://github.com/equinor/commonlib-reader/actions/workflows/snyk.yml)
21
- Connector package for Equinor Commonlib api. Used for getting code tables.
23
+ Connector package for Equinor [Commonlib](https://commonlib.equinor.com/) [api](https://commonlibapi.equinor.com/swagger/index.html).
24
+
25
+ Current features:
26
+ - Reading code tables
27
+ - Getting facility data using the [Facility](commonlib_reader/facility.py) class
28
+ - [IMS source ](commonlib_reader/ims.py) lookup tables for facilities
29
+ - Getting Tag category, Tag type, Tag format, and Tag format element data. See [tag.py](commonlib_reader/tag.py)
30
+ - Getting [units of measure](commonlib_reader/ims.py) definitions.
22
31
 
23
- Some additional functionality for asset specific Tag categories and tag types is also included.
24
32
 
25
33
  ## Use
26
34
  Try it out by running the [demo](examples/demo.py).
@@ -1,7 +1,13 @@
1
1
  # commonlib-reader [![SNYK dependency check](https://github.com/equinor/commonlib-reader/actions/workflows/snyk.yml/badge.svg)](https://github.com/equinor/commonlib-reader/actions/workflows/snyk.yml)
2
- Connector package for Equinor Commonlib api. Used for getting code tables.
2
+ Connector package for Equinor [Commonlib](https://commonlib.equinor.com/) [api](https://commonlibapi.equinor.com/swagger/index.html).
3
+
4
+ Current features:
5
+ - Reading code tables
6
+ - Getting facility data using the [Facility](commonlib_reader/facility.py) class
7
+ - [IMS source ](commonlib_reader/ims.py) lookup tables for facilities
8
+ - Getting Tag category, Tag type, Tag format, and Tag format element data. See [tag.py](commonlib_reader/tag.py)
9
+ - Getting [units of measure](commonlib_reader/ims.py) definitions.
3
10
 
4
- Some additional functionality for asset specific Tag categories and tag types is also included.
5
11
 
6
12
  ## Use
7
13
  Try it out by running the [demo](examples/demo.py).
@@ -29,6 +29,7 @@ class Facility:
29
29
 
30
30
  Parameters:
31
31
  code (str): The code used to retrieve facility data from the data source.
32
+ STID plant code is preferred, but SAP codes and governmental facility names can also be used.
32
33
 
33
34
  Raises:
34
35
  ValueError: If no data is found for the provided code.
@@ -1,4 +1,4 @@
1
- from typing import List, Union
1
+ from typing import List, Optional, Union
2
2
  from commonlib_reader.utils import get_code, get_code_param, query_sql
3
3
  import re
4
4
 
@@ -110,6 +110,13 @@ class TagFormat:
110
110
  return f"{self.description} - {self.tag_category}"
111
111
 
112
112
  def is_valid(self, tagNo: str):
113
+ """Check if input tag number matches this tag format.
114
+ Args:
115
+ tagNo (str): Tag number to check.
116
+ Returns:
117
+ bool: True if tag number matches this tag format, else False.
118
+ """
119
+
113
120
  return re.fullmatch(self.regex, tagNo)
114
121
 
115
122
  def get_id(self) -> int:
@@ -149,10 +156,28 @@ class TagFormat:
149
156
  return regex_group_named
150
157
 
151
158
  def split_tag(self, tagNo: str) -> dict:
159
+ """Split input tag number into its elements based on this tag format.
160
+ Args:
161
+ tagNo (str): Tag number to split.
162
+ Returns:
163
+ dict: Dictionary where keys are tag format element descriptions and values are the corresponding parts of the tag number.
164
+ Raises:
165
+ ValueError: If the tag number does not match the tag format.
166
+ """
167
+
168
+ # if not self.is_valid(tagNo):
169
+ # raise ValueError(
170
+ # f"Tag number '{tagNo}' does not match format '{self}'"
171
+ # )
172
+
152
173
  # Change naming pattern for groups in commonlib-regex to match python re format
153
174
  py_pattern = re.sub(r"\(\?<(\w+)>", r"(?P<\1>", self.regex_grouped)
154
175
  matches = re.match(py_pattern, tagNo)
155
176
 
177
+ if matches is None:
178
+ raise ValueError(
179
+ f"Tag number '{tagNo}' does not match format '{self.regex_grouped}'"
180
+ )
156
181
  # Tag format element descriptions may contain space and can not be used as group name in regex. Instead create dict where description is key.
157
182
  d = {}
158
183
  for e in self.get_tagformat_elements():
@@ -178,7 +203,15 @@ class TagFormat:
178
203
  TagFormat: The requested tag format object.
179
204
  """
180
205
  t = TagCategory.get_category(inst_code=inst_code, name=tag_category)
181
- return t[0].get_format(description)
206
+ if len(t) == 0:
207
+ raise ValueError(f"TagCategory {tag_category} not found")
208
+
209
+ tf = t[0].get_format(description)
210
+ if tf is None:
211
+ raise ValueError(
212
+ f"TagFormat {description} not found in TagCategory {tag_category} for installation {inst_code}"
213
+ )
214
+ return tf
182
215
 
183
216
 
184
217
  class TagType:
@@ -234,12 +267,14 @@ class TagType:
234
267
  TagType: The TagType object matching the criteria.
235
268
  """
236
269
  t = TagCategory.get_category(inst_code=inst_code, name=tag_category)
270
+ if len(t) == 0:
271
+ raise ValueError(f"TagCategory {tag_category} not found")
237
272
  return t[0].get_type(name)
238
273
 
239
274
  @staticmethod
240
275
  def get_tagtype_no_cache(
241
276
  name: str, inst_code: str, tag_category: str = ""
242
- ) -> "TagType":
277
+ ) -> List["TagType"]:
243
278
  """
244
279
  Retrieve tag types for the given name and installation code without using the cache. Optionally filter by tag category.
245
280
 
@@ -268,8 +303,10 @@ class TagType:
268
303
 
269
304
  @staticmethod
270
305
  def get_master_tagtype(
271
- name: str, tag_category: str = "", technology: str = "Oil&Gas"
272
- ) -> "TagType":
306
+ name: str,
307
+ tag_category: Optional[str] = "",
308
+ technology: Optional[str] = "Oil&Gas",
309
+ ) -> List["TagType"]:
273
310
  """
274
311
  Retrieve master tag types based on name, category, and technology. If name is empty,
275
312
  sorts by name and returns all. If name is provided, filters by name.
@@ -319,7 +356,7 @@ class TagType:
319
356
  @staticmethod
320
357
  def get_master_tagtype_mappings(
321
358
  name: str, tag_category: str = "", technology: str = "Oil&Gas"
322
- ):
359
+ ) -> List["TagType"]:
323
360
  """
324
361
  Retrieve mappings of the master tag types based on name, category,
325
362
  and technology. Raises a ValueError if no relevant mappings are found
@@ -421,7 +458,7 @@ class TagCategory:
421
458
 
422
459
  return self._formats
423
460
 
424
- def get_format(self, description: str) -> TagFormat:
461
+ def get_format(self, description: str) -> Union[TagFormat, None]:
425
462
  """Get single tag format for this specific tag category.
426
463
 
427
464
  Args:
@@ -451,7 +488,7 @@ class TagCategory:
451
488
 
452
489
  return self._types
453
490
 
454
- def get_type(self, type: str) -> Union[TagType, None]:
491
+ def get_type(self, type: str) -> TagType:
455
492
  """Get single type from this specific tag category.
456
493
 
457
494
  Args:
@@ -464,13 +501,25 @@ class TagCategory:
464
501
  if curr_type.name == type:
465
502
  return curr_type
466
503
 
467
- return None
504
+ raise ValueError(
505
+ f"TagType {type} not found in TagCategory {self.name} for installation {self.inst_code}"
506
+ )
468
507
 
469
508
  def __str__(self):
470
509
  return f"{self.TagCategoryId}:{self.name} - {self.description}"
471
510
 
472
511
  @staticmethod
473
512
  def get_category(inst_code: str, name="") -> List["TagCategory"]:
513
+ """
514
+ Retrieve tag categories for a given installation code. If name is provided, filters by name.
515
+
516
+ Parameters:
517
+ inst_code (str): Installation code for which to retrieve tag categories.
518
+ name (str, optional): Name of the tag category to filter by. Defaults to an empty string.
519
+ Returns:
520
+ List[TagCategory]: A list of TagCategory objects.
521
+ """
522
+
474
523
  categories = _get_category_data(inst_code=inst_code)
475
524
  tc = [TagCategory(x, inst_code=inst_code) for x in categories]
476
525
  tc.sort(key=lambda x: x.TagCategoryId)
@@ -1,9 +1,9 @@
1
1
  import json
2
- from typing import Dict, List
2
+ from typing import Dict, List, Optional
3
3
  from urllib.parse import urljoin
4
4
 
5
5
  import requests
6
- from msal_bearer.BearerAuth import BearerAuth, get_login_name
6
+ from msal_bearer import BearerAuth, get_user_name
7
7
 
8
8
 
9
9
  def get_library_names():
@@ -17,7 +17,7 @@ def get_disciplines():
17
17
  return get_code("Discipline")
18
18
 
19
19
 
20
- def get_code(code: str, scope: str = None, name: str = None):
20
+ def get_code(code: str, scope: Optional[str] = None, name: Optional[str] = None):
21
21
  params = {}
22
22
  if scope is not None:
23
23
  params["scope"] = scope
@@ -61,7 +61,7 @@ def get_auth() -> BearerAuth:
61
61
  tenantID=tenantID,
62
62
  clientID=clientID,
63
63
  scopes=scopes,
64
- username=f"{get_login_name()}@equinor.com",
64
+ username=f"{get_user_name()}@equinor.com",
65
65
  )
66
66
 
67
67
  return auth
@@ -0,0 +1,28 @@
1
+ [project]
2
+ name = "commonlib-reader"
3
+ version = "1.0.6"
4
+ description = "Reader for Equinor commonlib api."
5
+ authors = [
6
+ { name = "Åsmund Våge Fannemel", email = "asmf@equinor.com" }
7
+ ]
8
+ readme = "README.md"
9
+ license = { text = "MIT" }
10
+ urls = {"Repository" = "https://github.com/equinor/commonlib-reader.git"}
11
+ requires-python = ">=3.9.2,<4.0.0"
12
+ dependencies = [
13
+ "msal_bearer>=1.3.3,<2.0.0",
14
+ "pandas>=2.1.4",
15
+ "requests>=2.32.4",
16
+ "urllib3>=2.5.0" # Pinned to avoid security issues in some older versions
17
+ ]
18
+
19
+ [tool.poetry.group.test.dependencies]
20
+ pytest = "^7.4.4"
21
+ pytest-cov = "^6.0.0"
22
+
23
+ [tool.poetry.group.dev.dependencies]
24
+ black = "^24.1.1"
25
+
26
+ [build-system]
27
+ requires = ["poetry-core"]
28
+ build-backend = "poetry.core.masonry.api"
@@ -1,25 +0,0 @@
1
- [tool.poetry]
2
- name = "commonlib-reader"
3
- version = "1.0.3"
4
- description = "Reader for Equinor commonlibapi."
5
- authors = ["Åsmund Våge Fannemel <asmf@equinor.com>"]
6
- readme = "README.md"
7
- packages = [{ include = "commonlib_reader" }]
8
- repository = "https://github.com/equinor/commonlib-reader.git"
9
-
10
- [tool.poetry.dependencies]
11
- python = "^3.9"
12
- msal_bearer = ">0.2.1,<2.0.0"
13
- pandas = "^2.1.4"
14
- requests = "^2.31.0"
15
-
16
- [tool.poetry.group.test.dependencies]
17
- pytest = "^7.4.4"
18
- pytest-cov = "^6.0.0"
19
-
20
- [tool.poetry.group.dev.dependencies]
21
- black = "^24.1.1"
22
-
23
- [build-system]
24
- requires = ["poetry-core"]
25
- build-backend = "poetry.core.masonry.api"