0ef24b1937 2011-04-06 1: #!/usr/bin/env python
0ef24b1937 2011-04-06 2:
0ef24b1937 2011-04-06 3: from __future__ import division, print_function, unicode_literals
0ef24b1937 2011-04-06 4:
0ef24b1937 2011-04-06 5: import gevent.monkey
0ef24b1937 2011-04-06 6: gevent.monkey.patch_all()
0ef24b1937 2011-04-06 7:
0ef24b1937 2011-04-06 8: import fcntl, gevent.core, gevent.pool, gevent.queue, gevent.socket, os, psycopg2, re, sys
0ef24b1937 2011-04-06 9:
0ef24b1937 2011-04-06 10: # //inclusion start
0ef24b1937 2011-04-06 11: # Copyright (C) 2010 Daniele Varrazzo <daniele.varrazzo@gmail.com>
0ef24b1937 2011-04-06 12: # and licensed under the MIT license:
0ef24b1937 2011-04-06 13:
0ef24b1937 2011-04-06 14: def gevent_wait_callback(conn, timeout=None):
0ef24b1937 2011-04-06 15: """A wait callback useful to allow gevent to work with Psycopg."""
0ef24b1937 2011-04-06 16: while 1:
0ef24b1937 2011-04-06 17: state = conn.poll()
0ef24b1937 2011-04-06 18: if state == psycopg2.extensions.POLL_OK:
0ef24b1937 2011-04-06 19: break
0ef24b1937 2011-04-06 20: elif state == psycopg2.extensions.POLL_READ:
0ef24b1937 2011-04-06 21: gevent.socket.wait_read(conn.fileno(), timeout=timeout)
0ef24b1937 2011-04-06 22: elif state == psycopg2.extensions.POLL_WRITE:
0ef24b1937 2011-04-06 23: gevent.socket.wait_write(conn.fileno(), timeout=timeout)
0ef24b1937 2011-04-06 24: else:
0ef24b1937 2011-04-06 25: raise psycopg2.OperationalError("Bad result from poll: %r" % state)
0ef24b1937 2011-04-06 26:
0ef24b1937 2011-04-06 27: if not hasattr(psycopg2.extensions, 'set_wait_callback'):
0ef24b1937 2011-04-06 28: raise ImportError("support for coroutines not available in this Psycopg version (%s)" % psycopg2.__version__)
0ef24b1937 2011-04-06 29: psycopg2.extensions.set_wait_callback(gevent_wait_callback)
0ef24b1937 2011-04-06 30:
0ef24b1937 2011-04-06 31: # //inclusion end
fc934cead1 2009-10-13 32:
fc934cead1 2009-10-13 33: # this classes processes config file and substitutes default values
fc934cead1 2009-10-13 34: class Config:
ae30851739 2010-08-12 35: __slots__ = frozenset(['_config', '_default', '_section', 'options'])
b93dc49210 2009-10-13 36: _default = {
fc934cead1 2009-10-13 37: 'log': {
fc934cead1 2009-10-13 38: 'silent': 'no',
fc934cead1 2009-10-13 39: },
fc934cead1 2009-10-13 40: 'database': {
fc934cead1 2009-10-13 41: 'database': 'squidTag',
fc934cead1 2009-10-13 42: },}
fc934cead1 2009-10-13 43:
fc934cead1 2009-10-13 44: # function to read in config file
fc934cead1 2009-10-13 45: def __init__(self):
0ef24b1937 2011-04-06 46: import ConfigParser, optparse, os
ae30851739 2010-08-12 47:
d500448801 2009-10-05 48: parser = optparse.OptionParser()
d500448801 2009-10-05 49: parser.add_option('-c', '--config', dest = 'config',
d500448801 2009-10-05 50: help = 'config file location', metavar = 'FILE',
d500448801 2009-10-05 51: default = '/usr/local/etc/squid-tagger.conf')
ae30851739 2010-08-12 52: parser.add_option('-d', '--dump', dest = 'dump',
ae30851739 2010-08-12 53: help = 'dump database', action = 'store_true', metavar = 'bool',
ae30851739 2010-08-12 54: default = False)
31e69c4237 2010-08-12 55: parser.add_option('-f', '--flush-database', dest = 'flush_db',
31e69c4237 2010-08-12 56: help = 'flush previous database on load', default = False,
31e69c4237 2010-08-12 57: action = 'store_true', metavar = 'bool')
31e69c4237 2010-08-12 58: parser.add_option('-l', '--load', dest = 'load',
31e69c4237 2010-08-12 59: help = 'load database', action = 'store_true', metavar = 'bool',
31e69c4237 2010-08-12 60: default = False)
d301d9adc6 2010-08-13 61: parser.add_option('-D', '--dump-conf', dest = 'dump_conf',
d301d9adc6 2010-08-13 62: help = 'dump filtering rules', default = False, metavar = 'bool',
d301d9adc6 2010-08-13 63: action = 'store_true')
d301d9adc6 2010-08-13 64: parser.add_option('-L', '--load-conf', dest = 'load_conf',
d301d9adc6 2010-08-13 65: help = 'load filtering rules', default = False, metavar = 'bool',
d301d9adc6 2010-08-13 66: action = 'store_true')
7c13294e9f 2010-08-07 67:
ae30851739 2010-08-12 68: (self.options, args) = parser.parse_args()
7c13294e9f 2010-08-07 69:
ae30851739 2010-08-12 70: assert os.access(self.options.config, os.R_OK), "Fatal error: can't read {}".format(self.options.config)
7c13294e9f 2010-08-07 71:
0ef24b1937 2011-04-06 72: self._config = ConfigParser.ConfigParser()
ae30851739 2010-08-12 73: self._config.readfp(open(self.options.config))
fc934cead1 2009-10-13 74:
fc934cead1 2009-10-13 75: # function to select config file section or create one
d500448801 2009-10-05 76: def section(self, section):
fc934cead1 2009-10-13 77: if not self._config.has_section(section):
fc934cead1 2009-10-13 78: self._config.add_section(section)
d500448801 2009-10-05 79: self._section = section
d500448801 2009-10-05 80:
fc934cead1 2009-10-13 81: # function to get config parameter, if parameter doesn't exists the default
fc934cead1 2009-10-13 82: # value or None is substituted
d500448801 2009-10-05 83: def __getitem__(self, name):
fc934cead1 2009-10-13 84: if not self._config.has_option(self._section, name):
b93dc49210 2009-10-13 85: if self._section in self._default:
b93dc49210 2009-10-13 86: if name in self._default[self._section]:
fc934cead1 2009-10-13 87: self._config.set(self._section, name, self._default[self._section][name])
fc934cead1 2009-10-13 88: else:
fc934cead1 2009-10-13 89: self._config.set(self._section, name, None)
fc934cead1 2009-10-13 90: else:
fc934cead1 2009-10-13 91: self._config.set(self._section, name, None)
b93dc49210 2009-10-13 92: return(self._config.get(self._section, name))
d500448801 2009-10-05 93:
fc934cead1 2009-10-13 94: # initializing and reading in config file
d500448801 2009-10-05 95: config = Config()
d500448801 2009-10-05 96:
fad48b740c 2012-07-07 97: import logging, logging.handlers
2654b86697 2012-07-09 98:
2654b86697 2012-07-09 99: # wrapper around logging handler to make it queue records and don't stall when sending them
2654b86697 2012-07-09 100:
2654b86697 2012-07-09 101: class SysLogHandlerQueue(logging.handlers.SysLogHandler):
67e8b3309d 2012-07-09 102: __slots__ = frozenset(['_running', '_tail', '_worker'])
2654b86697 2012-07-09 103:
2654b86697 2012-07-09 104: def __init__(self):
2654b86697 2012-07-09 105: logging.handlers.SysLogHandler.__init__(self, '/dev/log')
2654b86697 2012-07-09 106: self._tail = gevent.queue.Queue()
67e8b3309d 2012-07-09 107: self._worker = None
2654b86697 2012-07-09 108:
2654b86697 2012-07-09 109: def emit(self, record):
46225bbe65 2013-01-24 110: try:
46225bbe65 2013-01-24 111: self._tail.put(record)
46225bbe65 2013-01-24 112: except (KeyboardInterrupt, SystemExit):
46225bbe65 2013-01-24 113: raise
46225bbe65 2013-01-24 114: except:
46225bbe65 2013-01-24 115: self.handleError(record)
67e8b3309d 2012-07-09 116: if self._worker == None:
2654b86697 2012-07-09 117: # in case queue is empty we will spawn new worker
2654b86697 2012-07-09 118: # all workers are logged so we can kill them on close()
67e8b3309d 2012-07-09 119: self._worker = gevent.spawn(self._writer)
2654b86697 2012-07-09 120:
2654b86697 2012-07-09 121: def _writer(self):
2654b86697 2012-07-09 122: # here we are locking the queue so we can be sure we are the only one
2654b86697 2012-07-09 123: while not self._tail.empty():
2654b86697 2012-07-09 124: logging.handlers.SysLogHandler.emit(self, self._tail.get())
67e8b3309d 2012-07-09 125: self._worker = None
2654b86697 2012-07-09 126:
2654b86697 2012-07-09 127: def close(self):
67e8b3309d 2012-07-09 128: if self._worker != None:
67e8b3309d 2012-07-09 129: gevent.kill(self._worker)
2654b86697 2012-07-09 130: logging.handlers.SysLogHandler.close(self)
2654b86697 2012-07-09 131:
fad48b740c 2012-07-07 132: logger = logging.getLogger('squidTag')
fad48b740c 2012-07-07 133: logger.setLevel(logging.INFO)
2654b86697 2012-07-09 134: handler = SysLogHandlerQueue()
fad48b740c 2012-07-07 135: handler.setFormatter(logging.Formatter(str('squidTag[%(process)s]: %(message)s')))
fad48b740c 2012-07-07 136: logger.addHandler(handler)
39b97ced92 2011-06-05 137:
39b97ced92 2011-06-05 138: # tiny wrapper around a file to make reads from it geventable
39b97ced92 2011-06-05 139: # or should i move this somewhere?
39b97ced92 2011-06-05 140:
39b97ced92 2011-06-05 141: class FReadlineQueue(gevent.queue.Queue):
9d7d80e594 2013-11-28 142: # storing fileno descriptor, leftover
9d7d80e594 2013-11-28 143: __slots__ = frozenset(['_fn', '_tail'])
39b97ced92 2011-06-05 144:
9d7d80e594 2013-11-28 145: def __init__(self, fd):
39b97ced92 2011-06-05 146: # initialising class
39b97ced92 2011-06-05 147: gevent.queue.Queue.__init__(self)
9d7d80e594 2013-11-28 148: self._fn = fd.fileno()
39b97ced92 2011-06-05 149: # using empty tail
39b97ced92 2011-06-05 150: self._tail = ''
39b97ced92 2011-06-05 151: # putting file to nonblocking mode
9d7d80e594 2013-11-28 152: gevent.os.make_nonblocking(fd)
9d7d80e594 2013-11-28 153: # starting main loop
9d7d80e594 2013-11-28 154: gevent.spawn(self._frobber)
9d7d80e594 2013-11-28 155:
9d7d80e594 2013-11-28 156: def _frobber(self):
9d7d80e594 2013-11-28 157: while True:
9d7d80e594 2013-11-28 158: # reading one buffer from stream
9d7d80e594 2013-11-28 159: buf = gevent.os.nb_read(self._fn, 4096)
9d7d80e594 2013-11-28 160: # EOF found
9d7d80e594 2013-11-28 161: if len(buf) == 0:
9d7d80e594 2013-11-28 162: break
9d7d80e594 2013-11-28 163: # splitting stream by line ends
9d7d80e594 2013-11-28 164: rows = buf.decode('l1').split('\n')
9d7d80e594 2013-11-28 165: # adding tail to the first element if there is some tail
9d7d80e594 2013-11-28 166: if len(self._tail) > 0:
9d7d80e594 2013-11-28 167: rows[0] = self._tail + rows[0]
9d7d80e594 2013-11-28 168: # popping out last (incomplete) element
9d7d80e594 2013-11-28 169: self._tail = rows.pop(-1)
9d7d80e594 2013-11-28 170: # dropping all complete elements to the queue
9d7d80e594 2013-11-28 171: for row in rows:
9d7d80e594 2013-11-28 172: self.put_nowait(row)
9d7d80e594 2013-11-28 173: logger.info('< ' + row)
9d7d80e594 2013-11-28 174: # sending EOF
9d7d80e594 2013-11-28 175: self.put_nowait(None)
9d7d80e594 2013-11-28 176:
9d7d80e594 2013-11-28 177: stdin = FReadlineQueue(sys.stdin)
d823fa83dd 2012-07-07 178:
2654b86697 2012-07-09 179: # wrapper against file handler that makes possible to queue some writes without stalling
2654b86697 2012-07-09 180:
d823fa83dd 2012-07-07 181: class FWritelineQueue(gevent.queue.JoinableQueue):
9d7d80e594 2013-11-28 182: # storing fileno, leftover
9d7d80e594 2013-11-28 183: __slots__ = frozenset(['_fn', '_tail'])
d823fa83dd 2012-07-07 184:
9d7d80e594 2013-11-28 185: def __init__(self, fd):
d823fa83dd 2012-07-07 186: # initialising class
d823fa83dd 2012-07-07 187: gevent.queue.JoinableQueue.__init__(self)
d823fa83dd 2012-07-07 188: # storing fileno
9d7d80e594 2013-11-28 189: self._fn = fd.fileno()
9d7d80e594 2013-11-28 190: # putting file to nonblocking mode
9d7d80e594 2013-11-28 191: gevent.os.make_nonblocking(fd)
d823fa83dd 2012-07-07 192: # using empty tail
d823fa83dd 2012-07-07 193: self._tail = None
d823fa83dd 2012-07-07 194:
d823fa83dd 2012-07-07 195: def __del__(self):
d823fa83dd 2012-07-07 196: # purge queue before deleting
d823fa83dd 2012-07-07 197: if not self.empty():
d823fa83dd 2012-07-07 198: self.join()
d823fa83dd 2012-07-07 199:
d823fa83dd 2012-07-07 200: def put(self, item, block=True, timeout=None):
d823fa83dd 2012-07-07 201: # calling real put
d823fa83dd 2012-07-07 202: gevent.queue.JoinableQueue.put(self, item, block, timeout)
9d7d80e594 2013-11-28 203: # starting main loop
9d7d80e594 2013-11-28 204: gevent.spawn(self._frobber)
d823fa83dd 2012-07-07 205:
9d7d80e594 2013-11-28 206: def _frobber(self):
d823fa83dd 2012-07-07 207: # checking leftover
d823fa83dd 2012-07-07 208: while True:
d823fa83dd 2012-07-07 209: if self._tail == None:
d823fa83dd 2012-07-07 210: try:
d823fa83dd 2012-07-07 211: self._tail = str(self.get_nowait()).encode('utf-8') + '\n'
d823fa83dd 2012-07-07 212: except gevent.queue.Empty:
d823fa83dd 2012-07-07 213: self._tail = None
d823fa83dd 2012-07-07 214: return
d823fa83dd 2012-07-07 215: # writing tail
9d7d80e594 2013-11-28 216: written = gevent.os.nb_write(self._fn, self._tail)
d823fa83dd 2012-07-07 217: length = len(self._tail)
d823fa83dd 2012-07-07 218: if written == length:
d823fa83dd 2012-07-07 219: self._tail = None
d823fa83dd 2012-07-07 220: elif written < length:
d823fa83dd 2012-07-07 221: self._tail = self._tail[written:]
39b97ced92 2011-06-05 222:
39b97ced92 2011-06-05 223: # wrapper around database
39b97ced92 2011-06-05 224: class tagDB(object):
39b97ced92 2011-06-05 225: __slots__ = frozenset(['_cursor', '_db'])
39b97ced92 2011-06-05 226:
39b97ced92 2011-06-05 227: def __init__(self):
39b97ced92 2011-06-05 228: config.section('database')
d2c7ba18a4 2011-09-14 229: if config['host'] == None:
d2c7ba18a4 2011-09-14 230: self._db = psycopg2.connect(
d2c7ba18a4 2011-09-14 231: database = config['database'],
d2c7ba18a4 2011-09-14 232: user = config['user'],
d2c7ba18a4 2011-09-14 233: password = config['password']
d2c7ba18a4 2011-09-14 234: )
d2c7ba18a4 2011-09-14 235: else:
d2c7ba18a4 2011-09-14 236: self._db = psycopg2.connect(
d2c7ba18a4 2011-09-14 237: database = config['database'],
d2c7ba18a4 2011-09-14 238: host = config['host'],
d2c7ba18a4 2011-09-14 239: user = config['user'],
d2c7ba18a4 2011-09-14 240: password = config['password']
d2c7ba18a4 2011-09-14 241: )
39b97ced92 2011-06-05 242: self._cursor = self._db.cursor()
39b97ced92 2011-06-05 243:
39b97ced92 2011-06-05 244: def _field_names(self):
39b97ced92 2011-06-05 245: names = []
39b97ced92 2011-06-05 246: for record in self._cursor.description:
39b97ced92 2011-06-05 247: names.append(record.name)
39b97ced92 2011-06-05 248: return(names)
39b97ced92 2011-06-05 249:
39b97ced92 2011-06-05 250: def check(self, site, ip_address):
39b97ced92 2011-06-05 251: self._cursor.execute("select * from (select redirect_url, regexp from site_rule where site <@ tripdomain(%s) and netmask >>= %s order by array_length(site, 1) desc) a group by redirect_url, regexp", [site, ip_address])
39b97ced92 2011-06-05 252: return(self._cursor.fetchall())
39b97ced92 2011-06-05 253:
39b97ced92 2011-06-05 254: def dump(self):
39b97ced92 2011-06-05 255: self._cursor.execute("select untrip(site) as site, tag::text, regexp from urls order by site, tag")
39b97ced92 2011-06-05 256: return(self._field_names(), self._cursor.fetchall())
39b97ced92 2011-06-05 257:
39b97ced92 2011-06-05 258: def load(self, data):
39b97ced92 2011-06-05 259: if config.options.flush_db:
39b97ced92 2011-06-05 260: self._cursor.execute('delete from urls;')
39b97ced92 2011-06-05 261: bundle = []
39b97ced92 2011-06-05 262: for row in data:
39b97ced92 2011-06-05 263: if len(row) == 2:
39b97ced92 2011-06-05 264: bundle.append([row[0], row[1], None])
39b97ced92 2011-06-05 265: else:
39b97ced92 2011-06-05 266: bundle.append([row[0], row[1], row[2]])
39b97ced92 2011-06-05 267: self._cursor.executemany("insert into urls (site, tag, regexp) values (tripdomain(%s), %s, %s)", bundle)
39b97ced92 2011-06-05 268: self._cursor.execute("update urls set regexp = NULL where regexp = ''")
39b97ced92 2011-06-05 269: self._db.commit()
39b97ced92 2011-06-05 270:
39b97ced92 2011-06-05 271: def load_conf(self, csv_data):
39b97ced92 2011-06-05 272: self._cursor.execute('delete from rules;')
39b97ced92 2011-06-05 273: bundle = []
39b97ced92 2011-06-05 274: for row in csv_data:
39b97ced92 2011-06-05 275: bundle.append([row[0], row[1], int(row[2]), int(row[3]), row[4], row[5], row[6]])
39b97ced92 2011-06-05 276: self._cursor.executemany("insert into rules (netmask, redirect_url, from_weekday, to_weekday, from_time, to_time, tag) values (%s::text::cidr, %s, %s, %s, %s::text::time, %s::text::time, %s::text::text[])", bundle)
39b97ced92 2011-06-05 277: self._db.commit()
39b97ced92 2011-06-05 278:
39b97ced92 2011-06-05 279: def dump_conf(self):
39b97ced92 2011-06-05 280: self._cursor.execute("select netmask, redirect_url, from_weekday, to_weekday, from_time, to_time, tag::text from rules")
39b97ced92 2011-06-05 281: return(self._field_names(), self._cursor.fetchall())
39b97ced92 2011-06-05 282:
39b97ced92 2011-06-05 283: # abstract class with basic checking functionality
39b97ced92 2011-06-05 284: class Checker(object):
d823fa83dd 2012-07-07 285: __slots__ = frozenset(['_db', '_log', '_queue', '_request', '_stdout'])
39b97ced92 2011-06-05 286:
39b97ced92 2011-06-05 287: def __init__(self, queue, logger):
39b97ced92 2011-06-05 288: self._db = tagDB()
39b97ced92 2011-06-05 289: self._log = logger
2654b86697 2012-07-09 290: self._log.info('started')
25bd939a42 2013-08-07 291: self._request = re.compile('^([0-9]+)\ ((http|ftp):\/\/)?([-\w.]+)(:[0-9]+)?(\/([^ ]*))?\ ([0-9.:]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST|CONNECT).*$')
39b97ced92 2011-06-05 292: self._queue = queue
9d7d80e594 2013-11-28 293: self._stdout = FWritelineQueue(sys.stdout)
39b97ced92 2011-06-05 294:
39b97ced92 2011-06-05 295: def process(self, id, site, ip_address, url_path, line = None):
2654b86697 2012-07-09 296: #self._log.info('trying {}'.format(site))
39b97ced92 2011-06-05 297: result = self._db.check(site, ip_address)
39b97ced92 2011-06-05 298: reply = None
2654b86697 2012-07-09 299: #self._log.info('got {} lines from database'.format(len(result)))
39b97ced92 2011-06-05 300: for row in result:
39b97ced92 2011-06-05 301: if row != None and row[0] != None:
25bd939a42 2013-08-07 302: if row[1] != None and url_path != None:
2654b86697 2012-07-09 303: self._log.info('trying regexp "{}" versus "{}"'.format(row[1], url_path))
39b97ced92 2011-06-05 304: try:
39b97ced92 2011-06-05 305: if re.compile(row[1]).match(url_path):
a326d03ba1 2013-03-13 306: reply = row[0].format(host = site, path = url_path)
39b97ced92 2011-06-05 307: else:
39b97ced92 2011-06-05 308: continue
39b97ced92 2011-06-05 309: except:
25bd939a42 2013-08-07 310: self._log.info("can't compile or execute regexp")
39b97ced92 2011-06-05 311: else:
a326d03ba1 2013-03-13 312: reply = row[0].format(host = site, path = url_path)
39b97ced92 2011-06-05 313: if reply != None:
d823fa83dd 2012-07-07 314: self.writeline('{} {}'.format(id, reply))
39b97ced92 2011-06-05 315: return(True)
d823fa83dd 2012-07-07 316: self.writeline('{}'.format(id))
39b97ced92 2011-06-05 317:
2654b86697 2012-07-09 318: def loop(self):
39b97ced92 2011-06-05 319: while True:
39b97ced92 2011-06-05 320: line = self._queue.get()
39b97ced92 2011-06-05 321: if line == None:
39b97ced92 2011-06-05 322: break
2654b86697 2012-07-09 323: #self._log.info('request: ' + line)
39b97ced92 2011-06-05 324: request = self._request.match(line)
39b97ced92 2011-06-05 325: if request:
39b97ced92 2011-06-05 326: id = request.group(1)
25bd939a42 2013-08-07 327: #proto = request.group(3)
25bd939a42 2013-08-07 328: site = request.group(4)
25bd939a42 2013-08-07 329: url_path = request.group(7)
25bd939a42 2013-08-07 330: ip_address = request.group(8)
39b97ced92 2011-06-05 331: self.process(id, site, ip_address, url_path, line)
39b97ced92 2011-06-05 332: else:
2654b86697 2012-07-09 333: self._log.info('bad request')
d823fa83dd 2012-07-07 334: self.writeline(line)
39b97ced92 2011-06-05 335:
39b97ced92 2011-06-05 336: def writeline(self, string):
2654b86697 2012-07-09 337: self._log.info('> ' + string)
d823fa83dd 2012-07-07 338: self._stdout.put(string)
39b97ced92 2011-06-05 339:
d301d9adc6 2010-08-13 340: if config.options.dump or config.options.load or config.options.dump_conf or config.options.load_conf:
d301d9adc6 2010-08-13 341: import csv
d301d9adc6 2010-08-13 342:
d301d9adc6 2010-08-13 343: tagdb = tagDB()
bde51dc0c7 2010-08-26 344: data_fields = ['site', 'tag', 'regexp']
d301d9adc6 2010-08-13 345: conf_fields = ['netmask', 'redirect_url', 'from_weekday', 'to_weekday', 'from_time', 'to_time', 'tag']
d301d9adc6 2010-08-13 346:
d301d9adc6 2010-08-13 347: if config.options.dump or config.options.dump_conf:
0ef24b1937 2011-04-06 348: csv_writer = csv.writer(sys.stdout)
d301d9adc6 2010-08-13 349: if config.options.dump:
bde51dc0c7 2010-08-26 350: dump = tagdb.dump()
bde51dc0c7 2010-08-26 351: elif config.options.dump_conf:
bde51dc0c7 2010-08-26 352: dump = tagdb.dump_conf()
bde51dc0c7 2010-08-26 353:
0ef24b1937 2011-04-06 354: csv_writer.writerow(dump[0])
0ef24b1937 2011-04-06 355: for line in dump[1]:
0ef24b1937 2011-04-06 356: csv_writer.writerow(line)
d301d9adc6 2010-08-13 357:
d301d9adc6 2010-08-13 358: elif config.options.load or config.options.load_conf:
d301d9adc6 2010-08-13 359: csv_reader = csv.reader(sys.stdin)
d301d9adc6 2010-08-13 360: first_row = next(csv_reader)
d301d9adc6 2010-08-13 361:
d301d9adc6 2010-08-13 362: if config.options.load:
bde51dc0c7 2010-08-26 363: fields = data_fields
bde51dc0c7 2010-08-26 364: load = tagdb.load
bde51dc0c7 2010-08-26 365: elif config.options.load_conf:
bde51dc0c7 2010-08-26 366: fields = conf_fields
bde51dc0c7 2010-08-26 367: load = tagdb.load_conf
bde51dc0c7 2010-08-26 368:
bde51dc0c7 2010-08-26 369: assert first_row == fields, 'File must contain csv data with theese columns: ' + repr(fields)
bde51dc0c7 2010-08-26 370: load(csv_reader)
d301d9adc6 2010-08-13 371:
d301d9adc6 2010-08-13 372: else:
d301d9adc6 2010-08-13 373: # main loop
39b97ced92 2011-06-05 374: Checker(stdin, logger).loop()