Fixed indentation in lark and updated comments.

metadata-restructure
Emily Frost 4 years ago
parent c53b9d81f5
commit e55e31d5bb
Signed by: Emily
GPG Key ID: AA5D42849F1CBDC9

3
.gitignore vendored

@ -129,3 +129,6 @@ dmypy.json
# Pyre type checker
.pyre/
# Kate swap files
*.kate-swp

@ -1,62 +0,0 @@
import xml.etree.ElementTree
import hashdb
class DatImportError(Exception):
'''This error is raised when a DAT import fails.'''
# TODO: l o g g i n g
# TODO: Consider using a context object to avoid keeping large XML trees in memory.
class Dat:
'''A Dat object processes DAT files into the data structures defined in hashdb. '''
def __init__(self, filename):
'''Open the given DAT file and gather metadata from it.'''
xml_tree = xml.etree.ElementTree.parse(filename)
self._xml_root = xml_tree.getroot()
dat_header = self._xml_root.find('header')
self.info = hashdb.DatInfo(name=dat_header.find('name').text,
description=dat_header.find('description').text,
platform=None,
version=dat_header.find('version').text)
def set_platform(self, platform_info):
'''
Set a platform for this DAT file.
DAT files don't include platform metadata, but are all platform-specific.
'''
new_info = hashdb.DatInfo(name=self.info.name,
description=self.info.description,
platform=platform_info.shortcode,
version=self.info.version)
self.info = new_info
def set_name(self, new_name):
'''
Override the DAT file's name.
DAT files often have less-than-helpful names.
'''
new_info = hashdb.DatInfo(name=new_name,
description=self.info.description,
platform=self.info.platform,
version=self.info.version)
self.info = new_info
def read_all_hashes(self):
'''Read every hash in the DAT file and return it as a large list of RomInfo tuples.'''
if self.info.platform is None:
raise DatImportError('DAT platform not set.')
rom_info_list = []
all_rom_entries = self._xml_root.findall('.//rom')
for rom in all_rom_entries:
rom_info = hashdb.RomInfo(sha1sum=rom.get('sha1'),
filename=rom.get('name'),
platform=self.info.platform,
datorigin=self.info.name)
rom_info_list.append(rom_info)
return rom_info_list

@ -13,6 +13,9 @@ import sqlalchemy.orm
# TODO: l o g g i n g
HASH_CHUNK_SIZE = 10485760 # 10mb
db_session = None
_db_session_maker = sqlalchemy.orm.sessionmaker()
_engine = None
_configured = False
@ -25,6 +28,11 @@ class MetadataDBSessionException(Exception):
def _uuidgen():
return str(uuid.uuid4())
'''
Metadata ORM classes for SQLAlchemy. For a detailed description of each piece of data, refer to
metadata/README.md
'''
class Platform(_SQLBase):
'''SQLAlchemy ORM class for platform metadata.'''
__tablename__ = 'platforms'
@ -33,6 +41,7 @@ class Platform(_SQLBase):
)
fullname = sqlalchemy.Column(sqlalchemy.String, nullable=False)
shortcode = sqlalchemy.Column(sqlalchemy.String, unique=True, nullable=False)
regional_names = sqlalchemy.Column(sqlalchemy.String)
release_groups = sqlalchemy.orm.relationship(
'ReleaseGroup', order_by=ReleaseGroup.id, back_populates='platform'
@ -53,7 +62,6 @@ class ReleaseGroup(_SQLBase):
name = sqlalchemy.Column(sqlalchemy.String, unique=True, nullable=False)
platform_id = sqlalchemy.Column(sqlalchemy.Integer, sqlalchemy.ForeignKey('platforms.id'))
platform = sqlalchemy.orm.relationship('Platform', back_populates='release_groups')
releases = sqlalchemy.orm.relationship('Release', back_populates='release_group')
def __repr__(self):
@ -76,7 +84,7 @@ class Release(_SQLBase):
release_group = sqlalchemy.orm.relationship('ReleaseGroup', back_populates='releases')
def __repr__(self):
return ('ROM Image: id: %s, en_name: %s' % (self.id, self.en_name )
return ('Release: id: %s, en_name: %s' % (self.id, self.en_name )
class Image(_SQLBase):
'''SQLAlchemy ORM class for ROM image metadata.'''
@ -105,6 +113,11 @@ class Image(_SQLBase):
)
'''
# 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.
'''
def configure(db_path):
'''
Configure and initialize the database for the entire module.
@ -118,7 +131,6 @@ def configure(db_path):
_db_session_maker.configure(bind=_engine)
_configured = True
# TODO: Passing the session object is a little clunky. Maybe there's a way to infer it somehow?
def search(session, table_object, **constraints):
'''
Search the database for entries matching the given constraints.

@ -8,17 +8,11 @@ directory structure.
## Planned features
* Validate ROM images.
* Import DAT files
* Rename/move ROM files
* Maintain a database of present ROMs
* A nice, Beets-like interface
* Grouping ROMS in archive files
## Known issues
* This probably isn't terribly efficient. It's Python parsing XML into an SQLite database and I only
know pretty basic database design.
* Python's `xml.etree` module has a couple of known security issues[1]. Stick to importing DATs from
known places and it shouldn't be an issue.
[1] - https://docs.python.org/3/library/xml.html#xml-vulnerabilities
* This probably isn't terribly efficient. It's Python parsing JSON into an SQLite database and I
onlyknow pretty basic database design.

Loading…
Cancel
Save