1
0
mirror of https://github.com/gryf/pygtktalog.git synced 2025-12-17 11:30:19 +01:00

* Refactored code in configuration and main controllers.

This commit is contained in:
2007-06-23 17:10:54 +00:00
parent 4edcc521cd
commit df9ea38a03
2 changed files with 230 additions and 13 deletions

View File

@@ -6,6 +6,9 @@ from gtkmvc import Model
import sys
import os
import gtk
import gobject
from ConfigParser import ConfigParser
class Ini(object):
@@ -62,7 +65,8 @@ class ConfigModel(Model):
"vertical panes":"v",
"export xls":"exportxls",
"cd drive":"cd",
"eject command":"eject",
"eject command":"ejectapp",
"eject":"eject",
"image support":"pil",
'confirm quit':'confirmquit',
'warn mount/umount errors':'mntwarn',
@@ -86,7 +90,7 @@ class ConfigModel(Model):
'showstatusbar',
)
dstring = ('cd','eject')
dstring = ('cd','ejectapp')
try:
path = os.environ['HOME']
@@ -95,12 +99,15 @@ class ConfigModel(Model):
def __init__(self):
Model.__init__(self)
self.category_tree = gtk.ListStore(gobject.TYPE_STRING)
return
def save(self):
try:
os.lstat("%s/.pygtktalog" % self.path)
except:
print "Saving preferences to %s/.pygtktalog" % self.path
if __debug__:
print "Saving preferences to %s/.pygtktalog" % self.path
newIni = Ini()
newIni.add_section("pyGTKtalog conf")
for opt in self.dictconf:
@@ -109,7 +116,8 @@ class ConfigModel(Model):
f = open("%s/.pygtktalog" % self.path,"w")
success = True
except:
print "Cannot open config file %s for writing." % (self.path, "/.pygtktalog")
if __debug__:
print "Cannot open config file %s for writing." % (self.path, "/.pygtktalog")
success = False
f.write(newIni.show())
f.close()
@@ -138,6 +146,7 @@ class ConfigModel(Model):
if __debug__:
print "load config file failed"
pass
def __str__(self):
"""show prefs in string way"""
string = "[varname]\tvalue\n"

View File

