samgis_core 3.1.1__py3-none-any.whl → 3.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.
@@ -1,6 +1,7 @@
1
1
  import os
2
2
  import subprocess
3
3
  from pathlib import Path
4
+ from typing import Mapping
4
5
 
5
6
  from dotenv import load_dotenv
6
7
 
@@ -51,7 +52,7 @@ def read_std_out_err(std_out_err: str, output_type: str, command: ListStr) -> No
51
52
  app_logger.info("########")
52
53
 
53
54
 
54
- def run_command(commands_list: ListStr, capture_output: bool = True, text: bool = True, check: bool = True) -> None:
55
+ def run_command(commands_list: ListStr, capture_output: bool = True, text: bool = True, check: bool = True, env: Mapping[str, str] | None = None) -> None:
55
56
  """
56
57
  Run a system command and capture its output.
57
58
  See https://docs.python.org/3.11/library/subprocess.html#subprocess.run for more details
@@ -63,6 +64,7 @@ def run_command(commands_list: ListStr, capture_output: bool = True, text: bool
63
64
  check: If check is true, and the process exits with a non-zero exit code, a CalledProcessError exception will
64
65
  be raised. Attributes of that exception hold the arguments, the exit code, and stdout and stderr if they
65
66
  were captured.
67
+ env: environment variables
66
68
 
67
69
  Returns:
68
70
 
@@ -72,7 +74,8 @@ def run_command(commands_list: ListStr, capture_output: bool = True, text: bool
72
74
  commands_list,
73
75
  capture_output=capture_output,
74
76
  text=text,
75
- check=check
77
+ check=check,
78
+ env=env
76
79
  )
77
80
  read_std_out_err(output_content_home.stdout, "stdout", commands_list)
78
81
  read_std_out_err(output_content_home.stderr, "stderr", commands_list)
@@ -143,6 +146,50 @@ def build_frontend(
143
146
  return True
144
147
 
145
148
 
149
+ def get_installed_node():
150
+ """
151
+ Get the installed node bin folder given the environment variables:
152
+ - NODE_DIR (current node installed), without the bin subfolder
153
+ - NODE_DIR_PARENT (parent folder of the node installed versions, e.g. `~/.nvm/versions/node/` )
154
+
155
+ Returns:
156
+ node bin path
157
+
158
+ """
159
+ node_dir = os.getenv("NODE_DIR")
160
+ if not (node_dir and Path(node_dir).exists() and Path(node_dir).is_dir()):
161
+ app_logger.error(f"node_dir:{node_dir} not found.")
162
+ node_dir_env = os.getenv("NODE_DIR_PARENT")
163
+ assert node_dir_env is not None and node_dir_env != "", "NODE_DIR_PARENT/NODE_DIR env variable not found."
164
+ node_dir_parent = Path(os.getenv("NODE_DIR_PARENT"))
165
+ app_logger.error(f"try with '{node_dir_parent}' ...")
166
+ assert node_dir_parent.exists() and node_dir_parent.is_dir(), "node_dir_parent not found"
167
+ list_node_folders = [f.path for f in os.scandir(node_dir_parent) if f.is_dir()]
168
+ list_node_folders.sort()
169
+ assert isinstance(list_node_folders, list) and len(list_node_folders) > 0, "no node folders found."
170
+ node_dir = list_node_folders.pop()
171
+ node_dir_bin = Path(node_dir) / "bin"
172
+ assert node_dir_bin.exists() and node_dir_bin.is_dir(), f"node's bin not found or not a directory: {node_dir}."
173
+ return node_dir_bin
174
+
175
+
176
+ def get_path_with_node_dir():
177
+ """
178
+ Get the PATH environment variable with the node bin folder.
179
+
180
+ Returns:
181
+ PATH environment variable with the node bin folder
182
+
183
+ """
184
+ node_dir = get_installed_node()
185
+ path = os.getenv("PATH")
186
+ if path is None or path == "":
187
+ return f"{node_dir}:"
188
+ if "node" not in path:
189
+ return f"{node_dir}:{path}"
190
+ return path
191
+
192
+
146
193
  if __name__ == '__main__':
147
194
  build_frontend(
148
195
  project_root_folder=Path(env_project_root_folder),
@@ -63,12 +63,14 @@ def base64_encode(sb: str | bytes) -> bytes:
63
63
  return base64.b64encode(sb_bytes)
64
64
 
65
65
 
66
- def hash_calculate(arr) -> str | bytes:
66
+ def hash_calculate(arr_or_path, is_file: bool, read_mode: str = "rb") -> str | bytes:
67
67
  """
68
68
  Return computed hash from input variable (typically a numpy array).
69
69
 
70
70
  Args:
71
- arr: input variable
71
+ arr_or_path: input variable
72
+ is_file: whether input is a file or not
73
+ read_mode: mode for reading file
72
74
 
73
75
  Returns:
74
76
  computed hash from input variable
@@ -77,24 +79,41 @@ def hash_calculate(arr) -> str | bytes:
77
79
  from base64 import b64encode
78
80
  from numpy import ndarray as np_ndarray
79
81
 
