berryworld 1.0.0.181358__py3-none-any.whl → 1.0.0.182055__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.
berryworld/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
1
  from .xml_parser import XMLparser
2
- from .handy_mix import HandyMix
2
+ from .handy_mix import HandyMix, URLResolver
3
3
  from .transportation_solver import TransportationAlgorithm
4
4
  from .allocation_solver import AllocationSolver
5
5
  from .pickle_management import PickleManagement
@@ -24,3 +24,4 @@ from .vivantio import Vivantio
24
24
  from .teams_logging import TeamsLogging
25
25
  from .vivantio_logging import VivantioLogging
26
26
  from .snowflake_conn import SnowflakeConn
27
+ from .logging import PythonLogs
berryworld/handy_mix.py CHANGED
@@ -234,3 +234,26 @@ class HandyMix:
234
234
  ~data[float_col].isnull(), float_col].astype(str).str.replace('.0', '', regex=True)
235
235
 
236
236
  return data
237
+
238
+
239
+ class URLResolver:
240
+ @staticmethod
241
+ def resolve_url_requested(url_requested, endpoint_requested):
242
+ """Resolve the URL requested.
243
+ :param url_requested: The base url that includes the project root
244
+ :param endpoint_requested: The endpoint that will be added to the base url
245
+ """
246
+ url_requested = str(url_requested)
247
+ endpoint_requested = str(endpoint_requested)
248
+
249
+ domains = ["berryworld.com", "berryworld.net"]
250
+
251
+ for domain in domains:
252
+ if domain in url_requested:
253
+ if url_requested.endswith('/'):
254
+ url_requested = url_requested.rstrip('/')
255
+
256
+ url_requested = f"{url_requested}{endpoint_requested}"
257
+ break
258
+
259
+ return url_requested
berryworld/logging.py ADDED
@@ -0,0 +1,31 @@
1
+
2
+
3
+
4
+ class PythonLogs:
5
+ """ Register the Python Logs """
6
+ def __init__(self, conn, batch_process):
7
+ self.conn = conn
8
+ self.batch_process = batch_process
9
+ self.batch_log_id = None
10
+
11
+ def start(self, log_type):
12
+ """ Start the Python Log
13
+ :param log_type: Type of the log
14
+ """
15
+ batch_log = self.conn.run_statement(
16
+ f"EXEC d365bc.spPythonBatchLogInsert @DataType = {self.batch_process}, @LogType = '{log_type}'",
17
+ commit_as_transaction=False)
18
+ self.batch_log_id = batch_log['OUTPUT'][0]
19
+
20
+ def end(self, batch_log_id=None):
21
+ """ End the Python Log
22
+ :param batch_log_id: Batch Log Id
23
+ """
24
+ if batch_log_id is None:
25
+ batch_log_id = self.batch_log_id
26
+ if batch_log_id is None:
27
+ raise Exception('Batch Log Id is not set')
28
+
29
+ self.conn.run_statement(
30
+ f"EXEC d365bc.spPythonBatchLogUpdate @BatchLogId = {batch_log_id}",
31
+ commit_as_transaction=False)
@@ -1,6 +1,7 @@
1
1
  import os
2
- import shutil
3
2
  import time
3
+ import shutil
4
+ import pandas as pd
4
5
  from pathlib import Path
5
6
  from datetime import datetime
6
7
 
@@ -9,13 +10,13 @@ class PersistentStorage:
9
10
  """ Connect to Persistent Storage """
10
11
 
11
12
  def __init__(self, base=None):
