samgis_core 3.0.6__py3-none-any.whl → 3.0.9__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.
@@ -0,0 +1,150 @@
1
+ import os
2
+ import subprocess
3
+ from pathlib import Path
4
+
5
+ from dotenv import load_dotenv
6
+
7
+ from samgis_core import app_logger
8
+ from samgis_core.utilities.type_hints import ListStr
9
+
10
+
11
+ load_dotenv()
12
+ root_folder = Path(globals().get("__file__", "./_")).absolute().parent.parent.parent
13
+ env_project_root_folder = os.getenv("PROJECT_ROOT_FOLDER", str(root_folder))
14
+ env_input_css_path = os.getenv("INPUT_CSS_PATH")
15
+
16
+
17
+ def assert_envs(envs_list: ListStr) -> None:
18
+ """
19
+ Assert env variables are not empty.
20
+
21
+ Args:
22
+ envs_list: list of env variables
23
+
24
+ Returns:
25
+
26
+ """
27
+ for current_env in envs_list:
28
+ try:
29
+ assert current_env is not None and current_env != ""
30
+ except AssertionError as aex:
31
+ app_logger.error(f"error on assertion for current_env: {current_env}.")
32
+ raise aex
33
+
34
+
35
+ def read_std_out_err(std_out_err: str, output_type: str, command: ListStr) -> None:
36
+ """
37
+ Capture the standard output or standard error content for a given system command pipe.
38
+
39
+ Args:
40
+ std_out_err: str
41
+ output_type: output type (stdout or stderr)
42
+ command: command executed
43
+
44
+ Returns:
45
+
46
+ """
47
+ output = std_out_err.split("\n")
48
+ app_logger.info(f"output type:{output_type} for command:{' '.join(command)}.")
49
+ for line in iter(output):
50
+ app_logger.info(f"output_content_home stdout:{line.strip()}.")
51
+ app_logger.info("########")
52
+
53
+
54
+ def run_command(commands_list: ListStr, capture_output: bool = True, text: bool = True, check: bool = True) -> None:
55
+ """
56
+ Run a system command and capture its output.
57
+ See https://docs.python.org/3.11/library/subprocess.html#subprocess.run for more details
58
+
59
+ Args:
60
+ commands_list: list of single string commands
61
+ capture_output: if true, stdout and stderr will be captured
62
+ text: if true, capture stdout and stderr as strings
63
+ check: If check is true, and the process exits with a non-zero exit code, a CalledProcessError exception will
64
+ be raised. Attributes of that exception hold the arguments, the exit code, and stdout and stderr if they
65
+ were captured.
66
+
67
+ Returns:
68
+
69
+ """
70
+ try:
71
+ output_content_home = subprocess.run(
72
+ commands_list,
73
+ capture_output=capture_output,
74
+ text=text,
75
+ check=check
76
+ )
77
+ read_std_out_err(output_content_home.stdout, "stdout", commands_list)
78
+ read_std_out_err(output_content_home.stderr, "stderr", commands_list)
79
+ except Exception as ex:
80
+ app_logger.error(f"ex:{ex}.")
81
+ raise ex
82
+
83
+
84
+ def build_frontend(
85
+ project_root_folder: str | Path,
86
+ input_css_path: str | Path,
87
+ output_dist_folder: Path = root_folder / "static" / "dist",
88
+ index_page_filename: str = "index.html",
89
+ output_css_filename: str = "output.css",
90
+ force_build: bool = False,
91
+ ) -> bool:
92
+ """
93
+ Build a static [Vue js](https://vuejs.org/), [tailwindcss](https://tailwindcss.com/) frontend.
94
+ If force_build is False, the function also check if index_page_filename and output_css_filename already exists:
95
+ in this case skip the build.
96
+
97
+ Args:
98
+ project_root_folder: Project folder that contains the static frontend
99
+ input_css_path: file path pointing to the input css file
100
+ output_dist_folder: dist folder path where to write the frontend bundle
101
+ index_page_filename: index html filename
102
+ output_css_filename: output css filename
103
+ force_build: useful to skip the frontend build
104
+
105
+ Returns:
106
+ state of the build (True in case of build completed, False in case of build skipped)
107
+
108
+ """
109
+ assert_envs([
110
+ str(project_root_folder),
111
+ str(input_css_path)
112
+ ])
113
+ project_root_folder = Path(project_root_folder)
114
+ index_html_pathfile = Path(output_dist_folder) / index_page_filename
115
+ output_css_pathfile = Path(output_dist_folder) / output_css_filename
116
+ if not force_build and output_css_pathfile.is_file() and index_html_pathfile.is_file():
117
+ app_logger.info("frontend ok, build_frontend not necessary...")
118
+ return False
119
+
120
+ # install deps
121
+ os.chdir(project_root_folder / "static")
122
+ current_folder = os.getcwd()
123
+ app_logger.info(f"current_folder:{current_folder}, install pnpm...")
124
+ run_command(["which", "npm"])
125
+ run_command(["npm", "install", "-g", "npm", "pnpm"])
126
+ app_logger.info(f"install pnpm dependencies...")
127
+ run_command(["pnpm", "install"])
128
+
129
+ # build frontend dist and assert for its correct build
130
+ output_css = str(output_dist_folder / output_css_filename)
131
+ output_index_html = str(output_dist_folder / index_page_filename)
132
+ output_dist_folder = str(output_dist_folder)
133
+ app_logger.info(f"pnpm: build '{output_dist_folder}'...")
134
+ run_command(["pnpm", "build"])
135
+ app_logger.info(f"pnpm: ls -l {output_index_html}:")
136
+ run_command(["ls", "-l", output_index_html])
137
+ cmd = ["pnpm", "tailwindcss", "-i", str(input_css_path), "-o", output_css]
138
+ app_logger.info(f"pnpm: {' '.join(cmd)}...")
139
+ run_command(["pnpm", "tailwindcss", "-i", str(input_css_path), "-o", output_css])
140
+ app_logger.info(f"pnpm: ls -l {output_css}:")
141
+ run_command(["ls", "-l", output_css])
142
+ app_logger.info(f"end!")
143
+ return True
144
+
145
+
146
+ if __name__ == '__main__':
147
+ build_frontend(
148
+ project_root_folder=Path(env_project_root_folder),
149
+ input_css_path=Path(env_input_css_path)
150
+ )
@@ -0,0 +1,43 @@
1
+ from pathlib import Path
2
+ import structlog
3
+
4
+ from samgis_core.utilities import session_logger
5
+
6
+
7
+ session_logger.setup_logging(json_logs=False, log_level="INFO")
8
+ logger = structlog.stdlib.get_logger(__name__)
9
+
10
+
11
+ def get_dependencies_freeze() -> dict:
12
+ """get a 'freeze.txt'-like dict of 'name':'version' metadata about installed packages."""
13
+ from importlib import metadata
14
+ return {dist.metadata["name"]: dist.version for dist in metadata.distributions()}
15
+
16
+
17
+ def get_requirements_txt(requirements_no_versions_filename: str | Path, requirements_output_filename: str | Path):
18
+ """
19
+ Write on disk a requirements.txt file with an updated list of dependencies from installed python packages.
20
+
21
+ Args:
22
+ requirements_no_versions_filename: full path of input requirements filename with no specified versions
23
+ requirements_output_filename: full path of output requirements.txt filename
24
+
25
+ Returns:
26
+
27
+ """
28
+ logger.info("start requirements.txt update...")
29
+ freeze_dict = get_dependencies_freeze()
30
+ logger.debug(f"freeze_dict:{freeze_dict}.")
31
+ with open(requirements_no_versions_filename) as req_src:
32
+ packages_no_requirements = req_src.read().split("\n")
33
+ requirements_output = {
34
+ name: version for name, version in sorted(freeze_dict.items()) if name in packages_no_requirements
35
+ }
36
+ logger.info(f"requirements to write:{requirements_output}.")
37
+ with open(requirements_output_filename, "w") as dst:
38
+ out = ""
39
+ for name, version in requirements_output.items():
40
+ out += f"{name}=={version}\n"
41
+ logger.debug(f"output requirements content:\n{out}!")
42
+ dst.write(out)
43
+ logger.info(f"written requirements to file:{requirements_output_filename}!")
@@ -140,6 +140,18 @@ def apply_coords(coords: ndarray, embedding: EmbeddingPILImage):
140
140
 
