diff options
author | 2015-08-26 21:26:23 -0400 | |
---|---|---|
committer | 2015-08-26 21:52:56 -0400 | |
commit | f15e273644025ebd5d2e24b859c763125261b767 (patch) | |
tree | 805d3d7e854cafa9bdf404f89138198d1ca047ad /examples | |
parent | setup: move man page generation/bundling to distutils extensions (diff) | |
download | pkgcore-f15e273644025ebd5d2e24b859c763125261b767.tar.gz pkgcore-f15e273644025ebd5d2e24b859c763125261b767.tar.bz2 pkgcore-f15e273644025ebd5d2e24b859c763125261b767.zip |
convert example scripts from optparse to argparse
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/changed_use.py | 64 | ||||
-rwxr-xr-x | examples/pclean.py | 63 | ||||
-rwxr-xr-x | examples/pkg_info.py | 23 | ||||
-rwxr-xr-x | examples/repo_list.py | 37 |
4 files changed, 88 insertions, 99 deletions
diff --git a/examples/changed_use.py b/examples/changed_use.py index 9e099909..0a892db4 100755 --- a/examples/changed_use.py +++ b/examples/changed_use.py @@ -1,10 +1,10 @@ #!/usr/bin/env python - # Copyright 2007 Charlie Shepherd from __future__ import print_function from operator import attrgetter +import os import sys try: @@ -19,43 +19,34 @@ except ImportError: raise sys.exit(1) +argparser = commandline.mk_argparser(color=False, version=False) +argparser.add_argument( + 'target', nargs='+', help='target package atoms') +argparser.add_argument( + '--repo', action=commandline.StoreRepoObject, + help='repo to use (default from domain if omitted).') +argparser.add_argument( + '--print_type', '-t', default="cpvstr", + choices=("slotted_atom", "versioned_atom", "cpvstr"), + help='''type of atom to output: + 'versioned_atom' : a valid versioned atom, + 'slotted_atom' : a valid slotted atom, + 'cpvstr' : the cpv of the package''') -class OptionParser(commandline.OptionParser): - - def __init__(self, **kwargs): - commandline.OptionParser.__init__( - self, description=__doc__, usage='%prog [options]', - **kwargs) - self.add_option('--repo', action='callback', type='string', - callback=commandline.config_callback, - callback_args=('repo',), - help='repo to use (default from domain if omitted).') - self.add_option('--verbose', '-v', action='store_true', default=False, - help='print packages that have not changed too') - self.add_option('--quiet', '-q', action='store_true', default=False, - help="don't print changed useflags") - self.add_option('--print_type', '-t', - type="choice", - choices=("slotted_atom", "versioned_atom", "cpvstr"), - default="cpvstr", - help='''type of atom to output: - 'versioned_atom' : a valid versioned atom, - 'slotted_atom' : a valid slotted atom, - 'cpvstr' : the cpv of the package''') - def check_values(self, values, args): - values, args = commandline.OptionParser.check_values( - self, values, args) +@argparser.bind_final_check +def check_args(parser, namespace): + domain = namespace.domain + namespace.vdb = domain.vdb[0] + if not namespace.repo: + namespace.repo = domain.repos[1] - domain = values.config.get_default('domain') - values.vdb = domain.vdb[0] - if not values.repo: - values.repo = domain.repos[1] + namespace.restrict = OrRestriction( + *commandline.convert_to_restrict(namespace.target)) + namespace.outputter = attrgetter(namespace.print_type) - values.restrict = OrRestriction(*commandline.convert_to_restrict(args)) - values.outputter = attrgetter(values.print_type) - return values, () +@argparser.bind_main_func def main(options, out, err): repo = options.repo for built in options.vdb.itermatch(options.restrict): @@ -69,11 +60,12 @@ def main(options, out, err): if options.quiet: out.write(options.outputter(current)) else: - out.write("for package %s, %d flags have changed:\n\t%s" % - (current.cpvstr, len(changed_flags), ' '.join(changed_flags))) + out.write( + "for package %s, %d flags have changed:\n\t%s" % + (current.cpvstr, len(changed_flags), ' '.join(changed_flags))) else: if options.verbose: out.write("%s is the same as it was before" % current.cpvstr) if __name__ == '__main__': - commandline.main({None: (OptionParser, main)}) + commandline.main(argparser) diff --git a/examples/pclean.py b/examples/pclean.py index ea6186ee..1847b9de 100755 --- a/examples/pclean.py +++ b/examples/pclean.py @@ -5,6 +5,7 @@ from __future__ import print_function +import argparse from os.path import basename import sys @@ -29,34 +30,35 @@ except ImportError: sys.exit(1) -class OptionParser(commandline.OptionParser): - def __init__(self, **kwargs): - commandline.OptionParser.__init__(self, description=__doc__, **kwargs) - self.add_option("--exclude", "-e", action='append', dest='excludes') - self.add_option("--exclude-file", "-E", action='callback', dest='excludes', - callback=commandline.read_file_callback, type="string", - help='path to the exclusion file') - self.add_option("--ignore-failures", "-i", action="store_true", - default=False, help="ignore checksum parsing errors") +argparser = commandline.mk_argparser(color=False, version=False) +argparser.add_argument( + "--exclude", "-e", action='append', dest='excludes') +argparser.add_argument( + "--exclude-file", "-E", + type=argparse.FileType('r'), + help='path to the exclusion file') +argparser.add_argument( + "--ignore-failures", "-i", action="store_true", + default=False, help="ignore checksum parsing errors") - def check_values(self, values, args): - values, args = commandline.OptionParser.check_values( - self, values, args) - if args: - self.error("This script takes no arguments") - domain = values.config.get_default('domain') - values.vdb = domain.vdb - values.repo = multiplex_tree(*get_virtual_repos(domain.repos, False)) - values.distdir = domain.fetcher.distdir - restrict = commandline.convert_to_restrict(values.excludes, default=None) - if restrict != [None]: - values.restrict = OrRestriction(negate=True, *restrict) - else: - values.restrict = packages.AlwaysTrue +@argparser.bind_final_check +def check_args(parser, namespace): + domain = namespace.domain + namespace.vdb = domain.vdb + namespace.repo = multiplex_tree(*get_virtual_repos(domain.repos, False)) + namespace.distdir = domain.fetcher.distdir + excludes = namespace.excludes if namespace.excludes is not None else [] + if namespace.exclude_file is not None: + excludes.extend(namespace.exclude_file.read().split('\n')) + restrict = commandline.convert_to_restrict(excludes, default=None) + if restrict != [None]: + namespace.restrict = OrRestriction(negate=True, *restrict) + else: + namespace.restrict = packages.AlwaysTrue - return values, () +@argparser.bind_main_func def main(options, out, err): if options.debug: out.write('starting scanning distdir %s...' % options.distdir) @@ -68,10 +70,12 @@ def main(options, out, err): pfiles = set() for pkg in options.repo.itermatch(options.restrict, sorter=sorted): try: - pfiles.update(fetchable.filename for fetchable in - iflatten_instance(pkg.fetchables, fetchable_kls)) + pfiles.update( + fetchable.filename for fetchable in + iflatten_instance(pkg.fetchables, fetchable_kls)) except ParseChksumError as e: - err.write("got corruption error '%s', with package %s " % + err.write( + "got corruption error '%s', with package %s " % (e, pkg.cpvstr)) if options.ignore_failures: err.write("skipping...") @@ -80,7 +84,8 @@ def main(options, out, err): err.write("aborting...") return 1 except Exception as e: - err.write("got error '%s', parsing package %s in repo '%s'" % + err.write( + "got error '%s', parsing package %s in repo '%s'" % (e, pkg.cpvstr, pkg.repo)) raise @@ -89,4 +94,4 @@ def main(options, out, err): out.write(pjoin(d, file)) if __name__ == '__main__': - commandline.main({None: (OptionParser, main)}) + commandline.main(argparser) diff --git a/examples/pkg_info.py b/examples/pkg_info.py index acdca12c..ec0d1695 100755 --- a/examples/pkg_info.py +++ b/examples/pkg_info.py @@ -18,18 +18,16 @@ except ImportError: sys.exit(1) -class OptionParser(commandline.OptionParser): - def __init__(self, **kwargs): - commandline.OptionParser.__init__( - self, description=__doc__, usage='%prog <atom>', - **kwargs) +argparser = commandline.mk_argparser(color=False, version=False) +argparser.add_argument( + 'target', nargs='+', help='target package atoms') - def check_values(self, values, args): - values, args = commandline.OptionParser.check_values( - self, values, args) - values.repo = values.config.get_default('domain').ebuild_repos - values.restrict = OrRestriction(*commandline.convert_to_restrict(args)) - return values, () + +@argparser.bind_final_check +def check_args(parser, namespace): + namespace.repo = namespace.domain.ebuild_repos + namespace.restrict = OrRestriction( + *commandline.convert_to_restrict(namespace.target)) def getter(pkg): @@ -37,6 +35,7 @@ def getter(pkg): getattr(pkg, "herds", None)) +@argparser.bind_main_func def main(options, out, err): for t, pkgs in itertools.groupby( options.repo.itermatch(options.restrict, sorter=sorted), getter): @@ -55,4 +54,4 @@ def main(options, out, err): out.write() if __name__ == '__main__': - commandline.main({None: (OptionParser, main)}) + commandline.main(argparser) diff --git a/examples/repo_list.py b/examples/repo_list.py index 7de7d632..8a33edd4 100755 --- a/examples/repo_list.py +++ b/examples/repo_list.py @@ -18,31 +18,23 @@ except ImportError: sys.exit(1) -class OptionParser(commandline.OptionParser): +argparser = commandline.mk_argparser(color=False, version=False) +argparser.add_argument( + '-r', '--repo', action=commandline.StoreRepoObject, + help='repo to give info about (default from domain if omitted)') - def __init__(self, **kwargs): - commandline.OptionParser.__init__( - self, description=__doc__, usage='%prog [options]', - **kwargs) - self.add_option("--repo", "-r", action='callback', type='string', - callback=commandline.config_callback, callback_args=('repo',), - help='repo to give info about (default from domain if omitted)') - def check_values(self, values, args): - values, args = commandline.OptionParser.check_values( - self, values, args) - - if args: self.error("This script takes no arguments") - - # Get repo(s) to operate on. - if values.repo: - repos = (values.repo,) - else: - repos = values.config.get_default('domain').repos - values.repos = get_virtual_repos(get_raw_repos(repos), False) +@argparser.bind_final_check +def check_args(parser, namespace): + # Get repo(s) to operate on. + if namespace.repo: + repos = (namespace.repo,) + else: + repos = namespace.domain.repos + namespace.repos = get_virtual_repos(get_raw_repos(repos), False) - return values, () +@argparser.bind_main_func def main(options, out, err): for repo in options.repos: out.write("Repo ID: %s" % repo.repo_id) @@ -55,5 +47,6 @@ def main(options, out, err): out.write("%d categories" % len(repo.packages)) out.write() + if __name__ == '__main__': - commandline.main({None: (OptionParser, main)}) + commandline.main(argparser) |