|
|
|
@ -1,10 +1,9 @@
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
metadatadb.py
|
|
|
|
metadatadb.py
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
import contextlib
|
|
|
|
import collections
|
|
|
|
import collections
|
|
|
|
import dataclasses
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import hashlib
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
import sqlalchemy
|
|
|
|
import sqlalchemy
|
|
|
|
@ -14,6 +13,7 @@ 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_maker = sqlalchemy.orm.sessionmaker()
|
|
|
|
_db_session_maker = sqlalchemy.orm.sessionmaker()
|
|
|
|
|
|
|
|
_engine = None
|
|
|
|
|
|
|
|
|
|
|
|
ImageData = collections.namedtuple('ImageData', 'UUID, sha1sum, filename, release_group')
|
|
|
|
ImageData = collections.namedtuple('ImageData', 'UUID, sha1sum, filename, release_group')
|
|
|
|
ReleaseGroupData = collections.namedtuple('ReleaseGroupData', 'UUID, name, platform')
|
|
|
|
ReleaseGroupData = collections.namedtuple('ReleaseGroupData', 'UUID, name, platform')
|
|
|
|
@ -49,72 +49,34 @@ def get_file_sha1sum(filename):
|
|
|
|
|
|
|
|
|
|
|
|
return sha1sum.hexdigest()
|
|
|
|
return sha1sum.hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
class MetadataDB:
|
|
|
|
def configure(db_path):
|
|
|
|
def __init__(self, db_path):
|
|
|
|
_engine = sqlalchemy.create_engine('sqlite:///%s' % db_path)
|
|
|
|
'''
|
|
|
|
|
|
|
|
If db file does not exist, create it and create necessary tables.
|
|
|
|
|
|
|
|
Either way, create a connection object.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
# TODO: This process needs real error handling.
|
|
|
|
|
|
|
|
# TODO: Add DAT import/credit support.
|
|
|
|
|
|
|
|
self._engine = sqlalchemy.create_engine('sqlite:///%s' % db_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_SQLBase.metadata.create_all(self._engine)
|
|
|
|
|
|
|
|
_db_session_maker.configure(bind=self._engine)
|
|
|
|
|
|
|
|
# TODO: Using One Big Session may have unintended consequences in other, less linear
|
|
|
|
|
|
|
|
# applications. For now, it works.
|
|
|
|
|
|
|
|
self._session = _db_session_maker()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_image(self, image_data):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_image(self, image_data):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_image(self, image_data):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_release_group(self, release_group_data):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_release_group(self, release_group_data):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_release_group(self, release_group_data):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_platform(self, platform):
|
|
|
|
_SQLBase.metadata.create_all(_engine)
|
|
|
|
''' Add a platform shortcode to the database. '''
|
|
|
|
_db_session_maker.configure(bind=_engine)
|
|
|
|
self._session.add(platform)
|
|
|
|
|
|
|
|
self._session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_platform(self, platform):
|
|
|
|
def search(session, table_object, inclusive=True, **constraints):
|
|
|
|
pass
|
|
|
|
query = session.query(table_object)
|
|
|
|
|
|
|
|
|
|
|
|
def remove_platform(self, platform):
|
|
|
|
|
|
|
|
'''Remove a specific platform from the database. '''
|
|
|
|
|
|
|
|
self._session.delete(platform)
|
|
|
|
|
|
|
|
self._session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def search_platforms(self, inclusive=True, **constraints):
|
|
|
|
|
|
|
|
'''Search for platforms, given the parameters. '''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query = self._session.query(Platform)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for key, value in constraints.items():
|
|
|
|
for key, value in constraints.items():
|
|
|
|
query = query.filter(getattr(Platform, key).ilike('%%%s%%' % value))
|
|
|
|
query = query.filter(getattr(table_object, key).ilike('%%%s%%' % value))
|
|
|
|
|
|
|
|
|
|
|
|
platform_list = []
|
|
|
|
item_list = []
|
|
|
|
for platform in query:
|
|
|
|
for item in query:
|
|
|
|
platform_list.append(platform)
|
|
|
|
item_list.append(item)
|
|
|
|
|
|
|
|
|
|
|
|
return platform_list
|
|
|
|
return item_list
|
|
|
|
|
|
|
|
|
|
|
|
def add_dat(self, dat_data):
|
|
|
|
@contextlib.contextmanager
|
|
|
|
pass
|
|
|
|
def get_db_session():
|
|
|
|
|
|
|
|
session = _db_session_maker()
|
|
|
|
def update_dat(self, dat_data):
|
|
|
|
try:
|
|
|
|
pass
|
|
|
|
yield session
|
|
|
|
|
|
|
|
|
|
|
|
def remove_dat(self, dat_data):
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
session.rollback()
|
|
|
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
session.close()
|
|
|
|
|