From f488da5cc84c00ea09c0188ee25f7ec4579d3ef3 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 7 Oct 2010 13:26:07 +0200 Subject: [PATCH] merge parseopt into config module --HG-- branch : trunk --- py/_test/config.py | 98 +++++++++++++++++++++++++++++++++- py/_test/parseopt.py | 112 --------------------------------------- testing/test_parseopt.py | 2 +- 3 files changed, 97 insertions(+), 115 deletions(-) delete mode 100644 py/_test/parseopt.py diff --git a/py/_test/config.py b/py/_test/config.py index 32bda8634..c7f2e6a66 100644 --- a/py/_test/config.py +++ b/py/_test/config.py @@ -1,7 +1,101 @@ import py, os from py._test.pluginmanager import PluginManager -from py._test import parseopt +import optparse +class Parser: + """ Parser for command line arguments. """ + + def __init__(self, usage=None, processopt=None): + self._anonymous = OptionGroup("custom options", parser=self) + self._groups = [] + self._processopt = processopt + self._usage = usage + self.hints = [] + + def processoption(self, option): + if self._processopt: + if option.dest: + self._processopt(option) + + def addnote(self, note): + self._notes.append(note) + + def getgroup(self, name, description="", after=None): + for group in self._groups: + if group.name == name: + return group + group = OptionGroup(name, description, parser=self) + i = 0 + for i, grp in enumerate(self._groups): + if grp.name == after: + break + self._groups.insert(i+1, group) + return group + + addgroup = getgroup + def addgroup(self, name, description=""): + py.log._apiwarn("1.1", "use getgroup() which gets-or-creates") + return self.getgroup(name, description) + + def addoption(self, *opts, **attrs): + """ add an optparse-style option. """ + self._anonymous.addoption(*opts, **attrs) + + def parse(self, args): + optparser = MyOptionParser(self) + groups = self._groups + [self._anonymous] + for group in groups: + if group.options: + desc = group.description or group.name + optgroup = optparse.OptionGroup(optparser, desc) + optgroup.add_options(group.options) + optparser.add_option_group(optgroup) + return optparser.parse_args([str(x) for x in args]) + + def parse_setoption(self, args, option): + parsedoption, args = self.parse(args) + for name, value in parsedoption.__dict__.items(): + setattr(option, name, value) + return args + + +class OptionGroup: + def __init__(self, name, description="", parser=None): + self.name = name + self.description = description + self.options = [] + self.parser = parser + + def addoption(self, *optnames, **attrs): + """ add an option to this group. """ + option = optparse.Option(*optnames, **attrs) + self._addoption_instance(option, shortupper=False) + + def _addoption(self, *optnames, **attrs): + option = optparse.Option(*optnames, **attrs) + self._addoption_instance(option, shortupper=True) + + def _addoption_instance(self, option, shortupper=False): + if not shortupper: + for opt in option._short_opts: + if opt[0] == '-' and opt[1].islower(): + raise ValueError("lowercase shortoptions reserved") + if self.parser: + self.parser.processoption(option) + self.options.append(option) + + +class MyOptionParser(optparse.OptionParser): + def __init__(self, parser): + self._parser = parser + optparse.OptionParser.__init__(self, usage=parser._usage) + def format_epilog(self, formatter): + hints = self._parser.hints + if hints: + s = "\n".join(["hint: " + x for x in hints]) + "\n" + s = "\n" + s + "\n" + return s + return "" class Conftest(object): """ the single place for accessing values and interacting towards conftest modules from py.test objects. @@ -150,7 +244,7 @@ class Config(object): def __init__(self): self.option = CmdOptions() - self._parser = parseopt.Parser( + self._parser = Parser( usage="usage: %prog [options] [file_or_dir] [file_or_dir] [...]", processopt=self._processopt, ) diff --git a/py/_test/parseopt.py b/py/_test/parseopt.py deleted file mode 100644 index 36af25628..000000000 --- a/py/_test/parseopt.py +++ /dev/null @@ -1,112 +0,0 @@ -""" -thin wrapper around Python's optparse.py -adding some extra checks and ways to systematically -have Environment variables provide default values -for options. basic usage: - - >>> parser = Parser() - >>> parser.addoption("--hello", action="store_true", dest="hello") - >>> option, args = parser.parse(['--hello']) - >>> option.hello - True - >>> args - [] - -""" -import py -import optparse - -class Parser: - """ Parser for command line arguments. """ - - def __init__(self, usage=None, processopt=None): - self._anonymous = OptionGroup("custom options", parser=self) - self._groups = [] - self._processopt = processopt - self._usage = usage - self.hints = [] - - def processoption(self, option): - if self._processopt: - if option.dest: - self._processopt(option) - - def addnote(self, note): - self._notes.append(note) - - def getgroup(self, name, description="", after=None): - for group in self._groups: - if group.name == name: - return group - group = OptionGroup(name, description, parser=self) - i = 0 - for i, grp in enumerate(self._groups): - if grp.name == after: - break - self._groups.insert(i+1, group) - return group - - addgroup = getgroup - def addgroup(self, name, description=""): - py.log._apiwarn("1.1", "use getgroup() which gets-or-creates") - return self.getgroup(name, description) - - def addoption(self, *opts, **attrs): - """ add an optparse-style option. """ - self._anonymous.addoption(*opts, **attrs) - - def parse(self, args): - optparser = MyOptionParser(self) - groups = self._groups + [self._anonymous] - for group in groups: - if group.options: - desc = group.description or group.name - optgroup = optparse.OptionGroup(optparser, desc) - optgroup.add_options(group.options) - optparser.add_option_group(optgroup) - return optparser.parse_args([str(x) for x in args]) - - def parse_setoption(self, args, option): - parsedoption, args = self.parse(args) - for name, value in parsedoption.__dict__.items(): - setattr(option, name, value) - return args - - -class OptionGroup: - def __init__(self, name, description="", parser=None): - self.name = name - self.description = description - self.options = [] - self.parser = parser - - def addoption(self, *optnames, **attrs): - """ add an option to this group. """ - option = optparse.Option(*optnames, **attrs) - self._addoption_instance(option, shortupper=False) - - def _addoption(self, *optnames, **attrs): - option = optparse.Option(*optnames, **attrs) - self._addoption_instance(option, shortupper=True) - - def _addoption_instance(self, option, shortupper=False): - if not shortupper: - for opt in option._short_opts: - if opt[0] == '-' and opt[1].islower(): - raise ValueError("lowercase shortoptions reserved") - if self.parser: - self.parser.processoption(option) - self.options.append(option) - - -class MyOptionParser(optparse.OptionParser): - def __init__(self, parser): - self._parser = parser - optparse.OptionParser.__init__(self, usage=parser._usage) - def format_epilog(self, formatter): - hints = self._parser.hints - if hints: - s = "\n".join(["hint: " + x for x in hints]) + "\n" - s = "\n" + s + "\n" - return s - return "" diff --git a/testing/test_parseopt.py b/testing/test_parseopt.py index 3b3bb2cba..31a67497f 100644 --- a/testing/test_parseopt.py +++ b/testing/test_parseopt.py @@ -1,5 +1,5 @@ import py -from py._test import parseopt +from py._test import config as parseopt class TestParser: def test_init(self, capsys):