# Path: pyehr/utils/services.py
# def get_service_configuration(configuration_file, logger=None):
#     if not logger:
#         logger = get_logger('service_configuration')
#     parser = SafeConfigParser(allow_no_value=True)
#     parser.read(configuration_file)
#     try:
#         conf = ServiceConfig(
#             parser.get('db', 'driver'),
#             parser.get('db', 'host'),
#             parser.get('db', 'database'),
#             parser.get('db', 'versioning_database'),
#             parser.get('db', 'port'),
#             parser.get('db', 'user'),
#             parser.get('db', 'passwd'),
#             parser.get('db', 'patients_repository'),
#             parser.get('db', 'ehr_repository'),
#             parser.get('db', 'ehr_versioning_repository'),
#             parser.get('index', 'url'),
#             parser.get('index', 'database'),
#             parser.get('index', 'user'),
#             parser.get('index', 'passwd'),
#             parser.get('db_service', 'host'),
#             parser.get('db_service', 'port'),
#             parser.get('db_service', 'server_engine'),
#             parser.get('query_service', 'host'),
#             parser.get('query_service', 'port'),
#             parser.get('query_service', 'server_engine')
#         )
#         return conf
#     except NoOptionError, nopt:
#         logger.critical('Missing option %s in configuration file', nopt)
#         return None
# 
# def check_pid_file(pid_file, logger):
#     if os.path.isfile(pid_file):
#         logger.info('Another dbservice daemon is running, exit')
#         sys.exit(0)
# 
# def create_pid(pid_file):
#     pid = str(os.getpid())
#     with open(pid_file, 'w') as ofile:
#         ofile.write(pid)
# 
# def destroy_pid(pid_file):
#     os.remove(pid_file)
# 
# def get_rotating_file_logger(logger_label, log_file, log_level='INFO',
#                              max_bytes=100*1024*1024, backup_count=3):
#     logger = logging.getLogger(logger_label)
#     if not isinstance(log_level, int):
#         try:
#             log_level = getattr(logging, log_level)
#         except AttributeError:
#             raise ValueError('unsupported literal log level: %s' % log_level)
#     logger.setLevel(log_level)
#     # clear existing handlers
#     logger.handlers = []
#     handler = RotatingFileHandler(log_file, max_bytes, backup_count)
#     formatter = logging.Formatter(LOG_FORMAT, datefmt=LOG_DATEFMT)
#     handler.setFormatter(formatter)
#     logger.addHandler(handler)
#     return logger


import sys, argparse
    import simplejson as json
    import json
import pyehr.ehr.services.dbmanager.errors as pyehr_errors
                import traceback
from functools import wraps
from bottle import post, get, run, response, request, abort, HTTPError
from pyehr.ehr.services.dbmanager.querymanager import QueryManager
from pyehr.utils import get_logger
from pyehr.utils.services import get_service_configuration, check_pid_file,\
    create_pid, destroy_pid, get_rotating_file_logger
        return 'QueryManager daemon running'


def get_parser():
    parser = argparse.ArgumentParser('Run the QueryService daemon')
    parser.add_argument('--config', type=str, required=True,
                        help='service configuration file')
    parser.add_argument('--debug', action='store_true',
                        help='Enable web server DEBUG mode')
    parser.add_argument('--pid-file', type=str, help='PID file for the queryservice daemon',
                        default='/tmp/pyehr_queryservice.pid')
    parser.add_argument('--log-file', type=str, help='LOG file (default=stderr)')
    parser.add_argument('--log-level', type=str, default='INFO',
                        help='LOG level (default=INFO)')
    return parser


def main(argv):
    parser = get_parser()
    args = parser.parse_args(argv)
    logger = get_logger('query_service_daemon_main')
    conf = get_service_configuration(args.config, logger)
    if not conf:
        msg = 'It was impossible to load configuration, exit'
        logger.critical(msg)
        sys.exit(msg)
    qservice = QueryService(log_file=args.log_file, log_level=args.log_level,
                            **conf.get_db_configuration())
    qservice.add_index_service(**conf.get_index_configuration())
    check_pid_file(args.pid_file, logger)

