libjam 0.0.17__py3-none-any.whl → 0.0.19__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.
- libjam/captain.py +38 -24
- libjam/drawer.py +35 -4
- {libjam-0.0.17.dist-info → libjam-0.0.19.dist-info}/METADATA +11 -18
- {libjam-0.0.17.dist-info → libjam-0.0.19.dist-info}/RECORD +6 -6
- {libjam-0.0.17.dist-info → libjam-0.0.19.dist-info}/WHEEL +0 -0
- {libjam-0.0.17.dist-info → libjam-0.0.19.dist-info}/licenses/LICENSE +0 -0
libjam/captain.py
CHANGED
@@ -1,13 +1,25 @@
|
|
1
1
|
# Imports
|
2
|
-
import sys
|
2
|
+
import sys
|
3
|
+
from inspect import signature
|
3
4
|
from .typewriter import Typewriter
|
4
|
-
from .clipboard import Clipboard
|
5
5
|
typewriter = Typewriter()
|
6
|
-
clipboard = Clipboard()
|
7
6
|
|
8
7
|
# Processes command line arguments
|
9
8
|
class Captain:
|
10
9
|
|
10
|
+
def get_args(self):
|
11
|
+
args = sys.argv
|
12
|
+
args.pop(0)
|
13
|
+
return args
|
14
|
+
|
15
|
+
# Returns a list of args a function requires.
|
16
|
+
def get_function_args(self, function):
|
17
|
+
args = str(signature(function))
|
18
|
+
args = args.removeprefix('(').removesuffix(')').replace(' ', '').split(',')
|
19
|
+
if 'self' in args:
|
20
|
+
args.remove('self')
|
21
|
+
return args
|
22
|
+
|
11
23
|
# Returns a generated a help page based on provided inputs
|
12
24
|
def generate_help(self, app: str, description: str, commands: dict, options: dict = None):
|
13
25
|
offset = 2; offset_string = ' ' * offset
|
@@ -49,8 +61,9 @@ class Captain:
|
|
49
61
|
|
50
62
|
|
51
63
|
# Interprets input arguments
|
52
|
-
def interpret(self, app: str, help: str, commands: dict,
|
64
|
+
def interpret(self, app: str, help: str, commands: dict, options: dict = None):
|
53
65
|
# Class vars
|
66
|
+
arguments = self.get_args()
|
54
67
|
chosen_command = None
|
55
68
|
self.function = None
|
56
69
|
self.arbitrary_args = False
|
@@ -70,52 +83,53 @@ class Captain:
|
|
70
83
|
# Long options
|
71
84
|
if argument.startswith("--"):
|
72
85
|
argument = argument.removeprefix("--")
|
86
|
+
if argument == '':
|
87
|
+
print(f"Invalid option '--'. Try {app} help")
|
88
|
+
sys.exit(-1)
|
73
89
|
for option in options:
|
74
90
|
strings = options.get(option).get('long')
|
75
|
-
if
|
91
|
+
if argument in strings:
|
76
92
|
options[option]['enabled'] = True
|
77
93
|
self.arg_found = True
|
78
94
|
if self.arg_found is False:
|
79
|
-
print(f"Option '{argument}'
|
95
|
+
print(f"Option '{argument}' unrecognised. Try {app} help")
|
80
96
|
sys.exit(-1)
|
81
97
|
|
82
98
|
# Short options
|
83
99
|
else:
|
84
100
|
argument = argument.removeprefix("-")
|
101
|
+
if argument == '':
|
102
|
+
print(f"Invalid option '-'. Try {app} help")
|
103
|
+
sys.exit(-1)
|
85
104
|
arguments = list(argument)
|
86
105
|
for argument in arguments:
|
87
106
|
command_found = False
|
88
107
|
for option in options:
|
89
108
|
strings = options.get(option).get('short')
|
90
|
-
if
|
109
|
+
if argument in strings:
|
91
110
|
options[option]['enabled'] = True
|
92
111
|
command_found = True
|
93
112
|
if command_found is False:
|
94
|
-
print(f"Option '{argument}'
|
113
|
+
print(f"Option '{argument}' unrecognised. Try {app} help")
|
95
114
|
sys.exit(-1)
|
96
115
|
|
97
116
|
# Commands
|
98
117
|
else:
|
99
118
|
if chosen_command is None:
|
100
|
-
if
|
119
|
+
if argument == 'help':
|
120
|
+
print(help)
|
121
|
+
sys.exit(0)
|
122
|
+
elif argument in commands:
|
101
123
|
chosen_command = argument
|
102
124
|
command_function = commands.get(chosen_command).get('function')
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
if clipboard.is_string_in_list(command_args, '*args'):
|
107
|
-
command_args.remove('*args')
|
125
|
+
command_function_args = self.get_function_args(command_function)
|
126
|
+
if '*args' in command_function_args:
|
127
|
+
command_function_args.remove('*args')
|
108
128
|
self.arbitrary_args = True
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
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)
|
129
|
+
self.required_args = len(command_function_args)
|
130
|
+
else:
|
131
|
+
print(f"Command '{argument}' unrecognised. Try {app} help")
|
132
|
+
sys.exit(-1)
|
119
133
|
|
120
134
|
# Command arguments
|
121
135
|
else:
|
libjam/drawer.py
CHANGED
@@ -45,18 +45,19 @@ class Drawer:
|
|
45
45
|
path = realpath(path)
|
46
46
|
return outpath(path)
|
47
47
|
|
48
|
-
# Returns True if
|
48
|
+
# Returns True if given a path to a folder.
|
49
49
|
def is_folder(self, path: str):
|
50
50
|
if path == '':
|
51
51
|
return False
|
52
52
|
path = realpath(path)
|
53
53
|
return os.path.isdir(path)
|
54
54
|
|
55
|
-
# Returns True if
|
55
|
+
# Returns True if given a path to a file.
|
56
56
|
def is_file(self, path: str):
|
57
57
|
path = realpath(path)
|
58
58
|
return os.path.isfile(path)
|
59
59
|
|
60
|
+
# Returns True if path exists.
|
60
61
|
def exists(self, path: str):
|
61
62
|
path = realpath(path)
|
62
63
|
is_file = self.is_folder(path)
|
@@ -78,7 +79,7 @@ class Drawer:
|
|
78
79
|
filetype = os.path.splitext(basename)[1].removeprefix('.')
|
79
80
|
return filetype
|
80
81
|
|
81
|
-
# Returns a list of files and folders in a given
|
82
|
+
# Returns a list of files and folders in a given path.
|
82
83
|
def get_all(self, path: str):
|
83
84
|
path = realpath(path)
|
84
85
|
relative_files = os.listdir(path)
|
@@ -234,6 +235,7 @@ class Drawer:
|
|
234
235
|
depth = len(depth)
|
235
236
|
return depth
|
236
237
|
|
238
|
+
# Returns the basename of file(s)/folder(s).
|
237
239
|
def basename(self, path: str or list):
|
238
240
|
if type(path) == str:
|
239
241
|
path = realpath(path)
|
@@ -367,7 +369,8 @@ class Drawer:
|
|
367
369
|
temp = str(tempfile.gettempdir())
|
368
370
|
return outpath(temp)
|
369
371
|
|
370
|
-
|
372
|
+
# Returns the weight of given file/folder, in bytes.
|
373
|
+
def get_filesize(self, path: str):
|
371
374
|
path = realpath(path)
|
372
375
|
try:
|
373
376
|
if self.is_file(path):
|
@@ -382,6 +385,34 @@ class Drawer:
|
|
382
385
|
typewriter.print('Program aborted while gathering size of files.')
|
383
386
|
sys.exit(1)
|
384
387
|
|
388
|
+
# Given a number of bytes, returns a human readable filesize as a tuple.
|
389
|
+
# Tuple format: (value: int, short_unit_name: str, long_unit_name: str)
|
390
|
+
# Example tuple: (6.986356, 'mb', 'megabytes')
|
391
|
+
def get_readable_filesize(self, filesize: int):
|
392
|
+
if filesize > 1000 ** 7:
|
393
|
+
value = filesize / 1000 ** 7
|
394
|
+
return (value, 'zb', 'zettabytes')
|
395
|
+
elif filesize > 1000 ** 6:
|
396
|
+
value = filesize / 1000 ** 6
|
397
|
+
return (value, 'eb', 'exabytes')
|
398
|
+
elif filesize > 1000 ** 5:
|
399
|
+
value = filesize / 1000 ** 5
|
400
|
+
return (value, 'pb', 'petabytes')
|
401
|
+
elif filesize > 1000 ** 4:
|
402
|
+
value = filesize / 1000 ** 4
|
403
|
+
return (value, 'tb', 'terabytes')
|
404
|
+
elif filesize > 1000 ** 3:
|
405
|
+
value = filesize / 1000 ** 3
|
406
|
+
return (value, 'gb', 'gigabytes')
|
407
|
+
elif filesize > 1000 ** 2:
|
408
|
+
value = filesize / 1000 ** 2
|
409
|
+
return (value, 'mb', 'megabytes')
|
410
|
+
elif filesize > 1000:
|
411
|
+
value = filesize / 1000 ** 1
|
412
|
+
return (value, 'kb', 'kilobytes')
|
413
|
+
else:
|
414
|
+
return (filesize, 'b', 'bytes')
|
415
|
+
|
385
416
|
def open(self, path: str):
|
386
417
|
path = realpath(path)
|
387
418
|
if PLATFORM == 'Linux':
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: libjam
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.19
|
4
4
|
Summary: A library jam for Python.
|
5
5
|
Project-URL: Homepage, https://github.com/philippkosarev/libjam
|
6
6
|
Project-URL: Issues, https://github.com/philippkosarev/libjam/issues
|
@@ -25,22 +25,22 @@ pip install libjam
|
|
25
25
|
libjam consists of of 6 modules:
|
26
26
|
|
27
27
|
### Captain
|
28
|
-
|
28
|
+
Responsible for handling command-line arguments.
|
29
29
|
|
30
30
|
### Drawer
|
31
|
-
|
31
|
+
Responsible for file operations.
|
32
32
|
|
33
33
|
### Typewriter
|
34
|
-
|
34
|
+
Responsible for transforming and printing text.
|
35
35
|
|
36
36
|
### Clipboard
|
37
|
-
|
37
|
+
Responsible for working with lists.
|
38
38
|
|
39
39
|
### Notebook
|
40
|
-
|
40
|
+
Responsible for configuration.
|
41
41
|
|
42
42
|
### Flashcard
|
43
|
-
|
43
|
+
Responsible for getting user input from the command line.
|
44
44
|
|
45
45
|
## Example project
|
46
46
|
```python
|
@@ -58,14 +58,11 @@ class CLI:
|
|
58
58
|
if options.get('world').get('enabled'):
|
59
59
|
print('world!')
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
# Inputs/Commands/Options configuration
|
61
|
+
# Setting commands and options
|
64
62
|
app = "example"
|
65
63
|
description = "An example app for the libjam library"
|
66
|
-
# help = "" # If you wish to set your own help page text
|
67
64
|
commands = {
|
68
|
-
'print': {'function':
|
65
|
+
'print': {'function': CLI.hello,
|
69
66
|
'description': 'Prints given string'},
|
70
67
|
}
|
71
68
|
options = {
|
@@ -73,17 +70,13 @@ options = {
|
|
73
70
|
'description': 'Appends \'world\' after printing given input'},
|
74
71
|
}
|
75
72
|
|
76
|
-
# Getting program arguments
|
77
|
-
arguments = sys.argv
|
78
|
-
# Removing script name from arguments
|
79
|
-
arguments.remove(arguments[0])
|
80
73
|
# Generating help
|
81
74
|
help = captain.generate_help(app, description, commands, options)
|
82
75
|
# Interpreting user input
|
83
|
-
interpretation = captain.interpret(app, help, commands,
|
76
|
+
interpretation = captain.interpret(app, help, commands, options)
|
84
77
|
# Getting parsed output
|
85
78
|
function = interpretation.get('function')
|
86
79
|
options = interpretation.get('options')
|
87
80
|
# Executing function
|
88
|
-
exec(f"
|
81
|
+
exec(f"CLI().{function}")
|
89
82
|
```
|
@@ -1,11 +1,11 @@
|
|
1
1
|
libjam/__init__.py,sha256=iLE2y9r0Sfe3oIeoFHKwFIyLL6d_KcLP7fINd7pPAlY,145
|
2
|
-
libjam/captain.py,sha256=
|
2
|
+
libjam/captain.py,sha256=cr3DbTVskwIRp8CSYYlvElxdugql54ED3Ujsv89mgAA,5868
|
3
3
|
libjam/clipboard.py,sha256=5HxlO8ztLJPlnSCq4PncGvY4JGc9C4J2uHcepjC6zmg,3496
|
4
|
-
libjam/drawer.py,sha256
|
4
|
+
libjam/drawer.py,sha256=vavoLR1T2U4Dl-nJPBSEolZJzqKqqOYQbhwQHNoH8zE,13233
|
5
5
|
libjam/flashcard.py,sha256=ulV4KPC3BRWLxwkQ87vsY0aM38nYpzOjUOISxTIRVDg,437
|
6
6
|
libjam/notebook.py,sha256=uGHqQD01SLrCIqMmzQayyk6PNeE15nJXJYUmmcJvf9U,4289
|
7
7
|
libjam/typewriter.py,sha256=6XcYIsCwoNkyyuwiDm3-QP52Dl70sJSGbO8ikjrPNvo,3996
|
8
|
-
libjam-0.0.
|
9
|
-
libjam-0.0.
|
10
|
-
libjam-0.0.
|
11
|
-
libjam-0.0.
|
8
|
+
libjam-0.0.19.dist-info/METADATA,sha256=VbzszFR0dLgsl5CVKmr3uuPdgbtbUWF1Zh887hFo7yw,1941
|
9
|
+
libjam-0.0.19.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
10
|
+
libjam-0.0.19.dist-info/licenses/LICENSE,sha256=gXf5dRMhNSbfLPYYTY_5hsZ1r7UU1OaKQEAQUhuIBkM,18092
|
11
|
+
libjam-0.0.19.dist-info/RECORD,,
|
File without changes
|
File without changes
|