5 import feedparser
, sqlite3
12 """Core class for managing a user's feeds, caching/accessing feed items, etc."""
14 def __init__( self
, home
=None ):
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."""
29 db
= sqlite3
.connect( self
._dbfile
, isolation_level
=isolation
)
34 db
= sqlite3
.connect( self
._dbfile
, isolation_level
=isolation
)
36 db
.row_factory
= sqlite3
.Row
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
)
53 """Check the internal schema of any sqlite databases to make sure they are the correct version."""
57 # Check the existance of the config table
59 for row
in cursor
.execute( "select name from sqlite_master where type='table' and name='config'" ):
60 if row
["name"] == "config":
63 # Create the config table if needed
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
70 latest
= len(self
._schema
)
71 for row
in cursor
.execute( "select ival from config where name='schema'" ):
74 # Update the database as necessary
76 lockdb
= self
.db("EXCLUSIVE")
77 while current
< latest
:
79 lockdb
.execute( self
._schema
[current
] )
80 except sqlite3
.IntegrityError
:
81 lockdb
.execute( "rollback" )
85 lockdb
.execute( "update config set ival=? where name='schema'", (latest
,) )
88 # invalidate cached connection
92 """Basic schema for the piranha.db file."""
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 )''',
102 if __name__
== "__main__":
103 """Piranha is a backend, so by default, do nothing."""