141
141
 
142
142
  def normalize_array(arr: ndarray, new_h: int | float = 255., type_normalization: str = "int") -> ndarray:
143
+ """
144
+ Normalize numpy array between 0 and 'new_h' value. Default dtype of output array is int
145
+
146
+
147
+ Args:
148
+ arr: input numpy array
149
+ new_h: max value of output array
150
+ type_normalization: default dtype of output array
151
+
152
+ Returns:
153
+ numpy array
154
+ """
143
155
  arr = arr.astype(float)
144
156
  arr_max = np.nanmax(arr)
145
157
  arr_min = np.nanmin(arr)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: samgis_core
3
- Version: 3.0.6
3
+ Version: 3.0.9
4
4
  Summary: SamGIS CORE
5
5
  License: MIT
6
6
  Author: alessandro trinca tornidor
@@ -6,12 +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=3aQ95XXpMNQoDKuhn1pJLJ25R1PM0gx03VMrakuopXs,5438
9
10
  samgis_core/utilities/plot_images.py,sha256=1pwTJC2_ju-QhJXDonvEw6FMK_9i1kQhLnC4tjn2EH0,3469
10
11
  samgis_core/utilities/serialize.py,sha256=aIjhEoibBpV_gpgOg6LiVxZCWjOkYxlzcboDZLQctJE,2689
