コード例 #1
0
ファイル: cli.py プロジェクト: brennv/python-anyconfig
def main(argv=None):
    """
    :param argv: Argument list to parse or None (sys.argv will be set).
    """
    (parser, options, args) = parse_args(argv=argv)
    API.LOGGER.setLevel(to_log_level(options.loglevel))

    _check_options_and_args(parser, options, args)

    cnf = API.to_container(os.environ.copy() if options.env else {})
    diff = API.load(args, options.itype,
                    ignore_missing=options.ignore_missing,
                    ac_merge=options.merge, ac_template=options.template,
                    ac_schema=options.schema)

    _exit_if_load_failure(diff, "Failed to load: args=%s" % ", ".join(args))
    cnf.update(diff)

    if options.args:
        diff = anyconfig.parser.parse(options.args)
        cnf.update(diff)

    _exit_if_only_to_validate(options.validate)

    if options.gen_schema:
        cnf = API.gen_schema(cnf)

    if options.get:
        cnf = _do_get(cnf, options.get)

    if options.set:
        (key, val) = options.set.split('=')
        API.set_(cnf, key, anyconfig.parser.parse(val))

    _output_result(cnf, options.output, options.otype, args[0], options.itype)
コード例 #2
0
def main(argv=None):
    """
    :param argv: Argument list to parse or None (sys.argv will be set).
    """
    (parser, options, args) = parse_args(argv=argv)
    LOGGER.setLevel(to_log_level(options.loglevel))

    _check_options_and_args(parser, options, args)

    cnf = os.environ.copy() if options.env else {}
    diff = _load_diff(args, options)
    API.merge(cnf, diff)

    if options.args:
        diff = anyconfig.parser.parse(options.args)
        API.merge(cnf, diff)

    _exit_if_only_to_validate(options.validate)

    if options.gen_schema:
        cnf = API.gen_schema(cnf)

    if options.query:
        cnf = _do_query(cnf, options.query)
    elif options.get:
        cnf = _do_get(cnf, options.get)

    if options.set:
        (key, val) = options.set.split('=')
        API.set_(cnf, key, anyconfig.parser.parse(val))

    _output_result(cnf, options.output, options.otype, args[0], options.itype)
コード例 #3
0
ファイル: cli.py プロジェクト: quamilek/python-anyconfig
def main(argv=sys.argv):
    """
    :param argv: Argument list to parse [sys.argv]
    """
    parser = option_parser()
    (options, args) = parser.parse_args(argv[1:])

    A.set_loglevel(to_log_level(options.loglevel))

    if not args:
        if options.list:
            tlist = ", ".join(A.list_types()) + "\n"
            sys.stdout.write("Supported config types: " + tlist)
            sys.exit(0)
        else:
            parser.print_usage()
            sys.exit(-1)

    data = data = os.environ.copy() if options.env else A.container()
    diff = A.load(args, options.itype, ignore_missing=options.ignore_missing,
                  merge=options.merge, ac_template=options.template)
    data.update(diff)

    if options.args:
        diff = A.loads(options.args, options.atype,
                       ac_template=options.template, ac_context=data)
        data.update(diff, options.merge)

    if options.get:
        (data, err) = A.get(data, options.get)
        if err:
            raise RuntimeError(err)

    if options.set:
        A.set_(data, *(options.set.split('=')))

    if options.output:
        cparser = A.find_loader(options.output, options.otype)
        if cparser is None:
            raise RuntimeError("No suitable dumper was found for %s",
                               options.output)

        cparser.dump(data, options.output)
    else:
        # TODO: Reuse input type automatically detected as it's impossible to
        # detect output type w/o options.output.
        if options.otype is None:
            if options.itype is None:
                raise RuntimeError("Please specify input and/or output type "
                                   "with -I (--itype) or -O (--otype) option")
            else:
                options.otype = options.itype

        cparser = A.find_loader(None, options.otype)
        sys.stdout.write(cparser.dumps(data))
コード例 #4
0
def main(argv=sys.argv):
    """
    :param argv: Argument list to parse [sys.argv]
    """
    parser = option_parser()
    (options, args) = parser.parse_args(argv[1:])

    A.set_loglevel(to_log_level(options.loglevel))

    if not args:
        if options.list:
            tlist = ", ".join(A.list_types()) + "\n"
            sys.stdout.write("Supported config types: " + tlist)
            sys.exit(0)
        else:
            parser.print_usage()
            sys.exit(-1)

    data = A.load(args,
                  options.itype,
                  options.merge,
                  ignore_missing=options.ignore_missing)

    if options.args:
        diff = A.loads(options.args, options.atype)
        data.update(diff, options.merge)

    if options.get:
        (data, err) = A.get(data, options.get)
        if err:
            raise RuntimeError(err)

    if options.set:
        A.set_(data, *(options.set.split('=')))

    if options.output:
        cparser = A.find_loader(options.output, options.otype)
        cparser.dump(data, options.output)
    else:
        # TODO: Reuse input type automatically detected as it's impossible to
        # detect output type w/o options.output.
        if options.otype is None:
            if options.itype is None:
                raise RuntimeError("Please specify input and/or output type "
                                   "with -I (--itype) or -O (--otype) option")
            else:
                options.otype = options.itype

        cparser = A.find_loader(None, options.otype)
        sys.stdout.write(cparser.dumps(data))
