167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
|
# wrapper around database
class tagDB(object):
__slots__ = frozenset(['_cursor', '_db'])
def __init__(self):
config.section('database')
if config['host'] == None:
self._db = psycopg2.connect(
database = config['database'],
host = config['host'],
user = config['user'],
password = config['password'],
)
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:
names.append(record.name)
return(names)
|
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
sys.stdout.write(string)
sys.stdout.flush()
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()
data_fields = ['site', 'tag', 'regexp']
conf_fields = ['netmask', 'redirect_url', 'from_weekday', 'to_weekday', 'from_time', 'to_time', 'tag']
|