Mercurial > hgrepos > Python > libs > ConfigMix
changeset 5:dc058099a4cb
Renamed the project from "MixConfig" to "ConfigMix"
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Tue, 08 Mar 2016 20:11:17 +0100 |
| parents | f76d85ccc5b9 |
| children | caaaddb11db1 |
| files | README configmix/__init__.py configmix/compat.py configmix/ini.py configmix/yaml.py mixconfig/__init__.py mixconfig/compat.py mixconfig/ini.py mixconfig/yaml.py setup.py |
| diffstat | 10 files changed, 293 insertions(+), 293 deletions(-) [+] |
line wrap: on
line diff
--- a/README Tue Mar 08 16:46:27 2016 +0100 +++ b/README Tue Mar 08 20:11:17 2016 +0100 @@ -1,14 +1,14 @@ .. -*- coding: utf-8; mode: rst; -*- =========== - MixConfig + ConfigMix =========== :Author: Franz Glasner -:Path: $HG(sw/libs/py/MixConfig.hg)$ +:Path: $HG(sw/libs/py/ConfigMix.hg)$ -"MixConfig" is a library for helping with configuration files. +"ConfigMix" is a library for helping with configuration files. Links
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/configmix/__init__.py Tue Mar 08 20:11:17 2016 +0100 @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +r""" + +:Author: Franz Glasner +:License: BSD License. + See LICENSE for details. + +""" +# +# NOTE: ONLY STANDARDLIB-IMPORTS IN THIS MODULE. +# ITS JUST FOR BOOTSTRAPPING WITH PYTHON2 and PYTHON3. +# NO `future`, `six`, ... +# + +from __future__ import division, print_function, absolute_import + + +__version__ = "0.0.dev0" + + +__all__ = []
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/configmix/compat.py Tue Mar 08 20:11:17 2016 +0100 @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +r"""Some minimal compatibility between Python2 and Python3 + +""" + +import sys + + +__all__ = [] + + +PY2 = sys.version_info[0] <= 2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/configmix/ini.py Tue Mar 08 20:11:17 2016 +0100 @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- + +from __future__ import division, absolute_import, print_function + +import sys +import os +import io +import locale +try: + from configparser import SafeConfigParser, NoSectionError, NoOptionError +except ImportError: + from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError + +from .compat import PY2 + + +__all__ = ["INIConfigParser", "NoSectionError", "NoOptionError"] + + +class INIConfigParser(SafeConfigParser): + + """A case sensitive config parser that returns all-unicode string + values. + + """ + + def __init__(self, filename, executable=None, encoding=None): + SafeConfigParser.__init__(self) + if executable is None: + executable = sys.argv[0] + if PY2: + if isinstance(filename, str): + filename = filename.decode(locale.getpreferredencoding()) + if isinstance(executable, str): + executable = executable.decode(locale.getpreferredencoding()) + self.executable = os.path.normpath(os.path.abspath(executable)) + if encoding is None: + encoding = locale.getpreferredencoding() + self.encoding = encoding + with io.open(filename, + mode="rt", + encoding=self.encoding) as cf: + self.readfp(cf, filename) + + def optionxform(self, option): + return option + + def get_path_list(self, section, option): + v = self.get(section, option) + return v.split(os.pathsep) + + def read(self, filenames): + raise NotImplementedError("use `readfp()' instead") + + def readfp(self, fp, filename): + if hasattr(self, "filename"): + raise RuntimeError("already initialized") + filename = os.path.normpath(os.path.abspath(filename)) + if PY2: + if isinstance(filename, str): + filename = filename.decode(locale.getpreferredencoding()) + self.set(None, "self", filename) + self.set(None, "here", os.path.dirname(filename)) + self.set(None, "root", os.path.dirname(self.executable)) + SafeConfigParser.readfp(self, fp, filename=filename) + self.filename = filename + self.root = os.path.dirname(self.executable) + + def getx(self, section, option): + """Extended get() with some automatic type conversion support. + + Default: Fetch as string (like `get()`). + + If annotated with ``:bool:`` fetch as bool, if annotated with + ``:int:`` fetch as int, if annotated with ``:float:`` fetch as + float. + + """ + v = self.get(section, option) + if v.startswith(":bool:"): + v = v[6:].lower() + if v not in self._BOOL_CVT: + raise ValueError("Not a boolean: %s" % (v, )) + return self._BOOL_CVT[v] + elif v.startswith(":int:"): + return int(v[5:], 0) + elif v.startswith(":float:"): + return float(v[7:]) + else: + return v + + _BOOL_CVT = {'1': True, 'yes': True, 'true': True, 'on': True, + '0': False, 'no': False, 'false': False, 'off': False}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/configmix/yaml.py Tue Mar 08 20:11:17 2016 +0100 @@ -0,0 +1,160 @@ +# -*- coding: utf-8 -*- +r"""Simple wrapper for yaml to support all-unicode strings when loading +configuration files. + +""" + +from __future__ import division, print_function, absolute_import + +try: + from collections import OrderedDict +except ImportError: + try: + from ordereddict import OrderedDict + except ImportError: + OrderedDict = None +import yaml +import yaml.constructor + + +__all__ = ["safe_load", "safe_load_all", "load", "load_all"] + + +class ConfigLoader(yaml.Loader): + + """A YAML loader, which makes all !!str strings to Unicode. Standard + PyYAML does this only in the non-ASCII case. + + If an `OrderedDict` implementation is available then all "map" and + "omap" nodes are constructed as `OrderedDict`. + This is against YAML specs but within configuration files it seems + more natural. + + """ + + def construct_yaml_str(self, node): + return self.construct_scalar(node) + + if OrderedDict: + + # + # From https://pypi.python.org/pypi/yamlordereddictloader/0.1.1 + # (MIT License) + # + + def construct_yaml_map(self, node): + data = OrderedDict() + yield data + value = self.construct_mapping(node) + data.update(value) + + def construct_mapping(self, node, deep=False): + if isinstance(node, yaml.MappingNode): + self.flatten_mapping(node) + else: + raise yaml.constructor.ConstructorError(None, None, + 'expected a mapping node, but found %s' % node.id, + node.start_mark) + + mapping = OrderedDict() + for key_node, value_node in node.value: + key = self.construct_object(key_node, deep=deep) + try: + hash(key) + except TypeError as err: + raise yaml.constructor.ConstructorError( + 'while constructing a mapping', node.start_mark, + 'found unacceptable key (%s)' % err, key_node.start_mark) + value = self.construct_object(value_node, deep=deep) + mapping[key] = value + return mapping + + +ConfigLoader.add_constructor( + "tag:yaml.org,2002:str", + ConfigLoader.construct_yaml_str) +if OrderedDict: + ConfigLoader.add_constructor( + "tag:yaml.org,2002:map", + ConfigLoader.construct_yaml_map) + ConfigLoader.add_constructor( + "tag:yaml.org,2002:omap", + ConfigLoader.construct_yaml_map) + + +class ConfigSafeLoader(yaml.SafeLoader): + + """A safe YAML loader, which makes all !!str strings to Unicode. + Standard PyYAML does this only in the non-ASCII case. + + If an `OrderedDict` implementation is available then all "map" and + "omap" nodes are constructed as `OrderedDict`. + This is against YAML specs but within configuration files it seems + more natural. + + """ + + def construct_yaml_str(self, node): + return self.construct_scalar(node) + + if OrderedDict: + + # + # From https://pypi.python.org/pypi/yamlordereddictloader/0.1.1 + # (MIT License) + # + + def construct_yaml_map(self, node): + data = OrderedDict() + yield data + value = self.construct_mapping(node) + data.update(value) + + def construct_mapping(self, node, deep=False): + if isinstance(node, yaml.MappingNode): + self.flatten_mapping(node) + else: + raise yaml.constructor.ConstructorError(None, None, + 'expected a mapping node, but found %s' % node.id, + node.start_mark) + + mapping = OrderedDict() + for key_node, value_node in node.value: + key = self.construct_object(key_node, deep=deep) + try: + hash(key) + except TypeError as err: + raise yaml.constructor.ConstructorError( + 'while constructing a mapping', node.start_mark, + 'found unacceptable key (%s)' % err, key_node.start_mark) + value = self.construct_object(value_node, deep=deep) + mapping[key] = value + return mapping + + +ConfigSafeLoader.add_constructor( + "tag:yaml.org,2002:str", + ConfigSafeLoader.construct_yaml_str) +if OrderedDict: + ConfigSafeLoader.add_constructor( + "tag:yaml.org,2002:map", + ConfigSafeLoader.construct_yaml_map) + ConfigSafeLoader.add_constructor( + "tag:yaml.org,2002:omap", + ConfigSafeLoader.construct_yaml_map) + + +def load(stream, Loader=ConfigLoader): + return yaml.load(stream, Loader) + + +def load_all(stream, Loader=ConfigLoader): + return yaml.load_all(stream, Loader) + + +def safe_load(stream): + return yaml.load(stream, Loader=ConfigSafeLoader) + + +def safe_load_all(stream): + return yaml.load_all(stream, Loader=ConfigSafeLoader)
--- a/mixconfig/__init__.py Tue Mar 08 16:46:27 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -r""" - -:Author: Franz Glasner -:License: BSD License. - See LICENSE for details. - -""" -# -# NOTE: ONLY STANDARDLIB-IMPORTS IN THIS MODULE. -# ITS JUST FOR BOOTSTRAPPING WITH PYTHON2 and PYTHON3. -# NO `future`, `six`, ... -# - -from __future__ import division, print_function, absolute_import - - -__version__ = "0.0.dev0" - - -__all__ = []
--- a/mixconfig/compat.py Tue Mar 08 16:46:27 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -# -*- coding: utf-8 -*- -r"""Some minimal compatibility between Python2 and Python3 - -""" - -import sys - - -__all__ = [] - - -PY2 = sys.version_info[0] <= 2
--- a/mixconfig/ini.py Tue Mar 08 16:46:27 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -# -*- coding: utf-8 -*- - -from __future__ import division, absolute_import, print_function - -import sys -import os -import io -import locale -try: - from configparser import SafeConfigParser, NoSectionError, NoOptionError -except ImportError: - from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError - -from .compat import PY2 - - -__all__ = ["INIConfigParser", "NoSectionError", "NoOptionError"] - - -class INIConfigParser(SafeConfigParser): - - """A case sensitive config parser that returns all-unicode string - values. - - """ - - def __init__(self, filename, executable=None, encoding=None): - SafeConfigParser.__init__(self) - if executable is None: - executable = sys.argv[0] - if PY2: - if isinstance(filename, str): - filename = filename.decode(locale.getpreferredencoding()) - if isinstance(executable, str): - executable = executable.decode(locale.getpreferredencoding()) - self.executable = os.path.normpath(os.path.abspath(executable)) - if encoding is None: - encoding = locale.getpreferredencoding() - self.encoding = encoding - with io.open(filename, - mode="rt", - encoding=self.encoding) as cf: - self.readfp(cf, filename) - - def optionxform(self, option): - return option - - def get_path_list(self, section, option): - v = self.get(section, option) - return v.split(os.pathsep) - - def read(self, filenames): - raise NotImplementedError("use `readfp()' instead") - - def readfp(self, fp, filename): - if hasattr(self, "filename"): - raise RuntimeError("already initialized") - filename = os.path.normpath(os.path.abspath(filename)) - if PY2: - if isinstance(filename, str): - filename = filename.decode(locale.getpreferredencoding()) - self.set(None, "self", filename) - self.set(None, "here", os.path.dirname(filename)) - self.set(None, "root", os.path.dirname(self.executable)) - SafeConfigParser.readfp(self, fp, filename=filename) - self.filename = filename - self.root = os.path.dirname(self.executable) - - def getx(self, section, option): - """Extended get() with some automatic type conversion support. - - Default: Fetch as string (like `get()`). - - If annotated with ``:bool:`` fetch as bool, if annotated with - ``:int:`` fetch as int, if annotated with ``:float:`` fetch as - float. - - """ - v = self.get(section, option) - if v.startswith(":bool:"): - v = v[6:].lower() - if v not in self._BOOL_CVT: - raise ValueError("Not a boolean: %s" % (v, )) - return self._BOOL_CVT[v] - elif v.startswith(":int:"): - return int(v[5:], 0) - elif v.startswith(":float:"): - return float(v[7:]) - else: - return v - - _BOOL_CVT = {'1': True, 'yes': True, 'true': True, 'on': True, - '0': False, 'no': False, 'false': False, 'off': False}
--- a/mixconfig/yaml.py Tue Mar 08 16:46:27 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,160 +0,0 @@ -# -*- coding: utf-8 -*- -r"""Simple wrapper for yaml to support all-unicode strings when loading -configuration files. - -""" - -from __future__ import division, print_function, absolute_import - -try: - from collections import OrderedDict -except ImportError: - try: - from ordereddict import OrderedDict - except ImportError: - OrderedDict = None -import yaml -import yaml.constructor - - -__all__ = ["safe_load", "safe_load_all", "load", "load_all"] - - -class ConfigLoader(yaml.Loader): - - """A YAML loader, which makes all !!str strings to Unicode. Standard - PyYAML does this only in the non-ASCII case. - - If an `OrderedDict` implementation is available then all "map" and - "omap" nodes are constructed as `OrderedDict`. - This is against YAML specs but within configuration files it seems - more natural. - - """ - - def construct_yaml_str(self, node): - return self.construct_scalar(node) - - if OrderedDict: - - # - # From https://pypi.python.org/pypi/yamlordereddictloader/0.1.1 - # (MIT License) - # - - def construct_yaml_map(self, node): - data = OrderedDict() - yield data - value = self.construct_mapping(node) - data.update(value) - - def construct_mapping(self, node, deep=False): - if isinstance(node, yaml.MappingNode): - self.flatten_mapping(node) - else: - raise yaml.constructor.ConstructorError(None, None, - 'expected a mapping node, but found %s' % node.id, - node.start_mark) - - mapping = OrderedDict() - for key_node, value_node in node.value: - key = self.construct_object(key_node, deep=deep) - try: - hash(key) - except TypeError as err: - raise yaml.constructor.ConstructorError( - 'while constructing a mapping', node.start_mark, - 'found unacceptable key (%s)' % err, key_node.start_mark) - value = self.construct_object(value_node, deep=deep) - mapping[key] = value - return mapping - - -ConfigLoader.add_constructor( - "tag:yaml.org,2002:str", - ConfigLoader.construct_yaml_str) -if OrderedDict: - ConfigLoader.add_constructor( - "tag:yaml.org,2002:map", - ConfigLoader.construct_yaml_map) - ConfigLoader.add_constructor( - "tag:yaml.org,2002:omap", - ConfigLoader.construct_yaml_map) - - -class ConfigSafeLoader(yaml.SafeLoader): - - """A safe YAML loader, which makes all !!str strings to Unicode. - Standard PyYAML does this only in the non-ASCII case. - - If an `OrderedDict` implementation is available then all "map" and - "omap" nodes are constructed as `OrderedDict`. - This is against YAML specs but within configuration files it seems - more natural. - - """ - - def construct_yaml_str(self, node): - return self.construct_scalar(node) - - if OrderedDict: - - # - # From https://pypi.python.org/pypi/yamlordereddictloader/0.1.1 - # (MIT License) - # - - def construct_yaml_map(self, node): - data = OrderedDict() - yield data - value = self.construct_mapping(node) - data.update(value) - - def construct_mapping(self, node, deep=False): - if isinstance(node, yaml.MappingNode): - self.flatten_mapping(node) - else: - raise yaml.constructor.ConstructorError(None, None, - 'expected a mapping node, but found %s' % node.id, - node.start_mark) - - mapping = OrderedDict() - for key_node, value_node in node.value: - key = self.construct_object(key_node, deep=deep) - try: - hash(key) - except TypeError as err: - raise yaml.constructor.ConstructorError( - 'while constructing a mapping', node.start_mark, - 'found unacceptable key (%s)' % err, key_node.start_mark) - value = self.construct_object(value_node, deep=deep) - mapping[key] = value - return mapping - - -ConfigSafeLoader.add_constructor( - "tag:yaml.org,2002:str", - ConfigSafeLoader.construct_yaml_str) -if OrderedDict: - ConfigSafeLoader.add_constructor( - "tag:yaml.org,2002:map", - ConfigSafeLoader.construct_yaml_map) - ConfigSafeLoader.add_constructor( - "tag:yaml.org,2002:omap", - ConfigSafeLoader.construct_yaml_map) - - -def load(stream, Loader=ConfigLoader): - return yaml.load(stream, Loader) - - -def load_all(stream, Loader=ConfigLoader): - return yaml.load_all(stream, Loader) - - -def safe_load(stream): - return yaml.load(stream, Loader=ConfigSafeLoader) - - -def safe_load_all(stream): - return yaml.load_all(stream, Loader=ConfigSafeLoader)
--- a/setup.py Tue Mar 08 16:46:27 2016 +0100 +++ b/setup.py Tue Mar 08 20:11:17 2016 +0100 @@ -20,21 +20,21 @@ _version_re = re.compile(br"^\s*__version__\s*=\s*(\"|')(.*)\1\s*(#.*)?$", re.MULTILINE) -with open(os.path.join(pkg_root, "mixconfig", "__init__.py"), "rb") as vf: +with open(os.path.join(pkg_root, "configmix", "__init__.py"), "rb") as vf: version = _version_re.search(vf.read()).group(2).decode("utf-8") with open(os.path.join(pkg_root, "README"), "rt") as rf: long_description = rf.read() setup( - name="MixConfig", + name="ConfigMix", version=version, author="Franz Glasner", license="BSD", - url="https://pypi.dom66.de/simple/mixconfig/", + url="https://pypi.dom66.de/simple/configmix/", description="Library for extended configuration files", long_description=long_description, - packages=["mixconfig"], + packages=["configmix"], include_package_data=False, zip_safe=True, platforms="any",
