File: hiyapyco_example.py

package info (click to toggle)
python-hiyapyco 0.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: python: 2,012; makefile: 237
file content (60 lines) | stat: -rwxr-xr-x 1,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /usr/bin/env python

import sys
import os
import logging
import argparse

basepath = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.dirname(basepath))

import hiyapyco

class LoggingAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        # print '%r %r %r' % (namespace, values, option_string)
        logger = logging.getLogger()
        logger.setLevel(values)
        setattr(namespace, self.dest, values)

logger = logging.getLogger()
logging.basicConfig(
    level=logging.WARN,
    format='%(levelname)s\t[%(name)s] %(funcName)s: %(message)s'
    )

parser = argparse.ArgumentParser()
parser.add_argument(
    '-l', '--loglevel',
    help='set loglevel',
    type=str,
    choices=[k for k in logging._nameToLevel.keys() if isinstance(k, str)],
    action=LoggingAction
    )
parser.add_argument(
        '-y', '--usedefaultyamlloader', dest='usedefaultyamlloader',
        action='store_true', default=False, help='use default yaml loader'
    )
parser.add_argument('-f', '--file', type=str, nargs='+', required=True, help='yaml file(s) to parse')
args = parser.parse_args()

if args.loglevel is None:
    logging.disable(logging.CRITICAL)

for mergemethod in hiyapyco.METHODS.keys():
    print('='*10, 'method=', mergemethod, '='*10)
    conf = hiyapyco.load(
        *args.file,
        method=hiyapyco.METHODS[mergemethod],
        interpolate=True,
        failonmissingfiles=True,
        usedefaultyamlloader=args.usedefaultyamlloader
        )
    print(conf)
    print('-'*10, 'YAML', '-'*10)
    print(hiyapyco.dump(conf))
    if len(args.file) < 2:
        break

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 smartindent nu