Moved data handling to a separate class.
[piranha.git] / Piranha.py
blobda4cce5572b1cf4d085ada7b9bac2f2b4e906127
2 import os, sys, getopt
3 from os import path
5 import feedparser, sqlite3
6 import PiranhaData
8 def version():
9 return "0.1"
11 class Piranha:
12 """Core class for managing a user's feeds, caching/accessing feed items, etc."""
14 def __init__( self, home=None ):
15 self._home = home
16 self._db = None
18 self.check_home()
19 self.check_db()
21 self._data = PiranhaData.PiranhaData( self.db() )
23 #### class management functions ####
25 def db( self, isolation=None ):
26 """Return a new cursor to the database. This will connect to the database if needed."""
27 if isolation == None:
28 if self._db == None:
29 db = sqlite3.connect( self._dbfile, isolation_level=isolation )
30 self._db = db
31 else:
32 db = self._db
33 else:
34 db = sqlite3.connect( self._dbfile, isolation_level=isolation )
36 db.row_factory = sqlite3.Row
37 return db
38 #end db()
40 def check_home( self ):
41 """Checks the existance of the user's Pirhanna directory and db file.
42 Creates both if they do not already exist."""
44 if None == self._home:
45 self._home = path.join( path.expanduser("~"), ".piranha" )
46 self._dbfile = path.join( self._home, "piranha.db" )
48 if not path.isdir( self._home ):
49 os.mkdir( self._home )
50 #end check_home()
52 def check_db( self ):
53 """Check the internal schema of any sqlite databases to make sure they are the correct version."""
54 db = self.db()
55 cursor = db.cursor()
57 # Check the existance of the config table
58 found = False
59 for row in cursor.execute( "select name from sqlite_master where type='table' and name='config'" ):
60 if row["name"] == "config":
61 found = True
63 # Create the config table if needed
64 if not found:
65 cursor.execute( "create table config( name text unique, ival int, rval real, tval text )" )
66 cursor.execute( "insert into config( name, ival ) values ( 'schema', 0 )" )
68 # Check the database version
69 current = 0
70 latest = len(self._schema)
71 for row in cursor.execute( "select ival from config where name='schema'" ):
72 current = row["ival"]
74 # Update the database as necessary
75 if current < latest:
76 lockdb = self.db("EXCLUSIVE")
77 while current < latest:
78 try:
79 lockdb.execute( self._schema[current] )
80 except sqlite3.IntegrityError:
81 lockdb.execute( "rollback" )
82 raise
83 current = current + 1
85 lockdb.execute( "update config set ival=? where name='schema'", (latest,) )
86 lockdb.commit() #done
88 # invalidate cached connection
89 self._db = None
90 #end check_db()
92 """Basic schema for the piranha.db file."""
93 _schema = [
94 '''create table category ( id integer primary key autoincrement, name text, parent integer )''',
95 '''create table feed ( id integer primary key autoincrement, category integer, url text unique, title text )''',
96 '''create table entry ( id integer primary key autoincrement, feed integer, sha1 text, status integer, stamp text, title text, author text, content blob )''',
97 '''create table tag ( id integer primary key autoincrement, name text unique )''',
98 '''create table tagged ( entry integer, tag integer )''',
100 #end Pirhanna
102 if __name__ == "__main__":
103 """Piranha is a backend, so by default, do nothing."""
104 pass