Passed
Pull Request — master (#6)
by Komail
01:03
created

{{cookiecutter.project_slug}}.cli   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 12
dl 0
loc 54
rs 10
c 0
b 0
f 0
1
"""
2
This module bonds the entry points (console scripts) to the argument parser
3
interface. By doing so, console scripts can either be bonded to:
4
    1. Raw python functions
5
    2. Different argument parsing solutions
6
7
New interfaces can be added by creating a submodule to this package and
8
exposing a "cli" function in that module. See the scenario examples below to
9
understand how to add the newly created interface to console scripts.
10
11
This module dynamically loads the submodules and exposes as its own attributes
12
the cli function of the submodules as the name of the submodule.
13
For example in the submodule "main.py" the "cli" function will be an attribute
14
of this module available for use/calling as "cli.main".
15
16
The following two scenarios are equivalent due to the dynamic sub module import
17
and attribute allocation.
18
19
Scenario 1:
20
  In Python modules/executables:
21
    from .cli import main
22
    main.cli()
23
  In setup.py or setup.cfg (recomended):
24
    console_scripts =
25
      my_script = {{ cookiecutter.project_slug }}.cli.main:cli
26
27
Scenario 2 (recomended):
28
  In Python modules/executables:
29
    from . import cli
30
    cli.main()
31
  In setup.py or setup.cfg (recomended):
32
    console_scripts =
33
      my_script = {{ cookiecutter.project_slug }}.cli:main
34
35
"""
36
import pkgutil
37
import importlib
38
import pathlib
39
import sys
40
41
for module in pkgutil.iter_modules(
42
        path=[pathlib.Path(__file__).parent.absolute()]):
43
44
    _cli = importlib.import_module(f".{module.name}", package=__name__)
45
46
    if hasattr(_cli, "cli"):
47
        setattr(sys.modules[__name__], module.name, _cli.cli)
48
    else:
49
        raise AttributeError(f'"{module.name}" module in cli does not have an'
50
                             ' cli attribute, required to interface with'
51
                             ' console scripts')
52
53
    del _cli
54