def insert_data(): '''Inserts static data from a data file into the database. ''' def themes(): '''Inserts the themes from themes.yml into the database. ''' data = read_data('themes') subtheme = set(['text', 'background', 'post']) for entry in data: entry_type = entry.pop('type') if entry_type in subtheme: actions.append(table['theme_%s' % entry_type].insert().values(**entry)) else: actions.append(table['theme'].insert().values(**entry)) def forum_import(subsection): '''Inserts data from a forum subsection into the database. ''' data = forum_data[subsection] for entry in data: actions.append(table[subsection].insert().values(**entry)) actions = [] forum_data = read_data('forum') themes() forum_import('nav') forum_import('tag') forum_import('url_redirect') for action in actions: connection.execute(action)
def _read_database(name, first=False, search=False): '''Reads a table from a database and returns the keys and rows or row. If the list 'search' is defined, it looks for search[0] in the entry search[1]. Result is either one row or multiple rows, depending on if first is true or not. ''' if search: condition = '%s == "%s"' % (search[1], search[0]) sql = table[name].select().where(condition) else: sql = table[name].select() in_table = connection.execute(sql) keys = in_table.keys() result = in_table.first() if first else in_table.fetchall() in_table.close() return keys, result