Skip to content

Commit

Permalink
feat: Configure cement framework to CLI development
Browse files Browse the repository at this point in the history
  • Loading branch information
artu-hnrq committed Aug 19, 2022
1 parent 26e45ca commit 37f4279
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Structure package development start point
- Adopt a single-source package versioning strategy
- Define package main entry-point
- Configure cement framework to CLI development

### CI
- Include release on tag workflow
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
#
# This file specifies other distributions required to be installed when package is.
# See https://pip.pypa.io/en/stable/reference/requirements-file-format/.


cement~=3.0
colorlog~=6.6
jinja2~=3.1
pyyaml~=6.0
21 changes: 16 additions & 5 deletions src/python_package/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@


import sys
from . import PACKAGE_NAME, __version__


def package_info():
return f'{PACKAGE_NAME} {__version__}'
from . import PACKAGE_NAME, __version__
from .cli import CommandLineInterface


def main():
print(package_info())
with CommandLineInterface(
label=PACKAGE_NAME,
version=__version__,
) as app:
try:
app.run()

except AssertionError as e:
print('AssertionError > %s' % e.args[0])
app.exit_code = 1

if app.debug is True:
import traceback
traceback.print_exc()


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions src/python_package/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .app import CommandLineInterface
56 changes: 56 additions & 0 deletions src/python_package/cli/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from cement import App

from .controllers import Base


# Use `cement` framework to build a cli structure
# See https://docs.builtoncement.com/getting-started/framework-overview#the-application-object.
class CommandLineInterface(App):
"""Package command line interface"""

# Customize App behavior through Meta attributes
# See https://cement.readthedocs.io/en/latest/api/core/foundation/.
class Meta:
# The name of the application
# > Set on package inicialization through __main__
# label = <package_name>

# The version of the application
version = None

# A handler class that implements the Config interface.
# See https://docs.builtoncement.com/core-foundation/configuration-settings
config_handler = 'yaml'

# Extension used to identify application and plugin configuration files.
config_file_suffix = '.yml'

# Default configuration dictionary. Must be of type `dict`
# config_defaults = None

# A handler class that implements the Log interface.
# See https://docs.builtoncement.com/core-foundation/logging-1
log_handler = 'colorlog'

# A handler class that implements the Output interface
# See https://docs.builtoncement.com/core-foundation/output-rendering
output_handler = 'jinja2'

# register handlers
handlers = [
Base,
]

# List of additional framework extensions to load
extensions = [
'colorlog',
'jinja2',
'yaml',
]

# Whether (or not) to call sys.exit() when close() is called
exit_on_close = True

@property
def version(self):
return self._meta.version
1 change: 1 addition & 0 deletions src/python_package/cli/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .base import Base
37 changes: 37 additions & 0 deletions src/python_package/cli/controllers/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from cement import Controller


# Controlers are used on `cement` to manage available commands
# See https://docs.builtoncement.com/core-foundation/controllers.
class Base(Controller):
"""
Application root controller
It's designed to handle the argument route at runtime
(argument parsing, mapping sub-commands to controllers, etc)
"""

# Customize Controler behavior through Meta attributes
# See https://cement.readthedocs.io/en/3.0/api/ext/ext_argparse/#cement.ext.ext_argparse.ArgparseController.
class Meta:
label = 'base'

# text displayed at the top of --help output
# description = '' # TODO

# The text that is displayed at the bottom when --help is passed
# epilog = '' # TODO

# Arguments to pass to the argument_handler.
# The format is a list of tuples which items are a ( list, dict )
arguments = [
(['-v', '--version'], {'action': 'version'}),
]

def _default(self):
"""Default action if no sub-command is passed."""

self.app.args.print_help()

def _pre_argument_parsing(self):
self._parser.version = self.app.version
2 changes: 2 additions & 0 deletions src/python_package/utils/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
# in this file.
# See https://github.com/django/django/blob/main/django/utils/version.py.


import subprocess

from datetime import datetime


Expand Down

0 comments on commit 37f4279

Please sign in to comment.