commonlib-reader 1.0.0__tar.gz → 1.0.1__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.1
2
2
  Name: commonlib-reader
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: Reader for Equinor commonlibapi.
5
5
  Home-page: https://github.com/equinor/commonlib-reader.git
6
6
  Author: Åsmund Våge Fannemel
@@ -11,13 +11,13 @@ 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)
14
+ Requires-Dist: msal_bearer (>0.2.1,<2.0.0)
15
15
  Requires-Dist: pandas (>=2.1.4,<3.0.0)
16
16
  Requires-Dist: requests (>=2.31.0,<3.0.0)
17
17
  Project-URL: Repository, https://github.com/equinor/commonlib-reader.git
18
18
  Description-Content-Type: text/markdown
19
19
 
20
- # commonlib-python
20
+ # 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
21
  Connector package for Equinor Commonlib api. Used for getting code tables.
22
22
 
23
23
  Some additional functionality for asset specific Tag categories and tag types is also included.
@@ -37,3 +37,4 @@ Poetry is preferred for developers. Clone and install with required packages for
37
37
 
38
38
  For testing with coverage run:
39
39
  `poetry run pytest --cov --cov-report=html`
40
+
@@ -1,4 +1,4 @@
1
- # commonlib-python
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
2
  Connector package for Equinor Commonlib api. Used for getting code tables.
3
3
 
4
4
  Some additional functionality for asset specific Tag categories and tag types is also included.
@@ -17,4 +17,4 @@ Poetry is preferred for developers. Clone and install with required packages for
17
17
  `poetry install`
18
18
 
19
19
  For testing with coverage run:
20
- `poetry run pytest --cov --cov-report=html`
20
+ `poetry run pytest --cov --cov-report=html`
@@ -33,7 +33,7 @@ class Facility:
33
33
  Raises:
34
34
  ValueError: If no data is found for the provided code.
35
35
  """
36
- self._data = Facility._get_facility(code=code)
36
+ self._data = Facility._get_facility_data(code=code)
37
37
  self._attributes = attributes_list_to_dict(self._data["attributes"])
38
38
 
39
39
  self.identity = self._data["identity"]
@@ -69,11 +69,11 @@ class Facility:
69
69
  Returns:
70
70
  List[Facility]: A list of Facility instances.
71
71
  """
72
- f = Facility._get_all_facilities()
72
+ f = Facility._get_all_facility_data()
73
73
  return [Facility(x["identity"]) for x in f]
74
74
 
75
75
  @classmethod
76
- def _get_all_facilities(cls) -> List[dict]:
76
+ def _get_all_facility_data(cls) -> List[dict]:
77
77
  """
78
78
  Retrieves all facility data, using a class level cache.
79
79
 
@@ -86,7 +86,7 @@ class Facility:
86
86
  return cls._cache
87
87
 
88
88
  @classmethod
89
- def _get_facility(cls, code) -> dict:
89
+ def _get_facility_data(cls, code) -> dict:
90
90
  """
91
91
  Retrieves the facility data for the given code.
92
92
 
@@ -99,7 +99,21 @@ class Facility:
99
99
  Raises:
100
100
  ValueError: If no data is found for the provided code.
101
101
  """
102
- for f in cls._get_all_facilities():
103
- if f["identity"] == code or f["name"] == code or f["description"] == code:
104
- return f
102
+
103
+ if isinstance(code, str):
104
+ for f in cls._get_all_facility_data():
105
+ if (
106
+ f["identity"] == code
107
+ or f["name"] == code
108
+ or f["description"] == code
109
+ ):
110
+ return f
111
+
112
+ for f in cls._get_all_facility_data():
113
+ for a in f["attributes"]:
114
+ if a["definitionName"] == "SAPPlant":
115
+ if a["displayValue"] == str(code):
116
+ return f
117
+ break
118
+
105
119
  raise ValueError(f"No data found for {code}")
@@ -1,5 +1,6 @@
1
1
  from typing import List, Union
2
2
  from commonlib_reader.utils import get_code, get_code_param, query_sql
3
+ import re
3
4
 
4
5
  # from commonlib_reader.utils import attributes_list_to_dict
5
6
 
@@ -9,9 +10,59 @@ _cache["TagCategory"] = {}
9
10
  _cache["TagFormat"] = {}
10
11
 
11
12
 
13
+ class TagFormatElement:
14
+ """A class representing the elements constructing a tag number.
15
+
16
+ Attributes:
17
+ description (str)
18
+ name (str)
19
+ IsValid (bool)
20
+ IsRequired (bool)
21
+ IsSelected (bool)
22
+
23
+ """
24
+
25
+ def __init__(self, data):
26
+ """
27
+ Initializes a TagFormatElement object.
28
+
29
+ Parameters:
30
+ data (dict): Data containing different properties of a tag format element.
31
+ """
32
+ self._data = data
33
+
34
+ self.description = data["Description"]
35
+ self.name = data["Name"]
36
+ self.IsValid = bool(data["IsValid"])
37
+ self.IsRequired = bool(data["Required"])
38
+ self.IsSelected = bool(data["SelectFlag"])
39
+
40
+ def get_TagFormat(self) -> "TagFormat":
41
+ tag_format_id = self._data["TagFormat_ID"]
42
+ res = query_sql(f"Select * from tagformat where Id = '{tag_format_id}'")
43
+ return TagFormat.get_tagformat(
44
+ res[0]["Description"], res[0]["Scope"], res[0]["TagCategory"]
45
+ )
46
+
47
+ @staticmethod
48
+ def get_TagFormatElements(
49
+ tag_format: Union["TagFormat", str]
50
+ ) -> List["TagFormatElement"]:
51
+ if isinstance(tag_format, TagFormat):
52
+ tag_format_id = tag_format.get_id()
53
+ else:
54
+ tag_format_id = tag_format
55
+
56
+ # q = f"SELECT * FROM [TagFormatElement] WHERE IsValid = 1 AND IsReleaseReady = 1 AND SelectFlag = 'true' AND TagFormat_ID = '{tag_format_id}'"
57
+
58
+ q = f"SELECT * FROM [TagFormatElement] WHERE IsValid = 1 AND IsReleaseReady = 1 AND TagFormat_ID = '{tag_format_id}'"
59
+ sql_result = query_sql(q)
60
+ return [TagFormatElement(x) for x in sql_result]
61
+
62
+
12
63
  class TagFormat:
13
64
  """
