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

* Code clean up with guidlines of pain in the ass pylint.

* Added method for fetching tag for files with id, tag name and so on.
This commit is contained in:
2008-04-24 13:20:01 +00:00
parent 8080ccc7f0
commit 0e9f1c5503

View File

@@ -26,7 +26,6 @@ import os
import sys import sys
import shutil import shutil
import tarfile import tarfile
import string
import math import math
import gtk import gtk
@@ -82,8 +81,8 @@ class MainModel(ModelMT):
'xbm', 'xpm', 'jp2', 'jpx', 'pnm'] 'xbm', 'xpm', 'jp2', 'jpx', 'pnm']
def __init__(self): def __init__(self):
"""initialize"""
ModelMT.__init__(self) ModelMT.__init__(self)
self.config = ConfigModel()
self.unsaved_project = False self.unsaved_project = False
self.filename = None # catalog saved/opened filename self.filename = None # catalog saved/opened filename
self.internal_dirname = None self.internal_dirname = None
@@ -91,7 +90,15 @@ class MainModel(ModelMT):
self.db_cursor = None self.db_cursor = None
self.abort = False self.abort = False
self.source = self.CD self.source = self.CD
self.config = ConfigModel()
self.config.load() self.config.load()
self.path = None
self.label = None
self.currentid = None
self.thread = None
self.busy = False
self.statusmsg = "Idle"
self.selected_tags = {}
# Directory tree: id, name, icon, type # Directory tree: id, name, icon, type
self.discs_tree = gtk.TreeStore(gobject.TYPE_INT, gobject.TYPE_STRING, self.discs_tree = gtk.TreeStore(gobject.TYPE_INT, gobject.TYPE_STRING,
@@ -118,53 +125,87 @@ class MainModel(ModelMT):
self.tag_cloud = [] self.tag_cloud = []
return return
def add_tags(self, id, tags): def add_tags(self, fid, tags):
"""add tag (if not exists) and connect it with file"""
for tag in tags.split(','): for tag in tags.split(','):
tag = tag.strip() tag = tag.strip()
# first, checkerap if we already have tag in tags table # SQL: first, chek if we already have tag in tags table; get id
sql = """SELECT id FROM tags WHERE tag = ?""" sql = """SELECT id FROM tags WHERE tag = ?"""
self.db_cursor.execute(sql, (tag, )) self.db_cursor.execute(sql, (tag, ))
res = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if not res: if not res:
# insert new tag # SQL: insert new tag
sql = """INSERT INTO tags(tag, group_id) sql = """INSERT INTO tags(tag, group_id)
VALUES(?, ?)""" VALUES(?, ?)"""
self.db_cursor.execute(sql, (tag, 1)) self.db_cursor.execute(sql, (tag, 1))
self.db_connection.commit() self.db_connection.commit()
# SQL: get tag id
sql = """SELECT id FROM tags WHERE tag = ?""" sql = """SELECT id FROM tags WHERE tag = ?"""
self.db_cursor.execute(sql, (tag, )) self.db_cursor.execute(sql, (tag, ))
res = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
tag_id = res[0] tag_id = res[0]
# then checkout if file have already tag assigned # SQL: then checkout if file have already tag assigned
sql = """SELECT file_id FROM tags_files sql = """SELECT file_id FROM tags_files
WHERE file_id = ? AND tag_id = ?""" WHERE file_id = ? AND tag_id = ?"""
self.db_cursor.execute(sql, (id, tag_id)) self.db_cursor.execute(sql, (fid, tag_id))
res = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if not res: if not res:
# SQL: connect tag with file
sql = """INSERT INTO tags_files(file_id, tag_id) sql = """INSERT INTO tags_files(file_id, tag_id)
VALUES(?, ?)""" VALUES(?, ?)"""
self.db_cursor.execute(sql, (id, tag_id)) self.db_cursor.execute(sql, (fid, tag_id))
self.db_connection.commit() self.db_connection.commit()
self.get_tags() self.get_tags()
return return
def get_tag_by_id(self, tag_id): def get_tag_by_id(self, tag_id):
"""get tag (string) by its id""" """get tag (string) by its id"""
# SQL: get tag by id
sql = """SELECT tag FROM tags WHERE id = ?""" sql = """SELECT tag FROM tags WHERE id = ?"""
self.db_cursor.execute(sql, (tag_id, )) self.db_cursor.execute(sql, (int(tag_id), ))
res = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if not res: if not res:
return None return None
return res[0] return res[0]
def get_file_tags(self, file_id):
"""get tags of file"""
# SQL: get tag by id
sql = """SELECT t.id, t.tag FROM tags t
LEFT JOIN tags_files f ON t.id=f.tag_id
WHERE f.file_id = ?
ORDER BY t.tag"""
self.db_cursor.execute(sql, (int(file_id), ))
res = self.db_cursor.fetchall()
tmp = {}
if len(res) == 0:
return None
for row in res:
tmp[row[0]] = row[1]
return tmp
def get_tags(self): def get_tags(self):
"""fill tags dict with values from db"""
if not self.selected_tags:
sql = """SELECT COUNT(f.file_id), t.id, t.tag FROM tags_files f sql = """SELECT COUNT(f.file_id), t.id, t.tag FROM tags_files f
LEFT JOIN tags t ON f.tag_id = t.id LEFT JOIN tags t ON f.tag_id = t.id
GROUP BY f.tag_id GROUP BY f.tag_id
ORDER BY t.tag""" ORDER BY t.tag"""
else:
id_filter = self.__filter()
sql = """SELECT COUNT(f.file_id), t.id, t.tag FROM tags_files f
LEFT JOIN tags t ON f.tag_id = t.id
WHERE f.file_id in """ + str(tuple(id_filter)) + \
"""GROUP BY f.tag_id
ORDER BY t.tag"""
self.db_cursor.execute(sql) self.db_cursor.execute(sql)
res = self.db_cursor.fetchall() res = self.db_cursor.fetchall()
@@ -177,71 +218,81 @@ class MainModel(ModelMT):
'count': row[0], 'count': row[0],
'color':'black'}) 'color':'black'})
def tag_weight(x): def tag_weight(initial_value):
"""Calculate 'weight' of tag. """Calculate 'weight' of tag.
Tags can have sizes between 9 to ~40. Upper size is calculated with Tags can have sizes between 9 to ~40. Upper size is calculated with
logarythm and can take in extereme situation around value 55 like logarythm and can take in extereme situation around value 55 like
for 1 milion tags.""" for 1 milion tags."""
if x==None or x==0: if not initial_value:
x = 1 initial_value = 1
return 4 * math.log(x, math.e) return 4 * math.log(initial_value, math.e)
# correct font sizes with tag_weight function. # correct font sizes with tag_weight function.
count = 0 count = 0
for ta in self.tag_cloud: for tag0 in self.tag_cloud:
tmp = int(tag_weight(ta['size'])) tmp = int(tag_weight(tag0['size']))
if tmp == 0: if tmp == 0:
tmp = 1 tmp = 1
self.tag_cloud[count]['size'] = tmp + 8 self.tag_cloud[count]['size'] = tmp + 8
count += 1 count += 1
def add_image(self, image, id, only_thumbs=False): def add_tag_to_path(self, tag_id):
"""add tag to filter"""
temp = {}
tag_name = self.get_tag_by_id(tag_id)
for i in self.selected_tags:
temp[i] = self.selected_tags[i]
temp[int(tag_id)] = tag_name
self.selected_tags = temp
def add_image(self, image, file_id, only_thumbs=False):
"""add single image to file/directory""" """add single image to file/directory"""
sql = """INSERT INTO images(file_id, thumbnail, filename) sql = """INSERT INTO images(file_id, thumbnail, filename)
VALUES(?, null, null)""" VALUES(?, null, null)"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
self.db_connection.commit() self.db_connection.commit()
sql = """SELECT id FROM images WHERE thumbnail is null sql = """SELECT id FROM images WHERE thumbnail is null
AND filename IS null AND file_id=?""" AND filename IS null AND file_id=?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
res = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if res: if res:
tp, ip, rc = Img(image, self.internal_dirname).save(res[0]) thp, imp, rec = Img(image, self.internal_dirname).save(res[0])
if rc != -1: if rec != -1:
sql = """UPDATE images SET filename=?, sql = """UPDATE images SET filename=?,
thumbnail=? WHERE id=?""" thumbnail=? WHERE id=?"""
if only_thumbs: if only_thumbs:
img = None img = None
else: else:
img = ip.split(self.internal_dirname)[1][1:] img = imp.split(self.internal_dirname)[1][1:]
self.db_cursor.execute(sql, self.db_cursor.execute(sql,
(img, (img,
tp.split(self.internal_dirname)[1][1:], thp.split(self.internal_dirname)[1][1:],
res[0])) res[0]))
self.db_connection.commit() self.db_connection.commit()
def del_images(self, id): def del_images(self, file_id):
"""removes images and their thumbnails from selected file/dir""" """removes images and their thumbnails from selected file/dir"""
# remove images # remove images
sql = """SELECT filename, thumbnail FROM images WHERE file_id =?""" sql = """SELECT filename, thumbnail FROM images WHERE file_id =?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
res = self.db_cursor.fetchall() res = self.db_cursor.fetchall()
if len(res) > 0: if len(res) > 0:
for fn in res: for filen in res:
if fn[0]: if filen[0]:
os.unlink(os.path.join(self.internal_dirname, fn[0])) os.unlink(os.path.join(self.internal_dirname, filen[0]))
os.unlink(os.path.join(self.internal_dirname, fn[1])) os.unlink(os.path.join(self.internal_dirname, filen[1]))
# remove images records # remove images records
sql = """DELETE FROM images WHERE file_id = ?""" sql = """DELETE FROM images WHERE file_id = ?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
self.db_connection.commit() self.db_connection.commit()
def delete_image(self, id): def delete_image(self, image_id):
"""removes image on specified image id""" """removes image on specified image id"""
sql = """SELECT filename, thumbnail FROM images WHERE id=?""" sql = """SELECT filename, thumbnail FROM images WHERE id=?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (image_id,))
res = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if res: if res:
if res[0]: if res[0]:
@@ -254,57 +305,56 @@ class MainModel(ModelMT):
print res[1] print res[1]
# remove images records # remove images records
sql = """DELETE FROM images WHERE id = ?""" sql = """DELETE FROM images WHERE id = ?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (image_id,))
self.db_connection.commit() self.db_connection.commit()
def add_thumbnail(self, img_fn, id): def add_thumbnail(self, img_fn, file_id):
"""generate and add thumbnail to selected file/dir""" """generate and add thumbnail to selected file/dir"""
if self.config.confd['thumbs']: if self.config.confd['thumbs']:
self.del_thumbnail(id) self.del_thumbnail(file_id)
p, e, ret_code = Thumbnail(img_fn, thumb = Thumbnail(img_fn, base=self.internal_dirname)
base=self.internal_dirname).save(id) retval = thumb.save(file_id)
if ret_code != -1: if retval[2] != -1:
path = retval[0].split(self.internal_dirname)[1][1:]
sql = """insert into thumbnails(file_id, filename) sql = """insert into thumbnails(file_id, filename)
values (?, ?)""" values (?, ?)"""
self.db_cursor.execute(sql, self.db_cursor.execute(sql, (file_id, path))
(id,
p.split(self.internal_dirname)[1][1:]))
self.db_connection.commit() self.db_connection.commit()
return True return True
return False return False
def del_thumbnail(self, id): def del_thumbnail(self, file_id):
"""removes thumbnail from selected file/dir""" """removes thumbnail from selected file/dir"""
# remove thumbnail files # remove thumbnail files
sql = """SELECT filename FROM thumbnails WHERE file_id=?""" sql = """SELECT filename FROM thumbnails WHERE file_id=?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
res = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if res: if res:
os.unlink(os.path.join(self.internal_dirname, res[0])) os.unlink(os.path.join(self.internal_dirname, res[0]))
# remove thumbs records # remove thumbs records
sql = """DELETE FROM thumbnails WHERE file_id=?""" sql = """DELETE FROM thumbnails WHERE file_id=?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
self.db_connection.commit() self.db_connection.commit()
def cleanup(self): def cleanup(self):
"""remove temporary directory tree from filesystem"""
self.__close_db_connection() self.__close_db_connection()
if self.internal_dirname != None: if self.internal_dirname != None:
try: try:
shutil.rmtree(self.internal_dirname) shutil.rmtree(self.internal_dirname)
except: except OSError:
pass pass
return return
def new(self): def new(self):
"""create new project"""
self.unsaved_project = False self.unsaved_project = False
self.__create_internal_dirname() self.__create_internal_dirname()
self.__connect_to_db() self.__connect_to_db()
self.__create_database() self.__create_database()
self.__clear_trees() self.__clear_trees()
return return
def save(self, filename=None): def save(self, filename=None):
@@ -329,10 +379,10 @@ class MainModel(ModelMT):
try: try:
tar = tarfile.open(filename, "r:gz") tar = tarfile.open(filename, "r:gz")
except: except IOError:
try: try:
tar = tarfile.open(filename, "r") tar = tarfile.open(filename, "r")
except: except IOError:
self.filename = None self.filename = None
self.internal_dirname = None self.internal_dirname = None
return return
@@ -364,16 +414,14 @@ class MainModel(ModelMT):
# Set correct owner, mtime and filemode on directories. # Set correct owner, mtime and filemode on directories.
for tarinfo in directories: for tarinfo in directories:
path = os.path.join('.', tarinfo.name)
try: try:
os.chown(tarinfo, '.') os.chown(tarinfo, '.')
os.utime(tarinfo, '.') os.utime(tarinfo, '.')
os.chmod(tarinfo, '.') os.chmod(tarinfo, '.')
except: except OSError:
if __debug__: if __debug__:
print "m_main.py: open(): setting corrext owner,", print "m_main.py: open(): setting corrext owner,",
print "mtime etc" print "mtime etc"
pass
tar.close() tar.close()
self.__connect_to_db() self.__connect_to_db()
@@ -398,10 +446,11 @@ class MainModel(ModelMT):
self.thread.start() self.thread.start()
return return
def rename(self, id, new_name=None): def rename(self, file_id, new_name=None):
"""change name of selected object id"""
if new_name: if new_name:
self.db_cursor.execute("update files set filename=? \ self.db_cursor.execute("update files set filename=? \
WHERE id=?", (new_name, id)) WHERE id=?", (new_name, file_id))
self.db_connection.commit() self.db_connection.commit()
self.__fetch_db_into_treestore() self.__fetch_db_into_treestore()
self.unsaved_project = True self.unsaved_project = True
@@ -411,63 +460,88 @@ class MainModel(ModelMT):
return return
def refresh_discs_tree(self): def refresh_discs_tree(self):
"""re-fetch discs tree"""
self.__fetch_db_into_treestore() self.__fetch_db_into_treestore()
def get_root_entries(self, id=None): def get_root_entries(self, parent_id):
"""Get all children down from sepcified root""" """Get all children down from sepcified root"""
try: self.__clear_files_tree()
self.files_list.clear()
except:
pass
# directories first # directories first
if not self.selected_tags:
sql = """SELECT id, filename, size, date FROM files sql = """SELECT id, filename, size, date FROM files
WHERE parent_id=? AND type=1 WHERE parent_id=? AND type=1
ORDER BY filename""" ORDER BY filename"""
self.db_cursor.execute(sql, (id,)) else:
id_filter = self.__filter()
if id_filter != None:
sql = """SELECT id, filename, size, date FROM files
WHERE parent_id=? AND type=1 AND id in """ + \
str(tuple(id_filter)) + """ ORDER BY filename"""
else:
sql="""SELECT id, filename, size, date FROM files
WHERE 1=0"""
self.db_cursor.execute(sql, (parent_id,))
data = self.db_cursor.fetchall() data = self.db_cursor.fetchall()
for ch in data: for row in data:
myiter = self.files_list.insert_before(None, None) myiter = self.files_list.insert_before(None, None)
self.files_list.set_value(myiter, 0, ch[0]) self.files_list.set_value(myiter, 0, row[0])
self.files_list.set_value(myiter, 1, ch[1]) self.files_list.set_value(myiter, 1, row[1])
self.files_list.set_value(myiter, 2, ch[2]) self.files_list.set_value(myiter, 2, row[2])
self.files_list.set_value(myiter, 3, self.files_list.set_value(myiter, 3,
datetime.fromtimestamp(ch[3])) datetime.fromtimestamp(row[3]))
self.files_list.set_value(myiter, 4, 1) self.files_list.set_value(myiter, 4, 1)
self.files_list.set_value(myiter, 5, 'direktorja') self.files_list.set_value(myiter, 5, 'direktorja')
self.files_list.set_value(myiter, 6, gtk.STOCK_DIRECTORY) self.files_list.set_value(myiter, 6, gtk.STOCK_DIRECTORY)
# all the rest # all the rest
if not self.selected_tags:
sql = """SELECT f.id, f.filename, f.size, f.date, f.type sql = """SELECT f.id, f.filename, f.size, f.date, f.type
FROM files f FROM files f
WHERE f.parent_id=? AND f.type!=1 WHERE f.parent_id=? AND f.type!=1
ORDER BY f.filename""" ORDER BY f.filename"""
self.db_cursor.execute(sql, (id,)) else:
if id_filter != None:
sql = """SELECT f.id, f.filename, f.size, f.date, f.type
FROM files f
WHERE f.parent_id=? AND f.type!=1 AND id IN """ + \
str(tuple(id_filter)) + """ ORDER BY f.filename"""
else:
sql="""SELECT f.id, f.filename, f.size, f.date, f.type
FROM files f
WHERE 1=0"""
self.db_cursor.execute(sql, (parent_id,))
data = self.db_cursor.fetchall() data = self.db_cursor.fetchall()
for ch in data: for row in data:
myiter = self.files_list.insert_before(None, None) myiter = self.files_list.insert_before(None, None)
self.files_list.set_value(myiter, 0, ch[0]) self.files_list.set_value(myiter, 0, row[0])
self.files_list.set_value(myiter, 1, ch[1]) self.files_list.set_value(myiter, 1, row[1])
self.files_list.set_value(myiter, 2, ch[2]) self.files_list.set_value(myiter, 2, row[2])
self.files_list.set_value(myiter, 3, datetime.fromtimestamp(ch[3])) self.files_list.set_value(myiter, 3,
self.files_list.set_value(myiter, 4, ch[4]) datetime.fromtimestamp(row[3]))
self.files_list.set_value(myiter, 4, row[4])
self.files_list.set_value(myiter, 5, 'kategoria srategoria') self.files_list.set_value(myiter, 5, 'kategoria srategoria')
if ch[4] == self.FIL: if row[4] == self.FIL:
self.files_list.set_value(myiter, 6, gtk.STOCK_FILE) self.files_list.set_value(myiter, 6, gtk.STOCK_FILE)
elif ch[4] == self.LIN: elif row[4] == self.LIN:
self.files_list.set_value(myiter, 6, gtk.STOCK_INDEX) self.files_list.set_value(myiter, 6, gtk.STOCK_INDEX)
return return
def get_parent_discs_value(self, child_id): def get_parent_discs_value(self, child_id):
"""get root id from specified child"""
if child_id: if child_id:
sql = """SELECT parent_id FROM files WHERE id=?""" sql = """SELECT parent_id FROM files WHERE id=?"""
self.db_cursor.execute(sql, (child_id,)) self.db_cursor.execute(sql, (child_id,))
set = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if set: if res:
return set[0] return res[0]
return None return None
def get_file_info(self, id): def get_file_info(self, file_id):
"""get file info from database""" """get file info from database"""
retval = {} retval = {}
sql = """SELECT f.filename, f.date, f.size, f.type, sql = """SELECT f.filename, f.date, f.size, f.type,
@@ -475,34 +549,34 @@ class MainModel(ModelMT):
FROM files f FROM files f
LEFT JOIN thumbnails t ON t.file_id = f.id LEFT JOIN thumbnails t ON t.file_id = f.id
WHERE f.id = ?""" WHERE f.id = ?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
set = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if set: if res:
retval['debug'] = {'id': id, retval['debug'] = {'id': file_id,
'date': datetime.fromtimestamp(set[1]), 'date': datetime.fromtimestamp(res[1]),
'size': set[2], 'type': set[3]} 'size': res[2], 'type': res[3]}
retval['filename'] = set[0] retval['filename'] = res[0]
if set[5]: if res[5]:
retval['description'] = set[5] retval['description'] = res[5]
if set[6]: if res[6]:
retval['note'] = set[6] retval['note'] = res[6]
if set[4]: if res[4]:
pa = os.path.join(self.internal_dirname, set[4]) path = os.path.join(self.internal_dirname, res[4])
retval['thumbnail'] = pa retval['thumbnail'] = path
sql = """SELECT id, filename, thumbnail FROM images sql = """SELECT id, thumbnail FROM images
WHERE file_id = ?""" WHERE file_id = ?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
set = self.db_cursor.fetchall() res = self.db_cursor.fetchall()
if set: if res:
self.images_store = gtk.ListStore(gobject.TYPE_INT, gtk.gdk.Pixbuf) self.images_store = gtk.ListStore(gobject.TYPE_INT, gtk.gdk.Pixbuf)
for idi, img, thb in set: for idi, thb in res:
im = os.path.join(self.internal_dirname,thb) img = os.path.join(self.internal_dirname, thb)
pix = gtk.gdk.pixbuf_new_from_file(im) pix = gtk.gdk.pixbuf_new_from_file(img)
self.images_store.append([idi, pix]) self.images_store.append([idi, pix])
retval['images'] = True retval['images'] = True
@@ -511,27 +585,27 @@ class MainModel(ModelMT):
flash, light_source, resolution, orientation flash, light_source, resolution, orientation
FROM exif FROM exif
WHERE file_id = ?""" WHERE file_id = ?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
set = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if set: if res:
self.exif_list = gtk.ListStore(gobject.TYPE_STRING, self.exif_list = gtk.ListStore(gobject.TYPE_STRING,
gobject.TYPE_STRING) gobject.TYPE_STRING)
for key in self.EXIF_DICT: for key in self.EXIF_DICT:
myiter = self.exif_list.insert_before(None, None) myiter = self.exif_list.insert_before(None, None)
self.exif_list.set_value(myiter, 0, self.EXIF_DICT[key]) self.exif_list.set_value(myiter, 0, self.EXIF_DICT[key])
self.exif_list.set_value(myiter, 1, set[key]) self.exif_list.set_value(myiter, 1, res[key])
retval['exif'] = True retval['exif'] = True
# gthumb # gthumb
sql = """SELECT note, place, date FROM gthumb WHERE file_id = ?""" sql = """SELECT note, place, date FROM gthumb WHERE file_id = ?"""
self.db_cursor.execute(sql, (id,)) self.db_cursor.execute(sql, (file_id,))
set = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if set: if res:
retval['gthumb'] = {'note': set[0], retval['gthumb'] = {'note': res[0],
'place': set[1], 'place': res[1],
'date': set[2]} 'date': res[2]}
return retval return retval
@@ -558,9 +632,9 @@ class MainModel(ModelMT):
return res[0], res[1] return res[0], res[1]
def delete(self, root_id, db_cursor=None, db_connection=None): def delete(self, root_id, db_cursor=None, db_connection=None):
"""Remove subtree from main tree, remove tags from database """Remove subtree (item and its children) from main tree, remove tags
remove all possible data, like thumbnails, images, gthumb info, exif from database remove all possible data, like thumbnails, images, gthumb
etc""" info, exif etc"""
fids = [] fids = []
@@ -576,6 +650,7 @@ class MainModel(ModelMT):
parent_id = res[0] parent_id = res[0]
def get_children(fid): def get_children(fid):
"""get children of specified id"""
fids.append(fid) fids.append(fid)
sql = """SELECT id FROM files where parent_id = ?""" sql = """SELECT id FROM files where parent_id = ?"""
db_cursor.execute(sql, (fid,)) db_cursor.execute(sql, (fid,))
@@ -587,8 +662,9 @@ class MainModel(ModelMT):
get_children(root_id) get_children(root_id)
def generator(): def generator():
for c in fids: """simple generator for use in executemany() function"""
yield (c,) for field in fids:
yield (field,)
# remove files records # remove files records
sql = """DELETE FROM files WHERE id = ?""" sql = """DELETE FROM files WHERE id = ?"""
@@ -605,8 +681,8 @@ class MainModel(ModelMT):
db_cursor.execute(sql) db_cursor.execute(sql)
res = db_cursor.fetchall() res = db_cursor.fetchall()
if len(res) > 0: if len(res) > 0:
for fn in res: for row in res:
os.unlink(os.path.join(self.internal_dirname, fn[0])) os.unlink(os.path.join(self.internal_dirname, row[0]))
# remove images # remove images
sql = """SELECT filename, thumbnail FROM images sql = """SELECT filename, thumbnail FROM images
@@ -614,10 +690,10 @@ class MainModel(ModelMT):
db_cursor.execute(sql) db_cursor.execute(sql)
res = db_cursor.fetchall() res = db_cursor.fetchall()
if len(res) > 0: if len(res) > 0:
for fn in res: for row in res:
if res[0]: if row[0]:
os.unlink(os.path.join(self.internal_dirname, fn[0])) os.unlink(os.path.join(self.internal_dirname, row[0]))
os.unlink(os.path.join(self.internal_dirname, fn[1])) os.unlink(os.path.join(self.internal_dirname, row[1]))
# remove thumbs records # remove thumbs records
sql = """DELETE FROM thumbnails WHERE file_id = ?""" sql = """DELETE FROM thumbnails WHERE file_id = ?"""
@@ -674,6 +750,7 @@ class MainModel(ModelMT):
parents = [] parents = []
def _recurse(fid): def _recurse(fid):
"""recursive gather direcotories ids and store it in list"""
parents.append(fid) parents.append(fid)
sql = """SELECT id FROM files sql = """SELECT id FROM files
WHERE type=? AND parent_id=? AND parent_id!=1""" WHERE type=? AND parent_id=? AND parent_id!=1"""
@@ -693,10 +770,10 @@ class MainModel(ModelMT):
files_count = 0 files_count = 0
for p in parents: for parent in parents:
sql = """SELECT count(id) FROM files sql = """SELECT count(id) FROM files
WHERE type!=0 AND type!=1 AND parent_id=?""" WHERE type!=0 AND type!=1 AND parent_id=?"""
self.db_cursor.execute(sql, (p,)) self.db_cursor.execute(sql, (parent,))
res = self.db_cursor.fetchone() res = self.db_cursor.fetchone()
if res: if res:
files_count += res[0] files_count += res[0]
@@ -746,14 +823,15 @@ class MainModel(ModelMT):
return os.path.join(self.internal_dirname, res[0]) return os.path.join(self.internal_dirname, res[0])
return None return None
def update_desc_and_note(self, id, desc='', note=''): def update_desc_and_note(self, file_id, desc='', note=''):
"""update note and description""" """update note and description"""
sql = """UPDATE files SET description=?, note=? WHERE id=?""" sql = """UPDATE files SET description=?, note=? WHERE id=?"""
self.db_cursor.execute(sql, (desc, note, id)) self.db_cursor.execute(sql, (desc, note, file_id))
return return
# private class functions # private class functions
def __bytes_to_human(self, integer): def __bytes_to_human(self, integer):
"""return integer digit in human readable string representation"""
if integer <= 0 or integer < 1024: if integer <= 0 or integer < 1024:
return "%d bytes" % integer return "%d bytes" % integer
@@ -763,31 +841,35 @@ class MainModel(ModelMT):
# s_int = str(integer)[-3:] # s_int = str(integer)[-3:]
# else: # else:
# s_int = str(integer)[-(3*int(i)+3):-(3*int(i))] + " " + s_int # s_int = str(integer)[-(3*int(i)+3):-(3*int(i))] + " " + s_int
power = None
t = integer temp = integer
for power in ['kB', 'MB', 'GB', 'TB']: for power in ['kB', 'MB', 'GB', 'TB']:
t = t /1024.0 temp = temp /1024.0
if t < 1 or t < 1024: if temp < 1 or temp < 1024:
break break
return "%0.2f %s (%d bytes)" % (t, power, integer) return "%0.2f %s (%d bytes)" % (temp, power, integer)
def __clear_trees(self): def __clear_trees(self):
"""clears treemodel and treestore of files and discs tree"""
self.__clear_files_tree() self.__clear_files_tree()
self.__clear_discs_tree() self.__clear_discs_tree()
def __clear_discs_tree(self): def __clear_discs_tree(self):
"""try to clear model for discs"""
try: try:
self.discs_tree.clear() self.discs_tree.clear()
except: except:
pass pass
def __clear_files_tree(self): def __clear_files_tree(self):
"""try to clear store for files/directories"""
try: try:
self.files_list.clear() self.files_list.clear()
except: except:
pass pass
def __connect_to_db(self): def __connect_to_db(self):
"""initialize db connection and store it in class attributes"""
self.db_connection = sqlite.connect("%s" % \ self.db_connection = sqlite.connect("%s" % \
(self.internal_dirname + '/db.sqlite'), (self.internal_dirname + '/db.sqlite'),
detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES) detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
@@ -795,6 +877,7 @@ class MainModel(ModelMT):
return return
def __close_db_connection(self): def __close_db_connection(self):
"""close db conection"""
if self.db_cursor != None: if self.db_cursor != None:
self.db_cursor.close() self.db_cursor.close()
self.db_cursor = None self.db_cursor = None
@@ -804,6 +887,8 @@ class MainModel(ModelMT):
return return
def __create_internal_dirname(self): def __create_internal_dirname(self):
"""create temporary directory for working thumb/image files and
database"""
self.cleanup() self.cleanup()
self.internal_dirname = "/tmp/pygtktalog%d" % \ self.internal_dirname = "/tmp/pygtktalog%d" % \
datetime.now().microsecond datetime.now().microsecond
@@ -814,6 +899,8 @@ class MainModel(ModelMT):
return return
def __compress_and_save(self): def __compress_and_save(self):
"""create (and optionaly compress) tar archive from working directory
and write it to specified file"""
try: try:
if self.config.confd['compress']: if self.config.confd['compress']:
tar = tarfile.open(self.filename, "w:gz") tar = tarfile.open(self.filename, "w:gz")
@@ -894,6 +981,51 @@ class MainModel(ModelMT):
self.db_cursor.execute(sql) self.db_cursor.execute(sql)
self.db_connection.commit() self.db_connection.commit()
def __filter(self):
"""return list of ids of files (AND their parent, even if they have no
assigned tags) that corresponds to tags"""
filtered_ids = []
count = 0
for tid in self.selected_tags:
temp1 = []
sql = """SELECT file_id
FROM tags_files
WHERE tag_id=? """
self.db_cursor.execute(sql, (tid, ))
data = self.db_cursor.fetchall()
for row in data:
temp1.append(row[0])
if count > 0:
filtered_ids = list(set(filtered_ids).intersection(temp1))
else:
filtered_ids = temp1
count += 1
parents = []
for i in filtered_ids:
sql = """SELECT parent_id
FROM files
WHERE id = ?"""
self.db_cursor.execute(sql, (i, ))
data = self.db_cursor.fetchone()
if data:
parents.append(data[0])
while True:
sql = """SELECT parent_id
FROM files
WHERE id = ? and id!=parent_id"""
self.db_cursor.execute(sql, (data[0], ))
data = self.db_cursor.fetchone()
if not data:
break
else:
parents.append(data[0])
return list(set(parents).union(filtered_ids))
def __scan(self): def __scan(self):
"""scan content of the given path""" """scan content of the given path"""
self.busy = True self.busy = True
@@ -1125,7 +1257,7 @@ class MainModel(ModelMT):
# Extensions - user defined actions # Extensions - user defined actions
if ext in self.config.confd['extensions'].keys(): if ext in self.config.confd['extensions'].keys():
cmd = self.config.confd['extensions'][ext] cmd = self.config.confd['extensions'][ext]
arg = string.replace(current_file, '"', '\\"') arg = current_file.replace('"', '\\"')
output = os.popen(cmd % arg).readlines() output = os.popen(cmd % arg).readlines()
desc = '' desc = ''
for line in output: for line in output:
@@ -1192,22 +1324,28 @@ class MainModel(ModelMT):
db_cursor = db_connection.cursor() db_cursor = db_connection.cursor()
# fetch all the directories # fetch all the directories
#try:
if not self.selected_tags:
sql = """SELECT id, parent_id, filename FROM files sql = """SELECT id, parent_id, filename FROM files
WHERE type=1 ORDER BY parent_id, filename""" WHERE type=1 ORDER BY parent_id, filename"""
db_cursor.execute(sql) else:
data = db_cursor.fetchall() id_filter = self.__filter()
try: if id_filter != None:
sql = """SELECT id, parent_id, filename FROM files sql = """SELECT id, parent_id, filename FROM files
WHERE type=1 ORDER BY parent_id, filename""" WHERE type=1 and id in """ + str(tuple(id_filter)) \
+ """ ORDER BY parent_id, filename"""
else:
sql="""SELECT id, parent_id, filename FROM files
WHERE 1=0"""
db_cursor.execute(sql) db_cursor.execute(sql)
data = db_cursor.fetchall() data = db_cursor.fetchall()
except: #except:
# cleanup # # cleanup
self.cleanup() # self.cleanup()
self.filename = None # self.filename = None
self.internal_dirname = None # self.internal_dirname = None
print "%s: Wrong database format!" % self.filename # print "%s: Wrong database format!" % self.filename
return # return
def get_children(parent_id = 1, iterator = None): def get_children(parent_id = 1, iterator = None):
"""fetch all children and place them in model""" """fetch all children and place them in model"""
@@ -1280,6 +1418,8 @@ class MainModel(ModelMT):
return return
def __decode_filename(self, txt): def __decode_filename(self, txt):
"""decode filename with encoding taken form ENV, returns unicode
string"""
if self.fsenc: if self.fsenc:
return txt.decode(self.fsenc) return txt.decode(self.fsenc)
else: else: