From 61acf236b69110f1e40800cd7d229706335a8ecb Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Tue, 18 Jul 2017 09:57:03 +0200 Subject: glob matchin on plugin relative to module --- __init__.py | 21 +++++++++++------- _test/test_program.py | 1 + example/smartformatter.py | 1 + setup.py | 56 ++++++++++++++++++++++++++++++++--------------- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/__init__.py b/__init__.py index 2d475a2..967150f 100644 --- a/__init__.py +++ b/__init__.py @@ -4,8 +4,8 @@ from __future__ import print_function _package_data = dict( full_package_name='ruamel.std.argparse', - version_info=(0, 8, 0), - __version__='0.8.0', + version_info=(0, 8, 1), + __version__='0.8.1', author='Anthon van der Neut', author_email='a.van.der.neut@ruamel.eu', description='Enhancements to argparse: extra actions, subparser aliases, smart formatter, a decorator based wrapper', # NOQA @@ -31,7 +31,7 @@ PY3 = sys.version_info[0] == 3 if PY3: string_types = str, else: - string_types = basestring, + string_types = basestring, # NOQA store_true = 'store_true' store_false = 'store_false' @@ -162,10 +162,14 @@ class ProgramBase(object): self._sub_parsers = None methods_with_sub_parsers = [] # list to process, multilevel all_methods_with_sub_parsers = [] - file_names = glob.glob('*/__plug_in__.py') if self._full_package_name: + module = import_module( + self._full_package_name, + ) + file_names = glob.glob(os.path.join(os.path.dirname(module.__file__), + '*/__plug_in__.py')) for file_name in file_names: - dn, fn = file_name.split(os.sep) + _, dn, fn = file_name.rsplit(os.sep, 2) fn = fn.replace('.py', '') module = import_module( '.' + dn + '.' + fn, @@ -194,7 +198,7 @@ class ProgramBase(object): parser = ssp.add_parser(*arg, **method._sub_parser['kw']) info['parser'] = parser res = add_subparsers(info.get('ordering', []), - parser, level=level+1) + parser, level=level + 1) if res is None: # only set default if there are no subparsers, otherwise # defaults override @@ -240,7 +244,7 @@ class ProgramBase(object): else: v = info[k] print(' ' + ' ' * level, k, '->', v) - dump(info.get('ordering', []), level=level+1) + dump(info.get('ordering', []), level=level + 1) self._sub_parsers = add_subparsers( ProgramBase._methods_with_sub_parsers, self._parser) @@ -316,7 +320,7 @@ class ProgramBase(object): parent._sub_parser['sp'] = ssp sub_parsers = ssp arg = method._sub_parser['args'] - if not arg or not isinstance(arg[0], basestring): + if not arg or not isinstance(arg[0], basestring): # NOQA arg = list(arg) arg.insert(0, method.__name__) sp = sub_parsers.add_parser(*arg, @@ -517,4 +521,5 @@ def set_default_subparser(self, name, args=None): else: args.insert(0, name) + argparse.ArgumentParser.set_default_subparser = set_default_subparser diff --git a/_test/test_program.py b/_test/test_program.py index d67b7e0..608ee35 100644 --- a/_test/test_program.py +++ b/_test/test_program.py @@ -226,6 +226,7 @@ class TestProgram: pho = ParseHelpOutput(capsys, error=sys.version_info < (3, 4)) assert pho.start('version', '42') + if __name__ == '__main__': p = Program() p._parse_args() diff --git a/example/smartformatter.py b/example/smartformatter.py index a64174f..c0f45f1 100644 --- a/example/smartformatter.py +++ b/example/smartformatter.py @@ -7,6 +7,7 @@ import argparse def exit(self, *args, **kw): pass + argparse.ArgumentParser.exit = exit # the 'D|....' in the second pass triggers generating defaults for all entries, diff --git a/setup.py b/setup.py index dbb552e..e540f93 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ from ast import parse # NOQA from setuptools import setup, Extension, Distribution # NOQA from setuptools.command import install_lib # NOQA from setuptools.command.sdist import sdist as _sdist # NOQA -from wheel.bdist_wheel import bdist_wheel as _bdist_wheel # NOQA + if __name__ != '__main__': raise NotImplementedError('should never include setup.py') @@ -169,6 +169,7 @@ def _package_data(fn): raise NotImplementedError return data + # make sure you can run "python ../some/dir/setup.py install" pkg_data = _package_data(__file__.replace('setup.py', '__init__.py')) @@ -236,23 +237,35 @@ class MyInstallLib(install_lib.install_lib): class MySdist(_sdist): def initialize_options(self): _sdist.initialize_options(self) + # see pep 527, new uploads should be tar.gz or .zip + # fmt = getattr(self, 'tarfmt', None) # because of unicode_literals - self.formats = [b'bztar'] if sys.version_info < (3, ) else ['bztar'] + # self.formats = fmt if fmt else [b'bztar'] if sys.version_info < (3, ) else ['bztar'] dist_base = os.environ.get('PYDISTBASE') - fpn = getattr(getattr(self, 'nsp', self), 'full_package_name', None) + fpn = getattr(getattr(self, 'nsp', self), 'full_package_name', None) if fpn and dist_base: print('setting distdir {}/{}'.format(dist_base, fpn)) self.dist_dir = os.path.join(dist_base, fpn) -class MyBdistWheel(_bdist_wheel): - def initialize_options(self): - _bdist_wheel.initialize_options(self) - dist_base = os.environ.get('PYDISTBASE') - fpn = getattr(getattr(self, 'nsp', self), 'full_package_name', None) - if fpn and dist_base: - print('setting distdir {}/{}'.format(dist_base, fpn)) - self.dist_dir = os.path.join(dist_base, fpn) +# try except so this doesn't bomb when you don't have wheel installed, implies +# generation of wheels in ./dist +try: + from wheel.bdist_wheel import bdist_wheel as _bdist_wheel # NOQA + + class MyBdistWheel(_bdist_wheel): + def initialize_options(self): + _bdist_wheel.initialize_options(self) + dist_base = os.environ.get('PYDISTBASE') + fpn = getattr(getattr(self, 'nsp', self), 'full_package_name', None) + if fpn and dist_base: + print('setting distdir {}/{}'.format(dist_base, fpn)) + self.dist_dir = os.path.join(dist_base, fpn) + + _bdist_wheel_available = True + +except ImportError: + _bdist_wheel_available = False class InMemoryZipFile(object): @@ -344,7 +357,7 @@ class NameSpacePackager(object): sys.exit(1) # If you only support an extension module on Linux, Windows thinks it # is pure. That way you would get pure python .whl files that take - # precedence for downloading on Linux over source with compilable C + # precedence for downloading on Linux over source with compilable C code if self._pkg_data.get('universal'): Distribution.is_pure = lambda *args: True else: @@ -828,7 +841,17 @@ def main(): nsp.check() nsp.create_dirs() MySdist.nsp = nsp - MyBdistWheel.nsp = nsp + if pkg_data.get('tarfmt'): + MySdist.tarfmt = pkg_data.get('tarfmt') + + cmdclass = dict( + install_lib=MyInstallLib, + sdist=MySdist, + ) + if _bdist_wheel_available: + MyBdistWheel.nsp = nsp + cmdclass['bdist_wheel'] = MyBdistWheel + kw = dict( name=nsp.full_package_name, namespace_packages=nsp.namespace_packages, @@ -837,11 +860,7 @@ def main(): url=nsp.url, author=nsp.author, author_email=nsp.author_email, - cmdclass=dict( - install_lib=MyInstallLib, - sdist=MySdist, - bdist_wheel=MyBdistWheel, - ), + cmdclass=cmdclass, package_dir=nsp.package_dir, entry_points=nsp.entry_points(), description=nsp.description, @@ -899,4 +918,5 @@ def main(): imz.delete_from_zip_file(nsp.full_package_name + '.*.pth') break + main() -- cgit v1.2.1