Index: squid-tagger.py ================================================================== --- squid-tagger.py +++ squid-tagger.py @@ -169,16 +169,23 @@ class tagDB(object): __slots__ = frozenset(['_cursor', '_db']) def __init__(self): config.section('database') - self._db = psycopg2.connect( - database = config['database'], - host = config['host'], - user = config['user'], - password = config['password'], - ) + if config['host'] == None: + self._db = psycopg2.connect( + database = config['database'], + user = config['user'], + password = config['password'] + ) + else: + self._db = psycopg2.connect( + database = config['database'], + host = config['host'], + user = config['user'], + password = config['password'] + ) self._cursor = self._db.cursor() def _field_names(self): names = [] for record in self._cursor.description: @@ -277,10 +284,75 @@ def loop(self): pool = gevent.pool.Pool() pool.spawn(self.check) pool.join() + +# this classes processes config file and substitutes default values +class Config: + __slots__ = frozenset(['_config', '_default', '_section', 'options']) + _default = { + 'log': { + 'silent': 'no', + }, + 'database': { + 'host': None, + 'database': 'squidTag', + },} + + # function to read in config file + def __init__(self): + import ConfigParser, optparse, os + + parser = optparse.OptionParser() + parser.add_option('-c', '--config', dest = 'config', + help = 'config file location', metavar = 'FILE', + default = '/usr/local/etc/squid-tagger.conf') + parser.add_option('-d', '--dump', dest = 'dump', + help = 'dump database', action = 'store_true', metavar = 'bool', + default = False) + parser.add_option('-f', '--flush-database', dest = 'flush_db', + help = 'flush previous database on load', default = False, + action = 'store_true', metavar = 'bool') + parser.add_option('-l', '--load', dest = 'load', + help = 'load database', action = 'store_true', metavar = 'bool', + default = False) + parser.add_option('-D', '--dump-conf', dest = 'dump_conf', + help = 'dump filtering rules', default = False, metavar = 'bool', + action = 'store_true') + parser.add_option('-L', '--load-conf', dest = 'load_conf', + help = 'load filtering rules', default = False, metavar = 'bool', + action = 'store_true') + + (self.options, args) = parser.parse_args() + + assert os.access(self.options.config, os.R_OK), "Fatal error: can't read {}".format(self.options.config) + + self._config = ConfigParser.ConfigParser() + self._config.readfp(open(self.options.config)) + + # function to select config file section or create one + def section(self, section): + if not self._config.has_section(section): + self._config.add_section(section) + self._section = section + + # function to get config parameter, if parameter doesn't exists the default + # value or None is substituted + def __getitem__(self, name): + if not self._config.has_option(self._section, name): + if self._section in self._default: + if name in self._default[self._section]: + self._config.set(self._section, name, self._default[self._section][name]) + else: + self._config.set(self._section, name, None) + else: + self._config.set(self._section, name, None) + return(self._config.get(self._section, name)) + +# initializing and reading in config file +config = Config() if config.options.dump or config.options.load or config.options.dump_conf or config.options.load_conf: import csv tagdb = tagDB()