ConfArgParse-1.1.20/0000755000076600000240000000000012503205151015370 5ustar jswamidassstaff00000000000000ConfArgParse-1.1.20/.hgtags0000644000076600000240000000020712257212642016656 0ustar jswamidassstaff00000000000000a6b1f33ce0c2490dd9376cfee4bdbab5cf71d959 1.1 a6b1f33ce0c2490dd9376cfee4bdbab5cf71d959 1.1 ba9b31e17cdb67d56f468060ce4a69144f46ce07 1.1 ConfArgParse-1.1.20/ConfArgParse.egg-info/0000755000076600000240000000000012503205151021374 5ustar jswamidassstaff00000000000000ConfArgParse-1.1.20/ConfArgParse.egg-info/dependency_links.txt0000644000076600000240000000000112503205150025441 0ustar jswamidassstaff00000000000000 ConfArgParse-1.1.20/ConfArgParse.egg-info/PKG-INFO0000644000076600000240000001100512503205150022465 0ustar jswamidassstaff00000000000000Metadata-Version: 1.1 Name: ConfArgParse Version: 1.1.20 Summary: An integrated argument/configuration file parser that follows the syntax of argparser Home-page: https://bitbucket.org/swamidass/confargparse/ Author: S. Joshua Swamidass Author-email: swamidass@gmail.com License: UNKNOWN Description: ============ ConfArgParse ============ This module adds a config file parsing capability to argparse. Usage ----- Start by importing the module and initializing the parser:: import confargparse parser = confargparse.ConfArgParser() The usage is identical to the argparse module:: parser.add_argument("-n", type=int) group = parser.add_argument_group("my group") group.add_argument("-g") Now, to use a configuration file (or list of sequentially read configuration files), just add the --conf-file option. python prog.py --conf-file conf.ini It is easy to write out a configuration file by applying all the options you want, and then adding the --export-conf-file option. python prog.py -n --export-conf-file > conf.ini API Changes ----------- All argparse code should be compatible by just drapping in the new object. This package adds a few important options to the API to figure out how to map namespace dests to configuration sections/names. The key concepts to note: 1. Parameters in configuration files map to specific section/name pairs. 2. Configuration file sections and names ignore case. Specifiying the Name ==================== By default all configuration names are the lowercase dest from argparse. Care must be taken to make sure that there are no name clashes from dests with different capitalizations. The default name can be changed by using the "name" keyword to add_argument:: parser.add_argument("-n", type=int, name="my_n") This targets the argument to "my_n" instead of "n" in the configuration file. Specifying the Section ====================== 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 initialization all include the "section" optional keyword argument. Specifying this section sets the section in the configuration the option will be targeted to. If the value is None, the object will inherit up as expected:: parser = ConfArgParser(section = "main") parser.add_argument("-n", type=int) group = parser.add_argument_group("my group", section="group") group.add_argument("-g") group.add_argument("-t", section="section2") In this example, the first argument targets to "n" name in the [main] section. The second argument targets to the "g" name in the [group] section. The third argument targets to the "t" name in the [section2] section. Excluding Arguments =================== Currently, positional arguments cannot be sent to the configuration file. If you would like to exclude additional arguments, just use the exclude keyword argument to add_arguments:: parser.add_argument("-n", type=int, exclude=True) Suggestions or BugFixes? ======================== Feel free to contact me. I am findable online with a google search: S. Joshua Swamidass. Please send bug fixes as pull requests to the bitbucket repository (`https://bitbucket.org/swamidass/confargparse `_). Please keep pull requests clean, so I can easily figure out if it should be merged into the main line. Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: License :: OSI Approved :: MIT License Classifier: License :: Free for non-commercial use Classifier: Natural Language :: English Classifier: Programming Language :: Python Classifier: Intended Audience :: Developers ConfArgParse-1.1.20/ConfArgParse.egg-info/requires.txt0000644000076600000240000000001012503205150023762 0ustar jswamidassstaff00000000000000hgtools ConfArgParse-1.1.20/ConfArgParse.egg-info/SOURCES.txt0000644000076600000240000000037212503205151023262 0ustar jswamidassstaff00000000000000.hgtags LICENSE MANIFEST.in README.txt VERSION confargparse.py setup.py ConfArgParse.egg-info/PKG-INFO ConfArgParse.egg-info/SOURCES.txt ConfArgParse.egg-info/dependency_links.txt ConfArgParse.egg-info/requires.txt ConfArgParse.egg-info/top_level.txtConfArgParse-1.1.20/ConfArgParse.egg-info/top_level.txt0000644000076600000240000000001512503205150024121 0ustar jswamidassstaff00000000000000confargparse ConfArgParse-1.1.20/confargparse.py0000644000076600000240000002605712257212257020441 0ustar jswamidassstaff00000000000000""" ============ ConfArgParse ============ This module adds a config file parsing capability to argparse. Usage ----- Start by importing the module and initializing the parser:: import confargparse parser = confargparse.ConfArgParser() The usage is identical to the argparse module:: parser.add_argument("-n", type=int) group = parser.add_argument_group("my group") group.add_argument("-g") Now, to use a configuration file (or list of sequentially read configuration files), just add the --conf-file option. python prog.py --conf-file conf.ini It is easy to write out a configuration file by applying all the options you want, and then adding the --export-conf-file option. python prog.py -n --export-conf-file > conf.ini API Changes ----------- All argparse code should be compatible by just drapping in the new object. This package adds a few important options to the API to figure out how to map namespace dests to configuration sections/names. The key concepts to note: 1. Parameters in configuration files map to specific section/name pairs. 2. Configuration file sections and names ignore case. Specifiying the Name ==================== By default all configuration names are the lowercase dest from argparse. Care must be taken to make sure that there are no name clashes from dests with different capitalizations. The default name can be changed by using the "name" keyword to add_argument:: parser.add_argument("-n", type=int, name="my_n") This targets the argument to "my_n" instead of "n" in the configuration file. Specifying the Section ====================== 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 initialization all include the "section" optional keyword argument. Specifying this section sets the section in the configuration the option will be targeted to. If the value is None, the object will inherit up as expected:: parser = ConfArgParser(section = "main") parser.add_argument("-n", type=int) group = parser.add_argument_group("my group", section="group") group.add_argument("-g") group.add_argument("-t", section="section2") In this example, the first argument targets to "n" name in the [main] section. The second argument targets to the "g" name in the [group] section. The third argument targets to the "t" name in the [section2] section. Excluding Arguments =================== Currently, positional arguments cannot be sent to the configuration file. If you would like to exclude additional arguments, just use the exclude keyword argument to add_arguments:: parser.add_argument("-n", type=int, exclude=True) Suggestions or BugFixes? ======================== Feel free to contact me. I am findable online with a google search: S. Joshua Swamidass. Please send bug fixes as pull requests to the bitbucket repository (`https://bitbucket.org/swamidass/confargparse `_). Please keep pull requests clean, so I can easily figure out if it should be merged into the main line. """ import argparse import sys from types import MethodType try: import ConfigParser except ImportError: import configparser def _repr(obj): class AtomRepr: def __init__(self, f): if type(f) == file: if f.name == "": self.s = "stdin" elif f.name == "": self.s = "stdout" else: if f.mode == "r": self.s = "file('%s')" % f.name 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)) if type(obj) == list: return repr([AtomRepr(o) for o in 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: my_kwargs = self._get_positional_kwargs(*args, **kwargs) positional = True else: my_kwargs = self._get_optional_kwargs(*args, **kwargs) positional = False dest = my_kwargs["dest"] section = my_kwargs.get("section", None) name = my_kwargs.get("name", dest) 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"] else: exclude = True action = argparse._ActionsContainer.add_argument(self, *args, **kwargs) action.section = section action.name = name action.parent = self action.exclude = exclude return action def _add_parser(self, *args, **kwargs): section = kwargs.get("section", None) 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"] 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._added_config_args = False kwargs["formatter_class"] = argparse.RawDescriptionHelpFormatter self.config_mapping = {} self.dest_mapping = {} 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) group.add_argument = MethodType(_add_argument, group, group.__class__) group.section = section return group def add_subparsers(self, *args, **kwargs): parser = argparse.ArgumentParser.add_subparsers(self, *args, **kwargs) return parser 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) group.add_argument = MethodType(_add_argument, group, group.__class__) group.section = section return group def add_mappings(self, warn=True): self.config_mapping = {} self.dest_mapping = {} actions = self._actions for a in actions: 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" 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!") if warn and dest in self.config_mapping: s, n, t = self.config_mapping[dest] if s != section or n != name: 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), {'__builtins__':__builtins__, "stdin": sys.stdin, "stdout": sys.stdout}, {}) except: continue args[dest] = val return args def args2config(self, args=sys.argv[1:]): args = argparse.ArgumentParser.parse_args(self, args) config = ConfigParser.RawConfigParser() sections = set() defaults_added = False for dest, (section, name) in self.config_mapping.items(): try: a = args.__getattribute__(dest) except: continue if section not in sections and section.lower() != "defaults": config.add_section(section) sections.add(section) if section.lower() == "defaults" and not defaults_added: defaults_added = True config.add_section("defaults") 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) def _parse_config(self, args): args, remaining_argv = self.conf_parser.parse_known_args(args) self.add_mappings() if args.conf_file: config = ConfigParser.SafeConfigParser() config.read(args.conf_file) config_options = self.config2dest(config) self.set_defaults(**config_options) if args.export_conf_file: self.args2config(remaining_argv).write(args.export_conf_file) sys.exit(0) return remaining_argv def parse_known_args(self, args=sys.argv[1:], namespace=None): self._add_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) return argparse.ArgumentParser.parse_args(self, args, namespace) ConfArgParse-1.1.20/LICENSE0000644000076600000240000000205412503204333016377 0ustar jswamidassstaff00000000000000Copyright (C) 2012-2013 S. Joshua Swamidass Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ConfArgParse-1.1.20/MANIFEST.in0000644000076600000240000000006312503204333017126 0ustar jswamidassstaff00000000000000include README.txt include VERSION include LICENSE ConfArgParse-1.1.20/PKG-INFO0000644000076600000240000001100512503205151016462 0ustar jswamidassstaff00000000000000Metadata-Version: 1.1 Name: ConfArgParse Version: 1.1.20 Summary: An integrated argument/configuration file parser that follows the syntax of argparser Home-page: https://bitbucket.org/swamidass/confargparse/ Author: S. Joshua Swamidass Author-email: swamidass@gmail.com License: UNKNOWN Description: ============ ConfArgParse ============ This module adds a config file parsing capability to argparse. Usage ----- Start by importing the module and initializing the parser:: import confargparse parser = confargparse.ConfArgParser() The usage is identical to the argparse module:: parser.add_argument("-n", type=int) group = parser.add_argument_group("my group") group.add_argument("-g") Now, to use a configuration file (or list of sequentially read configuration files), just add the --conf-file option. python prog.py --conf-file conf.ini It is easy to write out a configuration file by applying all the options you want, and then adding the --export-conf-file option. python prog.py -n --export-conf-file > conf.ini API Changes ----------- All argparse code should be compatible by just drapping in the new object. This package adds a few important options to the API to figure out how to map namespace dests to configuration sections/names. The key concepts to note: 1. Parameters in configuration files map to specific section/name pairs. 2. Configuration file sections and names ignore case. Specifiying the Name ==================== By default all configuration names are the lowercase dest from argparse. Care must be taken to make sure that there are no name clashes from dests with different capitalizations. The default name can be changed by using the "name" keyword to add_argument:: parser.add_argument("-n", type=int, name="my_n") This targets the argument to "my_n" instead of "n" in the configuration file. Specifying the Section ====================== 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 initialization all include the "section" optional keyword argument. Specifying this section sets the section in the configuration the option will be targeted to. If the value is None, the object will inherit up as expected:: parser = ConfArgParser(section = "main") parser.add_argument("-n", type=int) group = parser.add_argument_group("my group", section="group") group.add_argument("-g") group.add_argument("-t", section="section2") In this example, the first argument targets to "n" name in the [main] section. The second argument targets to the "g" name in the [group] section. The third argument targets to the "t" name in the [section2] section. Excluding Arguments =================== Currently, positional arguments cannot be sent to the configuration file. If you would like to exclude additional arguments, just use the exclude keyword argument to add_arguments:: parser.add_argument("-n", type=int, exclude=True) Suggestions or BugFixes? ======================== Feel free to contact me. I am findable online with a google search: S. Joshua Swamidass. Please send bug fixes as pull requests to the bitbucket repository (`https://bitbucket.org/swamidass/confargparse `_). Please keep pull requests clean, so I can easily figure out if it should be merged into the main line. Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: License :: OSI Approved :: MIT License Classifier: License :: Free for non-commercial use Classifier: Natural Language :: English Classifier: Programming Language :: Python Classifier: Intended Audience :: Developers ConfArgParse-1.1.20/README.txt0000644000076600000240000000620612503205150017071 0ustar jswamidassstaff00000000000000 ============ ConfArgParse ============ This module adds a config file parsing capability to argparse. Usage ----- Start by importing the module and initializing the parser:: import confargparse parser = confargparse.ConfArgParser() The usage is identical to the argparse module:: parser.add_argument("-n", type=int) group = parser.add_argument_group("my group") group.add_argument("-g") Now, to use a configuration file (or list of sequentially read configuration files), just add the --conf-file option. python prog.py --conf-file conf.ini It is easy to write out a configuration file by applying all the options you want, and then adding the --export-conf-file option. python prog.py -n --export-conf-file > conf.ini API Changes ----------- All argparse code should be compatible by just drapping in the new object. This package adds a few important options to the API to figure out how to map namespace dests to configuration sections/names. The key concepts to note: 1. Parameters in configuration files map to specific section/name pairs. 2. Configuration file sections and names ignore case. Specifiying the Name ==================== By default all configuration names are the lowercase dest from argparse. Care must be taken to make sure that there are no name clashes from dests with different capitalizations. The default name can be changed by using the "name" keyword to add_argument:: parser.add_argument("-n", type=int, name="my_n") This targets the argument to "my_n" instead of "n" in the configuration file. Specifying the Section ====================== 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 initialization all include the "section" optional keyword argument. Specifying this section sets the section in the configuration the option will be targeted to. If the value is None, the object will inherit up as expected:: parser = ConfArgParser(section = "main") parser.add_argument("-n", type=int) group = parser.add_argument_group("my group", section="group") group.add_argument("-g") group.add_argument("-t", section="section2") In this example, the first argument targets to "n" name in the [main] section. The second argument targets to the "g" name in the [group] section. The third argument targets to the "t" name in the [section2] section. Excluding Arguments =================== Currently, positional arguments cannot be sent to the configuration file. If you would like to exclude additional arguments, just use the exclude keyword argument to add_arguments:: parser.add_argument("-n", type=int, exclude=True) Suggestions or BugFixes? ======================== Feel free to contact me. I am findable online with a google search: S. Joshua Swamidass. Please send bug fixes as pull requests to the bitbucket repository (`https://bitbucket.org/swamidass/confargparse `_). Please keep pull requests clean, so I can easily figure out if it should be merged into the main line. ConfArgParse-1.1.20/setup.cfg0000644000076600000240000000007312503205151017211 0ustar jswamidassstaff00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 ConfArgParse-1.1.20/setup.py0000644000076600000240000000156712503205103017110 0ustar jswamidassstaff00000000000000from __future__ import print_function from setuptools import setup try: import confargparse docs = confargparse.__doc__ print (docs, file=open("README.txt", "w")) except: docs = file("README.txt").read() setup(name="ConfArgParse", version="1.1.20", install_requires=["hgtools"], description="An integrated argument/configuration file parser that follows the syntax of argparser", long_description=docs, author="S. Joshua Swamidass", url="https://bitbucket.org/swamidass/confargparse/", author_email="swamidass@gmail.com", classifiers=["Development Status :: 4 - Beta", "License :: OSI Approved :: MIT License", "License :: Free for non-commercial use", "Natural Language :: English", "Programming Language :: Python", "Intended Audience :: Developers", ], py_modules=['confargparse'] ) ConfArgParse-1.1.20/VERSION0000644000076600000240000000000312503204557016442 0ustar jswamidassstaff0000000000000019