| 1 |
# This file is part of the Ultimate Debian Database project
|
| 2 |
|
| 3 |
import aux
|
| 4 |
|
| 5 |
class gatherer:
|
| 6 |
"""
|
| 7 |
This is the base class of all gatherers which want to use the python
|
| 8 |
interface to be called by the dispatcher
|
| 9 |
|
| 10 |
Attributes:
|
| 11 |
connection: The connection to the SQL database
|
| 12 |
config: The hashmap representing the configuration"""
|
| 13 |
def __init__(self, connection, config, source):
|
| 14 |
self.connection = connection
|
| 15 |
self.config = config
|
| 16 |
self.source = source
|
| 17 |
self.my_config = config[source]
|
| 18 |
|
| 19 |
def run(self):
|
| 20 |
"""Called by the dispatcher for a source"""
|
| 21 |
raise NotImplementedError
|
| 22 |
|
| 23 |
def cursor(self):
|
| 24 |
"""Return the cursor for the current connection"""
|
| 25 |
return self.connection.cursor()
|
| 26 |
|
| 27 |
def setup(self):
|
| 28 |
if 'schema-dir' in self.config['general']:
|
| 29 |
schema_dir = self.config['general']['schema-dir']
|
| 30 |
if 'schema' in self.my_config:
|
| 31 |
schema = schema_dir + '/' + self.my_config['schema']
|
| 32 |
self.eval_sql_file(schema, self.my_config)
|
| 33 |
else:
|
| 34 |
raise Exception("'schema' not specified for source " + self.source)
|
| 35 |
else:
|
| 36 |
raise Exception("'schema-dir' not specified")
|
| 37 |
|
| 38 |
def drop(self):
|
| 39 |
if 'table' in self.my_config:
|
| 40 |
self.cursor().execute("DROP TABLE " + self.my_config['table'])
|
| 41 |
|
| 42 |
def eval_sql_file(self, path, d = None):
|
| 43 |
"""Load the SQL code from the file specified by <path>. Use pythons string
|
| 44 |
formating for the dictionary <d> if it is not None
|
| 45 |
Warning: No quoting for the elements of d is done"""
|
| 46 |
c = file(path).read()
|
| 47 |
if d is not None:
|
| 48 |
c = c % d
|
| 49 |
|
| 50 |
cur = self.cursor()
|
| 51 |
cur.execute(c)
|
| 52 |
|
| 53 |
def assert_my_config(self, *keywords):
|
| 54 |
for k in keywords:
|
| 55 |
if not k in self.my_config:
|
| 56 |
raise aux.ConfigException("%s not specified for source %s" % (k, self.source))
|