impose-cli 0.0.13__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.
- impose_cli-0.0.13/PKG-INFO +7 -0
- impose_cli-0.0.13/README.md +9 -0
- impose_cli-0.0.13/impose_cli/__init__.py +1 -0
- impose_cli-0.0.13/impose_cli/impose_cli.py +115 -0
- impose_cli-0.0.13/impose_cli.egg-info/PKG-INFO +7 -0
- impose_cli-0.0.13/impose_cli.egg-info/SOURCES.txt +9 -0
- impose_cli-0.0.13/impose_cli.egg-info/dependency_links.txt +1 -0
- impose_cli-0.0.13/impose_cli.egg-info/requires.txt +1 -0
- impose_cli-0.0.13/impose_cli.egg-info/top_level.txt +1 -0
- impose_cli-0.0.13/setup.cfg +4 -0
- impose_cli-0.0.13/setup.py +16 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Impose CLI
|
|
2
|
+
|
|
3
|
+
Impose CLI is a simple tool used to generate a CLI tool by using one decorator.
|
|
4
|
+
|
|
5
|
+
### How to use
|
|
6
|
+
Functions that are meant to be CLI commands should be decorator with @impose
|
|
7
|
+
In the main method, the impose_cli(target=None)() method should be called.
|
|
8
|
+
|
|
9
|
+
If target is None, then the commands will be from the same file. Otherwise, include the directory for your commands.
|
|
@@ -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 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
click
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
impose_cli
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
VERSION = '0.0.13'
|
|
4
|
+
DESCRIPTION = 'Create a CLI tool easily.'
|
|
5
|
+
|
|
6
|
+
# Setting up
|
|
7
|
+
setup(
|
|
8
|
+
name="impose_cli",
|
|
9
|
+
version=VERSION,
|
|
10
|
+
author="scdev",
|
|
11
|
+
author_email="samuel.chai.development@gmail.com",
|
|
12
|
+
description=DESCRIPTION,
|
|
13
|
+
packages=find_packages(),
|
|
14
|
+
install_requires=['click'],
|
|
15
|
+
keywords=['python', 'cli', 'click'],
|
|
16
|
+
)
|