Squid url redirector

Annotation For st-dump.py
anonymous

Annotation For st-dump.py

Origin for each line in st-dump.py from check-in c27c7eb208:

f1bafd194a 2010-03-18    1: #!/usr/bin/env python3.1
f1bafd194a 2010-03-18    2: 
f1bafd194a 2010-03-18    3: import configparser, csv, optparse, os, postgresql.api, sys
f1bafd194a 2010-03-18    4: 
f1bafd194a 2010-03-18    5: # wrapper around syslog, can be muted
f1bafd194a 2010-03-18    6: class Logger:
f1bafd194a 2010-03-18    7: 	__slots__ = frozenset(['_syslog'])
f1bafd194a 2010-03-18    8: 
f1bafd194a 2010-03-18    9: 	def __init__(self):
f1bafd194a 2010-03-18   10: 		config.section('log')
f1bafd194a 2010-03-18   11: 		if config['silent'] == 'yes':
f1bafd194a 2010-03-18   12: 			self._syslog = None
f1bafd194a 2010-03-18   13: 		else:
f1bafd194a 2010-03-18   14: 			import syslog
f1bafd194a 2010-03-18   15: 			self._syslog = syslog
f1bafd194a 2010-03-18   16: 			self._syslog.openlog('squidTag')
f1bafd194a 2010-03-18   17: 
f1bafd194a 2010-03-18   18: 	def info(self, message):
f1bafd194a 2010-03-18   19: 		if self._syslog:
f1bafd194a 2010-03-18   20: 			self._syslog.syslog(self._syslog.LOG_INFO, message)
f1bafd194a 2010-03-18   21: 
f1bafd194a 2010-03-18   22: 	def notice(self, message):
f1bafd194a 2010-03-18   23: 		if self._syslog:
f1bafd194a 2010-03-18   24: 			self._syslog.syslog(self._syslog.LOG_NOTICE, message)
f1bafd194a 2010-03-18   25: 
f1bafd194a 2010-03-18   26: # wrapper around database
f1bafd194a 2010-03-18   27: class tagDB:
f1bafd194a 2010-03-18   28: 	__slots__ = frozenset(['_prepared', '_dump_stmt', '_db'])
f1bafd194a 2010-03-18   29: 
f1bafd194a 2010-03-18   30: 	def __init__(self):
f1bafd194a 2010-03-18   31: 		self._prepared = set()
f1bafd194a 2010-03-18   32: 		self._db = False
c27c7eb208 2010-03-22   33: 		self._dump_stmt = self._curs().prepare("select untrip(site), tag, regexp from urls natural join site natural join tag order by site, tag")
f1bafd194a 2010-03-18   34: 
f1bafd194a 2010-03-18   35: 	def _curs(self):
f1bafd194a 2010-03-18   36: 		if not self._db:
f1bafd194a 2010-03-18   37: 			config.section('database')
f1bafd194a 2010-03-18   38: 			self._db = postgresql.open(
f1bafd194a 2010-03-18   39: 				'pq://{}:{}@{}/{}'.format(
f1bafd194a 2010-03-18   40: 					config['user'],
f1bafd194a 2010-03-18   41: 					config['password'],
f1bafd194a 2010-03-18   42: 					config['host'],
f1bafd194a 2010-03-18   43: 					config['database'],
f1bafd194a 2010-03-18   44: 			) )
f1bafd194a 2010-03-18   45: 		return(self._db)
f1bafd194a 2010-03-18   46: 
f1bafd194a 2010-03-18   47: 	def dump(self):
f1bafd194a 2010-03-18   48: 		return(self._dump_stmt())
f1bafd194a 2010-03-18   49: 
f1bafd194a 2010-03-18   50: # this classes processes config file and substitutes default values
f1bafd194a 2010-03-18   51: class Config:
f1bafd194a 2010-03-18   52: 	__slots__ = frozenset(['_config', '_default', '_section'])
f1bafd194a 2010-03-18   53: 	_default = {
f1bafd194a 2010-03-18   54: 		'reactor': {
f1bafd194a 2010-03-18   55: 			'reactor': 'thread',
f1bafd194a 2010-03-18   56: 		},
f1bafd194a 2010-03-18   57: 		'log': {
f1bafd194a 2010-03-18   58: 			'silent': 'no',
f1bafd194a 2010-03-18   59: 		},
f1bafd194a 2010-03-18   60: 		'database': {
f1bafd194a 2010-03-18   61: 			'host': 'localhost',
f1bafd194a 2010-03-18   62: 			'database': 'squidTag',
f1bafd194a 2010-03-18   63: 	},}
f1bafd194a 2010-03-18   64: 
f1bafd194a 2010-03-18   65: 	# function to read in config file
f1bafd194a 2010-03-18   66: 	def __init__(self):
f1bafd194a 2010-03-18   67: 		parser = optparse.OptionParser()
f1bafd194a 2010-03-18   68: 		parser.add_option('-c', '--config', dest = 'config',
f1bafd194a 2010-03-18   69: 			help = 'config file location', metavar = 'FILE',
f1bafd194a 2010-03-18   70: 			default = '/usr/local/etc/squid-tagger.conf')
f1bafd194a 2010-03-18   71: 
f1bafd194a 2010-03-18   72: 		(options, args) = parser.parse_args()
f1bafd194a 2010-03-18   73: 
f1bafd194a 2010-03-18   74: 		if not os.access(options.config, os.R_OK):
f1bafd194a 2010-03-18   75: 			print("Can't read {}: exitting".format(options.config))
f1bafd194a 2010-03-18   76: 			sys.exit(2)
f1bafd194a 2010-03-18   77: 
f1bafd194a 2010-03-18   78: 		self._config = configparser.ConfigParser()
f1bafd194a 2010-03-18   79: 		self._config.readfp(open(options.config))
f1bafd194a 2010-03-18   80: 
f1bafd194a 2010-03-18   81: 	# function to select config file section or create one
f1bafd194a 2010-03-18   82: 	def section(self, section):
f1bafd194a 2010-03-18   83: 		if not self._config.has_section(section):
f1bafd194a 2010-03-18   84: 			self._config.add_section(section)
f1bafd194a 2010-03-18   85: 		self._section = section
f1bafd194a 2010-03-18   86: 
f1bafd194a 2010-03-18   87: 	# function to get config parameter, if parameter doesn't exists the default
f1bafd194a 2010-03-18   88: 	# value or None is substituted
f1bafd194a 2010-03-18   89: 	def __getitem__(self, name):
f1bafd194a 2010-03-18   90: 		if not self._config.has_option(self._section, name):
f1bafd194a 2010-03-18   91: 			if self._section in self._default:
f1bafd194a 2010-03-18   92: 				if name in self._default[self._section]:
f1bafd194a 2010-03-18   93: 					self._config.set(self._section, name, self._default[self._section][name])
f1bafd194a 2010-03-18   94: 				else:
f1bafd194a 2010-03-18   95: 					self._config.set(self._section, name, None)
f1bafd194a 2010-03-18   96: 			else:
f1bafd194a 2010-03-18   97: 				self._config.set(self._section, name, None)
f1bafd194a 2010-03-18   98: 		return(self._config.get(self._section, name))
f1bafd194a 2010-03-18   99: 
f1bafd194a 2010-03-18  100: # initializing and reading in config file
f1bafd194a 2010-03-18  101: config = Config()
f1bafd194a 2010-03-18  102: 
f1bafd194a 2010-03-18  103: tagdb = tagDB()
f1bafd194a 2010-03-18  104: 
f1bafd194a 2010-03-18  105: csv_writer = csv.writer(sys.stdout)
f1bafd194a 2010-03-18  106: csv_writer.writerow(['site', 'tags', 'regexp'])
f1bafd194a 2010-03-18  107: for row in tagdb.dump():
f1bafd194a 2010-03-18  108: 	csv_writer.writerow([row[0], '{' + ','.join(row[1]) + '}', row[2]])