80
- if isinstance(arr, np_ndarray):
81
- hash_fn = sha256(arr.data)
82
- elif isinstance(arr, dict):
82
+ if is_file:
83
+ with open(arr_or_path, read_mode) as file_to_check:
84
+ # read contents of the file
85
+ arr_or_path = file_to_check.read()
86
+ # # pipe contents of the file through
87
+ # try:
88
+ # return hashlib.sha256(data).hexdigest()
89
+ # except TypeError:
90
+ # app_logger.warning(
91
+ # f"TypeError, re-try encoding arg:{arr_or_path},type:{type(arr_or_path)}."
92
+ # )
93
+ # return hashlib.sha256(data.encode("utf-8")).hexdigest()
94
+
95
+ if isinstance(arr_or_path, np_ndarray):
96
+ hash_fn = sha256(arr_or_path.data)
97
+ elif isinstance(arr_or_path, dict):
83
98
  import json
84
99
 
85
- serialized = serialize(arr)
86
- variable_to_hash = json.dumps(serialized, sort_keys=True).encode('utf-8')
100
+ serialized = serialize(arr_or_path)
101
+ variable_to_hash = json.dumps(serialized, sort_keys=True).encode("utf-8")
87
102
  hash_fn = sha256(variable_to_hash)
88
- elif isinstance(arr, str):
103
+ elif isinstance(arr_or_path, str):
89
104
  try:
90
- hash_fn = sha256(arr)
105
+ hash_fn = sha256(arr_or_path)
91
106
  except TypeError:
92
- app_logger.warning(f"TypeError, re-try encoding arg:{arr},type:{type(arr)}.")
93
- hash_fn = sha256(arr.encode('utf-8'))
94
- elif isinstance(arr, bytes):
95
- hash_fn = sha256(arr)
107
+ app_logger.warning(
108
+ f"TypeError, re-try encoding arg:{arr_or_path},type:{type(arr_or_path)}."
109
+ )
110
+ hash_fn = sha256(arr_or_path.encode("utf-8"))
111
+ elif isinstance(arr_or_path, bytes):
112
+ hash_fn = sha256(arr_or_path)
96
113
  else:
97
- raise ValueError(f"variable 'arr':{arr} of type '{type(arr)}' not yet handled.")
114
+ raise ValueError(
115
+ f"variable 'arr':{arr_or_path} of type '{type(arr_or_path)}' not yet handled."
116
+ )
98
117
  return b64encode(hash_fn.digest())
99
118
 
100
119
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: samgis_core
3
- Version: 3.1.1
3
+ Version: 3.2.0
4
4
  Summary: SamGIS CORE
5
5
  License: MIT
6
6
  Author: alessandro trinca tornidor
@@ -6,14 +6,14 @@ samgis_core/prediction_api/sam_onnx_inference.py,sha256=WFRc8TNMOix4g0IAteNbDAs0
6
6
  samgis_core/utilities/__init__.py,sha256=nL9pzdB4SdEF8m5gCbtlVCtdGLg9JjPm-FNxKBsIBZA,32
7
7
  samgis_core/utilities/constants.py,sha256=0xBdfGYwCg4O0OXFtTcMVNj-kryjbajcxOZhMVkVP7U,227
8
8
  samgis_core/utilities/create_folders_if_not_exists.py,sha256=Ee_4l83suZvY5r24D26SNKGJVH3KAHregsiWs-0W-gQ,3202
9
- samgis_core/utilities/frontend_builder.py,sha256=IK0D1koYwT1ZKHqVl6wllFxMu8NBhNoI2-tC1ju0QeQ,5415
9
+ samgis_core/utilities/frontend_builder.py,sha256=XyNe8d4hbnz_yIFZF1dVlA-sgoJliridBINSLTMDBfU,7268
10
10
  samgis_core/utilities/plot_images.py,sha256=1pwTJC2_ju-QhJXDonvEw6FMK_9i1kQhLnC4tjn2EH0,3469
11
11
  samgis_core/utilities/serialize.py,sha256=aIjhEoibBpV_gpgOg6LiVxZCWjOkYxlzcboDZLQctJE,2689
12
12
  samgis_core/utilities/session_logger.py,sha256=mlzLaeSC2b_MF3wLywyXYQsSPIvEd3OnhlD7_4VhMzo,5704
13
13
  samgis_core/utilities/type_hints.py,sha256=anbm8pHFWr_C1upCmncYOK-q9Iq82fO9_yftTqXeizc,1649
14
14
  samgis_core/utilities/update_requirements_txt.py,sha256=w0KQSyiM6b70_Yinf06g0ujqS41bnlR0-dKwD4enYE0,4356
15
- samgis_core/utilities/utilities.py,sha256=Yi-nBdgz75BOfPbBPs3pFE6ag6KyZ-wK_FAgHkGtNQs,4298
16
- samgis_core-3.1.1.dist-info/LICENSE,sha256=cmg7mi2IynvK5xYN_TJBikA008n6IJNjQIig1c3ge9Q,1083
17
- samgis_core-3.1.1.dist-info/METADATA,sha256=vM5W2n2ECro5PA8xsEW7XaUGeC1RTSxrrFU-3p6m-ec,1021
18
- samgis_core-3.1.1.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
19
- samgis_core-3.1.1.dist-info/RECORD,,
15
+ samgis_core/utilities/utilities.py,sha256=R1BPhJIf81Bz0JH4WdwHJjrO14Fq0OPyvE28BSi_zDc,5148
16
+ samgis_core-3.2.0.dist-info/LICENSE,sha256=cmg7mi2IynvK5xYN_TJBikA008n6IJNjQIig1c3ge9Q,1083
17
+ samgis_core-3.2.0.dist-info/METADATA,sha256=9qhIOe3VIdSY87XD0OE_jtVkpgbprLeI-9NiETZioRo,1021
18
+ samgis_core-3.2.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
19
+ samgis_core-3.2.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.9.1
2
+ Generator: poetry-core 1.9.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any