libjam 0.0.17__tar.gz → 0.0.19__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: libjam
3
- Version: 0.0.17
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
- responsible for handling command-line arguments
28
+ Responsible for handling command-line arguments.
29
29
 
30
30
  ### Drawer
31
- responsible for file operations
31
+ Responsible for file operations.
32
32
 
33
33
  ### Typewriter
34
- responsible for transforming and printing text
34
+ Responsible for transforming and printing text.
35
35
 
36
36
  ### Clipboard
37
- responsible for working with lists
37
+ Responsible for working with lists.
38
38
 
39
39
  ### Notebook
40
- responsible for configuration
40
+ Responsible for configuration.
41
41
 
42
42
  ### Flashcard
43
- responsible for getting user input from the command line
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
- cli = CLI()
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': cli.hello,
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, arguments, options)
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"cli.{function}")
81
+ exec(f"CLI().{function}")
89
82
  ```
@@ -11,22 +11,22 @@ pip install libjam
11
11
  libjam consists of of 6 modules:
12
12
 
13
13
  ### Captain
14
- responsible for handling command-line arguments
14
+ Responsible for handling command-line arguments.
15
15
 
16
16
  ### Drawer
17
- responsible for file operations
17
+ Responsible for file operations.
18
18
 
19
19
  ### Typewriter
20
- responsible for transforming and printing text
20
+ Responsible for transforming and printing text.
21
21
 
22
22
  ### Clipboard
23
- responsible for working with lists
23
+ Responsible for working with lists.
24
24
 
25
25
  ### Notebook
26
- responsible for configuration
26
+ Responsible for configuration.
27
27
 
28
28
  ### Flashcard
29
- responsible for getting user input from the command line
29
+ Responsible for getting user input from the command line.
30
30
 
31
31
  ## Example project
32
32
  ```python
@@ -44,14 +44,11 @@ class CLI:
44
44
  if options.get('world').get('enabled'):
45
45
  print('world!')
46
46
 
47
- cli = CLI()
48
-
49
- # Inputs/Commands/Options configuration
47
+ # Setting commands and options
50
48
  app = "example"
51
49
  description = "An example app for the libjam library"
52
- # help = "" # If you wish to set your own help page text
53
50
  commands = {
54
- 'print': {'function': cli.hello,
51
+ 'print': {'function': CLI.hello,
55
52
  'description': 'Prints given string'},
56
53
  }
57
54
  options = {
@@ -59,17 +56,13 @@ options = {
59
56
  'description': 'Appends \'world\' after printing given input'},
60
57
  }
61
58
 
62
- # Getting program arguments
63
- arguments = sys.argv
64
- # Removing script name from arguments
65
- arguments.remove(arguments[0])
66
59
  # Generating help
67
60
  help = captain.generate_help(app, description, commands, options)
68
61
  # Interpreting user input
69
- interpretation = captain.interpret(app, help, commands, arguments, options)
62
+ interpretation = captain.interpret(app, help, commands, options)
70
63
  # Getting parsed output
71
64
  function = interpretation.get('function')
72
65
  options = interpretation.get('options')
73
66
  # Executing function
74
- exec(f"cli.{function}")
67
+ exec(f"CLI().{function}")
75
68
  ```
@@ -0,0 +1,35 @@
1
+ #! /usr/bin/python
2
+
3
+ # Imports
4
+ import sys
5
+ from libjam import Captain
6
+
7
+ captain = Captain()
8
+
9
+ class CLI:
10
+ def hello(self, text):
11
+ print(text)
12
+ if options.get('world').get('enabled'):
13
+ print('world!')
14
+
15
+ # Setting commands and options
16
+ app = "example"
17
+ description = "An example app for the libjam library"
18
+ commands = {
19
+ 'print': {'function': CLI.hello,
20
+ 'description': 'Prints given string'},
21
+ }
22
+ options = {
23
+ 'world': {'long': ['world'], 'short': ['w'],
24
+ 'description': 'Appends \'world\' after printing given input'},
25
+ }
26
+
27
+ # Generating help
28
+ help = captain.generate_help(app, description, commands, options)
29
+ # Interpreting user input
30
+ interpretation = captain.interpret(app, help, commands, options)
31
+ # Getting parsed output
32
+ function = interpretation.get('function')
33
+ options = interpretation.get('options')
34
+ # Executing function
35
+ exec(f"CLI().{function}")
@@ -1,13 +1,25 @@
1
1
  # Imports
2
- import sys, re, inspect
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, arguments: list, options: dict = None):
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 clipboard.is_string_in_list(strings, argument):
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}' unrecognized. Try {app} help")
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 clipboard.is_string_in_list(strings, argument):
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}' unrecognized. Try {app} help")
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 clipboard.is_string_in_list(commands, argument):
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
- 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')
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
- 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)
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:
@@ -45,18 +45,19 @@ class Drawer:
45
45
  path = realpath(path)
46
46
  return outpath(path)
47
47
 
48
- # Returns True if give a path to folder
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 give a path to file
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 folder
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
- def get_file_size(self, path: str):
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':
@@ -14,7 +14,7 @@ include = [
14
14
 
15
15
  [project]
16
16
  name = "libjam"
17
- version = "0.0.17"
17
+ version = "0.0.19"
18
18
  authors = [
19
19
  { name="Philipp Kosarev", email="philipp.kosarev@gmail.com" },
20
20
  ]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes