import logging
import os
import sys
from argparse import ArgumentParser
from functools import partial

from daemon import daemon

from i3_configger.builder import build, gather_fragments
from i3_configger.inotify_eventloop import inotify_eventloop

log = logging.getLogger(__name__)


def configure_logging(verbose):
    level = logging.DEBUG if verbose else logging.INFO
    fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(stream=sys.stdout, format=fmt, level=level)


def get_cnf():
    p = ArgumentParser('i3-configger')
    p.add_argument('--verbose', action="store_true", default=False)
    g = p.add_mutually_exclusive_group()
    g.add_argument('--watch', action="store_true",
                   help="build new config on changes",
                   default=False)
    g.add_argument('--daemon', action="store_true",
                   help="run as deamon and watch changes in conf.d",
                   default=False)
    p.add_argument('--sources', action="store",
                   help="path to directory containing source files",
                   default=os.path.expanduser('~/.i3/config.d'))
    p.add_argument('--destination', action="store",
                   help="path to the i3 config file",
                   default=os.path.expanduser('~/.i3/config'))
    p.add_argument('--suffix', action="store",
                   help="suffix of the source files",
                   default='.i3conf')
    p.add_argument('--message', action="store_true",
                   help="customize timestamped generation message",
                   default='generated by i3 configger')
    return p.parse_args()


def main():
    cnf = get_cnf()
    configure_logging(cnf.verbose)
    log.debug("config: %s", cnf)
    gather_func = partial(gather_fragments, cnf.sources, cnf.suffix)
    build_func = partial(build, destination=cnf.destination, msg=cnf.message)
    if cnf.watch:
        inotify_eventloop(cnf.sources, cnf.suffix, gather_func, build_func)
    elif cnf.daemon:
        log.debug("daemonizing ...")
        with daemon.DaemonContext():
            inotify_eventloop(cnf.sources, cnf.suffix, gather_func, build_func)
    else:
        build_func(fragments=gather_func())


if __name__ == '__main__':
    sys.exit(main())

import logging
import os
import sys
from argparse import ArgumentParser
from functools import partial

from daemon import daemon

from i3_configger.builder import build, gather_fragments
from i3_configger.inotify_eventloop import inotify_eventloop

log = logging.getLogger(__name__)


def configure_logging(verbose):
    level = logging.DEBUG if verbose else logging.INFO
    fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(stream=sys.stdout, format=fmt, level=level)


def get_cnf():
    p = ArgumentParser('i3-configger')
    p.add_argument('--verbose', action="store_true", default=False)
    g = p.add_mutually_exclusive_group()
    g.add_argument('--watch', action="store_true",
                   help="build new config on changes",
                   default=False)
    g.add_argument('--daemon', action="store_true",
                   help="run as deamon and watch changes in conf.d",
                   default=False)
    p.add_argument('--sources', action="store",
                   help="path to directory containing source files",
                   default=os.path.expanduser('~/.i3/config.d'))
    p.add_argument('--destination', action="store",
                   help="path to the i3 config file",
                   default=os.path.expanduser('~/.i3/config'))
    p.add_argument('--suffix', action="store",
                   help="suffix of the source files",
                   default='.i3conf')
    p.add_argument('--message', action="store_true",
                   help="customize timestamped generation message",
                   default='generated by i3 configger')
    return p.parse_args()


def main():
    cnf = get_cnf()
    configure_logging(cnf.verbose)
    log.debug("config: %s", cnf)
    gather_func = partial(gather_fragments, cnf.sources, cnf.suffix)
    build_func = partial(build, cnf.destination, cnf.message)
    if cnf.watch:
        inotify_eventloop(cnf.sources, cnf.suffix, gather_func, build_func)
    elif cnf.daemon:
        log.debug("daemonizing ...")
        with daemon.DaemonContext():
            inotify_eventloop(cnf.sources, cnf.suffix, gather_func, build_func)
    else:
        build_func(fragments=gather_func())


if __name__ == '__main__':
    sys.exit(main())

use positional args