Classified the loose functions in metadata.py

metadata-restructure
Emily Frost 4 years ago
parent c0960f37d9
commit 8f1cdf7a6e
Signed by: Emily
GPG Key ID: AA5D42849F1CBDC9

@ -14,20 +14,8 @@ import sqlalchemy.orm
# TODO: l o g g i n g # TODO: l o g g i n g
HASH_CHUNK_SIZE = 10485760 # 10mb HASH_CHUNK_SIZE = 10485760 # 10mb
db_session = None
_db_session_maker = sqlalchemy.orm.sessionmaker()
_engine = None
_configured = False
_SQLBase = sqlalchemy.ext.declarative.declarative_base() _SQLBase = sqlalchemy.ext.declarative.declarative_base()
class MetadataDBSessionException(Exception):
'''This exception is raised when something goes wrong with a database session.'''
def _uuidgen():
return str(uuid.uuid4())
''' '''
Metadata ORM classes for SQLAlchemy. For a detailed description of each piece of data, refer to Metadata ORM classes for SQLAlchemy. For a detailed description of each piece of data, refer to
metadata/README.md metadata/README.md
@ -112,63 +100,69 @@ class Image(_SQLBase):
) )
) )
class hashdb:
''' _engine = None
# TODO: I was attempting to avoid writing a class to see if I could, but it is not worth the clunk.
Rewrite these functions into a class. # TODO: db_path's default should be set here, not in frontend.
''' def __init__(self, db_path):
'''
def configure(db_path): Configure and initialize the database for the entire module.
''' Currently, only SQLite is supported.
Configure and initialize the database for the entire module.
Currently, only SQLite is supported. db_path: Path for the SQLite database
'''
db_path: Path for the SQLite database self._engine = sqlalchemy.create_engine('sqlite:///%s' % db_path)
'''
_engine = sqlalchemy.create_engine('sqlite:///%s' % db_path) _SQLBase.metadata.create_all(self._engine)
self._session_maker.configure(bind=self._engine)
_SQLBase.metadata.create_all(_engine)
_db_session_maker.configure(bind=_engine) def search(self, table_object, **constraints):
_configured = True '''
Search the database for entries matching the given constraints.
def search(session, table_object, **constraints):
''' table_object: SQLAlchemy ORM table object, defined in the file above
Search the database for entries matching the given constraints. constraints: key-value pairs to match against specific fields in the database
Note: Currently, only the query.ilike method is supported. This is intended to
session: SQLAlchemy session, presumably from get_db_session eventually support the entire range of available filters.
table_object: SQLAlchemy ORM table object, defined in the file above '''
constraints: key-value pairs to match against specific fields in the database
Note: Currently, only the query.ilike method is supported. This is intended to with self._get_db_session() as session:
eventually support the entire range of available filters. # TODO: Consider making this return data recursively on items that reference other
''' # tables.
query = session.query(table_object) query = session.query(table_object)
for key, value in constraints.items(): for key, value in constraints.items():
query = query.filter(getattr(table_object, key).ilike('%%%s%%' % value)) query = query.filter(getattr(table_object, key).ilike('%%%s%%' % value))
item_list = [] item_list = []
for item in query: for item in query:
item_list.append(item) item_list.append(item)
return item_list return item_list
@contextlib.contextmanager def import_json(self, json_file):
def get_db_session(): '''
'''Get a SQLAlchemy database session with a proper context object. ''' Import metadata from a json file.
# TODO: There's probably a more reliable way of knowing whether the database was configured. '''
if not _configured:
raise MetadataDBSessionException('Tried to get session without configuring a database.') # json files can be large, but not too large for ram
# do not exit on invalid metadata, log the error and skip the object
session = _db_session_maker() pass
try:
yield session @contextlib.contextmanager
def _get_db_session():
except: '''Get a SQLAlchemy database session with a proper context object. '''
# TODO: Decide which exceptions to handle/eat here and which ones belong in UI.
# This one is okay to put off until you start really building UI. session = sqlalchemy.orm.sessionmaker()
session.rollback() try:
raise yield session
else:
session.commit() except:
finally: # TODO: Decide which exceptions to handle/eat here and which ones belong in UI.
session.close() # This one is okay to put off until you start really building UI.
session.rollback()
raise
else:
session.commit()
finally:
session.close()

Loading…
Cancel
Save