14
- A class that represents the format of a tag within the context of a Facility.
65
+ A class that represents the format of a tag number within the context of a Facility. Each tag number shall belong to a Tag Type.
15
66
 
16
67
  Attributes:
17
68
  inst_code (str): Installation code.
@@ -31,6 +82,10 @@ class TagFormat:
31
82
  inst_code (str): Installation code for this tag format.
32
83
  """
33
84
  self._data = data
85
+
86
+ # Cache tagformatelements
87
+ self._tfe = None
88
+
34
89
  # self._attributes = attributes_list_to_dict(self._data["attributes"])
35
90
 
36
91
  self.inst_code = inst_code
@@ -54,6 +109,47 @@ class TagFormat:
54
109
  def __str__(self):
55
110
  return f"{self.description} - {self.tag_category}"
56
111
 
112
+ def is_valid(self, tagNo: str):
113
+ return re.fullmatch(self.regex, tagNo)
114
+
115
+ def get_id(self) -> int:
116
+ """Get tagformat identity.
117
+
118
+ Returns:
119
+ int: Numeric identity for TagFormat. Used to cross reference databases.
120
+ """
121
+ res = query_sql(
122
+ f"SELECT Id from [TagFormat] where [TagFormat].[Identity] = '{self._data['identity']}'"
123
+ )
124
+ return res[0]["Id"]
125
+
126
+ def get_tagformat_elements(self) -> List[TagFormatElement]:
127
+ """Get TagFormatElement objects for this TagFormat.
128
+
129
+ Returns:
130
+ List[TagFormatElement]: List of TagFormatElement objects.
131
+ """
132
+ if self._tfe is None:
133
+ self._tfe = TagFormatElement.get_TagFormatElements(self)
134
+
135
+ return self._tfe
136
+
137
+ def split_tag(self, tagNo: str) -> dict:
138
+ # Change naming pattern for groups to match python re format
139
+ py_pattern = re.sub(r"\(\?<(\w+)>", r"(?P<\1>", self.regex_grouped)
140
+
141
+ # Change group number with group description
142
+
143
+ matches = re.match(py_pattern, tagNo)
144
+
145
+ d = {}
146
+ for e in self.get_tagformat_elements():
147
+ group_name = f"e{e.name}"
148
+ if group_name in matches.groupdict().keys():
149
+ d[e.description] = matches.group(group_name)
150
+
151
+ return d
152
+
57
153
  @staticmethod
58
154
  def get_tagformat(
59
155
  description: str, inst_code: str, tag_category: str
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "commonlib-reader"
3
- version = "1.0.0"
3
+ version = "1.0.1"
4
4
  description = "Reader for Equinor commonlibapi."
5
5
  authors = ["Åsmund Våge Fannemel <asmf@equinor.com>"]
6
6
  readme = "README.md"
@@ -9,13 +9,13 @@ repository = "https://github.com/equinor/commonlib-reader.git"
9
9
 
10
10
  [tool.poetry.dependencies]
11
11
  python = "^3.9"
12
- msal_bearer = ">0.2.1"
12
+ msal_bearer = ">0.2.1,<2.0.0"
13
13
  pandas = "^2.1.4"
14
14
  requests = "^2.31.0"
15
15
 
16
16
  [tool.poetry.group.test.dependencies]
17
17
  pytest = "^7.4.4"
18
- pytest-cov = "^5.0.0"
18
+ pytest-cov = "^6.0.0"
19
19
 
20
20
  [tool.poetry.group.dev.dependencies]
21
21
  black = "^24.1.1"