user = ''
password = ''
+
class AuthInitialPost():
db = ''
user = ''
password = ''
+
class UploadFileInitialPost(AuthInitialPost):
upload_fmt = CSV
datafile = ''
trim_upload = True
+
class UploadNullFileInitialPost(UploadFileInitialPost):
upload_null = True
null_rep = ''
+
class UploadTableInitialPost(UploadNullFileInitialPost):
table = ''
+
# The wtforms that suck data out of the html.
class UserWTForm(Form):
'''
self.uh.session[key] = value
-
def read(self):
'''
Read form data from the client
else:
self['action'] = ''
-
def write(self, result, errors):
'''
Produces the dict pyramid will use to render the form.
# Keep form variables handy
self['db'] = self._form.db.data
-
def write(self, result, errors):
'''
Produces the dict pyramid will use to render the form.
if hasattr(post['datafile'], 'file'):
self['localfh'] = post['datafile'].file
-
def write(self, result, errors):
'''
Produces the dict pyramid will use to render the form.
self['upload_null'] = self._form.upload_null.data
self['null_rep'] = self._form.null_rep.data
-
def write(self, result, errors):
'''
Produces the dict pyramid will use to render the form.
# Read our own data
self['table'] = self._form.table.data
-
def write(self, result, errors):
'''
Produces the dict pyramid will use to render the form.
'''
return 'NULL' if st == None else st
+
def is_checked(val):
'''Is the value something a html input entity recognizes as checked?'''
return val == CHECKED
+
# Some functions for logging
def escape_eol(string):
'''Change all the newlines to \n.'''
return string.replace('\n', r'\n')
+
def format_exception(ex):
'''Return an exception formatted as suffix text for a log message.'''
if isinstance(ex, psycopg2.DatabaseError):
msg = ': Error is ({0})'.format(msg)
return msg
+
# Error handling
class UploadError(Exception):
out = '{0}: data ({1})'.format(out, self.data)
return out
+
class Error(UploadError):
'''
Module exceptions rasied while setting up to read data lines
def __init__(self, e, descr='', detail=''):
super(Error, self).__init__(e=e, descr=descr, detail=detail)
+
class NoFileError(Error):
'''No file uploaded'''
def __init__(self, e, descr='', detail=''):
super(NoFileError, self).__init__(e, descr, detail)
+
class NoDBError(Error):
'''No database name given'''
def __init__(self, e, descr='', detail=''):
super(NoDBError, self).__init__(e, descr, detail)
+
class NoUserError(Error):
'''No user name supplied'''
def __init__(self, e, descr='', detail=''):
super(NoUserError, self).__init__(e, descr, detail)
+
class AuthFailError(Error):
'''Unable to connect to the db'''
def __init__(self, e, descr='', detail=''):
super(AuthFailError, self).__init__(e, descr, detail)
+
class DryRunError(Error):
'''Rollback due to dry_run config option'''
def __init__(self, e, descr='', detail=''):
super(DryRunError, self).__init__(e, descr, detail)
+
class CSRFError(Error):
'''Invalid CSRF token'''
def __init__(self, e, descr='', detail=''):
super(CSRFError, self).__init__(e, descr, detail)
+
class NoHeadersError(Error):
'''No column headings found'''
def __init__(self, e, descr='', detail=''):
super(NoHeadersError, self).__init__(e, descr, detail)
+
class NoDataError(Error):
'''No data uploaded'''
def __init__(self, e, descr='', detail=''):
super(NoDataError, self).__init__(e, descr, detail)
+
class DuplicateUploadError(Error):
'''The same filename updated twice into the same db'''
def __init__(self, e, descr='', detail=''):
super(DuplicateUploadError, self).__init__(e, descr, detail)
+
class DataInconsistencyError(Error):
def __init__(self, e, descr='', detail=''):
super(DataInconsistencyError, self).__init__(e, descr, detail)
+
class DBError(Error):
'''psycopg2 raised an error'''
def __init__(self, pgexc, e='process your request'):
detail,
hint)
+
class DBCommitError(DBError):
def __init__(self, pgexc):
super(DBCommitError, self).__init__(pgexc)
+
class DBDataLineError(DBError):
'''Database generated an error while the processor was running.'''
def __init__(self, lineno, e, descr='', detail='', data=''):
super(DataLineError, self).__init__(e, lineno, descr, detail, data)
+
class TooManyColsError(DataLineError):
def __init__(self, lineno, e, descr='', detail='', data=''):
super(TooManyColsError, self).__init__(lineno, e, descr, detail, data)
self.args = args
self.ec = ec
-
def execute(self, cur):
'''
Execute the sql statement.
self.raw = line
self.tuples = [mapper(st) for st in stol(line)]
+
def doublequote(st):
'''
Put string in double quotes escaping according to sql's
out += ch
return out + '"'
+
class UploadHeaders(UploadLine):
'''Uploaded heading line
return seq + ['' for i in range(len(seq) + 1, self.cols)]
-
class DataLineProcessor(object):
'''
A processor supplied uploaded lines (UploadDataLine instances)
'''
super(NoOpProcessor, self).__init__(ue, uh)
-
def eat(self, udl):
'''
Executes an sql command in the db.
'''
super(ExecuteSQL, self).__init__(ue, uh)
-
def eat(self, sqlc):
'''
Executes an sql command in the db.
raise NotImplementedError
-
def no_connection_response(self):
'''
The result dict to use when call_with_connection is not called.