From 07518d34d676df41bf2259cf66ba92444673c810 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 18 Aug 2011 20:59:16 +0200 Subject: Basic database support. --- config.py | 2 +- db.py | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ directory.py | 30 +++++++++++++- 3 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 db.py diff --git a/config.py b/config.py index 7fd35ad..3f8995b 100644 --- a/config.py +++ b/config.py @@ -4,7 +4,7 @@ except ImportError: from ConfigParser import ConfigParser, NoOptionError class Config(object): - config_section = 'foo' + config_section = 'ongaku' def __init__(self, filename = 'config'): self.config = ConfigParser() diff --git a/db.py b/db.py new file mode 100644 index 0000000..4819159 --- /dev/null +++ b/db.py @@ -0,0 +1,125 @@ +from config import config +from sqlalchemy import create_engine +engine = create_engine(config.get('db_path')) + +from sqlalchemy.ext.declarative import declarative_base +Base = declarative_base() + +from sqlalchemy import Column, Integer, String, ForeignKey +from sqlalchemy.orm import relationship, backref +from sqlalchemy import and_ + +from sqlalchemy.orm.exc import NoResultFound + +class Directory(Base): + __tablename__ = 'directories' + + id = Column(Integer, primary_key = True) + path = Column(String, nullable = False, index = True) + parent_id = Column(Integer, ForeignKey('directories.id')) + + parent = relationship('Directory') + + def __init__(self, path): + self.path = path + + def __repr__(self): + return ''.format(self.path) + + @staticmethod + def get(session, path): + try: + directory = session.query(Directory).filter(Directory.path == path).one() + except NoResultFound: + directory = Directory(path) + session.add(directory) + session.commit() + return directory + +class Artist(Base): + __tablename__ = 'artists' + + id = Column(Integer, primary_key = True) + name = Column(String, nullable = False, index = True) + + def __init__(self, name): + self.name = name + + def __repr__(self): + return ''.format(self.name.encode('utf-8')) + + @staticmethod + def get(session, name): + try: + artist = session.query(Artist).filter(Artist.name == name).one() + except NoResultFound: + artist = Artist(name) + session.add(artist) + session.commit() + return artist + +class Album(Base): + __tablename__ = 'albums' + + id = Column(Integer, primary_key = True) + name = Column(String, nullable = False, index = True) + artist_id = Column(Integer, ForeignKey('artists.id'), nullable = False) + + artist = relationship(Artist, backref = backref('albums', order_by = name)) + + def __init__(self, name, artist_id): + self.name = name + self.artist_id = artist_id + + def __repr__(self): + return ''.format(self.name) + + @staticmethod + def get(session, name, artist_id = None): + try: + album = session.query(Album).filter(Album.name == name).one() + except NoResultFound: + album = Album(name, artist_id) + session.add(album) + session.commit() + return album + +class Track(Base): + __tablename__ = 'tracks' + + id = Column(Integer, primary_key = True) + name = Column(String, index = True) + num = Column(Integer) + filename = Column(String, nullable = False, index = True) + file_index = Column(Integer) + directory_id = Column(Integer, ForeignKey('directories.id'), nullable = False) + artist_id = Column(Integer, ForeignKey('artists.id')) + album_id = Column(Integer, ForeignKey('albums.id')) + + directory = relationship(Directory, backref = backref('tracks', order_by = filename)) + + def __init__(self, name, num, filename, file_index, directory_id, artist_id, album_id): + self.name = name + self.num = num + self.filename = filename + self.file_index = file_index + self.directory_id = directory_id + self.artist_id = artist_id + self.album_id = album_id + + def __repr__(self): + return u''.format(self.name) + + @staticmethod + def get(session, name, num, filename, file_index, directory_id, artist_id, album_id): + try: + track = session.query(Track).filter(and_(Track.filename == filename, Track.file_index == file_index)).one() + except NoResultFound: + track = Track(name, num, filename, file_index, directory_id, artist_id, album_id) + session.add(track) + return track + +Base.metadata.create_all(engine) + +from sqlalchemy.orm import sessionmaker +Session = sessionmaker(bind = engine) diff --git a/directory.py b/directory.py index c7bef2d..2b79126 100644 --- a/directory.py +++ b/directory.py @@ -1,4 +1,4 @@ -import os, mimetypes, recode, events, cuesheet, mutagen +import os, mimetypes, recode, events, cuesheet, mutagen, db from config import config @@ -161,3 +161,31 @@ class File(DirectoryEntry): d = DirectoryEntry.json(self) d.update({'cached': os.path.exists(cache_path)}) return d + +def rec_scan(session, root): + directory = db.Directory.get(session, root) + + d = Directory(root, isabs = True) + for de in d.listdir(): + if isinstance(de, Directory): + rec_scan(session, de.abs_path) + else: + print de.metadata + artist = db.Artist.get(session, de.metadata['artist']) if 'artist' in de.metadata else None + album = db.Album.get(session, de.metadata['album'], artist.id if artist else None) if 'album' in de.metadata else None + track = db.Track.get(session, de.metadata['title'] if 'title' in de.metadata else None, None, + os.path.basename(de.path), de.track, directory.id, artist.id if artist else None, album.id if album else None) + +def scan(root = None): + if not root: + root = config.get('music_root') + + try: + session = db.Session() + rec_scan(session, root) + session.commit() + finally: + session.close() + +if __name__ == '__main__': + scan() -- cgit v1.2.3