This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
publik-bi/wcs_olap/cmd.py

60 lines
2.6 KiB
Python

import argparse
import ConfigParser
import os
import urlparse
import logging
import logging.config
from . import wcs_api
from .feeder import WcsOlapFeeder
import locale
def main():
locale.setlocale(locale.LC_ALL, '')
config = ConfigParser.ConfigParser()
global_config_file = '/etc/wcs_olap.ini'
if os.path.exists(global_config_file):
config.read(global_config_file)
if config.has_section('loggers'):
logging.config.fileConfig(global_config_file)
user_config_file = os.path.expanduser('~/.wcs_olap.ini')
if os.path.exists(user_config_file):
config.read(user_config_file)
if config.has_section('loggers'):
logging.config.fileConfig(user_config_file)
urls = [url for url in config.sections() if url.startswith('http://') or
url.startswith('https://')]
parser = argparse.ArgumentParser(description='Engine ES with W.C.S. data', add_help=False)
parser.add_argument('--url', help='url of the w.c.s. instance', required=not urls,
default=(urls or [None])[0])
args, rest = parser.parse_known_args()
defaults = {}
if getattr(args, 'url') and config.has_section(args.url):
defaults = dict(config.items(args.url))
parser.add_argument("-h", "--help", action="help", help="show this help message and exit")
parser.add_argument('--orig', help='origin of the request for signatures',
required='orig' not in defaults)
parser.add_argument('--key', help='HMAC key for signatures', required='key' not in defaults)
group = parser.add_mutually_exclusive_group(
required='email' not in defaults and 'name_id' not in defaults)
group.add_argument('--email', help='email for authentication')
group.add_argument('--name-id', help='NameID for authentication')
if defaults:
parser.set_defaults(**defaults)
parser.add_argument('--pg-dsn', help='Psycopg2 DB DSN', required='pg-dsn' not in defaults)
args = parser.parse_args()
api = wcs_api.WcsApi(url=args.url, orig=args.orig, key=args.key, email=args.email,
name_id=args.name_id)
domain = urlparse.urlparse(args.url).netloc.split(':')[0]
schema = defaults.get('schema') or domain.replace('.', '_')
logger = logging.getLogger('wcs-olap')
logger.info('starting synchronizing w.c.s. at %r with ES at %s:%s', args.url, args.es_host,
args.es_port)
feeder = WcsOlapFeeder(api=api, schema=schema, db_url=args.db_url, logger=logger)
feeder.feed()
logger.info('finished')
if __name__ == '__main__':
main()