# Path: pyehr/utils/services.py
# def get_service_configuration(configuration_file, logger=None):
#     if not logger:
#         logger = get_logger('service_configuration')
#     parser = SafeConfigParser(allow_no_value=True)
#     parser.read(configuration_file)
#     try:
#         conf = ServiceConfig(
#             parser.get('db', 'driver'),
#             parser.get('db', 'host'),
#             parser.get('db', 'database'),
#             parser.get('db', 'versioning_database'),
#             parser.get('db', 'port'),
#             parser.get('db', 'user'),
#             parser.get('db', 'passwd'),
#             parser.get('db', 'patients_repository'),
#             parser.get('db', 'ehr_repository'),
#             parser.get('db', 'ehr_versioning_repository'),
#             parser.get('index', 'url'),
#             parser.get('index', 'database'),
#             parser.get('index', 'user'),
#             parser.get('index', 'passwd'),
#             parser.get('db_service', 'host'),
#             parser.get('db_service', 'port'),
#             parser.get('db_service', 'server_engine'),
#             parser.get('query_service', 'host'),
#             parser.get('query_service', 'port'),
#             parser.get('query_service', 'server_engine')
#         )
#         return conf
#     except NoOptionError, nopt:
#         logger.critical('Missing option %s in configuration file', nopt)
#         return None
# 
# def check_pid_file(pid_file, logger):
#     if os.path.isfile(pid_file):
#         logger.info('Another dbservice daemon is running, exit')
#         sys.exit(0)
# 
# def create_pid(pid_file):
#     pid = str(os.getpid())
#     with open(pid_file, 'w') as ofile:
#         ofile.write(pid)
# 
# def destroy_pid(pid_file):
#     os.remove(pid_file)
# 
# def get_rotating_file_logger(logger_label, log_file, log_level='INFO',
#                              max_bytes=100*1024*1024, backup_count=3):
#     logger = logging.getLogger(logger_label)
#     if not isinstance(log_level, int):
#         try:
#             log_level = getattr(logging, log_level)
#         except AttributeError:
#             raise ValueError('unsupported literal log level: %s' % log_level)
#     logger.setLevel(log_level)
#     # clear existing handlers
#     logger.handlers = []
#     handler = RotatingFileHandler(log_file, max_bytes, backup_count)
#     formatter = logging.Formatter(LOG_FORMAT, datefmt=LOG_DATEFMT)
#     handler.setFormatter(formatter)
#     logger.addHandler(handler)
#     return logger


# Path: services/queryservice.py
import sys, argparse
    import simplejson as json
    import json
import pyehr.ehr.services.dbmanager.errors as pyehr_errors
                import traceback
from functools import wraps
from bottle import post, get, run, response, request, abort, HTTPError
from pyehr.ehr.services.dbmanager.querymanager import QueryManager
from pyehr.utils import get_logger
from pyehr.utils.services import get_service_configuration, check_pid_file,\
    create_pid, destroy_pid, get_rotating_file_logger

        return 'QueryManager daemon running'


def get_parser():
    parser = argparse.ArgumentParser('Run the QueryService daemon')
    parser.add_argument('--config', type=str, required=True,
                        help='service configuration file')
    parser.add_argument('--debug', action='store_true',
                        help='Enable web server DEBUG mode')
    parser.add_argument('--pid-file', type=str, help='PID file for the queryservice daemon',
                        default='/tmp/pyehr_queryservice.pid')
    parser.add_argument('--log-file', type=str, help='LOG file (default=stderr)')
    parser.add_argument('--log-level', type=str, default='INFO',
                        help='LOG level (default=INFO)')
    return parser


def main(argv):
    parser = get_parser()
    args = parser.parse_args(argv)
    logger = get_logger('query_service_daemon_main')
    conf = get_service_configuration(args.config, logger)
    if not conf:
        msg = 'It was impossible to load configuration, exit'
        logger.critical(msg)
        sys.exit(msg)
    qservice = QueryService(log_file=args.log_file, log_level=args.log_level,
                            **conf.get_db_configuration())
    qservice.add_index_service(**conf.get_index_configuration())
    check_pid_file(args.pid_file, logger)

    create_pid(args.pid_file)