"""Zgres config file"""
import os
import sys
import logging
import configparser

class StdOutFilter(logging.Filter):

    def filter(self, record):
        return record.levelno < logging.WARNING

def _add_common_args(parser, config_file):
    if config_file is not None:
        parser.add_argument('-c', '--config',
                dest='config',
                nargs='*',
                default=['/etc/zgres/{}'.format(config_file), '/etc/zgres/{}.d'.format(config_file)],
                help='Use this config file or directory. If a directory, all files ending with .ini are parsed. Order is important with latter files over-riding earlier ones.')
    verbosity = parser.add_mutually_exclusive_group()
    verbosity.add_argument('--debug',
            action='store_true',
            help='Print extra debug info on stdout')
    verbosity.add_argument('--verbose',
            action='store_true',
            help='Print extra info on stdout')
    verbosity.add_argument('--quiet',
            action='store_true',
            help='Print only errors')

def _setup_logging(config):
    root_logger = logging.getLogger()
    level = logging.WARN
    if config.quiet:
        level = logging.ERROR
    elif config.verbose:
        level = logging.INFO
    elif config.debug:
        level = logging.DEBUG
    root_logger.setLevel(level)
    # less than WARN to stderr
    stdout = logging.StreamHandler(sys.stdout)
    stdout.addFilter(StdOutFilter())
    root_logger.addHandler(stdout)
    # WARN and above to stderr
    stderr = logging.StreamHandler(sys.stderr)
    stderr.setLevel(logging.WARNING)
    root_logger.addHandler(stderr)

def _get_config(args):
    config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
    for file_or_dir in args.config:
        if os.path.isfile(file_or_dir):
            config.read_file(open(file_or_dir, 'r'))
        else:
            for cfg in sorted(os.listdir(file_or_dir)):
                if cfg.startswith('.') or not cfg.endswith('.ini'):
                    continue
                config.read_file(open(os.path.join(file_or_dir, cfg), 'r'))
    return config

def parse_args(parser, argv, config_file=None):
    _add_common_args(parser, config_file)
    args = parser.parse_args(args=argv[1:])
    if config_file is None:
        config = None
    else:
        config = _get_config(args)
    _setup_logging(args)
    return config

@@ -194,32 +194,31 @@
 NG%0A%0Adef 
-_
 add_co
-mmon_args
+nfig_file
 (parser,
@@ -236,44 +236,8 @@
 e):%0A
-    if config_file is not None:%0A    
     
@@ -286,20 +286,16 @@
         
-    
 dest='co
@@ -313,20 +313,16 @@
         
- 
-   
 nargs='*
@@ -336,20 +336,16 @@
         
-   
- 
 default=
@@ -422,20 +422,16 @@
 file)%5D,%0A
-    
         
@@ -594,16 +594,47 @@
  ones.')
+%0A%0Adef add_logging_args(parser):
 %0A    ver
@@ -1038,17 +1038,16 @@
 ')%0A%0Adef 
-_
 setup_lo
@@ -1619,27 +1619,32 @@
 r)%0A%0Adef 
-_get
+read
 _config
+_file
 (args):%0A
@@ -2181,44 +2181,108 @@
     
-_add_common_args(parser, config_file
+if config_file is not None:%0A        add_config_file(parser, config_file)%0A    add_logging_args(parser
 )%0A  
@@ -2323,16 +2323,40 @@
 gv%5B1:%5D)%0A
+    setup_logging(args)%0A
     if c
@@ -2428,44 +2428,24 @@
 g = 
-_get
+read
 _config
-(args)%0A    _setup_logging
+_file
 (arg

make some bits of the config public for re-use