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