# HG changeset patch # User dirk # Date 1315572774 -7200 # Node ID 91a24f4993188e5af28ecf592db87f864bbb5551 # Parent f4708d38419c74ac009ce29d1c4897d66aa629e6 introdue an abstraction for the name of the database so it can be changed via commandline parameter diff -r f4708d38419c -r 91a24f499318 backend/couchdb/CouchDb.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/couchdb/CouchDb.py Fri Sep 09 14:52:54 2011 +0200 @@ -0,0 +1,33 @@ + +from argparse import ArgumentParser + +database = "feedtest" + +def init(): + parser = ArgumentParser() + parser.add_argument("--dbname", nargs="?", help="Name of the database") + args = parser.parse_known_args() + dbname = args[0].dbname + if dbname is not None: + database = dbname + +# +# accessor methods for the various views +# + +def feedEntriesByFeed(): + return database + "/feedEntries_by_feed" + +def unreadFeedEntriesByFeed(): + return database + "/unread_feedEntries_by_feed" + +def feeds(): + return database + "/feeds" + +def feedEntryByLink(): + return database + "/feedEntry_by_link" + +def preference(): + return database + "/preference" + + diff -r f4708d38419c -r 91a24f499318 backend/couchdb/CouchDbBackend.py --- a/backend/couchdb/CouchDbBackend.py Mon Sep 05 19:43:34 2011 +0200 +++ b/backend/couchdb/CouchDbBackend.py Fri Sep 09 14:52:54 2011 +0200 @@ -5,8 +5,7 @@ from backend.couchdb.Feed import Feed from backend.couchdb.FeedEntry import FeedEntry import couchdb - -DATABASE = "feedtest" +from backend.couchdb import CouchDb class CouchDbBackend(AbstractBackend): ''' @@ -14,8 +13,9 @@ ''' def __init__(self): + CouchDb.init() server = couchdb.Server() - self.database = server[DATABASE] + self.database = server[CouchDb.database] self.prefs = None def preferences(self): @@ -41,7 +41,7 @@ def _retrieveEntriesForSelectedFeed(self, hideReadEntries): # TODO how to hide read entries if requested? - viewResults = FeedEntry.view(self.database, "feedtest/feedEntries_by_feed", + viewResults = FeedEntry.view(self.database, CouchDb.feedEntriesByFeed(), key=self.selectedFeed.id) return list(viewResults) @@ -77,15 +77,15 @@ feed.store(self.database) def deleteSelectedFeed(self): - viewResults = self.database.view("feedtest/feedEntries_by_feed", key=self.selectedFeed.id) + viewResults = self.database.view(CouchDb.feedEntriesByFeed(), key=self.selectedFeed.id) for row in viewResults: del self.database[row.id] del self.database[self.selectedFeed.id] def entriesForFeed(self, feed, hideReadEntries): - viewName = "feedtest/feedEntries_by_feed" + viewName = CouchDb.feedEntriesByFeed() if hideReadEntries: - viewName = "feedtest/unread_feedEntries_by_feed" + viewName = CouchDb.unreadFeedEntriesByFeed() return list(FeedEntry.view(self.database, viewName)) def markFeedEntriesAsRead(self, indices): diff -r f4708d38419c -r 91a24f499318 backend/couchdb/Feed.py --- a/backend/couchdb/Feed.py Mon Sep 05 19:43:34 2011 +0200 +++ b/backend/couchdb/Feed.py Fri Sep 09 14:52:54 2011 +0200 @@ -1,6 +1,7 @@ from couchdb.mapping import BooleanField, DateTimeField, Document, IntegerField, TextField from datetime import datetime, timedelta +import CouchDb class Feed(Document): doctype = TextField(default="feed") @@ -13,7 +14,7 @@ @staticmethod def all(database): - return Feed.view(database, "feedtest/feeds") + return Feed.view(database, CouchDb.feeds()) @staticmethod def create(url, title=None): diff -r f4708d38419c -r 91a24f499318 backend/couchdb/FeedEntry.py --- a/backend/couchdb/FeedEntry.py Mon Sep 05 19:43:34 2011 +0200 +++ b/backend/couchdb/FeedEntry.py Fri Sep 09 14:52:54 2011 +0200 @@ -1,6 +1,7 @@ from couchdb.mapping import BooleanField, DateTimeField, Document, TextField from datetime import datetime +import CouchDb class FeedEntry(Document): doctype = TextField(default="feedEntry") @@ -14,7 +15,7 @@ @staticmethod def findByLink(link, database): - result = FeedEntry.view(database, "feedtest/feedEntry_by_link", key=link) + result = FeedEntry.view(database, CouchDb.feedEntryByLink(), key=link) try: return iter(result).next() except StopIteration: diff -r f4708d38419c -r 91a24f499318 backend/couchdb/Preferences.py --- a/backend/couchdb/Preferences.py Mon Sep 05 19:43:34 2011 +0200 +++ b/backend/couchdb/Preferences.py Fri Sep 09 14:52:54 2011 +0200 @@ -1,3 +1,5 @@ + +import CouchDb DAYS_TO_KEEP_FEED_ENTRIES = "daysToKeepFeedEntries" HIDE_READ_FEED_ENTRIES = "hideReadFeedEntries" @@ -13,7 +15,7 @@ self.documentIsDirty = False def _initDocument(self): - viewResults = self.database.view("feedtest/preference") + viewResults = self.database.view(CouchDb.preference()) try: row = iter(viewResults).next() self.document = self.database[row.id]