Skip to content

Commit

Permalink
initial commit extracting from wildfish/git-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
OmegaDroid committed Jan 17, 2018
0 parents commit da083f4
Show file tree
Hide file tree
Showing 11 changed files with 478 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# argparsetree
3 changes: 3 additions & 0 deletions argparsetree/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .cmd import BaseCommand # noqa

__version__ = '0.0.0'
127 changes: 127 additions & 0 deletions argparsetree/cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from __future__ import print_function

from argparse import ArgumentParser

import sys


class BaseCommand(object):
"""
The base command object
:var description: The brief description of the command
:var sub_commands: A dictionary mapping names to sub commands. Each value should be a class inheriting from ``BaseCommand``.
:var sub_command_required: Flag whether a sub command is required on the command line if ``sub_commands`` are specified.
"""
description = None
sub_commands = {}

def __init__(self, name=None, argv=None):
"""
Creates the command
:param name: The name the command is registered to
:param argv: List of argument values. If ``None``, ``sys.argv[1:]`` is used.
"""
self.name = name
self._arg_parser = None
self.argv = argv or sys.argv[1:]

@property
def sub_parser_dest_name(self):
"""
The name of the argument the name of the sub command will be stored in
"""
if self.name:
return u'{0}__sub_command'.format(self.name)
return 'sub_command'

@property
def arg_parser(self):
if not self._arg_parser:
self._arg_parser = ArgumentParser(self.get_description())
self.add_args(self._arg_parser)
self.register_sub_commands(self._arg_parser)

return self._arg_parser

def parse_args(self):
"""
Parses the command line arguments
:return: The arguments taken from the command line
"""
return self.arg_parser.parse_args(self.argv)

def add_args(self, parser):
"""
Adds arguments to the argument parser. This is used to modify which arguments are processed by the command.
For a full description of the argument parser see https://docs.python.org/3/library/argparse.html.
:param parser: The argument parser object
"""
pass

def register_sub_commands(self, parser):
"""
Add any sub commands to the argument parser.
:param parser: The argument parser object
"""
sub_commands = self.get_sub_commands()
if sub_commands:
sub_parsers = parser.add_subparsers(dest=self.sub_parser_dest_name)

for name, cls in sub_commands.items():
cmd = cls(name)

sub_parser = sub_parsers.add_parser(name, help=cmd.get_description(), description=cmd.get_description())

cmd.add_args(sub_parser)
cmd.register_sub_commands(sub_parser)

def get_sub_commands(self):
"""
Gets a dictionary mapping names to sub commands. Values should be classes inheriting from Base.
:return: The list of sub commands.
"""
return self.sub_commands

def get_description(self):
"""
Gets the description of the command
"""
return self.description

def action(self, args):
"""
Performs the action of the command.
This should be implemented by sub classes.
:param args: The arguments parsed from parse_args
:return: The status code of the action (0 on success)
"""
self.arg_parser.print_help()
return 1

def run(self, args=None):
"""
Runs the command passing in the parsed arguments.
:param args: The arguments to run the command with. If ``None`` the arguments
are gathered from the argument parser. This is automatically set when calling
sub commands and in most cases should not be set for the root command.
:return: The status code of the action (0 on success)
"""
args = args or self.parse_args()

sub_command_name = getattr(args, self.sub_parser_dest_name, None)
if sub_command_name:
sub_commands = self.get_sub_commands()
cmd_cls = sub_commands[sub_command_name]
return cmd_cls(sub_command_name).run(args)

return self.action(args)
10 changes: 10 additions & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-r ./requirements-package.in

pip-tools
flake8
pytest
mock
tox
hypothesis
coverage
pytest-cov
34 changes: 34 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file requirements.txt requirements.in
#
attrs==17.4.0 # via hypothesis, pytest
certifi==2017.11.5 # via requests
chardet==3.0.4 # via requests
click==6.7 # via pip-tools
coverage==4.4.2
first==2.0.1 # via pip-tools
flake8==3.5.0
hypothesis==3.44.16
idna==2.6 # via requests
jsonpickle==0.9.5
mccabe==0.6.1 # via flake8
mock==2.0.0
pbr==3.1.1 # via mock
pip-tools==1.11.0
pluggy==0.6.0 # via pytest, tox
py==1.5.2 # via pytest, tox
pycodestyle==2.3.1 # via flake8
pyflakes==1.6.0 # via flake8
pyodbc==4.0.21
pytest-cov==2.5.1
pytest==3.3.2
requests-toolbelt==0.8.0
requests==2.18.4
shutilwhich==1.1.0
six==1.11.0
tox==2.9.1
urllib3==1.22 # via requests
virtualenv==15.1.0 # via tox
18 changes: 18 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[wheel]
universal = 1

[metadata]
description-file = README.rst

[tool:pytest]
exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,.ropeproject

[flake8]
exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,.ropeproject,.hypothesis
ignore = E501

[coverage:run]
branch = true

[coverage:report]
show_missing = true
74 changes: 74 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import re
import shutil
import sys
from setuptools import setup, find_packages

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))


def get_readme():
with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
return readme.read()


def get_version():
"""
Return package version as listed in `__version__` in `init.py`.
"""
with open(os.path.join(os.path.dirname(__file__), 'argparsetree', '__init__.py')) as init_py:
return re.search('__version__ = [\'"]([^\'"]+)[\'"]', init_py.read()).group(1)


version = get_version()

if sys.argv[-1] == 'publish':
if os.system('pip freeze | grep wheel'):
print('wheel not installed.\nUse `pip install wheel`.\nExiting.')
sys.exit()
if os.system('pip freeze | grep twine'):
print('twine not installed.\nUse `pip install twine`.\nExiting.')
sys.exit()
os.system('python setup.py sdist bdist_wheel')
os.system('twine upload dist/*')
print('You probably want to also tag the version now:')
print(' git tag -a {v} -m \'version {v}\''.format(v=version))
print(' git push --tags')
shutil.rmtree('dist')
shutil.rmtree('build')
shutil.rmtree('argparsetree.egg-info')
sys.exit()

setup(
name='argparsetree',
version=version,
packages=find_packages(),
include_package_data=True,
package_data={
'': [
'requirements-package.in',
'LICENSE',
],
},
exclude_package_data={
'': ['__pycache__', '*.py[co]'],
},
license='BSD 3-Clause',
description='Package for creating complex command line argument trees using argparse',
long_description=get_readme(),
url='https://github.com/wildfish/argparsetree',
author='Wildfish',
author_email='[email protected]',
keywords='class based argparse',
classifiers=[
'Development Status :: 4 - Beta',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)
Empty file added tests/__init__.py
Empty file.
Loading

0 comments on commit da083f4

Please sign in to comment.