Commits

S. Joshua Swamidass committed c63ced5

pep8 compliant

  • Participants
  • Parent commits ad5236b

Comments (0)

Files changed (1)

 Specifying the Section
 ======================
 
-By default, all configurations go to the [defaults] section.  Argument 
+By default, all configurations go to the [defaults] section.  Argument
 groups and subparsers inherit from the parser that initialized them.
 
 The add_argument_group, add_argument, add_subparsers, and ConfArgParser
 Suggestions or BugFixes?
 ========================
 
-Feel free to contact me. I am findable online with a google search: S. Joshua Swamidass.
+Feel free to contact me. I am findable online with a google search:
+S. Joshua Swamidass.
 """
 
 import argparse
 import sys
 from types import MethodType
 
+
 def _repr(obj):
     class AtomRepr:
-        def __init__(self, f): 
+        def __init__(self, f):
             if type(f) == file:
-                if f.name == "<stdin>": self.s = "sys.stdin"
-                elif f.name == "<stdout>": self.s = "sys.stdout"
-                else: self.s = "file('%s','%s')" % (f.name, f.mode)
-            else: self.s = repr(f)
-        def __repr__(self): return self.s
+                if f.name == "<stdin>":
+                    self.s = "sys.stdin"
+                elif f.name == "<stdout>":
+                    self.s = "sys.stdout"
+                else:
+                    self.s = "file('%s','%s')" % (f.name, f.mode)
+            else:
+                self.s = repr(f)
+
+        def __repr__(self):
+            return self.s
 
     if type(obj) == file:
         return repr(AtomRepr(obj))
     return repr(obj)
 
 
-
 def _add_argument(self, *args, **kwargs):
         chars = self.prefix_chars
         if not args or len(args) == 1 and args[0][0] not in chars:
         exclude = my_kwargs.get("conf_exclude", False)
 
         if not positional:
-            if "section" in kwargs: del kwargs["section"]
-            if "name" in kwargs: del kwargs["name"]
-            if "conf_exclude" in kwargs: del kwargs["conf_exclude"]
+            if "section" in kwargs:
+                del kwargs["section"]
+            if "name" in kwargs:
+                del kwargs["name"]
+            if "conf_exclude" in kwargs:
+                del kwargs["conf_exclude"]
         else:
             exclude = True
 
 
         return action
 
+
 def _add_parser(self, *args, **kwargs):
     section = kwargs.get("section", None)
-    if "section" in kwargs: del kwargs["section"]
+    if "section" in kwargs:
+        del kwargs["section"]
     parser = _prior_add_parser(self, *args, **kwargs)
     parser.section = section
     return parser
 
+
 _prior_add_parser = argparse._SubParsersAction.add_parser
 argparse._SubParsersAction.add_parser = _add_parser
 
+
 class ConfArgParser(argparse.ArgumentParser):
     def __init__(self, *args, **kwargs):
         self.section = kwargs.get("section", "defaults")
-        if "section" in kwargs: del kwargs["section"]
+        if "section" in kwargs:
+            del kwargs["section"]
 
         ckwargs = dict(kwargs)
         ckwargs["add_help"] = False
-        self.conf_parser = argparse.ArgumentParser( *args, **ckwargs )
-        group = self.conf_parser.add_argument_group("configuration file options")
-        group.add_argument("-c", "--conf_file",  help="specify config files", nargs="+", metavar="FILE")
-        group.add_argument("--export_conf_file", help="translate arguments into a config file",
-                metavar="FILE", const=sys.stdout, default=None, nargs="?", type=argparse.FileType('w'))
+        self.conf_parser = argparse.ArgumentParser(*args, **ckwargs)
+        group = self.conf_parser.add_argument_group(
+                "configuration file options")
+        group.add_argument("-c", "--conf-file",
+                help="specify config files", nargs="+", metavar="FILE")
+        group.add_argument("--export-conf-file",
+                help="translate arguments into a config file",
+                metavar="FILE", const=sys.stdout, default=None,
+                nargs="?", type=argparse.FileType('w'))
 
         self._added_config_args = False
         kwargs["formatter_class"] = argparse.RawDescriptionHelpFormatter
         self.config_mapping = {}
         self.dest_mapping = {}
 
-        argparse.ArgumentParser.__init__(self,*args, **kwargs)
+        argparse.ArgumentParser.__init__(self, *args, **kwargs)
 
     add_argument = _add_argument
 
     def add_argument_group(self, *args, **kwargs):
         section = kwargs.get("section", "defaults")
-        if "section" in kwargs: del kwargs["section"]
-        group = argparse.ArgumentParser.add_argument_group(self, *args, **kwargs)
+        if "section" in kwargs:
+            del kwargs["section"]
+        group = argparse.ArgumentParser.add_argument_group(self,
+                *args, **kwargs)
         group.add_argument = MethodType(_add_argument, group, group.__class__)
         group.section = section
         return group
 
     def add_mutually_exclusive_groupi(self, *args, **kwargs):
         section = kwargs.get("section", "defaults")
-        if "section" in kwargs: del kwargs["section"]
-        group = argparse.ArgumentParser.add_mutually_exclusive_group(self, *args, **kwargs)
+        if "section" in kwargs:
+            del kwargs["section"]
+        group = argparse.ArgumentParser.add_mutually_exclusive_group(self,
+                *args, **kwargs)
         group.add_argument = MethodType(_add_argument, group, group.__class__)
         group.section = section
         return group
         actions = self._actions
 
         for a in actions:
-            try: assert( not a.exclude )
-            except: continue
+            try:
+                assert(not a.exclude)
+            except:
+                continue
 
             name = a.name.lower()
 
             section = a.section
-            if section == None: section = a.parent.section
-            if section == None: section = self.section
-            if section == None: section = "defaults"
+            if section == None:
+                section = a.parent.section
+            if section == None:
+                section = self.section
+            if section == None:
+                section = "defaults"
             section = section.lower()
             dest = a.dest
 
             if warn and (section, name) in self.dest_mapping:
-                d,t = self.dest_mapping[(section, name)]
-                if d!= dest:
-                    raise ValueError("Changing destination of this config address!")
+                d, t = self.dest_mapping[(section, name)]
+                if d != dest:
+                    raise ValueError(
+                            "Changing destination of this config address!")
             if warn and dest in self.config_mapping:
-                s,n,t = self.config_mapping[dest]
+                s, n, t = self.config_mapping[dest]
                 if s != section or n != name:
-                    raise ValueError("Changing config address of this destination!")
+                    raise ValueError(
+                            "Changing config address of this destination!")
 
             self.config_mapping[dest] = (section, name)
             self.dest_mapping[(section, name)] = (dest)
+
     def config2dest(self, config):
         args = {}
         for dest, (section, name) in self.config_mapping.items():
-            try: val = eval(config.get(section, name))
-            except: continue
+            try:
+                val = eval(config.get(section, name))
+            except:
+                continue
             args[dest] = val
         return args
 
         for dest, (section, name) in self.config_mapping.items():
             try:
                 a = args.__getattribute__(dest)
-            except: continue
+            except:
+                continue
 
             if section not in sections and section.lower() != "defaults":
                 config.add_section(section)
                 defaults_added = True
                 config.add_section("defaults")
 
-            if a == None: config.set(section, name, "")
-            else: config.set(section, name, _repr(a))
+            if a == None:
+                config.set(section, name, "")
+            else:
+                config.set(section, name, _repr(a))
 
         return config
 
     def _add_config_args(self):
         if not self._added_config_args:
-           self._added_config_args=True
-           if self._subparsers == None: self._add_container_actions(self.conf_parser)
+            self._added_config_args = True
+            if self._subparsers == None:
+                self._add_container_actions(self.conf_parser)
 
     def _parse_config(self, args):
         args, remaining_argv = self.conf_parser.parse_known_args(args)
 
     def parse_known_args(self, args=sys.argv[1:], namespace=None):
         self._add_config_args()
-        if not self._subparsers: args = self._parse_config(args)
+        if not self._subparsers:
+            args = self._parse_config(args)
         return argparse.ArgumentParser.parse_known_args(self, args, namespace)
 
     def parse_args(self, args=sys.argv[1:], namespace=None):
         self._add_config_args()
-        if not self._subparsers: args = self._parse_config(args)
+        if not self._subparsers:
+            args = self._parse_config(args)
         return argparse.ArgumentParser.parse_args(self, args, namespace)
-
-
-