lukhed-basic-utils 0.3.0__tar.gz → 1.0.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.0.0/PKG-INFO +88 -0
- lukhed_basic_utils-1.0.0/README.md +74 -0
- lukhed_basic_utils-1.0.0/lukhed_basic_utils/commonCommon.py +9 -0
- lukhed_basic_utils-1.0.0/lukhed_basic_utils/githubCommon.py +391 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils/osCommon.py +6 -2
- lukhed_basic_utils-1.0.0/lukhed_basic_utils.egg-info/PKG-INFO +88 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils.egg-info/SOURCES.txt +2 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils.egg-info/requires.txt +2 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/setup.py +5 -3
- lukhed_basic_utils-0.3.0/PKG-INFO +0 -28
- lukhed_basic_utils-0.3.0/README.md +0 -14
- lukhed_basic_utils-0.3.0/lukhed_basic_utils.egg-info/PKG-INFO +0 -28
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/LICENSE +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils/__init__.py +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils/classCommon.py +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils/fileCommon.py +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils/listWorkCommon.py +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils/mathCommon.py +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils/requestsCommon.py +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils/stringCommon.py +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils/timeCommon.py +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils.egg-info/dependency_links.txt +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils.egg-info/top_level.txt +0 -0
- {lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/setup.cfg +0 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: lukhed_basic_utils
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A collection of basic utility functions
|
|
5
|
+
Home-page: https://github.com/lukhed/lukhed_basic_utils
|
|
6
|
+
Author: lukhed
|
|
7
|
+
Author-email: lukhed.mail@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
# lukhed_basic_utils
|
|
16
|
+
|
|
17
|
+
A collection of basic utility functions for Python projects.
|
|
18
|
+
|
|
19
|
+
## Installation and Usage
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install lukhed-basic-utils
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## osCommon
|
|
26
|
+
|
|
27
|
+
A set utility functions for interacting with the operating system's file and directory structures.
|
|
28
|
+
|
|
29
|
+
### Example Usage
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from lukhed_basic_utils import osCommon as osC
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
#### Creating Operating System Agnostic FIle Path Strings
|
|
37
|
+
```python
|
|
38
|
+
example_path = osC.create_file_path_string(list_of_dir=['subdir', 'file.txt'])
|
|
39
|
+
print(example_path)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
#output
|
|
44
|
+
/home/user/current_working_dir/subdir/file.txt
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### Retrieving File Names in Directory
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
return_files_in_dir_as_strings("/home/user/docs", return_file_names_only=True)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
# output
|
|
55
|
+
['file1.txt', 'file2.txt']
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## fileCommon
|
|
59
|
+
|
|
60
|
+
A set utility functions for working with local files.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from lukhed_basic_utils import fileCommon as fC
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## githubCommon
|
|
67
|
+
|
|
68
|
+
A class for working with the Github API, geared toward utilizing github repo's as storage or shared config.
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from lukhed_basic_utils.githubCommon import GithubHelper
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Retrieve JSON Content Stored In a Github Repo
|
|
75
|
+
```python
|
|
76
|
+
gC = GithubHelper(project='lukhed', repo_name='exampleRepo')
|
|
77
|
+
example_dict = gC.retrieve_file_content('someConfig.json')
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Other Utility Files
|
|
81
|
+
### classCommon
|
|
82
|
+
### listWorkCommon
|
|
83
|
+
### mathCommon
|
|
84
|
+
### stringCommon
|
|
85
|
+
### timeCommon
|
|
86
|
+
|
|
87
|
+
For more documentation, check pypi page:
|
|
88
|
+
https://pypi.org/project/lukhed-basic-utils/
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# lukhed_basic_utils
|
|
2
|
+
|
|
3
|
+
A collection of basic utility functions for Python projects.
|
|
4
|
+
|
|
5
|
+
## Installation and Usage
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install lukhed-basic-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## osCommon
|
|
12
|
+
|
|
13
|
+
A set utility functions for interacting with the operating system's file and directory structures.
|
|
14
|
+
|
|
15
|
+
### Example Usage
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from lukhed_basic_utils import osCommon as osC
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
#### Creating Operating System Agnostic FIle Path Strings
|
|
23
|
+
```python
|
|
24
|
+
example_path = osC.create_file_path_string(list_of_dir=['subdir', 'file.txt'])
|
|
25
|
+
print(example_path)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
#output
|
|
30
|
+
/home/user/current_working_dir/subdir/file.txt
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### Retrieving File Names in Directory
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
return_files_in_dir_as_strings("/home/user/docs", return_file_names_only=True)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
# output
|
|
41
|
+
['file1.txt', 'file2.txt']
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## fileCommon
|
|
45
|
+
|
|
46
|
+
A set utility functions for working with local files.
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from lukhed_basic_utils import fileCommon as fC
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## githubCommon
|
|
53
|
+
|
|
54
|
+
A class for working with the Github API, geared toward utilizing github repo's as storage or shared config.
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from lukhed_basic_utils.githubCommon import GithubHelper
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Retrieve JSON Content Stored In a Github Repo
|
|
61
|
+
```python
|
|
62
|
+
gC = GithubHelper(project='lukhed', repo_name='exampleRepo')
|
|
63
|
+
example_dict = gC.retrieve_file_content('someConfig.json')
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Other Utility Files
|
|
67
|
+
### classCommon
|
|
68
|
+
### listWorkCommon
|
|
69
|
+
### mathCommon
|
|
70
|
+
### stringCommon
|
|
71
|
+
### timeCommon
|
|
72
|
+
|
|
73
|
+
For more documentation, check pypi page:
|
|
74
|
+
https://pypi.org/project/lukhed-basic-utils/
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
from lukhed_basic_utils import osCommon as osC
|
|
2
|
+
from lukhed_basic_utils import fileCommon as fC
|
|
3
|
+
from lukhed_basic_utils import commonCommon as cC
|
|
4
|
+
from github import Github
|
|
5
|
+
from github.Repository import Repository
|
|
6
|
+
from github.GithubException import UnknownObjectException
|
|
7
|
+
import json
|
|
8
|
+
from typing import Optional
|
|
9
|
+
|
|
10
|
+
class GithubHelper:
|
|
11
|
+
def __init__(self, project='your_project_name', repo_name=None):
|
|
12
|
+
"""
|
|
13
|
+
A helper class for interacting with GitHub repositories, handling authentication,
|
|
14
|
+
and various file operations within a repository.
|
|
15
|
+
|
|
16
|
+
Upon instantiation, the class checks for an existing GitHub configuration file
|
|
17
|
+
(`githubConfig.json`) in the lukhed config directory. If a valid configuration
|
|
18
|
+
does not exist, it guides you through creating one, storing the credentials locally.
|
|
19
|
+
Once configurted, the class can then authenticate with GitHub using a personal access token
|
|
20
|
+
associated with a specific project.
|
|
21
|
+
|
|
22
|
+
Parameters:
|
|
23
|
+
project (str, optional): Name of the project to activate. Defaults to
|
|
24
|
+
'your_project_name'. Project names are not case sensitive.
|
|
25
|
+
repo_name (str, optional): Name of the repository to activate immediately
|
|
26
|
+
after instantiation. Defaults to None.
|
|
27
|
+
|
|
28
|
+
Attributes:
|
|
29
|
+
_resource_dir (str): Path to the lukhed config directory.
|
|
30
|
+
_github_config_file (str): Full path to the `githubConfig.json` file containing
|
|
31
|
+
user tokens for various projects.
|
|
32
|
+
_github_config (list): Loaded GitHub configuration data (list of dictionaries),
|
|
33
|
+
each containing a "project" and "token" key.
|
|
34
|
+
user (str | None): Authenticated GitHub username, set upon successful authentication.
|
|
35
|
+
project (str | None): Currently active project name (lowercase).
|
|
36
|
+
repo (github.Repository.Repository | None): GitHub repository object for the
|
|
37
|
+
active repository, if any.
|
|
38
|
+
_gh_object (github.Github | None): The authenticated GitHub instance used to
|
|
39
|
+
make API calls.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
# Check setup upon instantiation
|
|
43
|
+
cC.check_create_lukhed_config_path()
|
|
44
|
+
self._resource_dir = cC.get_lukhed_config_path()
|
|
45
|
+
self._github_config_file = osC.append_to_dir(self._resource_dir, 'githubConfig.json')
|
|
46
|
+
self._github_config = []
|
|
47
|
+
self.user = None
|
|
48
|
+
self.project = None
|
|
49
|
+
self.repo = None # type: Optional[Repository]
|
|
50
|
+
self._gh_object = None # type: Optional[Github]
|
|
51
|
+
self._check_setup(project)
|
|
52
|
+
|
|
53
|
+
if repo_name is not None:
|
|
54
|
+
self._set_repo(repo_name)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
###################
|
|
58
|
+
# Setup/COnfig
|
|
59
|
+
###################
|
|
60
|
+
def _check_setup(self, project):
|
|
61
|
+
need_setup = True
|
|
62
|
+
if osC.check_if_file_exists(self._github_config_file):
|
|
63
|
+
# Check for an active github configuration
|
|
64
|
+
self._github_config = fC.load_json_from_file(self._github_config_file)
|
|
65
|
+
if not self._github_config:
|
|
66
|
+
need_setup = True
|
|
67
|
+
else:
|
|
68
|
+
# check if project exists
|
|
69
|
+
self._activate_project(project)
|
|
70
|
+
need_setup = False
|
|
71
|
+
else:
|
|
72
|
+
# write default config to file
|
|
73
|
+
fC.dump_json_to_file(self._github_config_file, self._github_config)
|
|
74
|
+
need_setup = True
|
|
75
|
+
|
|
76
|
+
if need_setup:
|
|
77
|
+
self._prompt_for_setup()
|
|
78
|
+
|
|
79
|
+
def _activate_project(self, project):
|
|
80
|
+
projects = [x['project'].lower() for x in self._github_config]
|
|
81
|
+
project = project.lower()
|
|
82
|
+
|
|
83
|
+
if project in projects:
|
|
84
|
+
# Get the index of the item
|
|
85
|
+
index = projects.index(project)
|
|
86
|
+
token = self._github_config[index]['token']
|
|
87
|
+
if self._authenticate(token):
|
|
88
|
+
print(f"INFO: {project} project was activated")
|
|
89
|
+
self.active_project = project
|
|
90
|
+
self.user = self._gh_object.get_user().login
|
|
91
|
+
return True
|
|
92
|
+
else:
|
|
93
|
+
print("ERROR: Error while trying to authenticate.")
|
|
94
|
+
return False
|
|
95
|
+
else:
|
|
96
|
+
|
|
97
|
+
i = input((f'ERROR: There is no project "{project}" in the config file. Would you like to go thru setup '
|
|
98
|
+
'to add a new Github key for this project name? (y/n): '))
|
|
99
|
+
if i == 'y':
|
|
100
|
+
self._guided_setup()
|
|
101
|
+
else:
|
|
102
|
+
print("Ok, exiting...")
|
|
103
|
+
quit()
|
|
104
|
+
|
|
105
|
+
def _prompt_for_setup(self):
|
|
106
|
+
i = input("1. You do not have a valid config file to utilize github. Do you want to go thru easy setup? (y/n):")
|
|
107
|
+
if i == 'y':
|
|
108
|
+
self._guided_setup()
|
|
109
|
+
elif i == 'n':
|
|
110
|
+
print("OK, to use github functions, see https://github.com/lukhed/lukhed_basic_utils for more information.")
|
|
111
|
+
quit()
|
|
112
|
+
else:
|
|
113
|
+
print("Did not get an expected result of 'y' or 'n'. Please reinstantiate and try again. Exiting script.")
|
|
114
|
+
quit()
|
|
115
|
+
|
|
116
|
+
def _guided_setup(self):
|
|
117
|
+
input(("\n2. Starting setup\n"
|
|
118
|
+
"The github key you provide in this setup will be stored locally only. "
|
|
119
|
+
"After setup, you can see the config file in your directory at /lukhedConfig/githubConfig.json."
|
|
120
|
+
"\nPress any key to continue"))
|
|
121
|
+
|
|
122
|
+
token = input("\n3. Login to your Github account and go to https://github.com/settings/tokens. Generate a new "
|
|
123
|
+
"token and ensure to give it scopes that allow reading and writing to repos. "
|
|
124
|
+
"Copy the token, paste it below, then press enter:\n")
|
|
125
|
+
token = token.replace(" ", "")
|
|
126
|
+
project = input(("\n4. Provide a project name (this is needed for using the class) and press enter. "
|
|
127
|
+
"Note: projects are not case sensitive: "))
|
|
128
|
+
account_to_add = {"project": project.lower(), "token": token}
|
|
129
|
+
self._github_config.append(account_to_add)
|
|
130
|
+
self._update_github_config_file()
|
|
131
|
+
self._activate_project(project)
|
|
132
|
+
|
|
133
|
+
def _update_github_config_file(self):
|
|
134
|
+
fC.dump_json_to_file(self._github_config_file, self._github_config)
|
|
135
|
+
|
|
136
|
+
def _authenticate(self, token):
|
|
137
|
+
self._gh_object = Github(token)
|
|
138
|
+
return True
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
###################
|
|
142
|
+
# Repo Helpers
|
|
143
|
+
###################
|
|
144
|
+
def _parse_repo_dir_list_input(self, repo_dir_list):
|
|
145
|
+
if repo_dir_list is None:
|
|
146
|
+
repo_path = ""
|
|
147
|
+
elif type(repo_dir_list) is str:
|
|
148
|
+
repo_path = repo_dir_list
|
|
149
|
+
else:
|
|
150
|
+
repo_path = "/".join(repo_dir_list)
|
|
151
|
+
|
|
152
|
+
return repo_path
|
|
153
|
+
|
|
154
|
+
def _parse_content_for_upload(self, content):
|
|
155
|
+
if type(content) is dict or type(content) is list:
|
|
156
|
+
content = json.dumps(content)
|
|
157
|
+
else:
|
|
158
|
+
content = str(content)
|
|
159
|
+
|
|
160
|
+
return content
|
|
161
|
+
|
|
162
|
+
def _set_repo(self, repo_name):
|
|
163
|
+
try:
|
|
164
|
+
self.repo = self._gh_object.get_repo(self.user + "/" + repo_name)
|
|
165
|
+
print(f"INFO: {repo_name} repo was activated")
|
|
166
|
+
return True
|
|
167
|
+
except Exception as e:
|
|
168
|
+
print((f"ERROR: Error trying to set the repo to {repo_name}. Does this repo exist on your account? "
|
|
169
|
+
f"Use the method 'get_list_of_repo_names' to see the repos in your active project."
|
|
170
|
+
f"See the full error below:\n{e}"))
|
|
171
|
+
|
|
172
|
+
def _get_repo_contents(self, repo_path):
|
|
173
|
+
contents = self.repo.get_contents(repo_path)
|
|
174
|
+
return contents
|
|
175
|
+
|
|
176
|
+
def get_list_of_repo_names(self, print_names=False):
|
|
177
|
+
"""
|
|
178
|
+
Returns a list of repo names available in the active project. Optionally prints the list to console.
|
|
179
|
+
|
|
180
|
+
Parameters:
|
|
181
|
+
print_names (bool, optional): If True, prints the list of available repos to the console. Defaults to False.
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
list: A list of repo names associated with the active account.
|
|
185
|
+
"""
|
|
186
|
+
repos = []
|
|
187
|
+
for repo in self._gh_object.get_user().get_repos():
|
|
188
|
+
repos.append(repo.name)
|
|
189
|
+
if print_names:
|
|
190
|
+
print(repo.name)
|
|
191
|
+
|
|
192
|
+
def change_repo(self, repo_name):
|
|
193
|
+
"""
|
|
194
|
+
Changes the active repository.
|
|
195
|
+
|
|
196
|
+
Parameters:
|
|
197
|
+
repo_name (str): Name of the repository to switch to.
|
|
198
|
+
"""
|
|
199
|
+
self._set_repo(repo_name)
|
|
200
|
+
|
|
201
|
+
def change_project(self, project, repo_name=None):
|
|
202
|
+
"""
|
|
203
|
+
Changes the active project. Optionally switches the repository if repo_name is provided.
|
|
204
|
+
|
|
205
|
+
Parameters:
|
|
206
|
+
project (str): Name of the project to activate.
|
|
207
|
+
repo_name (str, optional): Name of the repository to switch to. Defaults to None.
|
|
208
|
+
"""
|
|
209
|
+
activated = self._activate_project(project)
|
|
210
|
+
|
|
211
|
+
if activated and repo_name is not None:
|
|
212
|
+
self._set_repo(repo_name)
|
|
213
|
+
|
|
214
|
+
def get_files_in_repo_path(self, path_as_list_or_str=None):
|
|
215
|
+
"""
|
|
216
|
+
Retrieves a list of file paths in the specified repository path.
|
|
217
|
+
|
|
218
|
+
Parameters:
|
|
219
|
+
path_as_list_or_str (list | str, optional): Path to a directory in the repository.
|
|
220
|
+
Can be provided as a list of directory segments or a single string. Defaults to None.
|
|
221
|
+
|
|
222
|
+
Returns:
|
|
223
|
+
list: A list of file paths (str) found at the specified location in the repository.
|
|
224
|
+
"""
|
|
225
|
+
repo_path = self._parse_repo_dir_list_input(path_as_list_or_str)
|
|
226
|
+
contents = self._get_repo_contents(repo_path)
|
|
227
|
+
|
|
228
|
+
return [x.path for x in contents]
|
|
229
|
+
|
|
230
|
+
def retrieve_file_content(self, path_as_list_or_str, decode=True):
|
|
231
|
+
"""
|
|
232
|
+
Retrieves the content of a file in the repository. Optionally decodes the content
|
|
233
|
+
and returns either raw text/binary or JSON (if the file is .json).
|
|
234
|
+
|
|
235
|
+
Parameters:
|
|
236
|
+
path_as_list_or_str (list | str): Path to the file in the repository, either
|
|
237
|
+
as a list of directory segments or a single string.
|
|
238
|
+
|
|
239
|
+
decode (bool, optional): If True, decodes the file content. If the file is JSON,
|
|
240
|
+
returns a Python dictionary; otherwise returns the raw decoded data. If False,
|
|
241
|
+
returns a ContentFile object. Defaults to True.
|
|
242
|
+
|
|
243
|
+
Returns:
|
|
244
|
+
dict | str | None: Decoded JSON object if .json file and decode=True, string content
|
|
245
|
+
for other file types if decode=True, ContentFile object if decode=False, or None
|
|
246
|
+
if the file is not found.
|
|
247
|
+
|
|
248
|
+
Example:
|
|
249
|
+
>>> # Retrieve and decode content of a JSON file
|
|
250
|
+
>>> json_data = self.retrieve_file_content(["data", "example.json"], decode=True)
|
|
251
|
+
>>> print(json_data)
|
|
252
|
+
{"key": "value", "numbers": [1, 2, 3]}
|
|
253
|
+
"""
|
|
254
|
+
repo_path = self._parse_repo_dir_list_input(path_as_list_or_str)
|
|
255
|
+
|
|
256
|
+
try:
|
|
257
|
+
contents = self._get_repo_contents(repo_path)
|
|
258
|
+
except UnknownObjectException as e:
|
|
259
|
+
# file not found exception
|
|
260
|
+
return None
|
|
261
|
+
|
|
262
|
+
if decode:
|
|
263
|
+
decoded = contents.decoded_content
|
|
264
|
+
if '.json' in repo_path:
|
|
265
|
+
return json.loads(decoded)
|
|
266
|
+
else:
|
|
267
|
+
return decoded
|
|
268
|
+
|
|
269
|
+
else:
|
|
270
|
+
return contents
|
|
271
|
+
|
|
272
|
+
def create_file(self, content, path_as_list_or_str=None, commit_message="no message"):
|
|
273
|
+
"""
|
|
274
|
+
Creates a new file in the repository with the specified content.
|
|
275
|
+
|
|
276
|
+
Parameters:
|
|
277
|
+
content (str | dict): The content to upload. If dict, it will be converted to JSON.
|
|
278
|
+
path_as_list_or_str (list | str, optional): Path to the file in the repository,
|
|
279
|
+
either as a list of directory segments or a single string. Defaults to None.
|
|
280
|
+
commit_message (str, optional): Commit message for the new file. Defaults to "no message".
|
|
281
|
+
|
|
282
|
+
Returns:
|
|
283
|
+
dict: A status dictionary returned by the GitHub API after file creation.
|
|
284
|
+
|
|
285
|
+
Example:
|
|
286
|
+
>>> # Create a file with some text content
|
|
287
|
+
>>> status = self.create_file("Hello, world!", ["docs", "hello.txt"], "Add hello.txt")
|
|
288
|
+
>>> print(status["commit"].sha)
|
|
289
|
+
6f0e918c...
|
|
290
|
+
"""
|
|
291
|
+
repo_path = self._parse_repo_dir_list_input(path_as_list_or_str)
|
|
292
|
+
content = self._parse_content_for_upload(content)
|
|
293
|
+
status = self.repo.create_file(path=repo_path, message=commit_message, content=content)
|
|
294
|
+
return status
|
|
295
|
+
|
|
296
|
+
def delete_file(self, path_as_list_or_str=None, commit_message="Delete file"):
|
|
297
|
+
"""
|
|
298
|
+
Deletes a file from the repository.
|
|
299
|
+
|
|
300
|
+
Parameters:
|
|
301
|
+
path_as_list_or_str (list | str, optional): Path to the file in the repository,
|
|
302
|
+
either as a list of directory segments or a single string. Defaults to None.
|
|
303
|
+
commit_message (str, optional): Commit message for the deletion. Defaults to "Delete file".
|
|
304
|
+
|
|
305
|
+
Returns:
|
|
306
|
+
dict | str: A status dictionary returned by the GitHub API after file deletion,
|
|
307
|
+
or an error message if deletion fails.
|
|
308
|
+
"""
|
|
309
|
+
repo_path = self._parse_repo_dir_list_input(path_as_list_or_str)
|
|
310
|
+
try:
|
|
311
|
+
# Get the file from the repository
|
|
312
|
+
file = self.repo.get_contents(repo_path)
|
|
313
|
+
|
|
314
|
+
# Delete the file
|
|
315
|
+
status = self.repo.delete_file(path=repo_path, message=commit_message, sha=file.sha)
|
|
316
|
+
return status
|
|
317
|
+
except Exception as e:
|
|
318
|
+
return e
|
|
319
|
+
|
|
320
|
+
def update_file(self, new_content, path_as_list_or_str, message="Updated content"):
|
|
321
|
+
"""
|
|
322
|
+
Updates the content of an existing file in the repository.
|
|
323
|
+
|
|
324
|
+
Parameters:
|
|
325
|
+
new_content (str | dict | list): The new content to upload. If dict or list, it will be converted to json.
|
|
326
|
+
path_as_list_or_str (list | str): Path to the existing file in the repository,
|
|
327
|
+
either as a list of directory segments or a single string.
|
|
328
|
+
message (str, optional): Commit message for the update. Defaults to "Updated content".
|
|
329
|
+
|
|
330
|
+
Returns:
|
|
331
|
+
dict: A status dictionary returned by the GitHub API after file update.
|
|
332
|
+
|
|
333
|
+
Example:
|
|
334
|
+
>>> # Update an existing text file
|
|
335
|
+
>>> update_status = self.update_file("New content goes here",
|
|
336
|
+
... ["docs", "hello.txt"],
|
|
337
|
+
... "Update hello.txt")
|
|
338
|
+
>>> print(update_status["commit"].sha)
|
|
339
|
+
83b2fa1c...
|
|
340
|
+
"""
|
|
341
|
+
new_content = self._parse_content_for_upload(new_content)
|
|
342
|
+
existing_contents = self.retrieve_file_content(path_as_list_or_str, decode=False)
|
|
343
|
+
|
|
344
|
+
status = self.repo.update_file(existing_contents.path, message=message, content=new_content,
|
|
345
|
+
sha=existing_contents.sha)
|
|
346
|
+
return status
|
|
347
|
+
|
|
348
|
+
def create_update_file(self, path_as_list_or_str, content):
|
|
349
|
+
"""
|
|
350
|
+
Creates a new file or updates an existing file with the given content.
|
|
351
|
+
|
|
352
|
+
Parameters:
|
|
353
|
+
path_as_list_or_str (list | str): Path to the file in the repository, either as a list
|
|
354
|
+
of directory segments or a single string.
|
|
355
|
+
content (str | dict): The content to upload. If dict, it may be converted to JSON
|
|
356
|
+
depending on file type.
|
|
357
|
+
|
|
358
|
+
Returns:
|
|
359
|
+
dict: A status dictionary returned by the GitHub API after file creation or update.
|
|
360
|
+
|
|
361
|
+
Example:
|
|
362
|
+
>>> # Create or update a file named "config.json"
|
|
363
|
+
>>> status = self.create_update_file(["configs", "config.json"], {"env": "dev", "debug": True})
|
|
364
|
+
>>> print(status["commit"].message)
|
|
365
|
+
Updated content
|
|
366
|
+
"""
|
|
367
|
+
if self.file_exists(path_as_list_or_str):
|
|
368
|
+
status = self.update_file(path_as_list_or_str, content)
|
|
369
|
+
else:
|
|
370
|
+
status = self.create_file(path_as_list_or_str, content)
|
|
371
|
+
|
|
372
|
+
return status
|
|
373
|
+
|
|
374
|
+
def file_exists(self, repo_dir_list=None):
|
|
375
|
+
"""
|
|
376
|
+
Checks if a file exists in the repository.
|
|
377
|
+
|
|
378
|
+
Parameters:
|
|
379
|
+
repo_dir_list (list | str, optional): Path to the file in the repository,
|
|
380
|
+
either as a list of directory segments or a single string. Defaults to None.
|
|
381
|
+
|
|
382
|
+
Returns:
|
|
383
|
+
bool: True if the file exists, False otherwise.
|
|
384
|
+
"""
|
|
385
|
+
self._parse_repo_dir_list_input(repo_dir_list)
|
|
386
|
+
res = self.retrieve_file_content(repo_dir_list, decode=False)
|
|
387
|
+
if res is None:
|
|
388
|
+
return False
|
|
389
|
+
else:
|
|
390
|
+
return True
|
|
391
|
+
|
|
@@ -86,12 +86,13 @@ def return_immediate_child_dirs_given_dir(full_dir_path):
|
|
|
86
86
|
"""
|
|
87
87
|
return [f.path for f in os.scandir(full_dir_path) if f.is_dir()]
|
|
88
88
|
|
|
89
|
-
def return_files_in_dir_as_strings(dir_path):
|
|
89
|
+
def return_files_in_dir_as_strings(dir_path, return_file_names_only=False):
|
|
90
90
|
"""
|
|
91
91
|
Returns a list of file paths in a given directory.
|
|
92
92
|
|
|
93
93
|
Parameters:
|
|
94
94
|
dir_path (str): The directory path to retrieve files from.
|
|
95
|
+
return_file_names_only (bool): If true, get file names only instead of the full path string.
|
|
95
96
|
|
|
96
97
|
Returns:
|
|
97
98
|
list: A sorted list of file paths as strings. Additional sorting is applied only on Linux.
|
|
@@ -110,7 +111,10 @@ def return_files_in_dir_as_strings(dir_path):
|
|
|
110
111
|
if "linux" in platform.system().lower():
|
|
111
112
|
file_list.sort()
|
|
112
113
|
|
|
113
|
-
|
|
114
|
+
if return_file_names_only:
|
|
115
|
+
return [extract_file_name_given_full_path(x) for x in file_list]
|
|
116
|
+
else:
|
|
117
|
+
return file_list
|
|
114
118
|
|
|
115
119
|
def check_if_dir_exists(full_path):
|
|
116
120
|
"""
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: lukhed-basic-utils
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A collection of basic utility functions
|
|
5
|
+
Home-page: https://github.com/lukhed/lukhed_basic_utils
|
|
6
|
+
Author: lukhed
|
|
7
|
+
Author-email: lukhed.mail@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
# lukhed_basic_utils
|
|
16
|
+
|
|
17
|
+
A collection of basic utility functions for Python projects.
|
|
18
|
+
|
|
19
|
+
## Installation and Usage
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install lukhed-basic-utils
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## osCommon
|
|
26
|
+
|
|
27
|
+
A set utility functions for interacting with the operating system's file and directory structures.
|
|
28
|
+
|
|
29
|
+
### Example Usage
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from lukhed_basic_utils import osCommon as osC
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
#### Creating Operating System Agnostic FIle Path Strings
|
|
37
|
+
```python
|
|
38
|
+
example_path = osC.create_file_path_string(list_of_dir=['subdir', 'file.txt'])
|
|
39
|
+
print(example_path)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
#output
|
|
44
|
+
/home/user/current_working_dir/subdir/file.txt
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### Retrieving File Names in Directory
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
return_files_in_dir_as_strings("/home/user/docs", return_file_names_only=True)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
# output
|
|
55
|
+
['file1.txt', 'file2.txt']
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## fileCommon
|
|
59
|
+
|
|
60
|
+
A set utility functions for working with local files.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from lukhed_basic_utils import fileCommon as fC
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## githubCommon
|
|
67
|
+
|
|
68
|
+
A class for working with the Github API, geared toward utilizing github repo's as storage or shared config.
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from lukhed_basic_utils.githubCommon import GithubHelper
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Retrieve JSON Content Stored In a Github Repo
|
|
75
|
+
```python
|
|
76
|
+
gC = GithubHelper(project='lukhed', repo_name='exampleRepo')
|
|
77
|
+
example_dict = gC.retrieve_file_content('someConfig.json')
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Other Utility Files
|
|
81
|
+
### classCommon
|
|
82
|
+
### listWorkCommon
|
|
83
|
+
### mathCommon
|
|
84
|
+
### stringCommon
|
|
85
|
+
### timeCommon
|
|
86
|
+
|
|
87
|
+
For more documentation, check pypi page:
|
|
88
|
+
https://pypi.org/project/lukhed-basic-utils/
|
{lukhed_basic_utils-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils.egg-info/SOURCES.txt
RENAMED
|
@@ -3,7 +3,9 @@ README.md
|
|
|
3
3
|
setup.py
|
|
4
4
|
lukhed_basic_utils/__init__.py
|
|
5
5
|
lukhed_basic_utils/classCommon.py
|
|
6
|
+
lukhed_basic_utils/commonCommon.py
|
|
6
7
|
lukhed_basic_utils/fileCommon.py
|
|
8
|
+
lukhed_basic_utils/githubCommon.py
|
|
7
9
|
lukhed_basic_utils/listWorkCommon.py
|
|
8
10
|
lukhed_basic_utils/mathCommon.py
|
|
9
11
|
lukhed_basic_utils/osCommon.py
|
|
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|
|
2
2
|
|
|
3
3
|
setup(
|
|
4
4
|
name="lukhed_basic_utils",
|
|
5
|
-
version="0.
|
|
5
|
+
version="1.0.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",
|
|
@@ -10,7 +10,7 @@ setup(
|
|
|
10
10
|
author_email="lukhed.mail@gmail.com",
|
|
11
11
|
url="https://github.com/lukhed/lukhed_basic_utils",
|
|
12
12
|
packages=find_packages(),
|
|
13
|
-
python_requires=">=3.
|
|
13
|
+
python_requires=">=3.9",
|
|
14
14
|
classifiers=[
|
|
15
15
|
"Programming Language :: Python :: 3",
|
|
16
16
|
"License :: OSI Approved :: MIT License",
|
|
@@ -20,6 +20,8 @@ setup(
|
|
|
20
20
|
"python-dateutil>=2.9.0",
|
|
21
21
|
"requests>=2.32.3",
|
|
22
22
|
"beautifulsoup4>=4.12.3",
|
|
23
|
-
"fake-useragent>=2.0.3"
|
|
23
|
+
"fake-useragent>=2.0.3",
|
|
24
|
+
"tzdata>=2023.3",
|
|
25
|
+
"PyGithub>= 2.5.0"
|
|
24
26
|
],
|
|
25
27
|
)
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: lukhed_basic_utils
|
|
3
|
-
Version: 0.3.0
|
|
4
|
-
Summary: A collection of basic utility functions
|
|
5
|
-
Home-page: https://github.com/lukhed/lukhed_basic_utils
|
|
6
|
-
Author: lukhed
|
|
7
|
-
Author-email: lukhed.mail@gmail.com
|
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
-
Classifier: Operating System :: OS Independent
|
|
11
|
-
Requires-Python: >=3.6
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE
|
|
14
|
-
|
|
15
|
-
# aceCommon
|
|
16
|
-
|
|
17
|
-
A collection of basic utility functions for Python projects.
|
|
18
|
-
|
|
19
|
-
## Installation and Usage
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
pip install lukhed_basic_utils
|
|
23
|
-
from lukhed_basic_utils import create_file_path_string
|
|
24
|
-
|
|
25
|
-
example_path = create_file_path_string(list_of_dir=['subdir', 'file.txt'])
|
|
26
|
-
|
|
27
|
-
print(path)
|
|
28
|
-
```
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# aceCommon
|
|
2
|
-
|
|
3
|
-
A collection of basic utility functions for Python projects.
|
|
4
|
-
|
|
5
|
-
## Installation and Usage
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
pip install lukhed_basic_utils
|
|
9
|
-
from lukhed_basic_utils import create_file_path_string
|
|
10
|
-
|
|
11
|
-
example_path = create_file_path_string(list_of_dir=['subdir', 'file.txt'])
|
|
12
|
-
|
|
13
|
-
print(path)
|
|
14
|
-
```
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: lukhed-basic-utils
|
|
3
|
-
Version: 0.3.0
|
|
4
|
-
Summary: A collection of basic utility functions
|
|
5
|
-
Home-page: https://github.com/lukhed/lukhed_basic_utils
|
|
6
|
-
Author: lukhed
|
|
7
|
-
Author-email: lukhed.mail@gmail.com
|
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
-
Classifier: Operating System :: OS Independent
|
|
11
|
-
Requires-Python: >=3.6
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE
|
|
14
|
-
|
|
15
|
-
# aceCommon
|
|
16
|
-
|
|
17
|
-
A collection of basic utility functions for Python projects.
|
|
18
|
-
|
|
19
|
-
## Installation and Usage
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
pip install lukhed_basic_utils
|
|
23
|
-
from lukhed_basic_utils import create_file_path_string
|
|
24
|
-
|
|
25
|
-
example_path = create_file_path_string(list_of_dir=['subdir', 'file.txt'])
|
|
26
|
-
|
|
27
|
-
print(path)
|
|
28
|
-
```
|
|
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-0.3.0 → lukhed_basic_utils-1.0.0}/lukhed_basic_utils.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|