From 30dbd5babf56398c16efd82f513935d98e36a81d Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Tue, 28 Jan 2014 12:21:00 +0100 Subject: initial version --- .hgignore | 11 ++++++ MANIFEST.in | 1 + Makefile | 30 ++++++++++++++ README.rst | 2 + __init__.py | 36 +++++++++++++++++ _action/__init__.py | 1 + _action/checksinglestore.py | 16 ++++++++ _action/count.py | 19 +++++++++ _action/splitappend.py | 26 +++++++++++++ setup.py | 95 +++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 237 insertions(+) create mode 100644 .hgignore create mode 100644 MANIFEST.in create mode 100644 Makefile create mode 100644 README.rst create mode 100644 __init__.py create mode 100644 _action/__init__.py create mode 100644 _action/checksinglestore.py create mode 100644 _action/count.py create mode 100644 _action/splitappend.py create mode 100755 setup.py diff --git a/.hgignore b/.hgignore new file mode 100644 index 0000000..f668120 --- /dev/null +++ b/.hgignore @@ -0,0 +1,11 @@ + +syntax: glob + +*.pyc +*~ +*.bak +*.o +dist +build +*.egg-info +.tox diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..f1cb737 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include setup.py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3b26679 --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ + +VERSION:=$$(python setup.py --version) +DIST:=dist/ruamel.std.argparse-$(VERSION).tar.gz +REGEN:=/usr/local/bin/ruamel_util_new util --std argparse --skip-hg --skip-util + +sdist: + python setup.py sdist + +wheel: + python setup.py bdist_wheel + +clean: + rm -rf build *.egg-info/ + find . -name "*.pyc" -exec rm {} + + +tar: + tar tf $(DIST) + +devpi: + devpi upload $(DIST) + +regen_setup: + rm -f ../argparse/setup.py + $(REGEN) + pep8 setup.py + +regen_makefile: + rm -f ../argparse/Makefile + $(REGEN) + diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..393ee03 --- /dev/null +++ b/README.rst @@ -0,0 +1,2 @@ +argparse extensions +=================== diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..27847d1 --- /dev/null +++ b/__init__.py @@ -0,0 +1,36 @@ +# coding: utf-8 + +import argparse +from argparse import ArgumentParser + +class SubParsersAction(argparse._SubParsersAction): + """support aliases, based on differences of 3.3 and 2.7 + """ + class _AliasesChoicesPseudoAction(argparse.Action): + + def __init__(self, name, aliases, help): + metavar = dest = name + if aliases: + metavar += ' (%s)' % ', '.join(aliases) + sup = super(SubParsersAction._AliasesChoicesPseudoAction, self) + sup.__init__(option_strings=[], dest=dest, help=help, + metavar=metavar) + + def add_parser(self, name, **kwargs): + # remove aliases and help kwargs so orginal add_parser does not get them + aliases = kwargs.pop('aliases', ()) + help = kwargs.pop('help', None) + parser = argparse._SubParsersAction.add_parser(self, name, **kwargs) + + if help is not None: + choice_action = self._AliasesChoicesPseudoAction(name, aliases, help) + self._choices_actions.append(choice_action) + if aliases is not None: + for alias in aliases: + self._name_parser_map[alias] = parser + + return parser + +from _action.checksinglestore import CheckSingleStoreAction +from _action.count import CountAction +from _action.splitappend import SplitAppendAction diff --git a/_action/__init__.py b/_action/__init__.py new file mode 100644 index 0000000..9cd9b75 --- /dev/null +++ b/_action/__init__.py @@ -0,0 +1 @@ +"""extra actions for argparse""" diff --git a/_action/checksinglestore.py b/_action/checksinglestore.py new file mode 100644 index 0000000..27d5cc8 --- /dev/null +++ b/_action/checksinglestore.py @@ -0,0 +1,16 @@ +# coding: utf-8 + +from __future__ import print_function + +import argparse + + +class CheckSingleStoreAction(argparse.Action): + """issue a warning when the store action is called multiple times""" + def __call__(self, parser, namespace, values, option_string=None): + if getattr(namespace, self.dest, None) is not None: + print( + 'WARNING: previous optional argument "' + option_string + " " + + getattr(namespace, self.dest) + '" overwritten by "' + + option_string + " " + values + '"') + setattr(namespace, self.dest, values) diff --git a/_action/count.py b/_action/count.py new file mode 100644 index 0000000..4130e4f --- /dev/null +++ b/_action/count.py @@ -0,0 +1,19 @@ +# coding: utf-8 + +import argparse + + +class CountAction(argparse.Action): + """argparse action for counting up and down + + parser = argparse.ArgumentParser() + parser.add_argument('--verbose', '-v', action=CountAction, const=1, nargs=0) + parser.add_argument('--quiet', '-q', action=CountAction, dest='verbose', + const=-1, nargs=0) + """ + def __call__(self, parser, namespace, values, option_string=None): + try: + val = getattr(namespace, self.dest) + self.const + except TypeError: # probably None + val = self.const + setattr(namespace, self.dest, val) diff --git a/_action/splitappend.py b/_action/splitappend.py new file mode 100644 index 0000000..77008e2 --- /dev/null +++ b/_action/splitappend.py @@ -0,0 +1,26 @@ +# coding: utf-8 + +import argparse + + +class SplitAppendAction(argparse._AppendAction): + """append to list, like normal "append", but split first + (default split on ',') + + parser = argparse.ArgumentParser() + parser.add_argument('-d', action=SplitAppendAction) + + the following argument have the same list as result: + -d ab -d cd -d kl -d mn + -d ab,cd,kl,mn + -d ab,cd -d kl,mn + """ + def __init__(self, *args, **kw): + self._split_chr = ',' + argparse.Action.__init__(self, *args, **kw) + + def __call__(self, parser, namespace, values, option_string=None): + # _AppendAction does not return a value + for value in values.split(self._split_chr): + argparse._AppendAction.__call__( + self, parser, namespace, value, option_string) diff --git a/setup.py b/setup.py new file mode 100755 index 0000000..ca18752 --- /dev/null +++ b/setup.py @@ -0,0 +1,95 @@ +#! /usr/bin/env python +# coding: utf-8 + +import sys +import os +from setuptools import setup, find_packages +from setuptools.command import install_lib +from textwrap import dedent + +name_space = 'ruamel' +full_package_name = name_space + '.std.argparse' + +exclude_files = [ + 'setup.py' +] + +class MyInstallLib(install_lib.install_lib): + "create __init__.py on the fly" + def run(self): + install_lib.install_lib.run(self) + init_txt = dedent('''\ + # coding: utf-8 + # Copyright © 2013 Anthon van der Neut, RUAMEL bvba + "generated __init__.py " + try: + __import__('pkg_resources').declare_namespace(__name__) + except ImportError: + pass + ''') + init_path = full_package_name.split('.')[:-1] + for product_init in [ + os.path.join( + *([self.install_dir] + init_path[:p+1] + ['__init__.py'])) for p + in range(len(init_path)) + ]: + if not os.path.exists(product_init): + print('creating %s' % product_init) + with open(product_init, "w") as fp: + fp.write(init_txt) + setup = os.path.join(self.install_dir, 'setup.py') + + def install(self): + fpp = full_package_name.split('.') # full package path + full_exclude_files = [os.path.join(*(fpp + [x])) for x in exclude_files] + alt_files = [] + outfiles = install_lib.install_lib.install(self) + for x in outfiles: + for full_exclude_file in full_exclude_files: + if full_exclude_file in x: + os.remove(x) + break + else: + alt_files.append(x) + for x in alt_files: + print ' ', x.split('site-packages/')[-1] + + return alt_files + + +def main(): + install_requires = [ + ] + packages = [full_package_name] + [(full_package_name + '.' + x) for x \ + in find_packages(exclude=['tests'])] + setup( + name=full_package_name, + version="0.1.1", + description="Documentation for " + full_package_name, + install_requires=[ + # 'configobj >=4.7.2', + ], + #install_requires=install_requires, + long_description="Long Description", + url='https://bitbucket.org/anthon_van_der_neut/' + full_package_name, + author='Anthon van der Neut', + author_email='a.van.der.neut@ruamel.eu', + license='Copyright Ruamel bvba 2007-2013', + package_dir={full_package_name: '.'}, + namespace_packages = [name_space], + packages=packages, + cmdclass={'install_lib': MyInstallLib}, + classifiers=['Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: Copyrighted', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + ] + ) + +if __name__ == '__main__': + if len(sys.argv) > 1 and sys.argv[1] == 'sdist': + assert full_package_name == os.path.abspath(os.path.dirname( + __file__)).split('site-packages' + os.path.sep)[1].replace( + os.path.sep, '.') + main() -- cgit v1.2.1