mirror of
https://github.com/gryf/pygtktalog.git
synced 2025-12-17 19:40:21 +01:00
* Changed module function - now it'll be responsible for view
generation with data fetched from db.
This commit is contained in:
95
db.py
95
db.py
@@ -3,41 +3,68 @@
|
|||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from pysqlite2 import dbapi2 as sqlite
|
from pysqlite2 import dbapi2 as sqlite
|
||||||
|
import pygtk
|
||||||
|
import gtk
|
||||||
|
import gobject
|
||||||
|
|
||||||
class dbfile:
|
class dbfile:
|
||||||
def __init__(self,db=None):
|
def __init__(self,winobj,connection,cursor):
|
||||||
if db !=None:
|
self.con = connection
|
||||||
self.con = sqlite.connect("%s" % db, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
|
self.cur = cursor
|
||||||
self.cur = self.con.cursor()
|
self.winobj = winobj
|
||||||
else:
|
# create tree model
|
||||||
name = datetime.datetime.now()
|
self.dirmodel = self.treemodel=gtk.TreeStore(gobject.TYPE_INT, gobject.TYPE_STRING)
|
||||||
name = "/tmp/pygtktalog%d.db" % name.microsecond
|
self.filemodel = self.treemodel=gtk.TreeStore(gobject.TYPE_INT, gobject.TYPE_STRING,gobject.TYPE_STRING)
|
||||||
self.con = sqlite.connect("%s" % name, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
|
|
||||||
self.cur = self.con.cursor()
|
|
||||||
self.createDB()
|
|
||||||
pass
|
|
||||||
def createDB(self):
|
|
||||||
"""make plain new database"""
|
|
||||||
self.cur.execute("create table files(id INTEGER PRIMARY KEY AUTOINCREMENT, filename TEXT, date timestamp, size integer, type integer)")
|
|
||||||
self.cur.execute("create table files_connect(id INTEGER PRIMARY KEY AUTOINCREMENT, parent integer, child integer, depth integer)")
|
|
||||||
|
|
||||||
def populate(self,fileobj):
|
def getDirectories(self,root=0):
|
||||||
"""add fileobject to database"""
|
"""get directory tree from DB"""
|
||||||
print "aa"
|
|
||||||
def make_tree(obj,spc,parent=0,depth=0):
|
self.cur.execute("select count(id) from files where type=1")
|
||||||
"""how about members?"""
|
self.count = self.cur.fetchone()[0]
|
||||||
self.cur.execute("insert into files(filename,date,size,type) values (?,?,?,?)", (obj.name,obj.date,obj.size,obj.filetype))
|
if self.count>0:
|
||||||
self.cur.execute("select seq from sqlite_sequence where name='files'")
|
frac = 1.0/self.count
|
||||||
file_id = self.cur.fetchone()
|
|
||||||
self.cur.execute("insert into files_connect(parent,child,depth) values(?,?,?) ",(file_id[0], file_id[0], 0))
|
|
||||||
parent = parent + 1
|
|
||||||
depth = depth + 1
|
|
||||||
print obj.name, parent, depth
|
|
||||||
for i in obj.members:
|
|
||||||
if i.filetype == "d":
|
|
||||||
print "%s[%s]" % (spc, i.name)
|
|
||||||
else:
|
else:
|
||||||
print "%s%s" % (spc, i.name)
|
frac = 1.0
|
||||||
make_tree(i,spc+".",parent,depth)
|
self.count = 0
|
||||||
make_tree(fileobj,".")
|
|
||||||
return ""
|
if self.winobj.sbid != 0:
|
||||||
|
self.winobj.status.remove(self.winobj.sbSearchCId, self.winobj.sbid)
|
||||||
|
self.winobj.sbid = self.winobj.status.push(self.winobj.sbSearchCId, "Fetching data from catalog file")
|
||||||
|
|
||||||
|
def get_children(id, name, parent):
|
||||||
|
"""fetch all children and place them in model"""
|
||||||
|
#{{{
|
||||||
|
myiter = self.dirmodel.insert_after(parent,None)
|
||||||
|
self.dirmodel.set_value(myiter,0,id)
|
||||||
|
self.dirmodel.set_value(myiter,1,name)
|
||||||
|
self.cur.execute("SELECT o.child, f.filename FROM files_connect o LEFT JOIN files f ON o.child=f.id WHERE o.parent=? AND o.depth=1 AND f.type=1 ORDER BY f.filename",(id,))
|
||||||
|
|
||||||
|
# progress
|
||||||
|
self.winobj.progress.set_fraction(frac * self.count)
|
||||||
|
while gtk.events_pending(): gtk.main_iteration()
|
||||||
|
self.count = self.count + 1
|
||||||
|
|
||||||
|
for cid,name in self.cur.fetchall():
|
||||||
|
get_children(cid, name, myiter)
|
||||||
|
#}}}
|
||||||
|
|
||||||
|
# get initial roots from first, default root (id: 1) in the tree,
|
||||||
|
# and then add other subroots to tree model
|
||||||
|
if __debug__:
|
||||||
|
data = datetime.datetime.now()
|
||||||
|
self.cur.execute("SELECT o.child, f.filename FROM files_connect o LEFT JOIN files f ON o.child=f.id WHERE o.parent=1 AND o.depth=1 AND f.type=1 ORDER BY f.filename")
|
||||||
|
|
||||||
|
# real volumes:
|
||||||
|
for id,name in self.cur.fetchall():
|
||||||
|
get_children(id,name,None)
|
||||||
|
if __debug__:
|
||||||
|
print "[db.py] tree generation time: ", (datetime.datetime.now() - data)
|
||||||
|
|
||||||
|
if self.winobj.sbid != 0:
|
||||||
|
self.winobj.status.remove(self.winobj.sbSearchCId, self.winobj.sbid)
|
||||||
|
self.winobj.sbid = self.winobj.status.push(self.winobj.sbSearchCId, "Idle")
|
||||||
|
|
||||||
|
self.winobj.progress.set_fraction(0)
|
||||||
|
|
||||||
|
return self.dirmodel
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user