12
- """ Initialize the class
13
- -----------------------------
13
+ """ Workout the root and the base path
14
+ :param base: Base path to be used for the project
14
15
  """
15
16
  if os.name == 'nt':
16
17
  root = os.getcwd()
17
18
  else:
18
- root = '/mnt/datascience-persistent-store-file-share/'
19
+ root = os.path.join(os.getcwd(), 'mnt/datascience-persistent-store-file-share')
19
20
 
20
21
  if base is not None:
21
22
  self.base_path = os.path.join(root, base)
@@ -24,47 +25,64 @@ class PersistentStorage:
24
25
 
25
26
  self.connect()
26
27
 
27
- def format_path(self, path, sub_path=None):
28
- if self.base_path in path:
29
- format_path = path
28
+ def format_path(self, path_, sub_path=None):
29
+ """ Format the path
30
+ :param path_: Path to be formatted
31
+ :param sub_path: Sub path to be added to the path
32
+ """
33
+ if self.base_path in path_:
34
+ format_path = path_
30
35
  else:
31
- format_path = os.path.join(self.base_path, path)
36
+ format_path = os.path.join(self.base_path, path_)
32
37
 
33
38
  if sub_path is not None:
34
39
  format_path = os.path.join(format_path, sub_path)
35
40
 
36
41
  return format_path
37
42
 
38
- def if_exists(self, path):
39
- path = self.format_path(path)
40
- obj = Path(path)
43
+ def if_exists(self, path_='.'):
44
+ """ Check if the path exists
45
+ :param path_: Path to be checked
46
+ """
47
+ path_ = os.path.join(self.base_path, str(self.format_path(path_)))
48
+ obj = Path(path_)
41
49
  status = obj.exists()
42
50
  return status
43
51
 
44
52
  def connect(self):
53
+ """ Check if the base path exists """
45
54
  if not self.if_exists(self.base_path):
46
55
  raise ValueError("The current project doesn't have access to persistent storage, please check YAML file.")
47
56
 
48
57
  def return_base_path(self):
58
+ """ Return the base path """
49
59
  return self.base_path
50
60
 
51
- def return_input_path(self, path):
52
- path = self.format_path(path)
53
- return path
61
+ def return_input_path(self, path_):
62
+ """ Return the input path
63
+ :param path_: Path to be returned
64
+ """
65
+ path_ = self.format_path(path_)
66
+ return path_
54
67
 
55
- def create_folder(self, path):
68
+ def create_folder(self, path_='.'):
69
+ """ Create a folder
70
+ :param path_: Path to create the folder
71
+ """
56
72
  created = True
57
- folder_path = self.format_path(path)
73
+ folder_path = os.path.join(self.base_path, str(self.format_path(path_)))
58
74
  try:
59
75
  Path(folder_path).mkdir(parents=True, exist_ok=True)
60
76
  except Exception as e:
61
77
  print(e)
62
78
  created = False
63
-
64
79
  return created
65
80
 
66
- def list_folders(self, path):
67
- folder_path = self.format_path(path)
81
+ def list_folders(self, path_='.'):
82
+ """ List all the folders in the path
83
+ :param path_: Path to list the folders
84
+ """
85
+ folder_path = os.path.join(self.base_path, str(self.format_path(path_)))
68
86
  try:
69
87
  if self.if_exists(folder_path):
70
88
  folders = [f for f in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, f))]
@@ -76,22 +94,27 @@ class PersistentStorage:
76
94
 
77
95
  return folders
78
96
 
79
- def create_file(self, data, path, file_name=None):
97
+ def create_file(self, data_, path_='.', file_name=None):
98
+ """ Create a file
99
+ :param data_: Data to be written to the file
100
+ :param path_: Path to create the file
101
+ :param file_name: Name of the file to be created
102
+ """
80
103
  created = True
81
- folder_path = self.format_path(path)
104
+ folder_path = os.path.join(self.base_path, str(self.format_path(path_)))
82
105
  if file_name is None:
83
106
  file_path = os.path.join(folder_path, datetime.now().strftime("%H_%M_%S_%f"))
84
107
  else:
85
108
  file_path = os.path.join(folder_path, file_name)
86
109
 
87
110
  try:
88
- if type(data) == bytes:
111
+ if type(data_) == bytes:
89
112
  with open(file_path, 'wb') as f:
90
- f.write(data)
113
+ f.write(data_)
91
114
  f.close()
92
115
  else:
93
116
  with open(file_path, 'w') as f:
94
- f.write(data)
117
+ f.write(data_)
95
118
  f.close()
96
119
  except Exception as e:
97
120
  print(e)
@@ -99,8 +122,11 @@ class PersistentStorage:
99
122
 
100
123
  return created
101
124
 
102
- def list_files(self, path):
103
- folder_path = self.format_path(path)
125
+ def list_files(self, path_='.'):
126
+ """ List all the files in the path
127
+ :param path_: Path to list the files
128
+ """
129
+ folder_path = os.path.join(self.base_path, str(self.format_path(path_)))
104
130
  try:
105
131
  if self.if_exists(folder_path):
106
132
  files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
@@ -113,8 +139,14 @@ class PersistentStorage:
113
139
  return files
114
140
 
115
141
  def move_files(self, source_folder, target_folder, source_files=None, move_all=False):
116
- source_path = self.format_path(source_folder)
117
- target_path = self.format_path(target_folder)
142
+ """ Move files from source to target folder
143
+ :param source_folder: Source folder
144
+ :param target_folder: Target folder
145
+ :param source_files: Files to be moved
146
+ :param move_all: Move all files
147
+ """
148
+ source_path = str(self.format_path(source_folder))
149
+ target_path = str(self.format_path(target_folder))
118
150
  if source_files is None:
119
151
  source_files = self.list_files(source_folder)
120
152
  all_source_files = source_files
@@ -144,6 +176,10 @@ class PersistentStorage:
144
176
  return moved_files
145
177
 
146
178
  def move_file(self, source_file_path, target_file_path):
179
+ """ Move a file from source to target path
180
+ :param source_file_path: Source file path
181
+ :param target_file_path: Target file path
182
+ """
147
183
  try:
148
184
  if self.if_exists(source_file_path) & Path(source_file_path).is_file():
149
185
  shutil.move(source_file_path, target_file_path)
@@ -152,16 +188,22 @@ class PersistentStorage:
152
188
  print(e)
153
189
  return False
154
190
 
155
- def get_create_time(self, path):
156
- path = self.format_path(path)
157
- if self.if_exists(path):
158
- create_time = os.path.getmtime(path)
191
+ def get_folder_creation(self, path_):
192
+ """ Get the create time of the file
193
+ :param path_: Path to get the create time
194
+ """
195
+ path_ = str(self.format_path(path_))
196
+ if self.if_exists(path_):
197
+ create_time = pd.to_datetime(time.ctime(os.path.getctime(path_)))
159
198
  return create_time
160
199
  else:
161
200
  return None
162
201
 
163
- def delete_folder(self, path):
164
- folder_path = self.format_path(path)
202
+ def delete_folder(self, path_):
203
+ """ Delete a folder
204
+ :param path_: Path to delete the folder
205
+ """
206
+ folder_path = os.path.join(self.base_path, str(self.format_path(path_)))
165
207
  delete = True
166
208
  try:
167
209
  if self.if_exists(folder_path):
@@ -172,14 +214,40 @@ class PersistentStorage:
172
214
 
173
215
  return delete
174
216
 
175
- def delete_file(self, path):
176
- file_path = self.format_path(path)
217
+ def delete_file(self, path_):
218
+ """ Delete a file
219
+ :param path_: Path to delete the file
220
+ """
221
+ file_path = os.path.join(self.base_path, str(self.format_path(path_)))
177
222
  delete = True
178
223
  try:
179
- if self.if_exists(file_path) & Path(path).is_file():
224
+ if self.if_exists(file_path) & Path(path_).is_file():
180
225
  os.remove(file_path)
181
226
  except Exception as e:
182
227
  print(e)
183
228
  delete = False
184
229
 
185
230
  return delete
231
+
232
+ def get_file_creation(self, path_):
233
+ """ Get when a file was created
234
+ :param path_: Path to the file
235
+ """
236
+ path_ = str(self.format_path(path_))
237
+ if self.if_exists(path_):
238
+ create_time = pd.to_datetime(time.ctime(os.path.getctime(path_)))
239
+ return create_time
240
+ else:
241
+ return None
242
+
243
+ def delete_older_files(self, path_, date):
244
+ """ Archive the files
245
+ :param path_: Path to the files
246
+ :param date: Date to archive the files
247
+ """
248
+ file_path = os.path.join(self.base_path, str(self.format_path(path_)))
249
+
250
+ files_to_remove = [file for file in os.listdir(file_path) if
251
+ date >= self.get_file_creation(os.path.join(file_path, file))]
252
+ for file in files_to_remove:
253
+ self.delete_file(os.path.join(file_path, file))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: berryworld
3
- Version: 1.0.0.181358
3
+ Version: 1.0.0.182055
4
4
  Summary: Handy classes to improve ETL processes
5
5
  Home-page: https://www.berryworld.com
6
6
  Author: BerryWorld ltd
@@ -1,4 +1,4 @@
1
- berryworld/__init__.py,sha256=MMGWZ2WI82CgxWapFBsC3913rb3vspOWEQwAa8yMSTw,1141
1
+ berryworld/__init__.py,sha256=FxoH2n864zRK6cVG47s8ciwDldfMB3zVzfa3ZRwtLWk,1185
2
2
  berryworld/aks_logs.py,sha256=Gb2_cokiZbEX01Yoptd0MxpDociaug-GrXdwliyxFBo,2293
3
3
  berryworld/allocation_solver.py,sha256=asFtaCAze6-eHUGWXA0kAp67UBS-Upj1KKdrVLj_ttQ,8513
4
4
  berryworld/app_logs.py,sha256=MKzKPYd3JuPfOQNAapIgaeZeFHw1z_w2mbn9I6QCADE,4180
@@ -9,10 +9,11 @@ berryworld/devops.py,sha256=BAsVonVwCXoApUOovkt-BCzwc6KnXjxRDGff_ejSGw8,9719
9
9
  berryworld/email_con.py,sha256=uSBzs_Ijz9pUPWt9e7U3TCB7i6q7hU1bB5vhsTB_tmw,14448
10
10
  berryworld/email_logging.py,sha256=LeSrTExhQhar49gJR2wGC1dS0lqsNpFl9pS3eYWqnuo,4936
11
11
  berryworld/generate_env.py,sha256=Tk9Z_u7cA4Ve8YYTyLH2qwmLVAuYoTIWoFc0h8Va8lY,7842
12
- berryworld/handy_mix.py,sha256=lRUes_V4jL-3ADOzwiKqi1Dukx-HWXvmf9-4s4MLwsA,9740
12
+ berryworld/handy_mix.py,sha256=4ou8_pwRLmV809ZVgWdRgH9Dlb6TpPOaarCjcREEwG4,10510
13
+ berryworld/logging.py,sha256=tOxzaFALQm3aVVEc3I7t8MU6PwgUI9VSnzNCH9yRryo,1013
13
14
  berryworld/logic_apps.py,sha256=a0uU4tNO3v2w7grdBv-OOx4hUf7VBIerJpwZ9U-29dQ,14591
14
15
  berryworld/microsoft_teams.py,sha256=8uPo0yku-euBj2VdzBoZCeX3IcsCCOqISLqaVZUVxfA,16030
15
- berryworld/persistent_storage.py,sha256=_lGdXa7IyxfMF3xNF9y26X_z9RDb2Ah7R0oF61HR8Gc,5764
16
+ berryworld/persistent_storage.py,sha256=uGDARPP_CxuIykvRhwTMoQE8AfC6IyGNaWZxRRzOzJc,8627
16
17
  berryworld/pickle_management.py,sha256=O49ojVtTqYCT510rVRTbZWWaur_-5q3HSVG03Azn8mQ,2393
17
18
  berryworld/postgres_connection.py,sha256=whKDnchd5Feqpmxpoh2vlyn36EKHR-dVEULYq0N_4wA,8287
18
19
  berryworld/power_automate.py,sha256=9rDuRy0v-Ttq-SThid4lOB_tD4ibkyEmobiROpa--g4,25414
@@ -29,8 +30,8 @@ tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
29
30
  tests/test_allocation_config.py,sha256=e12l6fE9U57eSPS35g6ekJ_hol7-RHg89JV60_m1BlE,4633
30
31
  tests/test_handy_mix_config.py,sha256=Un56mz9KJmdn4K4OwzHAHLSRzDU1Xv2nFrONNuzOG04,2594
31
32
  tests/test_xml_parser.py,sha256=3QTlhFEd6KbK6nRFKZnc35tad6wqukTbe4QrFi8mr_8,859
32
- berryworld-1.0.0.181358.dist-info/LICENSE,sha256=vtkVCJM6E2af2gnsi2XxKPr4WY-uIbvzVLXieFND0UU,1074
33
- berryworld-1.0.0.181358.dist-info/METADATA,sha256=znF4x6wLc65ByBfuts_wxOdfDJYTrxRJgcsPbdFnwnc,1174
34
- berryworld-1.0.0.181358.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
35
- berryworld-1.0.0.181358.dist-info/top_level.txt,sha256=GIZ5qy-P5oxfEH755vA1IMFeTVdX3-40JxMe6nOe5I8,17
36
- berryworld-1.0.0.181358.dist-info/RECORD,,
33
+ berryworld-1.0.0.182055.dist-info/LICENSE,sha256=vtkVCJM6E2af2gnsi2XxKPr4WY-uIbvzVLXieFND0UU,1074
34
+ berryworld-1.0.0.182055.dist-info/METADATA,sha256=8WVQFKkeQC12_VMXILN50UmUi14zsuGxkg8BEo4UZCc,1174
35
+ berryworld-1.0.0.182055.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
36
+ berryworld-1.0.0.182055.dist-info/top_level.txt,sha256=GIZ5qy-P5oxfEH755vA1IMFeTVdX3-40JxMe6nOe5I8,17
37
+ berryworld-1.0.0.182055.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.2.0)
2
+ Generator: setuptools (75.3.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5