Mercurial > hg > Feedworm
changeset 2:8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
author | Dirk Olmes <dirk@xanthippe.ping.de> |
---|---|
date | Mon, 26 Apr 2010 17:40:55 +0200 |
parents | 8b0280b4bb09 |
children | 2ac144a98370 |
files | Feed.py Mapping.py database-cassiopeia.ini feedupdate-main.py |
diffstat | 4 files changed, 62 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Feed.py Mon Apr 26 17:40:55 2010 +0200 @@ -0,0 +1,8 @@ + +class Feed(object): + def __init__(self, name, url): + self.name = name + self.url = url + + def __repr__(self): + return "<Feed (%d) %s>" % (self.id, self.name)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Mapping.py Mon Apr 26 17:40:55 2010 +0200 @@ -0,0 +1,21 @@ + +from Feed import Feed +from sqlalchemy import Column +from sqlalchemy import Integer +from sqlalchemy import MetaData +from sqlalchemy import String +from sqlalchemy import Table +from sqlalchemy.orm import mapper + +def createMapping(engine): + metadata = MetaData(engine) + + feedTable = Table("feed", metadata, + Column("pk", Integer, primary_key=True), + Column("name", String(255), nullable=False), + Column("url", String(255), nullable=False) + ) + + metadata.create_all() + + mapper(Feed, feedTable)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/database-cassiopeia.ini Mon Apr 26 17:40:55 2010 +0200 @@ -0,0 +1,3 @@ + +[database] +url = sqlite:///feedworm.sqlite
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/feedupdate-main.py Mon Apr 26 17:40:55 2010 +0200 @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +from ConfigParser import ConfigParser +from Feed import Feed +import Mapping +import socket +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +def loadDatabaseProperties(): + hostname = socket.gethostname() + filename = "database-%s.ini" % hostname + + parser = ConfigParser(); + parser.read(filename) + return parser.get("database", "url") + +def createFeed(session): + newFeed = Feed("Joel on Software", "http://www.joelonsoftware.com/rss.xml") + session.add(newFeed) + session.commit() + +if __name__ == "__main__": + databaseUrl = loadDatabaseProperties() + engine = create_engine(databaseUrl,echo=True) + Mapping.createMapping(engine) + SessionMaker = sessionmaker(bind = engine) + session = SessionMaker() + + createFeed(session)