from sqlobject import manage, sqlhub import py def setup_class(*classes): classes = list(_flatten(classes)) manage.drop_tables(classes, if_exists=True) manage.create_tables(classes) def _flatten(lst): for item in lst: if isinstance(item, (list, tuple)): for sub in _flatten(item): yield sub else: yield item def inserts(cls, data, schema=None): """ Creates a bunch of rows. You can use it like:: inserts(Person, [{'fname': 'blah', 'lname': 'doe'}, ...]) Or:: inserts(Person, [('blah', 'doe')], schema= ['fname', 'lname']) If you give a single string for the `schema` then it'll split that string to get the list of column names. """ if schema: if isinstance(schema, str): schema = schema.split() keywordData = [] for item in data: itemDict = {} for name, value in zip(schema, item): itemDict[name] = value keywordData.append(itemDict) data = keywordData results = [] for args in data: results.append(cls(**args)) return results def get_connection(): return sqlhub.get_connection() support_rules = { 'blob_data': ['-mssql'], # SQLite supports UTF-8 content, but not other encodings (I get # a UnicodeDecodeError): 'non_utf8_encoding': ['-sqlite'], # SQLite isn't accepting datetime instances in, even though # pysqlite2.dbapi2.Timestamp *is* datetime; I don't get it. 'datetime': ['-sqlite'], # MS SQL Server doesn't support tables with no columns: 'empty_table': ['-mssql', '-sqlite'], } def supports(operation): db_name = get_connection().plugin.uri.split(':', 1)[0] if operation not in support_rules: return True rules = support_rules[operation] if '-'+db_name in rules: return False if '+'+db_name in rules: return False if '-all' in rules: return False return True try: sorted except NameError: def sorted(lst): lst = list(lst) lst.sort() return lst try: enumerate except NameError: # Python 2.2 def enumerate(lst): return [(i, lst[i]) for i in range(len(lst))]