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 f1bafd194a:

f1bafd194a 2010-03-18 c.kworr@d4daf: #!/usr/bin/env python3.1
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: import configparser, csv, optparse, os, postgresql.api, sys
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: # wrapper around syslog, can be muted
f1bafd194a 2010-03-18 c.kworr@d4daf: class Logger:
f1bafd194a 2010-03-18 c.kworr@d4daf: 	__slots__ = frozenset(['_syslog'])
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 	def __init__(self):
f1bafd194a 2010-03-18 c.kworr@d4daf: 		config.section('log')
f1bafd194a 2010-03-18 c.kworr@d4daf: 		if config['silent'] == 'yes':
f1bafd194a 2010-03-18 c.kworr@d4daf: 			self._syslog = None
f1bafd194a 2010-03-18 c.kworr@d4daf: 		else:
f1bafd194a 2010-03-18 c.kworr@d4daf: 			import syslog
f1bafd194a 2010-03-18 c.kworr@d4daf: 			self._syslog = syslog
f1bafd194a 2010-03-18 c.kworr@d4daf: 			self._syslog.openlog('squidTag')
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 	def info(self, message):
f1bafd194a 2010-03-18 c.kworr@d4daf: 		if self._syslog:
f1bafd194a 2010-03-18 c.kworr@d4daf: 			self._syslog.syslog(self._syslog.LOG_INFO, message)
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 	def notice(self, message):
f1bafd194a 2010-03-18 c.kworr@d4daf: 		if self._syslog:
f1bafd194a 2010-03-18 c.kworr@d4daf: 			self._syslog.syslog(self._syslog.LOG_NOTICE, message)
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: # wrapper around database
f1bafd194a 2010-03-18 c.kworr@d4daf: class tagDB:
f1bafd194a 2010-03-18 c.kworr@d4daf: 	__slots__ = frozenset(['_prepared', '_dump_stmt', '_db'])
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 	def __init__(self):
f1bafd194a 2010-03-18 c.kworr@d4daf: 		self._prepared = set()
f1bafd194a 2010-03-18 c.kworr@d4daf: 		self._db = False
f1bafd194a 2010-03-18 c.kworr@d4daf: 		self._dump_stmt = self._curs().prepare("select untrip(site), tag, regexp from urls natural join site natural join tag")
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 	def _curs(self):
f1bafd194a 2010-03-18 c.kworr@d4daf: 		if not self._db:
f1bafd194a 2010-03-18 c.kworr@d4daf: 			config.section('database')
f1bafd194a 2010-03-18 c.kworr@d4daf: 			self._db = postgresql.open(
f1bafd194a 2010-03-18 c.kworr@d4daf: 				'pq://{}:{}@{}/{}'.format(
f1bafd194a 2010-03-18 c.kworr@d4daf: 					config['user'],
f1bafd194a 2010-03-18 c.kworr@d4daf: 					config['password'],
f1bafd194a 2010-03-18 c.kworr@d4daf: 					config['host'],
f1bafd194a 2010-03-18 c.kworr@d4daf: 					config['database'],
f1bafd194a 2010-03-18 c.kworr@d4daf: 			) )
f1bafd194a 2010-03-18 c.kworr@d4daf: 		return(self._db)
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 	def dump(self):
f1bafd194a 2010-03-18 c.kworr@d4daf: 		return(self._dump_stmt())
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: # this classes processes config file and substitutes default values
f1bafd194a 2010-03-18 c.kworr@d4daf: class Config:
f1bafd194a 2010-03-18 c.kworr@d4daf: 	__slots__ = frozenset(['_config', '_default', '_section'])
f1bafd194a 2010-03-18 c.kworr@d4daf: 	_default = {
f1bafd194a 2010-03-18 c.kworr@d4daf: 		'reactor': {
f1bafd194a 2010-03-18 c.kworr@d4daf: 			'reactor': 'thread',
f1bafd194a 2010-03-18 c.kworr@d4daf: 		},
f1bafd194a 2010-03-18 c.kworr@d4daf: 		'log': {
f1bafd194a 2010-03-18 c.kworr@d4daf: 			'silent': 'no',
f1bafd194a 2010-03-18 c.kworr@d4daf: 		},
f1bafd194a 2010-03-18 c.kworr@d4daf: 		'database': {
f1bafd194a 2010-03-18 c.kworr@d4daf: 			'host': 'localhost',
f1bafd194a 2010-03-18 c.kworr@d4daf: 			'database': 'squidTag',
f1bafd194a 2010-03-18 c.kworr@d4daf: 	},}
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 	# function to read in config file
f1bafd194a 2010-03-18 c.kworr@d4daf: 	def __init__(self):
f1bafd194a 2010-03-18 c.kworr@d4daf: 		parser = optparse.OptionParser()
f1bafd194a 2010-03-18 c.kworr@d4daf: 		parser.add_option('-c', '--config', dest = 'config',
f1bafd194a 2010-03-18 c.kworr@d4daf: 			help = 'config file location', metavar = 'FILE',
f1bafd194a 2010-03-18 c.kworr@d4daf: 			default = '/usr/local/etc/squid-tagger.conf')
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 		(options, args) = parser.parse_args()
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 		if not os.access(options.config, os.R_OK):
f1bafd194a 2010-03-18 c.kworr@d4daf: 			print("Can't read {}: exitting".format(options.config))
f1bafd194a 2010-03-18 c.kworr@d4daf: 			sys.exit(2)
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 		self._config = configparser.ConfigParser()
f1bafd194a 2010-03-18 c.kworr@d4daf: 		self._config.readfp(open(options.config))
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 	# function to select config file section or create one
f1bafd194a 2010-03-18 c.kworr@d4daf: 	def section(self, section):
f1bafd194a 2010-03-18 c.kworr@d4daf: 		if not self._config.has_section(section):
f1bafd194a 2010-03-18 c.kworr@d4daf: 			self._config.add_section(section)
f1bafd194a 2010-03-18 c.kworr@d4daf: 		self._section = section
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: 	# function to get config parameter, if parameter doesn't exists the default
f1bafd194a 2010-03-18 c.kworr@d4daf: 	# value or None is substituted
f1bafd194a 2010-03-18 c.kworr@d4daf: 	def __getitem__(self, name):
f1bafd194a 2010-03-18 c.kworr@d4daf: 		if not self._config.has_option(self._section, name):
f1bafd194a 2010-03-18 c.kworr@d4daf: 			if self._section in self._default:
f1bafd194a 2010-03-18 c.kworr@d4daf: 				if name in self._default[self._section]:
f1bafd194a 2010-03-18 c.kworr@d4daf: 					self._config.set(self._section, name, self._default[self._section][name])
f1bafd194a 2010-03-18 c.kworr@d4daf: 				else:
f1bafd194a 2010-03-18 c.kworr@d4daf: 					self._config.set(self._section, name, None)
f1bafd194a 2010-03-18 c.kworr@d4daf: 			else:
f1bafd194a 2010-03-18 c.kworr@d4daf: 				self._config.set(self._section, name, None)
f1bafd194a 2010-03-18 c.kworr@d4daf: 		return(self._config.get(self._section, name))
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: # initializing and reading in config file
f1bafd194a 2010-03-18 c.kworr@d4daf: config = Config()
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: tagdb = tagDB()
f1bafd194a 2010-03-18 c.kworr@d4daf: 
f1bafd194a 2010-03-18 c.kworr@d4daf: csv_writer = csv.writer(sys.stdout)
f1bafd194a 2010-03-18 c.kworr@d4daf: csv_writer.writerow(['site', 'tags', 'regexp'])
f1bafd194a 2010-03-18 c.kworr@d4daf: for row in tagdb.dump():
f1bafd194a 2010-03-18 c.kworr@d4daf: 	csv_writer.writerow([row[0], '{' + ','.join(row[1]) + '}', row[2]])