lukhed-basic-utils 1.3.0__tar.gz → 1.4.0__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.
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/PKG-INFO +1 -1
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/githubCommon.py +247 -32
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils.egg-info/PKG-INFO +1 -1
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils.egg-info/SOURCES.txt +3 -1
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/setup.py +1 -1
- lukhed_basic_utils-1.4.0/tests/test_osCommon.py +81 -0
- lukhed_basic_utils-1.4.0/tests/test_timeCommon.py +18 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/LICENSE +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/README.md +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/__init__.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/chartJsCommon.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/classCommon.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/fileCommon.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/listWorkCommon.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/mathCommon.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/osCommon.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/requestsCommon.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/stringCommon.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils/timeCommon.py +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils.egg-info/dependency_links.txt +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils.egg-info/requires.txt +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils.egg-info/top_level.txt +0 -0
- {lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/setup.cfg +0 -0
|
@@ -6,8 +6,10 @@ from github.GithubException import UnknownObjectException
|
|
|
6
6
|
import json
|
|
7
7
|
from typing import Optional
|
|
8
8
|
|
|
9
|
+
|
|
9
10
|
class GithubHelper:
|
|
10
|
-
def __init__(self, project='your_project_name', repo_name=None, set_config_directory=None
|
|
11
|
+
def __init__(self, project='your_project_name', repo_name=None, set_config_directory=None,
|
|
12
|
+
essential_print_only=False):
|
|
11
13
|
"""
|
|
12
14
|
A helper class for interacting with GitHub repositories, handling authentication,
|
|
13
15
|
and various file operations within a repository.
|
|
@@ -26,6 +28,8 @@ class GithubHelper:
|
|
|
26
28
|
set_config_directory (str, optional): Full path to the directory that contains your GithubHelper config
|
|
27
29
|
file (token file). Default is None and this class will create a directory in your working directory
|
|
28
30
|
called 'lukhedConfig' to store the GithubHelper config file.
|
|
31
|
+
essential_print_only (bool, optional): If True, will only print to console essential setup or error
|
|
32
|
+
messages, defaults to False and full verbosity.
|
|
29
33
|
|
|
30
34
|
Attributes:
|
|
31
35
|
_resource_dir (str): Path to the lukhed config directory.
|
|
@@ -41,6 +45,8 @@ class GithubHelper:
|
|
|
41
45
|
make API calls.
|
|
42
46
|
"""
|
|
43
47
|
|
|
48
|
+
self.essential_print_only=essential_print_only
|
|
49
|
+
|
|
44
50
|
# Check setup upon instantiation
|
|
45
51
|
if set_config_directory is None:
|
|
46
52
|
osC.check_create_dir_structure(['lukhedConfig'])
|
|
@@ -54,10 +60,11 @@ class GithubHelper:
|
|
|
54
60
|
self._github_config_file = osC.append_to_dir(self._resource_dir, 'githubConfig.json')
|
|
55
61
|
self._github_config = []
|
|
56
62
|
self.user = None
|
|
57
|
-
self.project =
|
|
63
|
+
self.project = project.lower()
|
|
64
|
+
self.active_project = None
|
|
58
65
|
self.repo = None # type: Optional[Repository]
|
|
59
66
|
self._gh_object = None # type: Optional[Github]
|
|
60
|
-
self._check_setup(
|
|
67
|
+
self._check_setup()
|
|
61
68
|
|
|
62
69
|
if repo_name is not None:
|
|
63
70
|
self._set_repo(repo_name)
|
|
@@ -66,7 +73,7 @@ class GithubHelper:
|
|
|
66
73
|
###################
|
|
67
74
|
# Setup/Config
|
|
68
75
|
###################
|
|
69
|
-
def _check_setup(self
|
|
76
|
+
def _check_setup(self):
|
|
70
77
|
need_setup = True
|
|
71
78
|
if osC.check_if_file_exists(self._github_config_file):
|
|
72
79
|
# Check for an active github configuration
|
|
@@ -75,7 +82,7 @@ class GithubHelper:
|
|
|
75
82
|
need_setup = True
|
|
76
83
|
else:
|
|
77
84
|
# check if project exists
|
|
78
|
-
self._activate_project(
|
|
85
|
+
self._activate_project()
|
|
79
86
|
need_setup = False
|
|
80
87
|
else:
|
|
81
88
|
# write default config to file
|
|
@@ -85,28 +92,25 @@ class GithubHelper:
|
|
|
85
92
|
if need_setup:
|
|
86
93
|
self._prompt_for_setup()
|
|
87
94
|
|
|
88
|
-
def
|
|
95
|
+
def _check_print(self, to_print):
|
|
96
|
+
if not self.essential_print_only:
|
|
97
|
+
print(to_print)
|
|
98
|
+
|
|
99
|
+
def _activate_project(self):
|
|
89
100
|
try:
|
|
90
101
|
projects = [x['project'].lower() for x in self._github_config]
|
|
91
102
|
except Exception as e:
|
|
92
103
|
input((f"ERROR: Error while trying to parse the config file. It may be corrupt."
|
|
93
|
-
"You can delete the config directory and go through setup again. Press
|
|
94
|
-
quit()
|
|
95
|
-
|
|
96
|
-
try:
|
|
97
|
-
project = project.lower()
|
|
98
|
-
except Exception as e:
|
|
99
|
-
input((f"ERROR: Error while trying to parse project name '{project}'. Try another project name. "
|
|
100
|
-
"Press any button to quit."))
|
|
104
|
+
"You can delete the config directory and go through setup again. Press enter to quit."))
|
|
101
105
|
quit()
|
|
102
106
|
|
|
103
|
-
if project in projects:
|
|
107
|
+
if self.project in projects:
|
|
104
108
|
# Get the index of the item
|
|
105
|
-
index = projects.index(project)
|
|
109
|
+
index = projects.index(self.project)
|
|
106
110
|
token = self._github_config[index]['token']
|
|
107
111
|
if self._authenticate(token):
|
|
108
|
-
|
|
109
|
-
self.active_project = project
|
|
112
|
+
self._check_print(f"INFO: {self.project} project was activated")
|
|
113
|
+
self.active_project = self.project
|
|
110
114
|
self.user = self._gh_object.get_user().login
|
|
111
115
|
return True
|
|
112
116
|
else:
|
|
@@ -114,7 +118,7 @@ class GithubHelper:
|
|
|
114
118
|
return False
|
|
115
119
|
else:
|
|
116
120
|
|
|
117
|
-
i = input((f'ERROR: There is no project "{project}" in the config file. Would you like to go thru setup '
|
|
121
|
+
i = input((f'ERROR: There is no project "{self.project}" in the config file. Would you like to go thru setup '
|
|
118
122
|
'to add a new Github key for this project name? (y/n): '))
|
|
119
123
|
if i == 'y':
|
|
120
124
|
self._guided_setup()
|
|
@@ -136,19 +140,20 @@ class GithubHelper:
|
|
|
136
140
|
def _guided_setup(self):
|
|
137
141
|
input(("\n2. Starting setup\n"
|
|
138
142
|
"The github key you provide in this setup will be stored locally only. "
|
|
139
|
-
"After setup, you can see the config file in your
|
|
140
|
-
"\nPress
|
|
143
|
+
f"After setup, you can see the config file in your specified destination {self._github_config_file}"
|
|
144
|
+
"\nPress enter to continue"))
|
|
141
145
|
|
|
142
146
|
token = input("\n3. Login to your Github account and go to https://github.com/settings/tokens. Generate a new "
|
|
143
147
|
"token and ensure to give it scopes that allow reading and writing to repos. "
|
|
144
148
|
"Copy the token, paste it below, then press enter:\n")
|
|
145
149
|
token = token.replace(" ", "")
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
150
|
+
if self.project == 'your_project_name':
|
|
151
|
+
self.project = input(("\n4. Provide a project name (this is needed for using the class) and press enter. "
|
|
152
|
+
"Note: projects are not case sensitive: "))
|
|
153
|
+
account_to_add = {"project": self.project.lower(), "token": token}
|
|
149
154
|
self._github_config.append(account_to_add)
|
|
150
155
|
self._update_github_config_file()
|
|
151
|
-
self._activate_project(
|
|
156
|
+
self._activate_project()
|
|
152
157
|
|
|
153
158
|
def _update_github_config_file(self):
|
|
154
159
|
fC.dump_json_to_file(self._github_config_file, self._github_config)
|
|
@@ -163,7 +168,7 @@ class GithubHelper:
|
|
|
163
168
|
###################
|
|
164
169
|
def _activate_repo(self, repo_name):
|
|
165
170
|
self.repo = self._gh_object.get_repo(self.user + "/" + repo_name)
|
|
166
|
-
|
|
171
|
+
self._check_print(f"INFO: {repo_name} repo was activated")
|
|
167
172
|
|
|
168
173
|
def _parse_repo_dir_list_input(self, repo_dir_list):
|
|
169
174
|
if repo_dir_list is None:
|
|
@@ -188,26 +193,32 @@ class GithubHelper:
|
|
|
188
193
|
self._activate_repo(repo_name)
|
|
189
194
|
return True
|
|
190
195
|
except Exception as e:
|
|
191
|
-
|
|
192
|
-
f"See the full error below:\n{e}"))
|
|
193
|
-
create_repo = input(f"Do you want to create a private repo named {repo_name}? (y/n) ")
|
|
196
|
+
create_repo = input(f"Your Github account doesn't have repo: '{repo_name}'. Create it? (y/n) ")
|
|
194
197
|
if create_repo == 'y':
|
|
195
|
-
if self.create_repo(repo_name, private=True):
|
|
198
|
+
if self.create_repo(repo_name, private=True, create_readme=True):
|
|
199
|
+
|
|
196
200
|
self._activate_repo(repo_name)
|
|
201
|
+
else:
|
|
202
|
+
print(f"OK, no action taken to activate a repo")
|
|
203
|
+
return None
|
|
197
204
|
|
|
198
205
|
def _get_repo_contents(self, repo_path):
|
|
199
206
|
contents = self.repo.get_contents(repo_path)
|
|
200
207
|
return contents
|
|
201
208
|
|
|
202
|
-
def create_repo(self, repo_name, description="Repo created by lukhed-basic-utils", private=True
|
|
209
|
+
def create_repo(self, repo_name, description="Repo created by lukhed-basic-utils", private=True,
|
|
210
|
+
create_readme=False, readme_content=None):
|
|
203
211
|
"""
|
|
204
212
|
Creates a new repository on GitHub.
|
|
205
213
|
|
|
206
214
|
Parameters:
|
|
207
215
|
repo_name (str): The name of the repository to create.
|
|
208
|
-
description (str, optional): A brief description of the repository.
|
|
216
|
+
description (str, optional): A brief description of the repository for the commit message.
|
|
209
217
|
private (bool, optional): Determines whether the repository should be private.
|
|
210
218
|
Defaults to True (private repository).
|
|
219
|
+
create_readme (bool, optional): if True, Adds a README.md to the repo with description content.
|
|
220
|
+
readme_content (str, optional): If creating a readme, can control the content to be different than
|
|
221
|
+
description.
|
|
211
222
|
|
|
212
223
|
Returns:
|
|
213
224
|
bool: True if the repository was created successfully, False otherwise.
|
|
@@ -222,6 +233,22 @@ class GithubHelper:
|
|
|
222
233
|
private=private
|
|
223
234
|
)
|
|
224
235
|
print(f"Repository '{repo.name}' created successfully at {repo.html_url}")
|
|
236
|
+
|
|
237
|
+
if create_readme:
|
|
238
|
+
# Define the content for the README.md file
|
|
239
|
+
if readme_content is None:
|
|
240
|
+
readme_content = "# " + repo_name + f"\n\n{description}"
|
|
241
|
+
else:
|
|
242
|
+
readme_content = "# " + repo_name + f"\n\n{readme_content}"
|
|
243
|
+
|
|
244
|
+
# Create the README.md file in the repository
|
|
245
|
+
repo.create_file(
|
|
246
|
+
path="README.md",
|
|
247
|
+
message="Initial commit with README.md",
|
|
248
|
+
content=readme_content
|
|
249
|
+
)
|
|
250
|
+
print("README.md file created successfully.")
|
|
251
|
+
|
|
225
252
|
return True
|
|
226
253
|
except Exception as e:
|
|
227
254
|
print(f"An error occurred: {e}")
|
|
@@ -443,4 +470,192 @@ class GithubHelper:
|
|
|
443
470
|
else:
|
|
444
471
|
return True
|
|
445
472
|
|
|
473
|
+
|
|
474
|
+
class KeyManager(GithubHelper):
|
|
475
|
+
def __init__(self, key_name, config_file_preference='local', github_config_dir=None, provide_key_data=None):
|
|
476
|
+
"""
|
|
477
|
+
This class manages api key storage and retrieval from new api creation through continued use.
|
|
478
|
+
There are two options for storage set by the config_file_preference parameter:
|
|
479
|
+
|
|
480
|
+
'local' - stores/retrieve your api key on your local hard drive (working directory)
|
|
481
|
+
'github' - stores/retrieves your api key in a private repo (helpful to allow access across different hardware)
|
|
482
|
+
|
|
483
|
+
To add a new key to be managed, just instantiate the class with the desired parameters and it will walk you
|
|
484
|
+
through the steps.
|
|
485
|
+
|
|
486
|
+
Note: github option requires a github account (free) and a private access token (free). This class will
|
|
487
|
+
direct you on how to set that up if you are not familiar.
|
|
488
|
+
|
|
489
|
+
Once your config file is setup for your key on local or in github, instantiate the class with the same
|
|
490
|
+
parameters and your key data will be stored in self.key_data or you can use the get_key_data() method.
|
|
491
|
+
|
|
492
|
+
Parameters
|
|
493
|
+
----------
|
|
494
|
+
key_name : str()
|
|
495
|
+
A name for the api key that will be associated with your key in the config file.
|
|
496
|
+
To use the key in the future, you will use this key name to identify it. It Can be anything
|
|
497
|
+
but should be representative of the api you are using.
|
|
498
|
+
config_file_preference : str, optional
|
|
499
|
+
'local' to store your api key on your local hardware. 'github' to store
|
|
500
|
+
your api key in a private github repository. Defaults to 'local'., by default 'local'
|
|
501
|
+
github_config_dir : str(), optional
|
|
502
|
+
Full path to a local directory that contains your GithubHelper config file (token file). Default is
|
|
503
|
+
None and the GithubHelper class looks in your working directory for 'lukhedConfig' to get/store the
|
|
504
|
+
GithubHelper config file. If you utilize this option, you will have to provide the location each time
|
|
505
|
+
you want to use the key (helpful if you have multiple projects pointing to the same github access token).
|
|
506
|
+
provide_key_data : dict() or file path, optional
|
|
507
|
+
When adding a new key to be managed, use this option to provide the key data yourself
|
|
508
|
+
(instead of going through the guided setup where you paste the key). This is helpful for complex keys
|
|
509
|
+
that require more than one token or that have additional meta data with them. Two options:
|
|
510
|
+
dict or the full path str to the file that contains the json.
|
|
511
|
+
"""
|
|
512
|
+
|
|
513
|
+
self._config_dict = {}
|
|
514
|
+
self._config_type = config_file_preference.lower()
|
|
515
|
+
self._provided_key_data = provide_key_data
|
|
516
|
+
|
|
517
|
+
# Key name and file based on parameters
|
|
518
|
+
self.key_name = key_name
|
|
519
|
+
self.key_file_name = f"{key_name}.json"
|
|
520
|
+
|
|
521
|
+
# Set the github access token config location
|
|
522
|
+
self._default_local_config = osC.create_file_path_string(['lukhedConfig'])
|
|
523
|
+
self.github_config_dir = github_config_dir or self._default_local_config
|
|
524
|
+
|
|
525
|
+
# Default local key storage if local option chosen
|
|
526
|
+
self._local_key_storage = osC.create_file_path_string(['lukhedConfig', self.key_file_name])
|
|
527
|
+
|
|
528
|
+
# Default repo if github option chose
|
|
529
|
+
self._gh_repo = 'lukhedConfig'
|
|
530
|
+
|
|
531
|
+
self.key_data = None
|
|
532
|
+
if self._config_type == 'github':
|
|
533
|
+
super().__init__(project=key_name, repo_name=self._gh_repo, essential_print_only=True)
|
|
534
|
+
|
|
535
|
+
if not self._check_load_config_from_github():
|
|
536
|
+
self._guided_api_key_setup()
|
|
537
|
+
else:
|
|
538
|
+
if not self._check_load_config_from_local():
|
|
539
|
+
self._guided_api_key_setup()
|
|
540
|
+
|
|
541
|
+
self.get_key_data()
|
|
542
|
+
print(f"{self.key_name} data retrieved successfully")
|
|
543
|
+
|
|
544
|
+
def _check_load_config_from_github(self):
|
|
545
|
+
if self.file_exists(self.key_file_name):
|
|
546
|
+
self._config_dict = self.retrieve_file_content(self.key_file_name)
|
|
547
|
+
return True
|
|
548
|
+
else:
|
|
549
|
+
return False
|
|
550
|
+
|
|
551
|
+
def _check_load_config_from_local(self):
|
|
552
|
+
config_path = osC.check_create_dir_structure(['lukhedConfig'], return_path=True)
|
|
553
|
+
config_file = osC.append_to_dir(config_path, self.key_file_name)
|
|
554
|
+
if osC.check_if_file_exists(config_file):
|
|
555
|
+
self._config_dict = fC.load_json_from_file(config_file)
|
|
556
|
+
return True
|
|
557
|
+
else:
|
|
558
|
+
return False
|
|
559
|
+
|
|
560
|
+
def _guided_api_key_setup(self):
|
|
561
|
+
confirm = input((f"You don't have a key stored for '{self.key_name}'. Do you want to go through setup? (y/n)"))
|
|
562
|
+
if confirm != 'y':
|
|
563
|
+
print("OK, Exiting...")
|
|
564
|
+
quit()
|
|
565
|
+
|
|
566
|
+
if self._config_type == 'github':
|
|
567
|
+
input(("\n1. Starting setup\n"
|
|
568
|
+
f"The key for '{self.key_name}' you provide in this setup will be stored on your private github repo: "
|
|
569
|
+
f"'{self._gh_repo}/{self.key_file_name}'"
|
|
570
|
+
"\nPress enter to continue"))
|
|
571
|
+
elif self._config_type == 'local':
|
|
572
|
+
input(("\n1. Starting setup\n"
|
|
573
|
+
f"The key for '{self.key_name}' you provide in this setup will be stored locally at: "
|
|
574
|
+
f"{self._local_key_storage} "
|
|
575
|
+
"\nPress enter to continue"))
|
|
576
|
+
else:
|
|
577
|
+
print(f"ERROR: '{self._config_type}' is not a valid config_file_preference")
|
|
578
|
+
quit()
|
|
579
|
+
|
|
580
|
+
if self._provided_key_data:
|
|
581
|
+
if type(self._provided_key_data) == dict:
|
|
582
|
+
token_dict = self._provided_key_data
|
|
583
|
+
elif type(self._provided_key_data) == str:
|
|
584
|
+
token_dict = fC.load_json_from_file(self._provided_key_data)
|
|
585
|
+
else:
|
|
586
|
+
print(f"ERROR: Your input for 'provide_key_data' is not valid. Must be a dict or file path string.")
|
|
587
|
+
quit()
|
|
588
|
+
else:
|
|
589
|
+
token = input(f"\n2. Copy and paste your '{self.key_name}' key below.\n")
|
|
590
|
+
token = token.replace(" ", "")
|
|
591
|
+
token_dict = {"token": token}
|
|
592
|
+
|
|
593
|
+
if self._config_type == 'github':
|
|
594
|
+
r = self.create_update_file(self.key_file_name, token_dict, message=f'created config for {self.key_name}')
|
|
595
|
+
if r['commit']:
|
|
596
|
+
print(f"Config for {self.key_file_name} created successfully and the key is ready for use.")
|
|
597
|
+
self._config_dict = token_dict
|
|
598
|
+
return True
|
|
599
|
+
else:
|
|
600
|
+
print(f"ERROR: Something went wrong in creating your config file on Github. Try again and if the "
|
|
601
|
+
"problem persists you can check and file bugs at the below link or try the local key storage "
|
|
602
|
+
"method\n\n https://github.com/lukhed/lukhed_basic_utils")
|
|
603
|
+
return False
|
|
604
|
+
else:
|
|
605
|
+
fC.dump_json_to_file(self._local_key_storage, token_dict)
|
|
606
|
+
print(f"Config for {self.key_name} created successfully and the key is ready for use.")
|
|
607
|
+
self._config_dict = token_dict
|
|
608
|
+
return True
|
|
609
|
+
|
|
610
|
+
def get_key_data(self):
|
|
611
|
+
"""
|
|
612
|
+
Provides your store keyed data (also stored in class variable: key_data)
|
|
613
|
+
|
|
614
|
+
Returns
|
|
615
|
+
-------
|
|
616
|
+
dict()
|
|
617
|
+
{'token': 'your token'}
|
|
618
|
+
"""
|
|
619
|
+
|
|
620
|
+
if self.key_data is None:
|
|
621
|
+
if self._config_type == 'github':
|
|
622
|
+
self.key_data = self.retrieve_file_content([self.key_file_name])
|
|
623
|
+
elif self._config_type == 'local':
|
|
624
|
+
self.key_data = fC.load_json_from_file(self._local_key_storage)
|
|
625
|
+
else:
|
|
626
|
+
print(f"ERROR: '{self._config_type}' is not a valid config_file_preference")
|
|
627
|
+
quit()
|
|
628
|
+
|
|
629
|
+
return self.key_data
|
|
630
|
+
|
|
631
|
+
def force_update_key_data(self, new_key_data):
|
|
632
|
+
"""
|
|
633
|
+
This function will replace your current key data (json file) with the 'new_key_data' parameter.
|
|
634
|
+
|
|
635
|
+
Parameters
|
|
636
|
+
----------
|
|
637
|
+
new_key_data : dict() or file path
|
|
638
|
+
Two options:
|
|
639
|
+
dict or the full path str to the file that contains the json.
|
|
640
|
+
"""
|
|
641
|
+
|
|
642
|
+
if type(new_key_data) == dict:
|
|
643
|
+
token_dict = new_key_data
|
|
644
|
+
elif type(new_key_data) == str:
|
|
645
|
+
token_dict = fC.load_json_from_file(new_key_data)
|
|
646
|
+
else:
|
|
647
|
+
print(f"ERROR: Your input for 'new_key_data' is not valid. Must be a dict or file path string.")
|
|
648
|
+
quit()
|
|
649
|
+
|
|
650
|
+
if self._config_type == 'github':
|
|
651
|
+
r = self.update_file(token_dict, self.key_file_name, message="lukhed_basic_utils updated key data")
|
|
652
|
+
if r['commit']:
|
|
653
|
+
updated = True
|
|
654
|
+
else:
|
|
655
|
+
self.key_data = fC.dump_json_to_file(self._local_key_storage, token_dict)
|
|
656
|
+
updated = True
|
|
657
|
+
|
|
658
|
+
if updated:
|
|
659
|
+
print("INFO: Key updated successfully")
|
|
660
|
+
|
|
446
661
|
|
{lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils.egg-info/SOURCES.txt
RENAMED
|
@@ -16,4 +16,6 @@ lukhed_basic_utils.egg-info/PKG-INFO
|
|
|
16
16
|
lukhed_basic_utils.egg-info/SOURCES.txt
|
|
17
17
|
lukhed_basic_utils.egg-info/dependency_links.txt
|
|
18
18
|
lukhed_basic_utils.egg-info/requires.txt
|
|
19
|
-
lukhed_basic_utils.egg-info/top_level.txt
|
|
19
|
+
lukhed_basic_utils.egg-info/top_level.txt
|
|
20
|
+
tests/test_osCommon.py
|
|
21
|
+
tests/test_timeCommon.py
|
|
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|
|
2
2
|
|
|
3
3
|
setup(
|
|
4
4
|
name="lukhed_basic_utils",
|
|
5
|
-
version="1.
|
|
5
|
+
version="1.4.0",
|
|
6
6
|
description="A collection of basic utility functions",
|
|
7
7
|
long_description=open("README.md").read(),
|
|
8
8
|
long_description_content_type="text/markdown",
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
sys.path.append(str(Path(__file__).parent.parent))
|
|
6
|
+
from lukhed_basic_utils.osCommon import create_file_path_string, append_to_dir, create_root_path_starting_from_drive
|
|
7
|
+
|
|
8
|
+
def test_create_file_path_string():
|
|
9
|
+
print("Test 1: Base path provided as a list:")
|
|
10
|
+
path1 = create_file_path_string(
|
|
11
|
+
list_of_dir=['subdir', 'file.txt'],
|
|
12
|
+
base_path_list=['C:', 'Users', 'dad', 'Documents']
|
|
13
|
+
)
|
|
14
|
+
expected_path1 = Path('C:', 'Users', 'dad', 'Documents', 'subdir', 'file.txt')
|
|
15
|
+
print(path1)
|
|
16
|
+
assert Path(path1) == expected_path1, "Base path list test failed"
|
|
17
|
+
|
|
18
|
+
print("\nTest 2: Default to current working directory:")
|
|
19
|
+
path2 = create_file_path_string(list_of_dir=['subdir', 'file.txt'])
|
|
20
|
+
expected_path2 = Path.cwd() / 'subdir' / 'file.txt'
|
|
21
|
+
print(path2)
|
|
22
|
+
assert Path(path2) == expected_path2, "Default working directory test failed"
|
|
23
|
+
|
|
24
|
+
print("\nTest 3: Custom base path using ace_parent:")
|
|
25
|
+
path3 = create_file_path_string(list_of_dir=['config'], ace_parent=1)
|
|
26
|
+
# Adjust this test based on your project structure
|
|
27
|
+
print(path3)
|
|
28
|
+
assert 'config' in path3, "Relative ace_parent test failed"
|
|
29
|
+
|
|
30
|
+
print("\nTest 4: Current working directory fallback:")
|
|
31
|
+
path4 = create_file_path_string(list_of_dir=['mydir'])
|
|
32
|
+
expected_path4 = Path.cwd() / 'mydir'
|
|
33
|
+
print(path4)
|
|
34
|
+
assert Path(path4) == expected_path4, "Current working directory fallback test failed"
|
|
35
|
+
|
|
36
|
+
def test_append_to_dir():
|
|
37
|
+
print("Test 1: Single subdirectory append:")
|
|
38
|
+
base_dir = "C:/Users/dad/Documents"
|
|
39
|
+
to_append = "subdir"
|
|
40
|
+
result = append_to_dir(base_dir, to_append)
|
|
41
|
+
expected = os.path.join(base_dir, to_append)
|
|
42
|
+
print(result)
|
|
43
|
+
assert Path(result) == Path(expected), "Single subdirectory append test failed"
|
|
44
|
+
|
|
45
|
+
print("\nTest 2: Multiple subdirectories append as list:")
|
|
46
|
+
base_dir = "C:/Users/dad/Documents"
|
|
47
|
+
to_append = ["subdir1", "subdir2", "file.txt"]
|
|
48
|
+
result = append_to_dir(base_dir, to_append)
|
|
49
|
+
expected = os.path.join(base_dir, "subdir1", "subdir2", "file.txt")
|
|
50
|
+
print(result)
|
|
51
|
+
assert Path(result) == Path(expected), "Multiple subdirectories append test failed"
|
|
52
|
+
|
|
53
|
+
print("\nTest 3: Append with an empty base path:")
|
|
54
|
+
base_dir = ""
|
|
55
|
+
to_append = ["root", "subdir"]
|
|
56
|
+
result = append_to_dir(base_dir, to_append)
|
|
57
|
+
expected = os.path.join(base_dir, "root", "subdir")
|
|
58
|
+
print(result)
|
|
59
|
+
assert Path(result) == Path(expected), "Empty base path append test failed"
|
|
60
|
+
|
|
61
|
+
print("\nTest 4: Append single directory to a relative base path:")
|
|
62
|
+
base_dir = "mydir"
|
|
63
|
+
to_append = "subdir"
|
|
64
|
+
result = append_to_dir(base_dir, to_append)
|
|
65
|
+
expected = os.path.join(base_dir, to_append)
|
|
66
|
+
print(result)
|
|
67
|
+
assert Path(result) == Path(expected), "Relative base path append test failed"
|
|
68
|
+
|
|
69
|
+
print("\nTest 5: Append to root directory:")
|
|
70
|
+
base_dir = "/"
|
|
71
|
+
to_append = "etc"
|
|
72
|
+
result = append_to_dir(base_dir, to_append)
|
|
73
|
+
expected = os.path.join(base_dir, to_append)
|
|
74
|
+
print(result)
|
|
75
|
+
assert Path(result) == Path(expected), "Root directory append test failed"
|
|
76
|
+
|
|
77
|
+
def test_create_root_path_starting_from_drive():
|
|
78
|
+
print(create_root_path_starting_from_drive('C:'))
|
|
79
|
+
|
|
80
|
+
if __name__ == '__main__':
|
|
81
|
+
test_create_root_path_starting_from_drive()
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
sys.path.append(str(Path(__file__).parent.parent))
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
from lukhed_basic_utils.timeCommon import create_timestamp, get_date_tomorrow, get_date_yesterday
|
|
7
|
+
|
|
8
|
+
def test_create_time_stamp():
|
|
9
|
+
print(create_timestamp())
|
|
10
|
+
|
|
11
|
+
def test_get_date_tomorrow():
|
|
12
|
+
print(get_date_tomorrow())
|
|
13
|
+
|
|
14
|
+
def test_get_date_yesterday():
|
|
15
|
+
print(get_date_yesterday(convert_to_string_format="%Y%m%d"))
|
|
16
|
+
|
|
17
|
+
if __name__ == '__main__':
|
|
18
|
+
test_get_date_yesterday()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils.egg-info/requires.txt
RENAMED
|
File without changes
|
{lukhed_basic_utils-1.3.0 → lukhed_basic_utils-1.4.0}/lukhed_basic_utils.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|