libjam 0.0.1__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.
- __init__.py +6 -0
- captain.py +148 -0
- clipboard.py +113 -0
- drawer.py +359 -0
- flashcard.py +16 -0
- libjam-0.0.1.dist-info/METADATA +83 -0
- libjam-0.0.1.dist-info/RECORD +11 -0
- libjam-0.0.1.dist-info/WHEEL +4 -0
- libjam-0.0.1.dist-info/licenses/LICENSE +339 -0
- notebook.py +84 -0
- typewriter.py +119 -0
__init__.py
ADDED
captain.py
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
# Imports
|
2
|
+
import sys, re, inspect
|
3
|
+
from .typewriter import Typewriter
|
4
|
+
from .clipboard import Clipboard
|
5
|
+
typewriter = Typewriter()
|
6
|
+
clipboard = Clipboard()
|
7
|
+
|
8
|
+
# Processes command line arguments
|
9
|
+
class Captain:
|
10
|
+
|
11
|
+
# Returns a generated a help page based on provided inputs
|
12
|
+
def generate_help(self, app: str, description: str, commands: dict, options: dict = None):
|
13
|
+
offset = 2; offset_string = ' ' * offset
|
14
|
+
commands_list = []
|
15
|
+
for command in commands:
|
16
|
+
command_desc = commands.get(command).get('description')
|
17
|
+
commands_list.append(f"{command}")
|
18
|
+
commands_list.append(f"- {command_desc}")
|
19
|
+
commands_list.append("help")
|
20
|
+
commands_list.append("- Prints this page")
|
21
|
+
commands_string = typewriter.list_to_columns(commands_list, 2, offset)
|
22
|
+
if options is not None:
|
23
|
+
options_list = []
|
24
|
+
for option in options:
|
25
|
+
option_desc = options.get(option).get('description')
|
26
|
+
long = ', --'.join(options.get(option).get('long'))
|
27
|
+
short = ', -'.join(options.get(option).get('short'))
|
28
|
+
options_list.append(f"-{short}, --{long}")
|
29
|
+
options_list.append(f"- {option_desc}")
|
30
|
+
options_string = typewriter.list_to_columns(options_list, 2, offset)
|
31
|
+
|
32
|
+
# Creating the help string
|
33
|
+
help_string = ''
|
34
|
+
# Adding description
|
35
|
+
help_string += f"{typewriter.bolden('Description:')}\n"
|
36
|
+
help_string += f"{offset_string}{description}\n"
|
37
|
+
# Adding synopsys
|
38
|
+
help_string += f"{typewriter.bolden('Synopsis:')}\n"
|
39
|
+
help_string += f"{offset_string}{app} [OPTIONS] [COMMAND]\n"
|
40
|
+
# Adding commands
|
41
|
+
help_string += f"{typewriter.bolden('Commands:')}\n"
|
42
|
+
help_string += commands_string.rstrip()
|
43
|
+
# Adding options
|
44
|
+
if options is not None:
|
45
|
+
help_string += f"\n{typewriter.bolden('Options:')}\n"
|
46
|
+
help_string += options_string.rstrip()
|
47
|
+
|
48
|
+
return help_string
|
49
|
+
|
50
|
+
|
51
|
+
# Interprets input arguments
|
52
|
+
def interpret(self, app: str, help: str, commands: dict, arguments: list, options: dict = None):
|
53
|
+
# Class vars
|
54
|
+
chosen_command = None
|
55
|
+
self.function = None
|
56
|
+
self.arbitrary_args = False
|
57
|
+
self.required_args = 0
|
58
|
+
self.command_args = []
|
59
|
+
|
60
|
+
# Creating option bools
|
61
|
+
if options is not None:
|
62
|
+
for option in options:
|
63
|
+
options[option]['enabled'] = False
|
64
|
+
# Parsing arguments
|
65
|
+
for argument in arguments:
|
66
|
+
if argument.startswith("-"):
|
67
|
+
if options is not None:
|
68
|
+
self.arg_found = False
|
69
|
+
|
70
|
+
# Long options
|
71
|
+
if argument.startswith("--"):
|
72
|
+
argument = argument.removeprefix("--")
|
73
|
+
for option in options:
|
74
|
+
strings = options.get(option).get('long')
|
75
|
+
if clipboard.is_string_in_list(strings, argument):
|
76
|
+
options[option]['enabled'] = True
|
77
|
+
self.arg_found = True
|
78
|
+
if self.arg_found is False:
|
79
|
+
print(f"Option '{argument}' unrecognized. Try {app} help")
|
80
|
+
sys.exit(-1)
|
81
|
+
|
82
|
+
# Short options
|
83
|
+
else:
|
84
|
+
argument = argument.removeprefix("-")
|
85
|
+
arguments = list(argument)
|
86
|
+
for argument in arguments:
|
87
|
+
command_found = False
|
88
|
+
for option in options:
|
89
|
+
strings = options.get(option).get('short')
|
90
|
+
if clipboard.is_string_in_list(strings, argument):
|
91
|
+
options[option]['enabled'] = True
|
92
|
+
command_found = True
|
93
|
+
if command_found is False:
|
94
|
+
print(f"Option '{argument}' unrecognized. Try {app} help")
|
95
|
+
sys.exit(-1)
|
96
|
+
|
97
|
+
# Commands
|
98
|
+
else:
|
99
|
+
if chosen_command is None:
|
100
|
+
if clipboard.is_string_in_list(commands, argument):
|
101
|
+
chosen_command = argument
|
102
|
+
command_function = commands.get(chosen_command).get('function')
|
103
|
+
command_args = inspect.signature(command_function)
|
104
|
+
command_args = command_args.format().replace('(', '').replace(')', '').replace(' ', '')
|
105
|
+
command_args = command_args.split(',')
|
106
|
+
if clipboard.is_string_in_list(command_args, '*args'):
|
107
|
+
command_args.remove('*args')
|
108
|
+
self.arbitrary_args = True
|
109
|
+
if command_args == ['']:
|
110
|
+
command_args = []
|
111
|
+
self.required_args = len(command_args)
|
112
|
+
continue
|
113
|
+
elif argument == 'help':
|
114
|
+
print(help)
|
115
|
+
sys.exit(0)
|
116
|
+
if chosen_command is None:
|
117
|
+
print(f"Command '{argument}' unrecognized. Try {app} help")
|
118
|
+
sys.exit(-1)
|
119
|
+
|
120
|
+
# Command arguments
|
121
|
+
else:
|
122
|
+
if self.arbitrary_args is False:
|
123
|
+
if self.required_args == 0:
|
124
|
+
print(f"Command '{chosen_command}' does not take arguments.")
|
125
|
+
sys.exit(-1)
|
126
|
+
elif len(self.command_args) >= self.required_args:
|
127
|
+
s = ''
|
128
|
+
if self.required_args > 1: s = 's'
|
129
|
+
print(f"Command '{chosen_command}' requires only {self.required_args} argument{s}.")
|
130
|
+
sys.exit(-1)
|
131
|
+
self.command_args.append(argument)
|
132
|
+
if self.arbitrary_args is False and self.required_args > len(self.command_args):
|
133
|
+
print(f"Command '{chosen_command}' requires {self.required_args} arguments.")
|
134
|
+
sys.exit(-1)
|
135
|
+
|
136
|
+
# Checking if command is specified
|
137
|
+
if chosen_command is None:
|
138
|
+
print(f"No command specified. Try {app} help")
|
139
|
+
sys.exit(0)
|
140
|
+
|
141
|
+
function = commands.get(chosen_command).get('function')
|
142
|
+
function_name = function.__name__
|
143
|
+
function_params = ''
|
144
|
+
for item in self.command_args:
|
145
|
+
function_params += f"'{item}', "
|
146
|
+
function = f"{function_name}({function_params})"
|
147
|
+
|
148
|
+
return {'function': function, 'options': options}
|
clipboard.py
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# Deals with lists and such
|
2
|
+
class Clipboard:
|
3
|
+
|
4
|
+
# Checks if there is a string in a list
|
5
|
+
def is_string_in_list(self, input_list: list, input_string: str):
|
6
|
+
for item in input_list:
|
7
|
+
if item == input_string:
|
8
|
+
return True
|
9
|
+
return False
|
10
|
+
|
11
|
+
# Returns items present in both given lists
|
12
|
+
def get_duplicates(self, input_list1: list, input_list2: list):
|
13
|
+
result_list = []
|
14
|
+
for item in input_list1:
|
15
|
+
if self.is_string_in_list(input_list2, item):
|
16
|
+
result_list.append(item)
|
17
|
+
result_list = self.deduplicate(result_list)
|
18
|
+
return result_list
|
19
|
+
|
20
|
+
# Removes duplicates from a given list
|
21
|
+
def deduplicate(self, input_list: list):
|
22
|
+
result_list = list(set(input_list))
|
23
|
+
result_list.sort()
|
24
|
+
return result_list
|
25
|
+
|
26
|
+
def remove_duplicates(self, input_list1: list, input_list2: list):
|
27
|
+
result_list = []
|
28
|
+
duplicates = self.get_duplicates(input_list1, input_list2)
|
29
|
+
for item in input_list1:
|
30
|
+
if self.is_string_in_list(duplicates, item) is False:
|
31
|
+
result_list.append(item)
|
32
|
+
return result_list
|
33
|
+
|
34
|
+
|
35
|
+
# Returns input_list without any items from filter_list
|
36
|
+
def filter(self, input_list, filter_list):
|
37
|
+
result_list = []
|
38
|
+
for item in input_list:
|
39
|
+
if self.is_string_in_list(filter_list, item) is False:
|
40
|
+
result_list.append(item)
|
41
|
+
return result_list
|
42
|
+
|
43
|
+
|
44
|
+
# Returns a list of strings which contain substring
|
45
|
+
def match_substring(self, input_list: list, substring: str):
|
46
|
+
matching = []
|
47
|
+
for item in input_list:
|
48
|
+
if substring in item:
|
49
|
+
matching.append(item)
|
50
|
+
return matching
|
51
|
+
|
52
|
+
|
53
|
+
# Returns a list of strings which start with input_prefix
|
54
|
+
def match_prefix(self, input_list: list, input_prefix: str):
|
55
|
+
result_list = []
|
56
|
+
for item in input_list:
|
57
|
+
if item.startswith(input_prefix):
|
58
|
+
result_list.append(item)
|
59
|
+
return result_list
|
60
|
+
|
61
|
+
def remove_prefix(self, input_list: list, input_prefix: str):
|
62
|
+
result_list = []
|
63
|
+
for item in input_list:
|
64
|
+
result_list.append(item.removeprefix(input_prefix))
|
65
|
+
return result_list
|
66
|
+
|
67
|
+
# Returns a list of strings which ends with input_suffix
|
68
|
+
def match_suffix(self, input_list: list, input_suffix: str):
|
69
|
+
result_list = []
|
70
|
+
for item in input_list:
|
71
|
+
if item.endswith(input_suffix):
|
72
|
+
result_list.append(item)
|
73
|
+
return result_list
|
74
|
+
|
75
|
+
# Returns a list of strings which ends with input_suffix
|
76
|
+
def match_suffixes(self, input_list: list, input_suffixes: list):
|
77
|
+
result_list = []
|
78
|
+
for suffix in input_suffixes:
|
79
|
+
result_list += self.match_suffix(input_list, suffix)
|
80
|
+
return result_list
|
81
|
+
|
82
|
+
|
83
|
+
# Returns a list with lower-case strings
|
84
|
+
def lower(self, input_list: list):
|
85
|
+
result_list = []
|
86
|
+
for item in input_list:
|
87
|
+
result_list.append(item.lower())
|
88
|
+
return result_list
|
89
|
+
|
90
|
+
# Returns a list with upper-case strings
|
91
|
+
def upper(self, input_list: list):
|
92
|
+
result_list = []
|
93
|
+
for item in input_list:
|
94
|
+
result_list.append(item.upper())
|
95
|
+
return result_list
|
96
|
+
|
97
|
+
|
98
|
+
# Returns a list of strings containing given string, ignores case
|
99
|
+
def search(self, input_list: list, search_term: str):
|
100
|
+
result_list = []
|
101
|
+
search_term = search_term.lower()
|
102
|
+
for item in input_list:
|
103
|
+
if search_term in item.lower():
|
104
|
+
result_list.append(item)
|
105
|
+
return result_list
|
106
|
+
|
107
|
+
# Find & Replace for a list
|
108
|
+
def replace(self, input_list:list , oldstring: str, newstring: str):
|
109
|
+
result_list = []
|
110
|
+
for item in input_list:
|
111
|
+
item = item.replace(oldstring, newstring)
|
112
|
+
result_list.append(item)
|
113
|
+
return result_list
|
drawer.py
ADDED
@@ -0,0 +1,359 @@
|
|
1
|
+
# Imports
|
2
|
+
import os, sys, shutil, send2trash, platform, tempfile, pathlib
|
3
|
+
import zipfile, patoolib, rarfile
|
4
|
+
from .typewriter import Typewriter
|
5
|
+
from .clipboard import Clipboard
|
6
|
+
typewriter = Typewriter()
|
7
|
+
clipboard = Clipboard()
|
8
|
+
|
9
|
+
# Internal functions
|
10
|
+
joinpath = os.path.join
|
11
|
+
def realpath(path: str or list):
|
12
|
+
if type(path) == str:
|
13
|
+
return os.path.normpath(path)
|
14
|
+
elif type(path) == list:
|
15
|
+
result_list = []
|
16
|
+
for item in path:
|
17
|
+
result_list.append(os.path.normpath(item))
|
18
|
+
return result_list
|
19
|
+
|
20
|
+
def outpath(path: str or list):
|
21
|
+
if type(path) == str:
|
22
|
+
return path.replace(os.sep, '/')
|
23
|
+
elif type(path) == list:
|
24
|
+
result_list = []
|
25
|
+
for item in path:
|
26
|
+
result_list.append(item.replace(os.sep, '/'))
|
27
|
+
return result_list
|
28
|
+
|
29
|
+
|
30
|
+
# Deals with files
|
31
|
+
class Drawer:
|
32
|
+
|
33
|
+
# Returns True if give a path to folder
|
34
|
+
def is_folder(self, path: str):
|
35
|
+
path = realpath(path)
|
36
|
+
return os.path.isdir(path)
|
37
|
+
|
38
|
+
# Returns True if give a path to file
|
39
|
+
def is_file(self, path: str):
|
40
|
+
path = realpath(path)
|
41
|
+
return os.path.isfile(path)
|
42
|
+
|
43
|
+
def exists(self, path: str):
|
44
|
+
path = realpath(path)
|
45
|
+
is_file = self.is_folder(path)
|
46
|
+
is_folder = self.is_file(path)
|
47
|
+
if is_file or is_folder:
|
48
|
+
return True
|
49
|
+
else:
|
50
|
+
return False
|
51
|
+
|
52
|
+
|
53
|
+
# Returns the extension of a given file (a string)
|
54
|
+
def get_filetype(self, path: str):
|
55
|
+
path = realpath(path)
|
56
|
+
if self.is_folder(path):
|
57
|
+
return "folder"
|
58
|
+
elif self.is_file(path) is False:
|
59
|
+
return None
|
60
|
+
basename = self.basename(path)
|
61
|
+
filetype = os.path.splitext(basename)[1].removeprefix('.')
|
62
|
+
return filetype
|
63
|
+
|
64
|
+
# Returns a list of files and folders in a given folder
|
65
|
+
def get_all(self, path: str):
|
66
|
+
path = realpath(path)
|
67
|
+
relative_files = os.listdir(path)
|
68
|
+
absolute_files = []
|
69
|
+
for file in relative_files:
|
70
|
+
file = joinpath(path, file)
|
71
|
+
absolute_files.append(file)
|
72
|
+
return outpath(absolute_files)
|
73
|
+
|
74
|
+
# Returns a list of files in a given folder
|
75
|
+
def get_files(self, path: str):
|
76
|
+
path = realpath(path)
|
77
|
+
unfiltered_files = self.get_all(path)
|
78
|
+
files = []
|
79
|
+
for file in unfiltered_files:
|
80
|
+
if self.is_file:
|
81
|
+
files.append(file)
|
82
|
+
return outpath(files)
|
83
|
+
|
84
|
+
# Returns a list of folders in a given folder
|
85
|
+
def get_folders(self, path: str):
|
86
|
+
path = realpath(path)
|
87
|
+
folders = []
|
88
|
+
for item in self.get_all(path):
|
89
|
+
if self.is_folder(item):
|
90
|
+
folders.append(item)
|
91
|
+
return outpath(folders)
|
92
|
+
|
93
|
+
# Returns a list of all files in a given folder
|
94
|
+
def get_files_recursive(self, path: str):
|
95
|
+
path = realpath(path)
|
96
|
+
files = []
|
97
|
+
for folder in os.walk(path, topdown=True):
|
98
|
+
for file in folder[2]:
|
99
|
+
file = joinpath(folder[0], file)
|
100
|
+
files.append(file)
|
101
|
+
return outpath(files)
|
102
|
+
|
103
|
+
# Returns a list of all folders in a given folder
|
104
|
+
def get_folders_recursive(self, path: str):
|
105
|
+
path = realpath(path)
|
106
|
+
folders = []
|
107
|
+
for folder in os.walk(path, topdown=True):
|
108
|
+
for item in folder[1]:
|
109
|
+
item = joinpath(folder[0], item)
|
110
|
+
folders.append(item)
|
111
|
+
return outpath(folders)
|
112
|
+
|
113
|
+
|
114
|
+
# Renames a given file in a given path
|
115
|
+
def rename(self, folder: str, old_file: str, new_file: str):
|
116
|
+
folder = realpath(folder)
|
117
|
+
os.rename(f"{folder}/{old_file}", f"{folder}/{new_file}")
|
118
|
+
return 0
|
119
|
+
|
120
|
+
|
121
|
+
# Creates a new folder
|
122
|
+
def make_folder(self, path: str):
|
123
|
+
path = realpath(path)
|
124
|
+
path = os.mkdir(path)
|
125
|
+
return outpath(path)
|
126
|
+
|
127
|
+
# Creates a new file
|
128
|
+
def make_file(self, path: str):
|
129
|
+
path = realpath(path)
|
130
|
+
new = open(path, 'w')
|
131
|
+
new.close()
|
132
|
+
return outpath(path)
|
133
|
+
|
134
|
+
|
135
|
+
# Copies given file(s)/folder(s)
|
136
|
+
def copy(self, source: str, destination: str, overwrite=False):
|
137
|
+
source = realpath(source)
|
138
|
+
destination = realpath(destination)
|
139
|
+
if self.is_file(source):
|
140
|
+
shutil.copy(source, destination)
|
141
|
+
elif self.is_folder(source):
|
142
|
+
shutil.copytree(source, destination, dirs_exist_ok=overwrite)
|
143
|
+
return outpath(destination)
|
144
|
+
|
145
|
+
|
146
|
+
# Sends given file(s)/folder(s) to trash
|
147
|
+
def trash(self, path: str or list):
|
148
|
+
path = realpath(path)
|
149
|
+
if type(path) == str:
|
150
|
+
try:
|
151
|
+
send2trash.send2trash(path)
|
152
|
+
except FileNotFoundError:
|
153
|
+
print(f"File '{path}' wasn't found, skipping sending it to trash.")
|
154
|
+
elif type(path) == list:
|
155
|
+
for item in path:
|
156
|
+
try:
|
157
|
+
send2trash.send2trash(item)
|
158
|
+
except FileNotFoundError:
|
159
|
+
print(f"File '{path}' wasn't found, skipping sending it to trash.")
|
160
|
+
else:
|
161
|
+
return None
|
162
|
+
return outpath(path)
|
163
|
+
|
164
|
+
|
165
|
+
# Deletes a given file (using trash_file() instead is recommended)
|
166
|
+
def delete_file(self, path: str or list):
|
167
|
+
path = realpath(path)
|
168
|
+
if type(path) == str:
|
169
|
+
if self.is_file(path):
|
170
|
+
os.remove(path)
|
171
|
+
elif type(path) == list:
|
172
|
+
for item in path:
|
173
|
+
if self.is_file(path):
|
174
|
+
os.remove(item)
|
175
|
+
return outpath(path)
|
176
|
+
|
177
|
+
# Deletes a given folder (using trash() instead is recommended)
|
178
|
+
def delete_folder(self, path: str):
|
179
|
+
path = realpath(path)
|
180
|
+
try:
|
181
|
+
if self.is_folder(path):
|
182
|
+
shutil.rmtree(path)
|
183
|
+
else:
|
184
|
+
return None
|
185
|
+
except KeyboardInterrupt:
|
186
|
+
typewriter.print("Folder deletion cancelled.")
|
187
|
+
shutil.rmtree(path)
|
188
|
+
sys.exit(-1)
|
189
|
+
except PermissionError:
|
190
|
+
typewriter.print(f"Failed to delete folder '{path}', due to insufficient permissions.")
|
191
|
+
sys.exit(1)
|
192
|
+
return outpath(path)
|
193
|
+
|
194
|
+
# Returns the parent folder of given file/folder
|
195
|
+
def get_parent(self, path: str or list):
|
196
|
+
path = realpath(path)
|
197
|
+
if type(path) == str:
|
198
|
+
basename = self.basename(path)
|
199
|
+
parent = path.removesuffix(basename)
|
200
|
+
parent = parent.removesuffix(os.sep)
|
201
|
+
return outpath(parent)
|
202
|
+
elif type(path) == list:
|
203
|
+
result_list = []
|
204
|
+
for file in path:
|
205
|
+
basename = self.basename(file)
|
206
|
+
parent = file.removesuffix(basename)
|
207
|
+
parent = parent.removesuffix(os.sep)
|
208
|
+
result_list.append(parent)
|
209
|
+
return outpath(result_list)
|
210
|
+
|
211
|
+
# Returns depth of given file/folder
|
212
|
+
def get_depth(self, path: str):
|
213
|
+
path = realpath(path)
|
214
|
+
depth = path.split(sep=os.sep)
|
215
|
+
depth = len(depth)
|
216
|
+
return depth
|
217
|
+
|
218
|
+
def basename(self, path: str or list):
|
219
|
+
path = realpath(path)
|
220
|
+
if type(path) == str:
|
221
|
+
if self.is_folder(path):
|
222
|
+
path = os.path.basename(os.path.normpath(path))
|
223
|
+
else:
|
224
|
+
path = path.rsplit(os.sep,1)[-1]
|
225
|
+
return outpath(path)
|
226
|
+
elif type(path) == list:
|
227
|
+
return_list = []
|
228
|
+
for file in path:
|
229
|
+
if self.is_file(file):
|
230
|
+
file = os.path.basename(file)
|
231
|
+
elif self.is_folder(file):
|
232
|
+
file = os.path.basename(os.path.normpath(file))
|
233
|
+
return_list.append(file)
|
234
|
+
return outpath(return_list)
|
235
|
+
|
236
|
+
# Searches for string in list of basenames
|
237
|
+
def search_for_files(self, search_term: str, path: list):
|
238
|
+
path = realpath(path)
|
239
|
+
result_list = []
|
240
|
+
files = self.get_files_recursive(path)
|
241
|
+
for file in files:
|
242
|
+
basename = self.basename(file)
|
243
|
+
if search_term.lower() in basename.lower():
|
244
|
+
result_list.append(file)
|
245
|
+
return outpath(result_list)
|
246
|
+
|
247
|
+
def search_for_folder(self, search_term: str, path: str or list):
|
248
|
+
path = realpath(path)
|
249
|
+
result_list = []
|
250
|
+
if type(path) == str:
|
251
|
+
subfolders = self.get_folders(path)
|
252
|
+
for item in subfolders:
|
253
|
+
basename = self.basename(item)
|
254
|
+
if basename == search_term:
|
255
|
+
result_list.append(item)
|
256
|
+
elif type(path) == list:
|
257
|
+
for subfolder in path:
|
258
|
+
subfolders = self.get_folders(subfolder)
|
259
|
+
for item in subfolders:
|
260
|
+
basename = self.basename(item)
|
261
|
+
if basename == search_term:
|
262
|
+
result_list.append(item)
|
263
|
+
return outpath(result_list)
|
264
|
+
|
265
|
+
def search_for_folders(self, search_term: str, path: str or list):
|
266
|
+
path = realpath(path)
|
267
|
+
result_list = []
|
268
|
+
def search(search_term: str, path: str):
|
269
|
+
folders = self.get_folders_recursive(path)
|
270
|
+
for folder in folders:
|
271
|
+
basename = self.basename(folder)
|
272
|
+
if search_term.lower() in basename.lower():
|
273
|
+
result_list.append(folder)
|
274
|
+
if type(path) == str:
|
275
|
+
search(search_term, path)
|
276
|
+
elif type(path) == list:
|
277
|
+
for subfolder in path:
|
278
|
+
search(search_term, subfolder)
|
279
|
+
return outpath(result_list)
|
280
|
+
|
281
|
+
# Finds a folder with specified files
|
282
|
+
def find_folders_with_files(self, path: str, required_files: list):
|
283
|
+
path = realpath(path)
|
284
|
+
matches = []
|
285
|
+
for req in required_files:
|
286
|
+
matched_files = self.get_parent(self.search(req, path))
|
287
|
+
matched_files = Clipboard().deduplicate(matched_files)
|
288
|
+
if matched_files != []:
|
289
|
+
matches += matched_files
|
290
|
+
return outpath(matches)
|
291
|
+
|
292
|
+
# Checks if a given file is an archive
|
293
|
+
archive_types = ['zip', 'rar', '7z']
|
294
|
+
def is_archive(self, path: str):
|
295
|
+
path = realpath(path)
|
296
|
+
filetype = self.get_filetype(path)
|
297
|
+
if clipboard.is_string_in_list(self.archive_types, filetype):
|
298
|
+
return True
|
299
|
+
else:
|
300
|
+
return False
|
301
|
+
|
302
|
+
# Extracts a given archive
|
303
|
+
def extract_archive(self, archive: str, extract_location: str, progress_function=None):
|
304
|
+
archive = realpath(archive)
|
305
|
+
extract_location = realpath(extract_location)
|
306
|
+
archive_type = self.get_filetype(archive)
|
307
|
+
archive_basename = self.basename(archive).removesuffix(f".{archive_type}")
|
308
|
+
extract_location = joinpath(extract_location, archive_basename)
|
309
|
+
try:
|
310
|
+
if archive_type == 'zip': archive_function = zipfile.ZipFile
|
311
|
+
elif archive_type == 'rar': archive_function = rarfile.RarFile
|
312
|
+
elif archive_type == '7z':
|
313
|
+
try:
|
314
|
+
patoolib.extract_archive(archive, outdir=extract_location, verbosity=-1)
|
315
|
+
return extract_location
|
316
|
+
except PatoolError:
|
317
|
+
print("Please install the 'p7zip' package to process 7zip archives.")
|
318
|
+
return None
|
319
|
+
else:
|
320
|
+
return None
|
321
|
+
if archive_type == 'zip' or archive_type == 'rar':
|
322
|
+
archive_obj = archive_function(archive)
|
323
|
+
archived_files = archive_obj.namelist()
|
324
|
+
to_extract = len(archived_files)
|
325
|
+
extracted = 0
|
326
|
+
for archived_file in archived_files:
|
327
|
+
archive_obj.extract(archived_file, path=extract_location)
|
328
|
+
extracted += 1
|
329
|
+
if progress_function is not None:
|
330
|
+
progress_function(extracted, to_extract)
|
331
|
+
except KeyboardInterrupt:
|
332
|
+
typewriter.print("Archive extraction cancelled.")
|
333
|
+
self.delete_folder(extract_location)
|
334
|
+
exit()
|
335
|
+
return outpath(extract_location)
|
336
|
+
|
337
|
+
# Returns the home folder
|
338
|
+
def get_home(self):
|
339
|
+
home = str(pathlib.Path.home())
|
340
|
+
return outpath(home)
|
341
|
+
|
342
|
+
# Returns the temporary folder
|
343
|
+
def get_temp(self):
|
344
|
+
temp = str(tempfile.gettempdir())
|
345
|
+
return outpath(temp)
|
346
|
+
|
347
|
+
def get_file_size(self, path: str):
|
348
|
+
try:
|
349
|
+
size = 0
|
350
|
+
if self.is_file(path):
|
351
|
+
size += os.path.getsize(path)
|
352
|
+
elif self.is_folder(path):
|
353
|
+
subfiles = self.get_files_recursive(path)
|
354
|
+
for subfile in subfiles:
|
355
|
+
size += os.path.getsize(subfile)
|
356
|
+
return size
|
357
|
+
except KeyboardInterrupt:
|
358
|
+
typewriter.print('Program aborted while gathering size of files.')
|
359
|
+
sys.exit(1)
|
flashcard.py
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Asks user for input
|
2
|
+
class Flashcard:
|
3
|
+
def yn_prompt(self, question: str):
|
4
|
+
yes_choices = ['yes', 'y']
|
5
|
+
no_choices = ['no', 'n']
|
6
|
+
while True:
|
7
|
+
try:
|
8
|
+
user_input = input(f'{question} [y/n]: ')
|
9
|
+
answer = None
|
10
|
+
if user_input.lower() in yes_choices:
|
11
|
+
return True
|
12
|
+
elif user_input.lower() in no_choices:
|
13
|
+
return False
|
14
|
+
except KeyboardInterrupt:
|
15
|
+
print()
|
16
|
+
exit()
|
@@ -0,0 +1,83 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: libjam
|
3
|
+
Version: 0.0.1
|
4
|
+
Summary: A library jam for Python.
|
5
|
+
Project-URL: Homepage, https://github.com/philippkosarev/libjam
|
6
|
+
Project-URL: Issues, https://github.com/philippkosarev/libjam/issues
|
7
|
+
Author-email: Philipp Kosarev <philipp.kosarev@gmail.com>
|
8
|
+
License-Expression: GPL-2.0
|
9
|
+
License-File: LICENSE
|
10
|
+
Classifier: Operating System :: OS Independent
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
12
|
+
Requires-Python: >=3.13.0
|
13
|
+
Description-Content-Type: text/markdown
|
14
|
+
|
15
|
+
# libjam
|
16
|
+
A library jam for Python.
|
17
|
+
|
18
|
+
## Modules
|
19
|
+
libjam consists of of 6 modules:
|
20
|
+
|
21
|
+
### Captain
|
22
|
+
responsible for handling command-line arguments
|
23
|
+
|
24
|
+
### Drawer
|
25
|
+
responsible for file operations
|
26
|
+
|
27
|
+
### Typewriter
|
28
|
+
responsible for transforming and printing text
|
29
|
+
|
30
|
+
### Clipboard
|
31
|
+
responsible for working with lists
|
32
|
+
|
33
|
+
### Notebook
|
34
|
+
responsible for configuration
|
35
|
+
|
36
|
+
### Flashcard
|
37
|
+
responsible for getting user input from the command line
|
38
|
+
|
39
|
+
## Example project
|
40
|
+
```python
|
41
|
+
#! /usr/bin/python
|
42
|
+
|
43
|
+
# Imports
|
44
|
+
import sys
|
45
|
+
from libjam import Captain
|
46
|
+
|
47
|
+
captain = Captain()
|
48
|
+
|
49
|
+
class CLI:
|
50
|
+
def hello(self, text):
|
51
|
+
print(text)
|
52
|
+
if options.get('world').get('enabled'):
|
53
|
+
print('world!')
|
54
|
+
|
55
|
+
cli = CLI()
|
56
|
+
|
57
|
+
# Inputs/Commands/Options configuration
|
58
|
+
app = "example"
|
59
|
+
description = "An example app for the libjam library"
|
60
|
+
# help = "" # If you wish to set your own help page text
|
61
|
+
commands = {
|
62
|
+
'print': {'function': cli.hello,
|
63
|
+
'description': 'Prints given string'},
|
64
|
+
}
|
65
|
+
options = {
|
66
|
+
'world': {'long': ['world'], 'short': ['w'],
|
67
|
+
'description': 'Appends \'world\' after printing given input'},
|
68
|
+
}
|
69
|
+
|
70
|
+
# Getting program arguments
|
71
|
+
arguments = sys.argv
|
72
|
+
# Removing script name from arguments
|
73
|
+
arguments.remove(arguments[0])
|
74
|
+
# Generating help
|
75
|
+
help = captain.generate_help(app, description, commands, options)
|
76
|
+
# Interpreting user input
|
77
|
+
interpretation = captain.interpret(app, help, commands, arguments, options)
|
78
|
+
# Getting parsed output
|
79
|
+
function = interpretation.get('function')
|
80
|
+
options = interpretation.get('options')
|
81
|
+
# Executing function
|
82
|
+
exec(f"cli.{function}")
|
83
|
+
```
|
@@ -0,0 +1,11 @@
|
|
1
|
+
__init__.py,sha256=iLE2y9r0Sfe3oIeoFHKwFIyLL6d_KcLP7fINd7pPAlY,145
|
2
|
+
captain.py,sha256=igx-ecKJBI_vBN-pW7KmSEnmYMIHQEb9tFzpy5qHmI8,5636
|
3
|
+
clipboard.py,sha256=5HxlO8ztLJPlnSCq4PncGvY4JGc9C4J2uHcepjC6zmg,3496
|
4
|
+
drawer.py,sha256=6LHAKzsBrBMQYoN1GkToa_zcgQFCx4PjRXsUT4vnkQ8,11069
|
5
|
+
flashcard.py,sha256=ulV4KPC3BRWLxwkQ87vsY0aM38nYpzOjUOISxTIRVDg,437
|
6
|
+
notebook.py,sha256=VQ7Slq_xAahACvIdSMEfo1K7_IpOC-agYezCtKS2OW0,2773
|
7
|
+
typewriter.py,sha256=waKY1sDxGzI2ZT5dSzFmLGbaNTIpEM5Zhg_OlPFUAng,3730
|
8
|
+
libjam-0.0.1.dist-info/METADATA,sha256=DEPY2SZQ8w6xwoHO-mboqab1mSgetPIm40A2EqL78TY,2001
|
9
|
+
libjam-0.0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
10
|
+
libjam-0.0.1.dist-info/licenses/LICENSE,sha256=gXf5dRMhNSbfLPYYTY_5hsZ1r7UU1OaKQEAQUhuIBkM,18092
|
11
|
+
libjam-0.0.1.dist-info/RECORD,,
|
@@ -0,0 +1,339 @@
|
|
1
|
+
GNU GENERAL PUBLIC LICENSE
|
2
|
+
Version 2, June 1991
|
3
|
+
|
4
|
+
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
5
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies
|
7
|
+
of this license document, but changing it is not allowed.
|
8
|
+
|
9
|
+
Preamble
|
10
|
+
|
11
|
+
The licenses for most software are designed to take away your
|
12
|
+
freedom to share and change it. By contrast, the GNU General Public
|
13
|
+
License is intended to guarantee your freedom to share and change free
|
14
|
+
software--to make sure the software is free for all its users. This
|
15
|
+
General Public License applies to most of the Free Software
|
16
|
+
Foundation's software and to any other program whose authors commit to
|
17
|
+
using it. (Some other Free Software Foundation software is covered by
|
18
|
+
the GNU Lesser General Public License instead.) You can apply it to
|
19
|
+
your programs, too.
|
20
|
+
|
21
|
+
When we speak of free software, we are referring to freedom, not
|
22
|
+
price. Our General Public Licenses are designed to make sure that you
|
23
|
+
have the freedom to distribute copies of free software (and charge for
|
24
|
+
this service if you wish), that you receive source code or can get it
|
25
|
+
if you want it, that you can change the software or use pieces of it
|
26
|
+
in new free programs; and that you know you can do these things.
|
27
|
+
|
28
|
+
To protect your rights, we need to make restrictions that forbid
|
29
|
+
anyone to deny you these rights or to ask you to surrender the rights.
|
30
|
+
These restrictions translate to certain responsibilities for you if you
|
31
|
+
distribute copies of the software, or if you modify it.
|
32
|
+
|
33
|
+
For example, if you distribute copies of such a program, whether
|
34
|
+
gratis or for a fee, you must give the recipients all the rights that
|
35
|
+
you have. You must make sure that they, too, receive or can get the
|
36
|
+
source code. And you must show them these terms so they know their
|
37
|
+
rights.
|
38
|
+
|
39
|
+
We protect your rights with two steps: (1) copyright the software, and
|
40
|
+
(2) offer you this license which gives you legal permission to copy,
|
41
|
+
distribute and/or modify the software.
|
42
|
+
|
43
|
+
Also, for each author's protection and ours, we want to make certain
|
44
|
+
that everyone understands that there is no warranty for this free
|
45
|
+
software. If the software is modified by someone else and passed on, we
|
46
|
+
want its recipients to know that what they have is not the original, so
|
47
|
+
that any problems introduced by others will not reflect on the original
|
48
|
+
authors' reputations.
|
49
|
+
|
50
|
+
Finally, any free program is threatened constantly by software
|
51
|
+
patents. We wish to avoid the danger that redistributors of a free
|
52
|
+
program will individually obtain patent licenses, in effect making the
|
53
|
+
program proprietary. To prevent this, we have made it clear that any
|
54
|
+
patent must be licensed for everyone's free use or not licensed at all.
|
55
|
+
|
56
|
+
The precise terms and conditions for copying, distribution and
|
57
|
+
modification follow.
|
58
|
+
|
59
|
+
GNU GENERAL PUBLIC LICENSE
|
60
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
61
|
+
|
62
|
+
0. This License applies to any program or other work which contains
|
63
|
+
a notice placed by the copyright holder saying it may be distributed
|
64
|
+
under the terms of this General Public License. The "Program", below,
|
65
|
+
refers to any such program or work, and a "work based on the Program"
|
66
|
+
means either the Program or any derivative work under copyright law:
|
67
|
+
that is to say, a work containing the Program or a portion of it,
|
68
|
+
either verbatim or with modifications and/or translated into another
|
69
|
+
language. (Hereinafter, translation is included without limitation in
|
70
|
+
the term "modification".) Each licensee is addressed as "you".
|
71
|
+
|
72
|
+
Activities other than copying, distribution and modification are not
|
73
|
+
covered by this License; they are outside its scope. The act of
|
74
|
+
running the Program is not restricted, and the output from the Program
|
75
|
+
is covered only if its contents constitute a work based on the
|
76
|
+
Program (independent of having been made by running the Program).
|
77
|
+
Whether that is true depends on what the Program does.
|
78
|
+
|
79
|
+
1. You may copy and distribute verbatim copies of the Program's
|
80
|
+
source code as you receive it, in any medium, provided that you
|
81
|
+
conspicuously and appropriately publish on each copy an appropriate
|
82
|
+
copyright notice and disclaimer of warranty; keep intact all the
|
83
|
+
notices that refer to this License and to the absence of any warranty;
|
84
|
+
and give any other recipients of the Program a copy of this License
|
85
|
+
along with the Program.
|
86
|
+
|
87
|
+
You may charge a fee for the physical act of transferring a copy, and
|
88
|
+
you may at your option offer warranty protection in exchange for a fee.
|
89
|
+
|
90
|
+
2. You may modify your copy or copies of the Program or any portion
|
91
|
+
of it, thus forming a work based on the Program, and copy and
|
92
|
+
distribute such modifications or work under the terms of Section 1
|
93
|
+
above, provided that you also meet all of these conditions:
|
94
|
+
|
95
|
+
a) You must cause the modified files to carry prominent notices
|
96
|
+
stating that you changed the files and the date of any change.
|
97
|
+
|
98
|
+
b) You must cause any work that you distribute or publish, that in
|
99
|
+
whole or in part contains or is derived from the Program or any
|
100
|
+
part thereof, to be licensed as a whole at no charge to all third
|
101
|
+
parties under the terms of this License.
|
102
|
+
|
103
|
+
c) If the modified program normally reads commands interactively
|
104
|
+
when run, you must cause it, when started running for such
|
105
|
+
interactive use in the most ordinary way, to print or display an
|
106
|
+
announcement including an appropriate copyright notice and a
|
107
|
+
notice that there is no warranty (or else, saying that you provide
|
108
|
+
a warranty) and that users may redistribute the program under
|
109
|
+
these conditions, and telling the user how to view a copy of this
|
110
|
+
License. (Exception: if the Program itself is interactive but
|
111
|
+
does not normally print such an announcement, your work based on
|
112
|
+
the Program is not required to print an announcement.)
|
113
|
+
|
114
|
+
These requirements apply to the modified work as a whole. If
|
115
|
+
identifiable sections of that work are not derived from the Program,
|
116
|
+
and can be reasonably considered independent and separate works in
|
117
|
+
themselves, then this License, and its terms, do not apply to those
|
118
|
+
sections when you distribute them as separate works. But when you
|
119
|
+
distribute the same sections as part of a whole which is a work based
|
120
|
+
on the Program, the distribution of the whole must be on the terms of
|
121
|
+
this License, whose permissions for other licensees extend to the
|
122
|
+
entire whole, and thus to each and every part regardless of who wrote it.
|
123
|
+
|
124
|
+
Thus, it is not the intent of this section to claim rights or contest
|
125
|
+
your rights to work written entirely by you; rather, the intent is to
|
126
|
+
exercise the right to control the distribution of derivative or
|
127
|
+
collective works based on the Program.
|
128
|
+
|
129
|
+
In addition, mere aggregation of another work not based on the Program
|
130
|
+
with the Program (or with a work based on the Program) on a volume of
|
131
|
+
a storage or distribution medium does not bring the other work under
|
132
|
+
the scope of this License.
|
133
|
+
|
134
|
+
3. You may copy and distribute the Program (or a work based on it,
|
135
|
+
under Section 2) in object code or executable form under the terms of
|
136
|
+
Sections 1 and 2 above provided that you also do one of the following:
|
137
|
+
|
138
|
+
a) Accompany it with the complete corresponding machine-readable
|
139
|
+
source code, which must be distributed under the terms of Sections
|
140
|
+
1 and 2 above on a medium customarily used for software interchange; or,
|
141
|
+
|
142
|
+
b) Accompany it with a written offer, valid for at least three
|
143
|
+
years, to give any third party, for a charge no more than your
|
144
|
+
cost of physically performing source distribution, a complete
|
145
|
+
machine-readable copy of the corresponding source code, to be
|
146
|
+
distributed under the terms of Sections 1 and 2 above on a medium
|
147
|
+
customarily used for software interchange; or,
|
148
|
+
|
149
|
+
c) Accompany it with the information you received as to the offer
|
150
|
+
to distribute corresponding source code. (This alternative is
|
151
|
+
allowed only for noncommercial distribution and only if you
|
152
|
+
received the program in object code or executable form with such
|
153
|
+
an offer, in accord with Subsection b above.)
|
154
|
+
|
155
|
+
The source code for a work means the preferred form of the work for
|
156
|
+
making modifications to it. For an executable work, complete source
|
157
|
+
code means all the source code for all modules it contains, plus any
|
158
|
+
associated interface definition files, plus the scripts used to
|
159
|
+
control compilation and installation of the executable. However, as a
|
160
|
+
special exception, the source code distributed need not include
|
161
|
+
anything that is normally distributed (in either source or binary
|
162
|
+
form) with the major components (compiler, kernel, and so on) of the
|
163
|
+
operating system on which the executable runs, unless that component
|
164
|
+
itself accompanies the executable.
|
165
|
+
|
166
|
+
If distribution of executable or object code is made by offering
|
167
|
+
access to copy from a designated place, then offering equivalent
|
168
|
+
access to copy the source code from the same place counts as
|
169
|
+
distribution of the source code, even though third parties are not
|
170
|
+
compelled to copy the source along with the object code.
|
171
|
+
|
172
|
+
4. You may not copy, modify, sublicense, or distribute the Program
|
173
|
+
except as expressly provided under this License. Any attempt
|
174
|
+
otherwise to copy, modify, sublicense or distribute the Program is
|
175
|
+
void, and will automatically terminate your rights under this License.
|
176
|
+
However, parties who have received copies, or rights, from you under
|
177
|
+
this License will not have their licenses terminated so long as such
|
178
|
+
parties remain in full compliance.
|
179
|
+
|
180
|
+
5. You are not required to accept this License, since you have not
|
181
|
+
signed it. However, nothing else grants you permission to modify or
|
182
|
+
distribute the Program or its derivative works. These actions are
|
183
|
+
prohibited by law if you do not accept this License. Therefore, by
|
184
|
+
modifying or distributing the Program (or any work based on the
|
185
|
+
Program), you indicate your acceptance of this License to do so, and
|
186
|
+
all its terms and conditions for copying, distributing or modifying
|
187
|
+
the Program or works based on it.
|
188
|
+
|
189
|
+
6. Each time you redistribute the Program (or any work based on the
|
190
|
+
Program), the recipient automatically receives a license from the
|
191
|
+
original licensor to copy, distribute or modify the Program subject to
|
192
|
+
these terms and conditions. You may not impose any further
|
193
|
+
restrictions on the recipients' exercise of the rights granted herein.
|
194
|
+
You are not responsible for enforcing compliance by third parties to
|
195
|
+
this License.
|
196
|
+
|
197
|
+
7. If, as a consequence of a court judgment or allegation of patent
|
198
|
+
infringement or for any other reason (not limited to patent issues),
|
199
|
+
conditions are imposed on you (whether by court order, agreement or
|
200
|
+
otherwise) that contradict the conditions of this License, they do not
|
201
|
+
excuse you from the conditions of this License. If you cannot
|
202
|
+
distribute so as to satisfy simultaneously your obligations under this
|
203
|
+
License and any other pertinent obligations, then as a consequence you
|
204
|
+
may not distribute the Program at all. For example, if a patent
|
205
|
+
license would not permit royalty-free redistribution of the Program by
|
206
|
+
all those who receive copies directly or indirectly through you, then
|
207
|
+
the only way you could satisfy both it and this License would be to
|
208
|
+
refrain entirely from distribution of the Program.
|
209
|
+
|
210
|
+
If any portion of this section is held invalid or unenforceable under
|
211
|
+
any particular circumstance, the balance of the section is intended to
|
212
|
+
apply and the section as a whole is intended to apply in other
|
213
|
+
circumstances.
|
214
|
+
|
215
|
+
It is not the purpose of this section to induce you to infringe any
|
216
|
+
patents or other property right claims or to contest validity of any
|
217
|
+
such claims; this section has the sole purpose of protecting the
|
218
|
+
integrity of the free software distribution system, which is
|
219
|
+
implemented by public license practices. Many people have made
|
220
|
+
generous contributions to the wide range of software distributed
|
221
|
+
through that system in reliance on consistent application of that
|
222
|
+
system; it is up to the author/donor to decide if he or she is willing
|
223
|
+
to distribute software through any other system and a licensee cannot
|
224
|
+
impose that choice.
|
225
|
+
|
226
|
+
This section is intended to make thoroughly clear what is believed to
|
227
|
+
be a consequence of the rest of this License.
|
228
|
+
|
229
|
+
8. If the distribution and/or use of the Program is restricted in
|
230
|
+
certain countries either by patents or by copyrighted interfaces, the
|
231
|
+
original copyright holder who places the Program under this License
|
232
|
+
may add an explicit geographical distribution limitation excluding
|
233
|
+
those countries, so that distribution is permitted only in or among
|
234
|
+
countries not thus excluded. In such case, this License incorporates
|
235
|
+
the limitation as if written in the body of this License.
|
236
|
+
|
237
|
+
9. The Free Software Foundation may publish revised and/or new versions
|
238
|
+
of the General Public License from time to time. Such new versions will
|
239
|
+
be similar in spirit to the present version, but may differ in detail to
|
240
|
+
address new problems or concerns.
|
241
|
+
|
242
|
+
Each version is given a distinguishing version number. If the Program
|
243
|
+
specifies a version number of this License which applies to it and "any
|
244
|
+
later version", you have the option of following the terms and conditions
|
245
|
+
either of that version or of any later version published by the Free
|
246
|
+
Software Foundation. If the Program does not specify a version number of
|
247
|
+
this License, you may choose any version ever published by the Free Software
|
248
|
+
Foundation.
|
249
|
+
|
250
|
+
10. If you wish to incorporate parts of the Program into other free
|
251
|
+
programs whose distribution conditions are different, write to the author
|
252
|
+
to ask for permission. For software which is copyrighted by the Free
|
253
|
+
Software Foundation, write to the Free Software Foundation; we sometimes
|
254
|
+
make exceptions for this. Our decision will be guided by the two goals
|
255
|
+
of preserving the free status of all derivatives of our free software and
|
256
|
+
of promoting the sharing and reuse of software generally.
|
257
|
+
|
258
|
+
NO WARRANTY
|
259
|
+
|
260
|
+
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
261
|
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
262
|
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
263
|
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
264
|
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
265
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
266
|
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
267
|
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
268
|
+
REPAIR OR CORRECTION.
|
269
|
+
|
270
|
+
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
271
|
+
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
272
|
+
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
273
|
+
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
274
|
+
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
275
|
+
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
276
|
+
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
277
|
+
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
278
|
+
POSSIBILITY OF SUCH DAMAGES.
|
279
|
+
|
280
|
+
END OF TERMS AND CONDITIONS
|
281
|
+
|
282
|
+
How to Apply These Terms to Your New Programs
|
283
|
+
|
284
|
+
If you develop a new program, and you want it to be of the greatest
|
285
|
+
possible use to the public, the best way to achieve this is to make it
|
286
|
+
free software which everyone can redistribute and change under these terms.
|
287
|
+
|
288
|
+
To do so, attach the following notices to the program. It is safest
|
289
|
+
to attach them to the start of each source file to most effectively
|
290
|
+
convey the exclusion of warranty; and each file should have at least
|
291
|
+
the "copyright" line and a pointer to where the full notice is found.
|
292
|
+
|
293
|
+
<one line to give the program's name and a brief idea of what it does.>
|
294
|
+
Copyright (C) <year> <name of author>
|
295
|
+
|
296
|
+
This program is free software; you can redistribute it and/or modify
|
297
|
+
it under the terms of the GNU General Public License as published by
|
298
|
+
the Free Software Foundation; either version 2 of the License, or
|
299
|
+
(at your option) any later version.
|
300
|
+
|
301
|
+
This program is distributed in the hope that it will be useful,
|
302
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
303
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
304
|
+
GNU General Public License for more details.
|
305
|
+
|
306
|
+
You should have received a copy of the GNU General Public License along
|
307
|
+
with this program; if not, write to the Free Software Foundation, Inc.,
|
308
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
309
|
+
|
310
|
+
Also add information on how to contact you by electronic and paper mail.
|
311
|
+
|
312
|
+
If the program is interactive, make it output a short notice like this
|
313
|
+
when it starts in an interactive mode:
|
314
|
+
|
315
|
+
Gnomovision version 69, Copyright (C) year name of author
|
316
|
+
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
317
|
+
This is free software, and you are welcome to redistribute it
|
318
|
+
under certain conditions; type `show c' for details.
|
319
|
+
|
320
|
+
The hypothetical commands `show w' and `show c' should show the appropriate
|
321
|
+
parts of the General Public License. Of course, the commands you use may
|
322
|
+
be called something other than `show w' and `show c'; they could even be
|
323
|
+
mouse-clicks or menu items--whatever suits your program.
|
324
|
+
|
325
|
+
You should also get your employer (if you work as a programmer) or your
|
326
|
+
school, if any, to sign a "copyright disclaimer" for the program, if
|
327
|
+
necessary. Here is a sample; alter the names:
|
328
|
+
|
329
|
+
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
330
|
+
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
331
|
+
|
332
|
+
<signature of Ty Coon>, 1 April 1989
|
333
|
+
Ty Coon, President of Vice
|
334
|
+
|
335
|
+
This General Public License does not permit incorporating your program into
|
336
|
+
proprietary programs. If your program is a subroutine library, you may
|
337
|
+
consider it more useful to permit linking proprietary applications with the
|
338
|
+
library. If this is what you want to do, use the GNU Lesser General
|
339
|
+
Public License instead of this License.
|
notebook.py
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Imports
|
2
|
+
import os, tomllib, configparser
|
3
|
+
from .drawer import Drawer
|
4
|
+
|
5
|
+
# Jam classes
|
6
|
+
drawer = Drawer()
|
7
|
+
|
8
|
+
# Deals with configs and reading/writing files
|
9
|
+
class Notebook:
|
10
|
+
|
11
|
+
def __init__(self):
|
12
|
+
# Pre-requisites
|
13
|
+
script_folder = os.path.dirname(os.path.realpath(__file__))
|
14
|
+
script_folder = drawer.get_parent(script_folder)
|
15
|
+
self.config_template_file = f"{script_folder}/config.toml.in"
|
16
|
+
|
17
|
+
# Checking if config exists, and creating one if it does not
|
18
|
+
def check_config(self, config_file: str):
|
19
|
+
config_template = open(self.config_template_file, 'r').read()
|
20
|
+
config_folder = drawer.get_parent(config_file)
|
21
|
+
if drawer.is_folder(config_folder) is False:
|
22
|
+
drawer.make_folder(config_folder)
|
23
|
+
if drawer.is_file(config_file) is False:
|
24
|
+
drawer.make_file(config_file)
|
25
|
+
with open(config_file, 'w') as config:
|
26
|
+
config.write(config_template)
|
27
|
+
print(f"Created configuration file in '{config_folder}'.")
|
28
|
+
return config_file
|
29
|
+
|
30
|
+
# parsing a toml config
|
31
|
+
def read_toml(self, config_file: str):
|
32
|
+
config_file = os.path.normpath(config_file)
|
33
|
+
# Parsing config
|
34
|
+
data = open(config_file, 'r').read()
|
35
|
+
try:
|
36
|
+
data = tomllib.loads(data)
|
37
|
+
for category in data:
|
38
|
+
for item in data.get(category):
|
39
|
+
path = data.get(category).get(item)
|
40
|
+
if type(path) == str:
|
41
|
+
data[category][item] = path.replace(os.sep, '/')
|
42
|
+
return data
|
43
|
+
except:
|
44
|
+
print(f"Encountered error reading '{config_file}'")
|
45
|
+
print(f"Contents of '{config_file}':")
|
46
|
+
print(data)
|
47
|
+
return None
|
48
|
+
|
49
|
+
# Reads ini file and returns its contents in the form of a dict
|
50
|
+
def read_ini(self, ini_file: str, inline_comments=False):
|
51
|
+
if drawer.is_file(ini_file) is False:
|
52
|
+
return None
|
53
|
+
ini_file = os.path.normpath(ini_file)
|
54
|
+
if inline_comments is True:
|
55
|
+
parser = configparser.ConfigParser(inline_comment_prefixes=('#', ';'))
|
56
|
+
else:
|
57
|
+
parser = configparser.ConfigParser()
|
58
|
+
try:
|
59
|
+
parser.read(ini_file)
|
60
|
+
except configparser.ParsingError:
|
61
|
+
return None
|
62
|
+
sections = parser.sections()
|
63
|
+
data = {}
|
64
|
+
for section in sections:
|
65
|
+
keys = {}
|
66
|
+
for key in parser[section]:
|
67
|
+
value = parser[section][key]
|
68
|
+
keys[key] = value
|
69
|
+
data[section] = keys
|
70
|
+
return data
|
71
|
+
|
72
|
+
def write_ini(self, ini_file: str, contents: dict):
|
73
|
+
if drawer.is_file(ini_file) is False:
|
74
|
+
return None
|
75
|
+
ini_file = os.path.normpath(ini_file)
|
76
|
+
parser = configparser.ConfigParser()
|
77
|
+
for section in contents:
|
78
|
+
for var_name in contents.get(section):
|
79
|
+
value = contents.get(section).get(var_name)
|
80
|
+
if (section in parser) == False:
|
81
|
+
parser[section] = {}
|
82
|
+
parser[section][var_name] = value
|
83
|
+
with open(ini_file, 'w') as file:
|
84
|
+
parser.write(file)
|
typewriter.py
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# Imports
|
2
|
+
import shutil
|
3
|
+
|
4
|
+
# Responsible for formatting, modification and printing of strings
|
5
|
+
class Typewriter:
|
6
|
+
def __init__(self):
|
7
|
+
# Shorthand vars
|
8
|
+
self.BOLD = '\033[1m'
|
9
|
+
self.NORMAL = '\033[0m'
|
10
|
+
self.CLEAR = '\x1b[2K'
|
11
|
+
self.CURSOR_UP = '\033[1A'
|
12
|
+
|
13
|
+
# Gets a string, makes it bold, returns the string
|
14
|
+
def bolden(self, text: str):
|
15
|
+
text = f'{self.BOLD}{text}{self.NORMAL}'
|
16
|
+
return text
|
17
|
+
|
18
|
+
# Clears a given number of lines in the terminal
|
19
|
+
# if given 0 the current line will be erased
|
20
|
+
def clear_lines(self, lines: int):
|
21
|
+
if lines == 0:
|
22
|
+
print("\r" + self.CLEAR, end='')
|
23
|
+
return
|
24
|
+
for line in range(lines):
|
25
|
+
print(self.CURSOR_UP + self.CLEAR, end='')
|
26
|
+
|
27
|
+
# Clears current line to print a new one.
|
28
|
+
# Usecase: after typewriter.print_status()
|
29
|
+
def print(self, text: str):
|
30
|
+
self.clear_lines(0)
|
31
|
+
print(text)
|
32
|
+
|
33
|
+
# Prints on the same line
|
34
|
+
def print_status(self, status: str):
|
35
|
+
self.clear_lines(0)
|
36
|
+
print(f" {status}", end='\r')
|
37
|
+
|
38
|
+
# Prints on the same line
|
39
|
+
def print_progress(self, status: str, current: int, total: int):
|
40
|
+
width = 25
|
41
|
+
progress_float= (current / total)
|
42
|
+
percent = int(round((progress_float* 100), 0))
|
43
|
+
percent_string = str(percent)
|
44
|
+
if percent < 100:
|
45
|
+
percent_string = ' ' + percent_string
|
46
|
+
if percent < 10:
|
47
|
+
percent_string = ' ' + percent_string
|
48
|
+
progress_width = int(progress_float * width)
|
49
|
+
progress_bar = '=' * progress_width + ' ' * (width - progress_width)
|
50
|
+
self.print_status(f"{percent_string}% [{progress_bar}] {status}: {current}/{total}")
|
51
|
+
|
52
|
+
# Given a list, it returns a string with the elements of the given list
|
53
|
+
# arranged in in columns
|
54
|
+
def list_to_columns(self, text_list: list, num_of_columns = None, offset = 2):
|
55
|
+
column_width = len(max(text_list, key=len))
|
56
|
+
# Automatically set num of columns if not specified otherwise
|
57
|
+
if num_of_columns is None:
|
58
|
+
terminal_width = shutil.get_terminal_size()[0] - 1
|
59
|
+
num_of_columns = int(terminal_width / (column_width + offset))
|
60
|
+
if num_of_columns < 1: num_of_columns = 1
|
61
|
+
# Creating a list of columns
|
62
|
+
columns = []
|
63
|
+
iteration = 0
|
64
|
+
for item in text_list:
|
65
|
+
current_column = iteration % num_of_columns
|
66
|
+
if len(columns) <= current_column:
|
67
|
+
columns.append([])
|
68
|
+
columns[current_column].append(item)
|
69
|
+
iteration += 1
|
70
|
+
# Equalising width of columns
|
71
|
+
current_column = 0
|
72
|
+
for column in columns:
|
73
|
+
column_width = 0
|
74
|
+
# Getting column width
|
75
|
+
for text in column:
|
76
|
+
if len(text) > column_width:
|
77
|
+
column_width = len(text)
|
78
|
+
# Adding spaces
|
79
|
+
current_text = 0
|
80
|
+
for text in column:
|
81
|
+
spaces = ' ' * ((column_width - len(text)) + 1)
|
82
|
+
columns[current_column][current_text] = text + spaces
|
83
|
+
current_text += 1
|
84
|
+
current_column += 1
|
85
|
+
# Adding offset
|
86
|
+
iteration = 0
|
87
|
+
for text in columns[0]:
|
88
|
+
columns[0][iteration] = ' ' * offset + text
|
89
|
+
iteration += 1
|
90
|
+
# Adding newlines
|
91
|
+
last_column = len(columns) - 1
|
92
|
+
iteration = 0
|
93
|
+
for text in columns[last_column]:
|
94
|
+
columns[last_column][iteration] = text + '\n'
|
95
|
+
iteration += 1
|
96
|
+
# Creating list of rows
|
97
|
+
rows = []
|
98
|
+
for row in range(len(columns[0])):
|
99
|
+
rows.append([])
|
100
|
+
current_row = 0
|
101
|
+
# print(columns)
|
102
|
+
# print()
|
103
|
+
for row in rows:
|
104
|
+
current_column = 0
|
105
|
+
for column in columns:
|
106
|
+
try:
|
107
|
+
text = columns[current_column][current_row]
|
108
|
+
except IndexError:
|
109
|
+
continue
|
110
|
+
rows[current_row].append(text)
|
111
|
+
current_column += 1
|
112
|
+
current_row += 1
|
113
|
+
# Adding rows' text to output
|
114
|
+
output = ''
|
115
|
+
for row in rows:
|
116
|
+
for text in row:
|
117
|
+
output += text
|
118
|
+
# Returning string
|
119
|
+
return output
|