コード例 #5
0
def _do_filter(cnf, args):
    """
    :param cnf: Mapping object represents configuration data
    :param args: :class:`~argparse.Namespace` object
    :return: `cnf` may be updated
    """
    if args.query:
        cnf = API.query(cnf, args.query)
    elif args.get:
        cnf = _do_get(cnf, args.get)
    elif args.set:
        (key, val) = args.set.split('=')
        API.set_(cnf, key, anyconfig.parser.parse(val))

    return cnf
コード例 #6
0
ファイル: cli.py プロジェクト: ssato/python-anyconfig
def _do_filter(cnf, args):
    """
    :param cnf: Mapping object represents configuration data
    :param args: :class:`argparse.Namespace` object
    :return: 'cnf' may be updated
    """
    if args.query:
        cnf = API.query(cnf, args.query)
    elif args.get:
        cnf = _do_get(cnf, args.get)
    elif args.set:
        (key, val) = args.set.split('=')
        API.set_(cnf, key, anyconfig.parser.parse(val))

    return cnf
コード例 #7
0
ファイル: base.py プロジェクト: robotice/robotice
    def _set(self, key, val, dic=None, deliemeter=DELIMETER, dump=False):
        """def set_(dic, path, val, seps=P.PATH_SEPS, strategy=None):"""

        dumped = False

        # find and update file
        for path in glob.glob(self.config_path):
            _data = anyconfig.load(path)
            item = conf_api.get(_data, key, seps=deliemeter)[0]
            if item:
                conf_api.set_(_data, key, val, seps=deliemeter)
                if dump:
                    dumped = True
                    return self.dump(_data, path)

        if not dumped:
            raise Exception("Critical error in dump config to disk !")
コード例 #8
0
def main(argv=None):
    """
    :param argv: Argument list to parse or None (sys.argv will be set).
    """
    (parser, options, args) = parse_args(argv=argv)
    API.LOGGER.setLevel(to_log_level(options.loglevel))

    _check_options_and_args(parser, options, args)

    cnf = API.to_container(os.environ.copy() if options.env else {})
    diff = API.load(args,
                    options.itype,
                    ignore_missing=options.ignore_missing,
                    ac_merge=options.merge,
                    ac_template=options.template,
                    ac_schema=options.schema)

    _exit_if_load_failure(diff, "Failed to load: args=%s" % ", ".join(args))
    cnf.update(diff)

    if options.args:
        diff = anyconfig.parser.parse(options.args)
        cnf.update(diff)

    _exit_if_only_to_validate(options.validate)

    if options.gen_schema:
        cnf = API.gen_schema(cnf)

    if options.get:
        cnf = _do_get(cnf, options.get)

    if options.set:
        (key, val) = options.set.split('=')
        API.set_(cnf, key, anyconfig.parser.parse(val))

    _output_result(cnf, options.output, options.otype, args[0], options.itype)
コード例 #9
0
ファイル: cli.py プロジェクト: genme/python-anyconfig
def main(argv=None):
    """
    :param argv: Argument list to parse or None (sys.argv will be set).
    """
    if argv is None:
        argv = sys.argv

    parser = option_parser()
    (options, args) = parser.parse_args(argv[1:])

    A.set_loglevel(to_log_level(options.loglevel))

    if not args:
        if options.list:
            tlist = ", ".join(A.list_types()) + "\n"
            sys.stdout.write("Supported config types: " + tlist)
            sys.exit(0)
        else:
            parser.print_usage()
            sys.exit(1)

    if options.validate and options.schema is None:
        sys.stderr.write("--validate option requires --scheme option")
        sys.exit(1)

    data = data = os.environ.copy() if options.env else A.container()
    diff = A.load(args, options.itype,
                  ignore_missing=options.ignore_missing,
                  merge=options.merge, ac_template=options.template,
                  ac_schema=options.schema)

    if diff is None:
        sys.stderr.write("Validation failed")
        sys.exit(1)

    data.update(diff)

    if options.args:
        diff = A.loads(options.args, options.atype,
                       ac_template=options.template, ac_context=data)
        data.update(diff, options.merge)

    if options.validate:
        A.LOGGER.info("Validation succeeds")
        sys.exit(0)

    if options.gen_schema:
        data = A.gen_schema(data)

    if options.get:
        (data, err) = A.get(data, options.get)
        if err:
            raise RuntimeError(err)

        # Output primitive types as it is.
        if not anyconfig.mergeabledict.is_mergeabledict_or_dict(data):
            sys.stdout.write(str(data) + '\n')
            return

    if options.set:
        (key, val) = options.set.split('=')
        A.set_(data, key, anyconfig.parser.parse(val))

    if options.output:
        cparser = A.find_loader(options.output, options.otype)
        if cparser is None:
            raise RuntimeError("No suitable dumper was found for %s",
                               options.output)

        cparser.dump(data, options.output)
    else:
        if options.otype is None:
            if options.itype is None:
                psr = A.find_loader(args[0])
                if psr is not None:
                    options.otype = psr.type()  # Reuse detected input type
                else:
                    raise RuntimeError("Please specify input and/or output "
                                       "type with -I (--itype) or -O "
                                       "(--otype) option")
            else:
                options.otype = options.itype

        cparser = A.find_loader(None, options.otype)
        sys.stdout.write(cparser.dumps(data) + '\n')