@@ -2,21 +2,229 @@
import utils._importer
import utils.globals
from gtkmvc import Model
from gtkmvc.model_mt import ModelMT
from models.m_config import ConfigModel
try:
import threading as _threading
except ImportError:
if __debug__:
print "import exception: _threading"
import dummy_threading as _threading
class MainModel(Model):
"""Our model contains a numeric counter and a numeric value that
holds the value that the counter must be assigned to when we the
model is reset"""
from pysqlite2 import dbapi2 as sqlite
import datetime
import mx.DateTime
import os
import mimetypes
from m_config import ConfigModel
class MainModel(ModelMT):
"""Create, load, save, manipulate db file which is container for our data"""
__properties__ = {}
__properties__ = {'busy': False, 'statusmsg': '', 'progress': 0}
config = ConfigModel()
unsaved_project = False
filename = None
internal_filename = None
modified = False
db_connection = None
db_cursor = None
def __init__(self):
Model.__init__(self)
self.config = ConfigModel()
ModelMT.__init__(self)
self.config.load()
return
def cleanup(self):
self.__close_db_connection()
if self.internal_filename != None:
try:
os.unlink(self.internal_filename)
except:
if __debug__:
print "m_db.py: 31"
print self.internal_filename
pass
try:
os.unlink(self.internal_filename + '-journal')
except:
if __debug__:
print "m_db.py: 38"
print self.internal_filename+'-journal'
pass
return
def load(self):
pass
def new(self):
self.modified = False
self.__create_internal_filename()
self.__connect_to_db()
self.__create_database()
return
def save(self):
return
def scan(self,path,label):
"""scan files in separated thread"""
# flush buffer to release db lock.
self.db_connection.commit()
self.path = path
self.label = label
if self.busy:
return
self.thread = _threading.Thread(target=self.__scan)
self.thread.start()
return
# private class functions
def __connect_to_db(self):
self.db_connection = sqlite.connect("%s" % self.internal_filename, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
self.db_cursor = self.db_connection.cursor()
return
def __close_db_connection(self):
if self.db_cursor != None:
self.db_cursor.close()
if self.db_connection != None:
self.db_connection.close()
return
def __create_internal_filename(self):
self.cleanup()
self.internal_filename = "/tmp/pygtktalog%d.db" % datetime.datetime.now().microsecond
return
def __compress(self):
return
def __create_database(self):
"""make all necessary tables in db file"""
self.db_cursor.execute("create table files(id INTEGER PRIMARY KEY AUTOINCREMENT, filename TEXT, date datetime, size integer, type integer);")
self.db_cursor.execute("create table files_connect(id INTEGER PRIMARY KEY AUTOINCREMENT, parent numeric, child numeric, depth numeric);")
self.db_cursor.execute("insert into files values(1, 'root', 0, 0, 0);")
self.db_cursor.execute("insert into files_connect values(1, 1, 1, 0);")
def __scan(self):
"""scan content of the given path"""
# TODO: zmienić sposób pobierania danych do bazy, bo wooolne to jak pies!
self.busy = True
# jako, że to jest w osobnym wątku, a sqlite się przypieprza, że musi mieć
# konekszyn dla tego samego wątku, więc robimy osobne połączenie dla tego zadania.
db_connection = sqlite.connect("%s" % self.internal_filename, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
db_cursor = db_connection.cursor()
timestamp = datetime.datetime.now()
mime = mimetypes.MimeTypes()
mov_ext = ('mkv','avi','ogg','mpg','wmv','mp4','mpeg')
img_ext = ('jpg','jpeg','png','gif','bmp','tga','tif','tiff','ilbm','iff','pcx')
# count files in directory tree
count = 0
#if self.sbid != 0:
# self.status.remove(self.sbSearchCId, self.sbid)
self.statusmsg = "Calculating number of files in directory tree..."
for root,kat,plik in os.walk(self.path):
for p in plik:
count+=1
step = 1.0/count
self.count = 0
def __recurse(path,name,wobj,date=0,frac=0,size=0,idWhere=1):
"""recursive scans the path
path = path string
name = field name
wobj = obiekt katalogu
date = data pliku
frac - kolejny krok potrzebny w np. statusbarze.
idWhere - simple id parent, or "place" where to add node
"""
#{{{
_size = size
walker = os.walk(path)
root,dirs,files = walker.next()
ftype = 1
db_cursor.execute("insert into files(filename, date, size, type) values(?,?,?,?)",(name, date, 0, ftype))
db_cursor.execute("select seq FROM sqlite_sequence WHERE name='files'")
currentid=db_cursor.fetchone()[0]
db_cursor.execute("insert into files_connect(parent,child,depth) values(?,?,?)",(currentid, currentid, 0))
if idWhere>0:
db_cursor.execute("insert into files_connect(parent, child, depth) select r1.parent, r2.child, r1.depth + r2.depth + 1 as depth FROM files_connect r1, files_connect r2 WHERE r1.child = ? AND r2.parent = ? ",(idWhere, currentid))
for i in dirs:
st = os.stat(os.path.join(root,i))
_size = _size + __recurse(os.path.join(path,i),i,wobj,st.st_mtime,frac,0,currentid)
for i in files:
self.count = self.count + 1
st = os.stat(os.path.join(root,i))
### scan files
# if i[-3:].lower() in mov_ext or \
# mime.guess_type(i)!= (None,None) and \
# mime.guess_type(i)[0].split("/")[0] == 'video':
# # video only
# info = filetypeHelper.guess_video(os.path.join(root,i))
# elif i[-3:].lower() in img_ext or \
# mime.guess_type(i)!= (None,None) and \
# mime.guess_type(i)[0].split("/")[0] == 'image':
# pass
### end of scan
### progress/status
# if wobj.sbid != 0:
# wobj.status.remove(wobj.sbSearchCId, wobj.sbid)
self.statusmsg = "Scannig: %s" % (os.path.join(root,i))
self.progress = frac * self.count
# # PyGTK FAQ entry 23.20
# while gtk.events_pending(): gtk.main_iteration()
_size = _size + st.st_size
db_cursor.execute('insert into files(filename, date, size, type) values(?,?,?,?)',(i, st.st_mtime, st.st_size,2))
db_cursor.execute("select seq FROM sqlite_sequence WHERE name='files'")
currentfileid=db_cursor.fetchone()[0]
db_cursor.execute("insert into files_connect(parent,child,depth) values(?,?,?)",(currentfileid, currentfileid, 0))
if currentid>0:
db_cursor.execute("insert into files_connect(parent, child, depth) select r1.parent, r2.child, r1.depth + r2.depth + 1 as depth FROM files_connect r1, files_connect r2 WHERE r1.child = ? AND r2.parent = ? ",(currentid, currentfileid))
db_connection.commit()
db_cursor.execute("update files set size=? where id=?",(_size, currentid))
return _size
#}}}
#self.db_connection.commit()
__recurse(self.path,self.label,self,0,step)
db_connection.commit()
db_cursor.close()
db_connection.close()
self.modified = True
self.busy = False
#self.__display_main_tree()
#if self.sbid != 0:
# self.status.remove(self.sbSearchCId, self.sbid)
self.statusmsg = "Idle"
self.progress = 0
if __debug__:
print "scan time: ", (datetime.datetime.now() - timestamp)
pass # end of class