11
12
  samgis_core/utilities/session_logger.py,sha256=mlzLaeSC2b_MF3wLywyXYQsSPIvEd3OnhlD7_4VhMzo,5704
12
13
  samgis_core/utilities/type_hints.py,sha256=anbm8pHFWr_C1upCmncYOK-q9Iq82fO9_yftTqXeizc,1649
13
- samgis_core/utilities/utilities.py,sha256=4EBnsTBontnmaZRVtI4VAbvISdmYyg2yEnn8Sb28dUc,4014
14
- samgis_core-3.0.6.dist-info/LICENSE,sha256=cmg7mi2IynvK5xYN_TJBikA008n6IJNjQIig1c3ge9Q,1083
15
- samgis_core-3.0.6.dist-info/METADATA,sha256=cF143cyh8z5hhFc3_ZXad95rLjfin9prTKMBBteZLfo,1109
16
- samgis_core-3.0.6.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
17
- samgis_core-3.0.6.dist-info/RECORD,,
14
+ samgis_core/utilities/update_requirements_txt.py,sha256=1tPnSSLVBxU3TB4Wpa2oJE8VYsqb0qspe6c2XdFHRBY,1751
15
+ samgis_core/utilities/utilities.py,sha256=Yi-nBdgz75BOfPbBPs3pFE6ag6KyZ-wK_FAgHkGtNQs,4298
16
+ samgis_core-3.0.9.dist-info/LICENSE,sha256=cmg7mi2IynvK5xYN_TJBikA008n6IJNjQIig1c3ge9Q,1083
17
+ samgis_core-3.0.9.dist-info/METADATA,sha256=YKV0W3lCTtE2Z3VYIcyHE7tBCMii348OLxzSTzm-v_A,1109
18
+ samgis_core-3.0.9.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
19
+ samgis_core-3.0.9.dist-info/RECORD,,