libjam 0.0.17__tar.gz → 0.0.18__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.18
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
@@ -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
  ```
@@ -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:
@@ -52,11 +52,12 @@ class Drawer:
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 give a path to 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,6 +369,7 @@ class Drawer:
367
369
  temp = str(tempfile.gettempdir())
368
370
  return outpath(temp)
369
371
 
372
+ # Returns the size of given file/folder, in bytes.
370
373
  def get_file_size(self, path: str):
371
374
  path = realpath(path)
372
375
  try:
@@ -14,7 +14,7 @@ include = [
14
14
 
15
15
  [project]
16
16
  name = "libjam"
17
- version = "0.0.17"
17
+ version = "0.0.18"
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