impose-cli 0.0.13__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.
impose_cli/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from impose_cli.impose_cli import impose, impose_cli
@@ -0,0 +1,115 @@
1
+ import ast
2
+ import click
3
+ from importlib.util import spec_from_file_location, module_from_spec
4
+ from inspect import getframeinfo, currentframe
5
+ from inspect import Parameter, signature as sig
6
+ from os import listdir
7
+ from os.path import exists, join, dirname, abspath
8
+
9
+
10
+ def impose(func):
11
+ return func
12
+
13
+
14
+ def impose_cli(target: str = None) -> None:
15
+ """
16
+ If target is a directory, then each file will be a group and all functions will be a command
17
+ If medium is a file, then there will be no group and all functions will be a command
18
+ :param target:
19
+ :return:
20
+ """
21
+
22
+ class ImposeFunctionVisitor(ast.NodeVisitor):
23
+ def __init__(self):
24
+ self.imposed_functions = []
25
+
26
+ def visit_FunctionDef(self, node):
27
+ if hasattr(node, 'decorator_list'):
28
+ for decorator in node.decorator_list:
29
+ if isinstance(decorator, ast.Name) and decorator.id == 'impose':
30
+ self.imposed_functions.append(node.name)
31
+
32
+ def find_imposed_functions(filename):
33
+ with open(filename, 'r') as file:
34
+ file_contents = file.read()
35
+ module = ast.parse(file_contents)
36
+ visitor = ImposeFunctionVisitor()
37
+ visitor.visit(module)
38
+ return visitor.imposed_functions
39
+
40
+ def find_files(origin, target):
41
+ if target is None:
42
+ return [origin]
43
+
44
+ else:
45
+ directory = join(dirname(origin), target)
46
+ if not exists(directory):
47
+ raise FileNotFoundError(f"There is no directory {directory}.")
48
+
49
+ dirs = [x for x in listdir(directory) if x.endswith('.py')]
50
+ return [abspath(join(directory, f)) for f in dirs]
51
+
52
+
53
+ def load_module_from_file(file_path, module_name):
54
+ spec = spec_from_file_location(module_name, file_path)
55
+ module = module_from_spec(spec)
56
+ spec.loader.exec_module(module)
57
+ return module
58
+
59
+ def analyze_functions(module, function):
60
+ signature = sig(getattr(module, function))
61
+ parameters = signature.parameters
62
+ args_with_defaults = [(p.name, p.annotation if p.annotation.__name__ != '_empty' else None) for p in parameters.values() if p.default != Parameter.empty]
63
+ args_without_defaults = [(p.name, p.annotation if p.annotation.__name__ != '_empty' else None) for p in parameters.values() if p.default == Parameter.empty]
64
+ return args_without_defaults, args_with_defaults
65
+
66
+ def create_dynamic_group(group_name, command_list):
67
+ dynamic_group = click.Group(name=group_name)
68
+
69
+ for cmd in command_list:
70
+ dynamic_command = click.Command(name=cmd['name'].replace('_', '-'), callback=cmd['callback'])
71
+
72
+ for arg in cmd['arguments']:
73
+ arg_name = arg[0].replace('_', '-')
74
+ if arg[1] is not None:
75
+ dynamic_command.params.append(click.Argument((arg_name,), type=arg[1]))
76
+ else:
77
+ dynamic_command.params.append(click.Argument((arg_name,)))
78
+
79
+ for opt in cmd['options']:
80
+ opt_name = opt[0].replace('_', '-')
81
+ if opt[1] is not None:
82
+ dynamic_command.params.append(click.Argument((f"--{opt_name}",), type=opt[1]))
83
+ else:
84
+ dynamic_command.params.append(click.Option((f"--{opt_name}",)))
85
+
86
+ dynamic_group.add_command(dynamic_command)
87
+
88
+ return dynamic_group
89
+
90
+ origin_files = find_files(getframeinfo(currentframe().f_back)[0], target)
91
+
92
+ meta = {}
93
+ for origin_file in origin_files:
94
+ module_name = origin_file.split('/')[-1].replace('.py', '')
95
+ meta[module_name] = []
96
+ module = load_module_from_file(origin_file, module_name)
97
+ functions = find_imposed_functions(origin_file)
98
+ for function in functions:
99
+ args, opts = analyze_functions(module, function)
100
+ meta[module_name].append({
101
+ "name": function,
102
+ "arguments": args,
103
+ "options": opts,
104
+ "callback": getattr(module, function)
105
+ })
106
+
107
+ main_group = create_dynamic_group('cli', [])
108
+ if target is not None:
109
+ keys = meta.keys()
110
+ for key in keys:
111
+ main_group.add_command(create_dynamic_group(key, meta[key]))
112
+ else:
113
+ main_group = create_dynamic_group('cli', meta[list(meta.keys())[0]])
114
+
115
+ return main_group
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.1
2
+ Name: impose-cli
3
+ Version: 0.0.13
4
+ Summary: Create a CLI tool easily.
5
+ Author: scdev
6
+ Author-email: samuel.chai.development@gmail.com
7
+ Keywords: python,cli,click
8
+ Requires-Dist: click
9
+
@@ -0,0 +1,6 @@
1
+ impose_cli/__init__.py,sha256=2XmH8muqjy4EsYR_N6fQvuL7L3Bp99m-xgWmKie-c2Q,52
2
+ impose_cli/impose_cli.py,sha256=WO7Bwdeg_DKdvBnqDWwgHRwnjZl6ml3lfqa4u7Io-_8,4398
3
+ impose_cli-0.0.13.dist-info/METADATA,sha256=oFCsAZAma9alYdQCMohb84vsl7aASV6qzzdTuZbRTbU,201
4
+ impose_cli-0.0.13.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
5
+ impose_cli-0.0.13.dist-info/top_level.txt,sha256=lK5IEC0gUvQ-XIxuxHj4m8YcFsGGDQFRWW9pmN5jp44,11
6
+ impose_cli-0.0.13.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.38.4)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ impose_cli