# -*- coding: utf-8 -*-

import os
import sys
import yaml
import yamlordereddictloader
import clif
from pprint import pformat
from collections import OrderedDict

_SELF = sys.modules[__name__]


def init(args):
    setattr(_SELF, 'args', args)
    if 'conf_file' not in args:
        raise clif.CliError('no configuration file')

    try:
        config = yaml.load(open(args.conf_file), Loader=yamlordereddictloader.Loader) or {}
    except Exception as err:
        config = {}

    for param, value in config.items():
        setattr(_SELF, param.upper(), replace_paths(value))

    # Init logs.
    commands = [value for (arg, value) in sorted(args) if arg.startswith('command')]
    clif.logger.init('-'.join(commands),
                     os.path.join(args.logdir, *commands),
                     args.loglevel)
    load_commands_conf(commands)

    clif.logger.debug('configuration parameters:\n%s' %
                      pformat({attr: getattr(_SELF, attr)
                               for attr in vars(_SELF)
                               if attr.isupper() and not attr.startswith('_')}))

def replace_paths(value):
    return {str: lambda: value.replace('__FILE__', sys.path[0]),
            list: lambda: [replace_paths(elt) for elt in value],
            dict: lambda: {key: replace_paths(val) for key, val in value.items()},
            OrderedDict: lambda: {key: replace_paths(val) for key, val in value.items()}
           }.get(type(value), lambda: value)()

def load_commands_conf(commands):
    # Load intermediary configuration files.
    cmd_path = os.path.join(sys.path[0], 'conf')
    for cmd in commands:
        cmd_path = os.path.join(cmd_path, cmd)
        cmd_file = '%s.yml' % cmd_path
        if os.path.exists(cmd_file):
            load_file(cmd_file)

    # Load every file in the directory of the command (if exits).
    if os.path.exists(cmd_path):
        for filename in os.listdir(cmd_path):
            if filename.startswith('.'):
                continue
            filepath = os.path.join(cmd_path, filename)
            if os.path.isfile(filepath):
                load_file(filepath, False)

def load_file(filepath, root=True):
    clif.logger.debug("loading configuration file '%s'" % filepath)
    filename, fileext = os.path.splitext(os.path.basename(filepath))
    try:
        if fileext == '.yml':
            conf = yaml.load(open(filepath), Loader=yamlordereddictloader.Loader)
            conf = replace_paths(conf)
            if root:
                for attr, value in conf.items():
                    setattr(_SELF, attr.replace('-', '_').upper(), value)
            else:
                setattr(_SELF, filename.upper(), conf)
        else:
            with open(filepath) as fhandler:
                setattr(_SELF, filename.upper(), fhandler.read())
    except Exception as err:
        clif.logger.error("unable to load configuration file '%s': %s"
                      % (filepath, err),
                     quit=True)

@@ -92,16 +92,45 @@
 rt clif%0A
+import clif.logger as logger%0A
 from ppr
@@ -706,29 +706,24 @@
 mand')%5D%0A    
-clif.
 logger.init(
@@ -758,21 +758,16 @@
         
-    
- 
 os.path.
@@ -788,37 +788,32 @@
 ir, *commands),%0A
-     
                 
@@ -861,29 +861,24 @@
 mands)%0A%0A    
-clif.
 logger.debug
@@ -929,21 +929,16 @@
         
-     
 pformat(
@@ -983,37 +983,32 @@
                 
-  
-   
 for attr in vars
@@ -1015,21 +1015,16 @@
 (_SELF)%0A
-     
         
@@ -2165,29 +2165,24 @@
 =True):%0A    
-clif.
 logger.debug
@@ -2836,21 +2836,16 @@
         
-clif.
 logger.e
@@ -2893,38 +2893,16 @@
 %25s': %25s%22
-%0A                     
  %25 (file

Correct a bug with logger import.