How to use the anyconfig.compat function in anyconfig

To help you get started, we’ve selected a few anyconfig examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ssato / python-anyconfig / tests / api.py View on Github external
def test_49_loads_w_validation_error(self):
        if not anyconfig.schema.JSONSCHEMA_IS_AVAIL:
            skip_test()

        cnf_s = """{"a": "aaa"}"""
        scm_s = TT.dumps(SCM_0, "json")
        cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_schema=scm_s)
        self.assertTrue(cnf_2 is None, cnf_2)


class Test_22_single_load(TestBase):

    a_path = os.path.join(resdir(), "00-cnf.json")
    cnf = CNF_1
    pathlib = anyconfig.compat.pathlib

    def test_10__single_load(self):
        res = TT.single_load(self.a_path)
        self.assert_dicts_equal(res, self.cnf)

    def test_12__single_load__ac_parser(self):
        res = TT.single_load(self.a_path, ac_parser="json")
        self.assert_dicts_equal(res, self.cnf)

    def test_14__single_load__ac_parser_by_id(self):
        cid = anyconfig.backend.json.Parser.cid()
        res = TT.single_load(self.a_path, ac_parser=cid)
        self.assert_dicts_equal(res, self.cnf)

    def test_20__single_load__stream(self):
        res = TT.single_load(open(self.a_path), ac_parser="json")
github ssato / python-anyconfig / tests / extras.py View on Github external
def test_10_ac_compat(self):
        fun = "NullHandler"
        sys.modules["logging"] = None
        import anyconfig.compat

        self.assertFalse(fun in globals())
        self.assertFalse(getattr(anyconfig.compat, fun) is None)
github ssato / python-anyconfig / src / anyconfig / backend / xml.py View on Github external
def load_from_string(self, content, container, **opts):
        """
        Load config from XML snippet (a string 'content').

        :param content:
            XML snippet string of str (python 2) or bytes (python 3) type
        :param container: callble to make a container object
        :param opts: optional keyword parameters passed to

        :return: Dict-like object holding config parameters
        """
        root = ET.fromstring(content)
        if anyconfig.compat.IS_PYTHON_3:
            stream = BytesIO(content)
        else:
            stream = anyconfig.compat.StringIO(content)
        nspaces = _namespaces_from_file(stream)
        return root_to_container(root, container=container,
                                 nspaces=nspaces, **opts)
github ssato / python-anyconfig / src / anyconfig / utils.py View on Github external
def is_path(obj):
    """
    Is given object 'obj' a file path?

    :param obj: file path or something
    :return: True if 'obj' is a file path
    """
    return isinstance(obj, anyconfig.compat.STR_TYPES)
github ssato / python-anyconfig / src / anyconfig / template.py View on Github external
Compile and render template and return the result as a string.

    :param template_file: Absolute or relative path to the template file
    :param ctx: Context dict needed to instantiate templates
    :param paths: Template search paths
    :param ask: Ask user for missing template location if True
    :param filters: Custom filters to add into template engine
    :return: Compiled result (str)
    """
    try:
        return render_impl(filepath, ctx, paths, filters)
    except TemplateNotFound as mtmpl:
        if not ask:
            raise

        usr_tmpl = anyconfig.compat.raw_input(os.linesep + ""
                                              "*** Missing template "
                                              "'%s'. Please enter absolute "
                                              "or relative path starting from "
                                              "'.' to the template file: " %
                                              mtmpl)
        usr_tmpl = os.path.normpath(usr_tmpl.strip())
        paths = make_template_paths(usr_tmpl, paths)

        return render_impl(usr_tmpl, ctx, paths, filters)
github ssato / python-anyconfig / anyconfig / backend / sqlite.py View on Github external
"""
    :param val: Value to detect its type
    :return: Expression of type in SQLite such like 'INTEGER' and 'TEXT'

    >>> _sqlite_type(1092)
    'INTEGER'
    >>> _sqlite_type(1.0)
    'REAL'
    >>> _sqlite_type("xyz")
    'TEXT'
    >>> ref = anyconfig.backend.relations.Ref(relvar="A.A", id=0)
    >>> _sqlite_type(ref)
    'INTEGER'
    """
    vtype = type(val)
    if vtype in anyconfig.compat.STR_TYPES:
        return "TEXT"
    elif vtype == int or _is_ref(val):
        return "INTEGER"
    elif vtype == float:
        return "REAL"
    else:
        return "BLOB"
github ssato / python-anyconfig / anyconfig / backend / backends.py View on Github external
def cmp_cps(lhs, rhs):
    """Compare config parsers by these priorities.
    """
    return anyconfig.compat.cmp(lhs.priority(), rhs.priority())
github ssato / python-anyconfig / src / anyconfig / backend / base.py View on Github external
def dump_to_string(self, cnf, **kwargs):
        """
        Dump config 'cnf' to a string.

        :param cnf: Configuration data to dump
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        """
        stream = anyconfig.compat.StringIO()
        self.dump_to_stream(cnf, stream, **kwargs)
        return stream.getvalue()