# HG changeset patch # User Dirk Olmes # Date 1272296455 -7200 # Node ID 8a624ee48a742f133bbc7300803b8eec89bdb8f6 # Parent 8b0280b4bb0921f358006c09489ed2cbda385bed First skeleton for sqlalchemy: define the mapping and create the first feed diff -r 8b0280b4bb09 -r 8a624ee48a74 Feed.py --- /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 "" % (self.id, self.name) diff -r 8b0280b4bb09 -r 8a624ee48a74 Mapping.py --- /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) diff -r 8b0280b4bb09 -r 8a624ee48a74 database-cassiopeia.ini --- /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 diff -r 8b0280b4bb09 -r 8a624ee48a74 feedupdate-main.py --- /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)