diff options
author | Priit Laes <plaes@plaes.org> | 2010-06-25 16:25:30 +0300 |
---|---|---|
committer | Priit Laes <plaes@plaes.org> | 2010-06-25 16:25:30 +0300 |
commit | 6500c007d0597953f82c14c5043bd58a65c01a30 (patch) | |
tree | cc225d809a5b737bdc2b82e23e54c67aca52bb9d /grumpy/models.py | |
parent | Use two separate collections: one for packages and one for ebuilds (diff) | |
download | gsoc2010-grumpy-6500c007d0597953f82c14c5043bd58a65c01a30.tar.gz gsoc2010-grumpy-6500c007d0597953f82c14c5043bd58a65c01a30.tar.bz2 gsoc2010-grumpy-6500c007d0597953f82c14c5043bd58a65c01a30.zip |
SQLAlchemy/PostgreSQL instead of MongoDB
Diffstat (limited to 'grumpy/models.py')
-rw-r--r-- | grumpy/models.py | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/grumpy/models.py b/grumpy/models.py new file mode 100644 index 0000000..f7b4c6b --- /dev/null +++ b/grumpy/models.py @@ -0,0 +1,117 @@ +# -*- coding: utf-8 -*- +""" + grumpy.models + ~~~~~~~~~~~~~ + + This module contains the models and low-level database glue for the + application. + + :copyright: (c) by 2010 Priit Laes. + :license: BSD, see LICENSE for details. +""" +from sqlalchemy import Column, ForeignKey, Integer, String, Table +from sqlalchemy.orm import relationship +from sqlalchemy.ext.declarative import declarative_base + +Base = declarative_base() + +# Association tables +package_developers = Table('package_developers', Base.metadata, + Column('package_id', Integer, ForeignKey('packages.id')), + Column('developer_id', Integer, ForeignKey('developers.id')) + ) +package_herds = Table('package_herds', Base.metadata, + Column('package_id', Integer, ForeignKey('packages.id')), + Column('herd_id', Integer, ForeignKey('herds.id')) + ) + +class Developer(Base): + """Represents developers in the system""" + + __tablename__ = 'developers' + id = Column('id', Integer, primary_key=True) + email = Column('email', String, nullable=False, unique=True) + + def __init__(self, email): + self.email = email + +class Ebuild(Base): + """Represents single ebuilds (cpv) in the system""" + + __tablename__ = 'ebuilds' + + id = Column('id', Integer, primary_key=True) + cpv = Column('cpv', String, nullable=False, unique=True) + eapi = Column('eapi', Integer, nullable=False) + # Regular USE flags + iuse = Column('iuse', String) + # Forced USE flags (+use) + fiuse = Column('fiuse', String) + keywords = Column('keywords', String) + slot = Column('slot', String) + version = Column('version', String, nullable=False) + _package = Column('package_id', Integer, ForeignKey('packages.id')) + # TODO: depend, rdepend, licenses + # TODO: extra info? + + package = relationship("Package") + + def __init__(self, package, version, revision, eapi, slot, keywords, \ + iuse, fiuse): + self.package = package + self.version = version + if revision is not None: + self.version += '-r%s' % revision + self.cpv = "%s/%s-%s" % (package.pkg, package.cat, self.version) + self.eapi = eapi + self.slot = slot + self.iuse = ','.join(iuse) + self.fiuse = ','.join(fiuse) + self.keywords = ','.join(keywords) + + def __repr__(self): + return '<%s> - %s' % (self.__class__.__name__, self.cpv) + +class Herd(Base): + """Represents herds in the system""" + + __tablename__ = 'herds' + + id = Column('id', Integer, primary_key=True) + name = Column('name', String, nullable=False, unique=True) + # TODO: Do we really need to store other information about herds here? + + def __init__(self, name): + self.name = name + +class Package(Base): + """Represents packages in the system""" + + __tablename__ = 'packages' + + id = Column('id', Integer, primary_key=True) + cat = Column('cat', String, nullable=False) + pkg = Column('pkg', String, nullable=False) + cp = Column('cp', String, nullable=False, unique=True) + desc = Column('desc', String) + ldesc = Column('ldesc', String) + homepage = Column('homepage', String) + + ebuilds = relationship(Ebuild) + devs = relationship(Developer, secondary=package_developers, \ + backref='packages') + herds = relationship(Herd, secondary=package_herds, backref='packages') + + def __init__(self, cat, pkg, desc, ldesc, homepage): + self.cat = cat + self.pkg = pkg + self.cp = "%s/%s" % (pkg, cat) + self.desc = desc + self.ldesc = ldesc + self.homepage = homepage + self.devs = [] + self.herds = [] + self.ebuilds = [] + + def __repr__(self): + return '<%s> - %s' % (self.__class__.__name__, self.cp) |