Squid url redirector

Annotation For squid-tagger.py
anonymous

Annotation For squid-tagger.py

Origin for each line in squid-tagger.py from check-in d301d9adc6:

d500448801 2009-10-05    1: #!/usr/bin/env python3.1
d500448801 2009-10-05    2: 
ae30851739 2010-08-12    3: import postgresql.api, re, sys
d500448801 2009-10-05    4: 
b93dc49210 2009-10-13    5: # wrapper around syslog, can be muted
d500448801 2009-10-05    6: class Logger:
d500448801 2009-10-05    7: 	__slots__ = frozenset(['_syslog'])
d500448801 2009-10-05    8: 
d500448801 2009-10-05    9: 	def __init__(self):
d500448801 2009-10-05   10: 		config.section('log')
d500448801 2009-10-05   11: 		if config['silent'] == 'yes':
d500448801 2009-10-05   12: 			self._syslog = None
d500448801 2009-10-05   13: 		else:
d500448801 2009-10-05   14: 			import syslog
d500448801 2009-10-05   15: 			self._syslog = syslog
d500448801 2009-10-05   16: 			self._syslog.openlog('squidTag')
d500448801 2009-10-05   17: 
d500448801 2009-10-05   18: 	def info(self, message):
4b22e25f24 2009-10-07   19: 		if self._syslog:
d500448801 2009-10-05   20: 			self._syslog.syslog(self._syslog.LOG_INFO, message)
d500448801 2009-10-05   21: 
d500448801 2009-10-05   22: 	def notice(self, message):
4b22e25f24 2009-10-07   23: 		if self._syslog:
d500448801 2009-10-05   24: 			self._syslog.syslog(self._syslog.LOG_NOTICE, message)
d500448801 2009-10-05   25: 
b93dc49210 2009-10-13   26: # wrapper around database
d500448801 2009-10-05   27: class tagDB:
d301d9adc6 2010-08-13   28: 	__slots__ = frozenset(('_check_stmt', '_db'))
b93dc49210 2009-10-13   29: 
b93dc49210 2009-10-13   30: 	def __init__(self):
9450c03d41 2010-08-07   31: 		config.section('database')
9450c03d41 2010-08-07   32: 		self._db = postgresql.open(
9450c03d41 2010-08-07   33: 			'pq://{}:{}@{}/{}'.format(
9450c03d41 2010-08-07   34: 				config['user'],
9450c03d41 2010-08-07   35: 				config['password'],
9450c03d41 2010-08-07   36: 				config['host'],
9450c03d41 2010-08-07   37: 				config['database'],
9450c03d41 2010-08-07   38: 		) )
ae30851739 2010-08-12   39: 		self._check_stmt = None
b93dc49210 2009-10-13   40: 
b93dc49210 2009-10-13   41: 	def check(self, site, ip_address):
ae30851739 2010-08-12   42: 		if self._check_stmt == None:
ae30851739 2010-08-12   43: 			self._check_stmt = self._db.prepare("select redirect_url, regexp from site_rule where site <@ tripdomain($1) and netmask >> $2::text::inet order by array_length(site, 1) desc")
b93dc49210 2009-10-13   44: 		return(self._check_stmt(site, ip_address))
ae30851739 2010-08-12   45: 
ae30851739 2010-08-12   46: 	def dump(self):
d301d9adc6 2010-08-13   47: 		return(self._db.prepare("select untrip(site), tag, regexp from urls natural join site natural join tag order by site, tag")())
31e69c4237 2010-08-12   48: 
31e69c4237 2010-08-12   49: 	def load(self, csv_data):
31e69c4237 2010-08-12   50: 		with self._db.xact():
31e69c4237 2010-08-12   51: 			if config.options.flush_db:
31e69c4237 2010-08-12   52: 				self._db.execute('delete from urls;')
31e69c4237 2010-08-12   53: 				if config.options.flush_site:
31e69c4237 2010-08-12   54: 					self._db.execute('delete from site;');
31e69c4237 2010-08-12   55: 			insertreg = self._db.prepare("select set($1, $2, $3)")
31e69c4237 2010-08-12   56: 			insert = self._db.prepare("select set($1, $2)")
31e69c4237 2010-08-12   57: 			for row in csv_data:
31e69c4237 2010-08-12   58: 				if len(row[2]) > 0:
31e69c4237 2010-08-12   59: 					insertreg(row[0], row[1], row[2])
31e69c4237 2010-08-12   60: 				else:
31e69c4237 2010-08-12   61: 					insert(row[0], row[1])
31e69c4237 2010-08-12   62: 		self._db.execute('vacuum analyze site;')
31e69c4237 2010-08-12   63: 		self._db.execute('vacuum analyze urls;')
d301d9adc6 2010-08-13   64: 
d301d9adc6 2010-08-13   65: 	def load_conf(self, csv_data):
d301d9adc6 2010-08-13   66: 		with self._db.xact():
d301d9adc6 2010-08-13   67: 			self._db.execute('delete from rules;')
d301d9adc6 2010-08-13   68: 			insertconf = self._db.prepare("insert into rules (netmask, redirect_url, from_weekday, to_weekday, from_time, to_time, id_tag) values ($1::text::cidr, $2, $3, $4, $5::text::time, $6::text::time, get_tag($7::text::text[]))")
d301d9adc6 2010-08-13   69: 			for row in csv_data:
d301d9adc6 2010-08-13   70: 				insertconf(row[0], row[1], int(row[2]), int(row[3]), row[4], row[5], row[6])
d301d9adc6 2010-08-13   71: 		self._db.execute('vacuum analyze rules;')
d301d9adc6 2010-08-13   72: 
d301d9adc6 2010-08-13   73: 	def dump_conf(self):
d301d9adc6 2010-08-13   74: 		return(self._db.prepare("select netmask, redirect_url, from_weekday, to_weekday, from_time, to_time, tag from rules natural join tag")())
b93dc49210 2009-10-13   75: 
b93dc49210 2009-10-13   76: # abstract class with basic checking functionality
b93dc49210 2009-10-13   77: class Checker:
ed7808827d 2009-10-14   78: 	__slots__ = frozenset(['_db', '_log'])
7e3418d94f 2009-10-12   79: 
7e3418d94f 2009-10-12   80: 	def __init__(self):
b93dc49210 2009-10-13   81: 		self._db = tagDB()
b93dc49210 2009-10-13   82: 		self._log = Logger()
7c13294e9f 2010-08-07   83: 		self._log.info('started\n')
b93dc49210 2009-10-13   84: 
ed7808827d 2009-10-14   85: 	def process(self, id, site, ip_address, url_path, line = None):
b93dc49210 2009-10-13   86: 		self._log.info('trying {}\n'.format(site))
b93dc49210 2009-10-13   87: 		result = self._db.check(site, ip_address)
b93dc49210 2009-10-13   88: 		reply = '-'
b93dc49210 2009-10-13   89: 		for row in result:
b93dc49210 2009-10-13   90: 			if row != None and row[0] != None:
b93dc49210 2009-10-13   91: 				if row[1] != None:
b93dc49210 2009-10-13   92: 					self._log.info('trying regexp "{}" versus "{}"\n'.format(row[1], url_path))
d2c54d0451 2010-03-01   93: 					try:
d2c54d0451 2010-03-01   94: 						if re.compile(row[1]).match(url_path):
1fa8a88371 2010-07-14   95: 							reply = row[0].format(url_path)
d2c54d0451 2010-03-01   96: 							break
d2c54d0451 2010-03-01   97: 						else:
d2c54d0451 2010-03-01   98: 							continue
d2c54d0451 2010-03-01   99: 					except:
d2c54d0451 2010-03-01  100: 						self._log.info("can't compile regexp")
b93dc49210 2009-10-13  101: 				else:
1fa8a88371 2010-07-14  102: 					reply = row[0].format(url_path)
b93dc49210 2009-10-13  103: 					break
b93dc49210 2009-10-13  104: 		self.writeline('{} {}\n'.format(id, reply))
7e3418d94f 2009-10-12  105: 
7e3418d94f 2009-10-12  106: 	def check(self, line):
7e3418d94f 2009-10-12  107: 		request = re.compile('^([0-9]+)\ (http|ftp):\/\/([-\w.:]+)\/([^ ]*)\ ([0-9.]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST).*$').match(line)
7e3418d94f 2009-10-12  108: 		if request:
7e3418d94f 2009-10-12  109: 			id = request.group(1)
7e3418d94f 2009-10-12  110: 			#proto = request.group(2)
7e3418d94f 2009-10-12  111: 			site = request.group(3)
7e3418d94f 2009-10-12  112: 			url_path = request.group(4)
7e3418d94f 2009-10-12  113: 			ip_address = request.group(5)
ed7808827d 2009-10-14  114: 			self.process(id, site, ip_address, url_path, line)
26fc9b34d9 2010-08-07  115: 			return(True)
7e3418d94f 2009-10-12  116: 		else:
7e3418d94f 2009-10-12  117: 			self._log.info('bad request\n')
b93dc49210 2009-10-13  118: 			self.writeline(line)
26fc9b34d9 2010-08-07  119: 			return(False)
b93dc49210 2009-10-13  120: 
b93dc49210 2009-10-13  121: 	def writeline(self, string):
b93dc49210 2009-10-13  122: 		self._log.info('sending: ' + string)
b93dc49210 2009-10-13  123: 		sys.stdout.write(string)
b93dc49210 2009-10-13  124: 		sys.stdout.flush()
b93dc49210 2009-10-13  125: 
ed7808827d 2009-10-14  126: 	def loop(self):
ed7808827d 2009-10-14  127: 		while True:
ed7808827d 2009-10-14  128: 			line = sys.stdin.readline()
ed7808827d 2009-10-14  129: 			if len(line) == 0:
ed7808827d 2009-10-14  130: 				break
ed7808827d 2009-10-14  131: 			self.check(line)
ed7808827d 2009-10-14  132: 
b93dc49210 2009-10-13  133: # threaded checking facility
b93dc49210 2009-10-13  134: class CheckerThread(Checker):
ed7808827d 2009-10-14  135: 	__slots__ = frozenset(['_lock', '_lock_exit', '_lock_queue', '_queue'])
b93dc49210 2009-10-13  136: 
b93dc49210 2009-10-13  137: 	def __init__(self):
ae30851739 2010-08-12  138: 		import _thread
ae30851739 2010-08-12  139: 
ed7808827d 2009-10-14  140: 		# basic initialisation
b93dc49210 2009-10-13  141: 		Checker.__init__(self)
ed7808827d 2009-10-14  142: 
b93dc49210 2009-10-13  143: 		# Spin lock. Loop acquires it on start then releases it when holding queue
b93dc49210 2009-10-13  144: 		# lock. This way the thread proceeds without stops while queue has data and
b93dc49210 2009-10-13  145: 		# gets stalled when no data present. The lock is released by queue writer
b93dc49210 2009-10-13  146: 		# after storing something into the queue
b93dc49210 2009-10-13  147: 		self._lock = _thread.allocate_lock()
ed7808827d 2009-10-14  148: 		self._lock_exit = _thread.allocate_lock()
b93dc49210 2009-10-13  149: 		self._lock_queue = _thread.allocate_lock()
b93dc49210 2009-10-13  150: 		self._lock.acquire()
b93dc49210 2009-10-13  151: 		self._queue = []
b93dc49210 2009-10-13  152: 		_thread.start_new_thread(self._start, ())
b93dc49210 2009-10-13  153: 
b93dc49210 2009-10-13  154: 	def _start(self):
b93dc49210 2009-10-13  155: 		while True:
b93dc49210 2009-10-13  156: 			self._lock.acquire()
ed7808827d 2009-10-14  157: 			with self._lock_queue:
ed7808827d 2009-10-14  158: 				# yes this should be written this way, and yes, this is why I hate threading
ed7808827d 2009-10-14  159: 				if len(self._queue) > 1:
ed7808827d 2009-10-14  160: 					if self._lock.locked():
ed7808827d 2009-10-14  161: 						self._lock.release()
ed7808827d 2009-10-14  162: 				req = self._queue.pop(0)
ed7808827d 2009-10-14  163: 			Checker.process(self, req[0], req[1], req[2], req[3])
ed7808827d 2009-10-14  164: 			with self._lock_queue:
ed7808827d 2009-10-14  165: 				if len(self._queue) == 0:
ed7808827d 2009-10-14  166: 					if self._lock_exit.locked():
ed7808827d 2009-10-14  167: 						self._lock_exit.release()
ed7808827d 2009-10-14  168: 
ed7808827d 2009-10-14  169: 	def process(self, id, site, ip_address, url_path, line):
ed7808827d 2009-10-14  170: 		with self._lock_queue:
ed7808827d 2009-10-14  171: 			self._queue.append((id, site, ip_address, url_path))
ed7808827d 2009-10-14  172: 			self._log.info('request {} queued ({})\n'.format(id, line))
ed7808827d 2009-10-14  173: 			if not self._lock_exit.locked():
ed7808827d 2009-10-14  174: 				self._lock_exit.acquire()
ed7808827d 2009-10-14  175: 			if self._lock.locked():
ed7808827d 2009-10-14  176: 				self._lock.release()
ed7808827d 2009-10-14  177: 
ed7808827d 2009-10-14  178: 	def loop(self):
ed7808827d 2009-10-14  179: 		while True:
ed7808827d 2009-10-14  180: 			line = sys.stdin.readline()
ed7808827d 2009-10-14  181: 			if len(line) == 0:
ed7808827d 2009-10-14  182: 				break
ed7808827d 2009-10-14  183: 			self.check(line)
ed7808827d 2009-10-14  184: 		self._lock_exit.acquire()
ed7808827d 2009-10-14  185: 
26fc9b34d9 2010-08-07  186: # kqueue enabled class for BSD's
ed7808827d 2009-10-14  187: class CheckerKqueue(Checker):
ed7808827d 2009-10-14  188: 	__slots__ = frozenset(['_kq', '_select', '_queue'])
ed7808827d 2009-10-14  189: 
ed7808827d 2009-10-14  190: 	def __init__(self):
ed7808827d 2009-10-14  191: 		# basic initialisation
ed7808827d 2009-10-14  192: 		Checker.__init__(self)
ed7808827d 2009-10-14  193: 
ed7808827d 2009-10-14  194: 		# importing select module
ed7808827d 2009-10-14  195: 		import select
ed7808827d 2009-10-14  196: 		self._select = select
ed7808827d 2009-10-14  197: 
ed7808827d 2009-10-14  198: 		# kreating kqueue
ed7808827d 2009-10-14  199: 		self._kq = self._select.kqueue()
7c13294e9f 2010-08-07  200: 		assert self._kq.fileno() != -1, "Fatal error: can't initialise kqueue."
ed7808827d 2009-10-14  201: 
ed7808827d 2009-10-14  202: 		# watching sys.stdin for data
ed7808827d 2009-10-14  203: 		self._kq.control([self._select.kevent(sys.stdin, self._select.KQ_FILTER_READ, self._select.KQ_EV_ADD)], 0)
ed7808827d 2009-10-14  204: 
ed7808827d 2009-10-14  205: 		# creating data queue
ed7808827d 2009-10-14  206: 		self._queue = []
ed7808827d 2009-10-14  207: 
ed7808827d 2009-10-14  208: 	def loop(self):
ed7808827d 2009-10-14  209: 		# Wait for data by default
ed7808827d 2009-10-14  210: 		timeout = None
26fc9b34d9 2010-08-07  211: 		eof = False
26fc9b34d9 2010-08-07  212: 		buffer = ''
ed7808827d 2009-10-14  213: 		while True:
26fc9b34d9 2010-08-07  214: 			# checking if there is any data or witing for data to arrive
ed7808827d 2009-10-14  215: 			kevs = self._kq.control(None, 1, timeout)
7c13294e9f 2010-08-07  216: 
ae1c0114c1 2010-08-09  217: 			for kev in kevs:
ae1c0114c1 2010-08-09  218: 				if kev.filter == self._select.KQ_FILTER_READ and kev.data > 0:
ae1c0114c1 2010-08-09  219: 					# reading data in
ae1c0114c1 2010-08-09  220: 					new_buffer = sys.stdin.read(kev.data)
ae1c0114c1 2010-08-09  221: 					# if no data was sent - we have reached end of file
ae1c0114c1 2010-08-09  222: 					if len(new_buffer) == 0:
ae1c0114c1 2010-08-09  223: 						eof = True
ae1c0114c1 2010-08-09  224: 					else:
ae1c0114c1 2010-08-09  225: 						# adding current buffer to old buffer remains
ae1c0114c1 2010-08-09  226: 						buffer += new_buffer
ae1c0114c1 2010-08-09  227: 						# splitting to lines
ae1c0114c1 2010-08-09  228: 						lines = buffer.split('\n')
ae1c0114c1 2010-08-09  229: 						# last line that was not terminate by newline returns to buffer
ae1c0114c1 2010-08-09  230: 						buffer = lines[-1]
ae1c0114c1 2010-08-09  231: 						# an only if there was at least one newline
ae1c0114c1 2010-08-09  232: 						if len(lines) > 1:
ae1c0114c1 2010-08-09  233: 							for line in lines[:-1]:
ae1c0114c1 2010-08-09  234: 								# add data to the queue
ae1c0114c1 2010-08-09  235: 								if self.check(line + '\n'):
ae1c0114c1 2010-08-09  236: 									# don't wait for more data, start processing
ae1c0114c1 2010-08-09  237: 									timeout = 0
ae1c0114c1 2010-08-09  238: 
ae1c0114c1 2010-08-09  239: 				# detect end of stream and exit if possible
ae1c0114c1 2010-08-09  240: 				if kev.flags >> 15 == 1:
ae1c0114c1 2010-08-09  241: 					self._kq.control([self._select.kevent(sys.stdin, self._select.KQ_FILTER_READ, self._select.KQ_EV_DELETE)], 0)
ae1c0114c1 2010-08-09  242: 					eof = True
ae1c0114c1 2010-08-09  243: 
ae1c0114c1 2010-08-09  244: 			if len(kevs) == 0:
7c13294e9f 2010-08-07  245: 				if len(self._queue) > 0:
7c13294e9f 2010-08-07  246: 					# get one request and process it
26fc9b34d9 2010-08-07  247: 					req = self._queue.pop(0)
26fc9b34d9 2010-08-07  248: 					Checker.process(self, req[0], req[1], req[2], req[3])
26fc9b34d9 2010-08-07  249: 					if len(self._queue) == 0:
26fc9b34d9 2010-08-07  250: 						# wait for data - we have nothing to process
26fc9b34d9 2010-08-07  251: 						timeout = None
7c13294e9f 2010-08-07  252: 
7c13294e9f 2010-08-07  253: 			# if queue is empty and we reached end of stream - we can exit
7c13294e9f 2010-08-07  254: 			if len(self._queue) == 0 and eof:
7c13294e9f 2010-08-07  255: 				break
ed7808827d 2009-10-14  256: 
ed7808827d 2009-10-14  257: 	def process(self, id, site, ip_address, url_path, line):
26fc9b34d9 2010-08-07  258: 		# simply adding data to the queue
ed7808827d 2009-10-14  259: 		self._queue.append((id, site, ip_address, url_path))
ed7808827d 2009-10-14  260: 		self._log.info('request {} queued ({})\n'.format(id, line))
7e3418d94f 2009-10-12  261: 
fc934cead1 2009-10-13  262: # this classes processes config file and substitutes default values
d500448801 2009-10-05  263: class Config:
ae30851739 2010-08-12  264: 	__slots__ = frozenset(['_config', '_default', '_section', 'options'])
b93dc49210 2009-10-13  265: 	_default = {
b93dc49210 2009-10-13  266: 		'reactor': {
b93dc49210 2009-10-13  267: 			'reactor': 'thread',
b93dc49210 2009-10-13  268: 		},
fc934cead1 2009-10-13  269: 		'log': {
fc934cead1 2009-10-13  270: 			'silent': 'no',
fc934cead1 2009-10-13  271: 		},
fc934cead1 2009-10-13  272: 		'database': {
fc934cead1 2009-10-13  273: 			'host': 'localhost',
fc934cead1 2009-10-13  274: 			'database': 'squidTag',
fc934cead1 2009-10-13  275: 	},}
d500448801 2009-10-05  276: 
fc934cead1 2009-10-13  277: 	# function to read in config file
d500448801 2009-10-05  278: 	def __init__(self):
ae30851739 2010-08-12  279: 		import configparser, optparse, os
ae30851739 2010-08-12  280: 
d500448801 2009-10-05  281: 		parser = optparse.OptionParser()
d500448801 2009-10-05  282: 		parser.add_option('-c', '--config', dest = 'config',
d500448801 2009-10-05  283: 			help = 'config file location', metavar = 'FILE',
d500448801 2009-10-05  284: 			default = '/usr/local/etc/squid-tagger.conf')
ae30851739 2010-08-12  285: 		parser.add_option('-d', '--dump', dest = 'dump',
ae30851739 2010-08-12  286: 			help = 'dump database', action = 'store_true', metavar = 'bool',
ae30851739 2010-08-12  287: 			default = False)
31e69c4237 2010-08-12  288: 		parser.add_option('-f', '--flush-database', dest = 'flush_db',
31e69c4237 2010-08-12  289: 			help = 'flush previous database on load', default = False,
31e69c4237 2010-08-12  290: 			action = 'store_true', metavar = 'bool')
31e69c4237 2010-08-12  291: 		parser.add_option('-F', '--flush-site', dest = 'flush_site',
31e69c4237 2010-08-12  292: 			help = 'when flushing previous dtabase flush site index too',
31e69c4237 2010-08-12  293: 			action = 'store_true', default = False, metavar = 'bool')
31e69c4237 2010-08-12  294: 		parser.add_option('-l', '--load', dest = 'load',
31e69c4237 2010-08-12  295: 			help = 'load database', action = 'store_true', metavar = 'bool',
31e69c4237 2010-08-12  296: 			default = False)
d301d9adc6 2010-08-13  297: 		parser.add_option('-D', '--dump-conf', dest = 'dump_conf',
d301d9adc6 2010-08-13  298: 			help = 'dump filtering rules', default = False, metavar = 'bool',
d301d9adc6 2010-08-13  299: 			action = 'store_true')
d301d9adc6 2010-08-13  300: 		parser.add_option('-L', '--load-conf', dest = 'load_conf',
d301d9adc6 2010-08-13  301: 			help = 'load filtering rules', default = False, metavar = 'bool',
d301d9adc6 2010-08-13  302: 			action = 'store_true')
d500448801 2009-10-05  303: 
ae30851739 2010-08-12  304: 		(self.options, args) = parser.parse_args()
d500448801 2009-10-05  305: 
ae30851739 2010-08-12  306: 		assert os.access(self.options.config, os.R_OK), "Fatal error: can't read {}".format(self.options.config)
d500448801 2009-10-05  307: 
d500448801 2009-10-05  308: 		self._config = configparser.ConfigParser()
ae30851739 2010-08-12  309: 		self._config.readfp(open(self.options.config))
d500448801 2009-10-05  310: 
fc934cead1 2009-10-13  311: 	# function to select config file section or create one
d500448801 2009-10-05  312: 	def section(self, section):
fc934cead1 2009-10-13  313: 		if not self._config.has_section(section):
fc934cead1 2009-10-13  314: 			self._config.add_section(section)
d500448801 2009-10-05  315: 		self._section = section
d500448801 2009-10-05  316: 
fc934cead1 2009-10-13  317: 	# function to get config parameter, if parameter doesn't exists the default
fc934cead1 2009-10-13  318: 	# value or None is substituted
d500448801 2009-10-05  319: 	def __getitem__(self, name):
fc934cead1 2009-10-13  320: 		if not self._config.has_option(self._section, name):
b93dc49210 2009-10-13  321: 			if self._section in self._default:
b93dc49210 2009-10-13  322: 				if name in self._default[self._section]:
fc934cead1 2009-10-13  323: 					self._config.set(self._section, name, self._default[self._section][name])
fc934cead1 2009-10-13  324: 				else:
fc934cead1 2009-10-13  325: 					self._config.set(self._section, name, None)
fc934cead1 2009-10-13  326: 			else:
fc934cead1 2009-10-13  327: 				self._config.set(self._section, name, None)
b93dc49210 2009-10-13  328: 		return(self._config.get(self._section, name))
d500448801 2009-10-05  329: 
fc934cead1 2009-10-13  330: # initializing and reading in config file
d500448801 2009-10-05  331: config = Config()
d500448801 2009-10-05  332: 
d301d9adc6 2010-08-13  333: if config.options.dump or config.options.load or config.options.dump_conf or config.options.load_conf:
d301d9adc6 2010-08-13  334: 	import csv
d301d9adc6 2010-08-13  335: 
d301d9adc6 2010-08-13  336: 	tagdb = tagDB()
d301d9adc6 2010-08-13  337: 	data_fields = ['site', 'tags', 'regexp']
d301d9adc6 2010-08-13  338: 	conf_fields = ['netmask', 'redirect_url', 'from_weekday', 'to_weekday', 'from_time', 'to_time', 'tag']
d301d9adc6 2010-08-13  339: 
d301d9adc6 2010-08-13  340: 	if config.options.dump or config.options.dump_conf:
d301d9adc6 2010-08-13  341: 		csv_writer = csv.writer(sys.stdout)
d301d9adc6 2010-08-13  342: 		if config.options.dump:
d301d9adc6 2010-08-13  343: 			# dumping database
d301d9adc6 2010-08-13  344: 			csv_writer.writerow(data_fields)
d301d9adc6 2010-08-13  345: 			for row in tagdb.dump():
d301d9adc6 2010-08-13  346: 				csv_writer.writerow([row[0], '{' + ','.join(row[1]) + '}', row[2]])
d301d9adc6 2010-08-13  347: 
d301d9adc6 2010-08-13  348: 		elif config.options.dump_conf:
d301d9adc6 2010-08-13  349: 			# dumping rules
d301d9adc6 2010-08-13  350: 			csv_writer.writerow(conf_fields)
d301d9adc6 2010-08-13  351: 			for row in tagdb.dump_conf():
d301d9adc6 2010-08-13  352: 				csv_writer.writerow([row[0], row[1], row[2], row[3], row[4], row[5], '{' + ','.join(row[6]) + '}'])
d301d9adc6 2010-08-13  353: 
d301d9adc6 2010-08-13  354: 	elif config.options.load or config.options.load_conf:
d301d9adc6 2010-08-13  355: 		csv_reader = csv.reader(sys.stdin)
d301d9adc6 2010-08-13  356: 		first_row = next(csv_reader)
d301d9adc6 2010-08-13  357: 
d301d9adc6 2010-08-13  358: 		if config.options.load:
d301d9adc6 2010-08-13  359: 			# loading database
d301d9adc6 2010-08-13  360: 			assert first_row == data_fields, 'File must contain csv data with theese columns: ' + data_fields
d301d9adc6 2010-08-13  361: 
d301d9adc6 2010-08-13  362: 			tagdb.load(csv_reader)
d301d9adc6 2010-08-13  363: 
d301d9adc6 2010-08-13  364: 		elif config.options.load_conf:
d301d9adc6 2010-08-13  365: 			# loading database
d301d9adc6 2010-08-13  366: 			assert first_row == conf_fields, 'File must contain csv data with theese columns: ' + conf_fields
d301d9adc6 2010-08-13  367: 
d301d9adc6 2010-08-13  368: 			tagdb.load_conf(csv_reader)
ae30851739 2010-08-12  369: 
ae30851739 2010-08-12  370: else:
ae30851739 2010-08-12  371: 	# main loop
ae30851739 2010-08-12  372: 	config.section('reactor')
ae30851739 2010-08-12  373: 	if config['reactor'] == 'thread':
ae30851739 2010-08-12  374: 		checker = CheckerThread()
ae30851739 2010-08-12  375: 	elif config['reactor'] == 'plain':
ae30851739 2010-08-12  376: 		checker = Checker()
ae30851739 2010-08-12  377: 	elif config['reactor'] == 'kqueue':
ae30851739 2010-08-12  378: 		checker = CheckerKqueue()
ae30851739 2010-08-12  379: 
ae30851739 2010-08-12  380: 	checker.loop()