Add dividers and rearrange some of the table column parsing.

This commit is contained in:
voussoir 2018-07-13 19:22:05 -07:00
parent 2f83bc2808
commit c37b54b456

View file

@ -9,6 +9,8 @@ import string
import traceback import traceback
import warnings import warnings
# FFmpeg ###########################################################################################
FFMPEG_NOT_FOUND = ''' FFMPEG_NOT_FOUND = '''
ffmpeg or ffprobe not found. ffmpeg or ffprobe not found.
Add them to your PATH or use symlinks such that they appear in: Add them to your PATH or use symlinks such that they appear in:
@ -37,11 +39,7 @@ def _load_ffmpeg():
ffmpeg = _load_ffmpeg() ffmpeg = _load_ffmpeg()
FILENAME_BADCHARS = '\\/:*?<>|"' # Database #########################################################################################
# Note: Setting user_version pragma in init sequence is safe because it only
# happens after the out-of-date check occurs, so no chance of accidentally
# overwriting it.
DATABASE_VERSION = 14 DATABASE_VERSION = 14
DB_PRAGMAS = f''' DB_PRAGMAS = f'''
@ -53,7 +51,6 @@ PRAGMA user_version = {DATABASE_VERSION};
DB_INIT = f''' DB_INIT = f'''
{DB_PRAGMAS} {DB_PRAGMAS}
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS users( CREATE TABLE IF NOT EXISTS users(
id TEXT PRIMARY KEY NOT NULL, id TEXT PRIMARY KEY NOT NULL,
@ -186,20 +183,6 @@ CREATE INDEX IF NOT EXISTS index_tag_synonyms_name on tag_synonyms(name);
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
''' '''
def _extract_columns(create_table_statement):
column_names = create_table_statement.split('(')[1].rsplit(')', 1)[0]
column_names = column_names.split(',')
column_names = [x.strip() for x in column_names]
column_names = [x.split(' ')[0] for x in column_names]
column_names = [c for c in column_names if c.lower() != 'foreign']
return column_names
def _extract_table_name(create_table_statement):
# CREATE TABLE table_name(
table_name = create_table_statement.split('(')[0].strip()
table_name = table_name.split()[-1]
return table_name
def _extract_table_statements(script): def _extract_table_statements(script):
for statement in script.split(';'): for statement in script.split(';'):
if 'create table' not in statement.lower(): if 'create table' not in statement.lower():
@ -207,21 +190,37 @@ def _extract_table_statements(script):
yield statement yield statement
def _extract_table_name(create_table_statement):
# CREATE TABLE table_name(...)
table_name = create_table_statement.split('(')[0].strip()
table_name = table_name.split()[-1]
return table_name
def _extract_columns_from_table(create_table_statement):
# CREATE TABLE table_name(column_name TYPE MODIFIERS, ...)
column_names = create_table_statement.split('(')[1].rsplit(')', 1)[0]
column_names = column_names.split(',')
column_names = [x.strip() for x in column_names]
column_names = [x.split(' ')[0] for x in column_names]
column_names = [c for c in column_names if c.lower() != 'foreign']
return column_names
def _reverse_index(columns): def _reverse_index(columns):
''' '''
A dictionary where the key is the item and the value is the index. Given an iterable, return a dictionary where the key is the item and the
Used to convert a stringy name into the correct number to then index into value is the index. Used to convert a stringy name into the correct number
an sql row. to then index into an sql row.
['test', 'toast'] -> {'test': 0, 'toast': 1} ['test', 'toast'] -> {'test': 0, 'toast': 1}
''' '''
return {column: index for (index, column) in enumerate(columns)} return {column: index for (index, column) in enumerate(columns)}
SQL_COLUMNS = { SQL_COLUMNS = {}
_extract_table_name(table): _extract_columns(table) for table_statement in _extract_table_statements(DB_INIT):
for table in _extract_table_statements(DB_INIT) table_name = _extract_table_name(table_statement)
} columns = _extract_columns_from_table(table_statement)
SQL_INDEX = {table: _reverse_index(columns) for (table, columns) in SQL_COLUMNS.items()} SQL_COLUMNS[table_name] = columns
SQL_INDEX = {table: _reverse_index(columns) for (table, columns) in SQL_COLUMNS.items()}
ALLOWED_ORDERBY_COLUMNS = [ ALLOWED_ORDERBY_COLUMNS = [
'extension', 'extension',
@ -236,8 +235,8 @@ ALLOWED_ORDERBY_COLUMNS = [
'random', 'random',
] ]
# Errors and warnings ##############################################################################
# Errors and warnings
WARNING_MINMAX_INVALID = 'Field "{field}": "{value}" is not a valid request. Ignored.' WARNING_MINMAX_INVALID = 'Field "{field}": "{value}" is not a valid request. Ignored.'
WARNING_ORDERBY_INVALID = 'Invalid orderby request "{request}". Ignored.' WARNING_ORDERBY_INVALID = 'Invalid orderby request "{request}". Ignored.'
WARNING_ORDERBY_BADCOL = '"{column}" is not a sorting option. Ignored.' WARNING_ORDERBY_BADCOL = '"{column}" is not a sorting option. Ignored.'
@ -245,7 +244,9 @@ WARNING_ORDERBY_BADDIRECTION = '''
You can\'t order "{column}" by "{direction}". Defaulting to descending. You can\'t order "{column}" by "{direction}". Defaulting to descending.
''' '''
# Operational info # Janitorial stuff #################################################################################
FILENAME_BADCHARS = '\\/:*?<>|"'
TRUTHYSTRING_TRUE = {s.lower() for s in ('1', 'true', 't', 'yes', 'y', 'on')} TRUTHYSTRING_TRUE = {s.lower() for s in ('1', 'true', 't', 'yes', 'y', 'on')}
TRUTHYSTRING_NONE = {s.lower() for s in ('null', 'none')} TRUTHYSTRING_NONE = {s.lower() for s in ('null', 'none')}
@ -269,6 +270,8 @@ ADDITIONAL_MIMETYPES = {
'srt': 'text/plain', 'srt': 'text/plain',
} }
# Photodb ##########################################################################################
DEFAULT_DATADIR = '.\\_etiquette' DEFAULT_DATADIR = '.\\_etiquette'
DEFAULT_DBNAME = 'phototagger.db' DEFAULT_DBNAME = 'phototagger.db'
DEFAULT_CONFIGNAME = 'config.json' DEFAULT_CONFIGNAME = 'config.json'