mirror of
https://github.com/gryf/pygtktalog.git
synced 2025-12-17 11:30:19 +01:00
* Moving truncated old main class into converter. Old model doesn't
needed anymore.
This commit is contained in:
@@ -24,6 +24,119 @@
|
|||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
|
import tarfile
|
||||||
|
|
||||||
|
try:
|
||||||
|
import sqlite3 as sqlite
|
||||||
|
except ImportError:
|
||||||
|
from pysqlite2 import dbapi2 as sqlite
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
class OldModel(object):
|
||||||
|
"""Create, load, save, manipulate db file which is container for data"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""initialize"""
|
||||||
|
self.db_cursor = None
|
||||||
|
self.db_connection = None
|
||||||
|
self.internal_dirname = None
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
"""remove temporary directory tree from filesystem"""
|
||||||
|
self.__close_db_connection()
|
||||||
|
if self.internal_dirname != None:
|
||||||
|
try:
|
||||||
|
shutil.rmtree(self.internal_dirname)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
return
|
||||||
|
|
||||||
|
def open(self, filename=None):
|
||||||
|
"""try to open db file"""
|
||||||
|
self.__create_internal_dirname()
|
||||||
|
self.filename = filename
|
||||||
|
|
||||||
|
try:
|
||||||
|
tar = tarfile.open(filename, "r:gz")
|
||||||
|
except:
|
||||||
|
try:
|
||||||
|
tar = tarfile.open(filename, "r")
|
||||||
|
except:
|
||||||
|
self.internal_dirname = None
|
||||||
|
return False
|
||||||
|
|
||||||
|
os.chdir(self.internal_dirname)
|
||||||
|
try:
|
||||||
|
tar.extractall()
|
||||||
|
if __debug__:
|
||||||
|
print "OldModel 73: extracted tarfile into",
|
||||||
|
print self.internal_dirname
|
||||||
|
except AttributeError:
|
||||||
|
# python 2.4 tarfile module lacks of method extractall()
|
||||||
|
directories = []
|
||||||
|
for tarinfo in tar:
|
||||||
|
if tarinfo.isdir():
|
||||||
|
# Extract directory with a safe mode, so that
|
||||||
|
# all files below can be extracted as well.
|
||||||
|
try:
|
||||||
|
os.makedirs(os.path.join('.', tarinfo.name), 0700)
|
||||||
|
except EnvironmentError:
|
||||||
|
pass
|
||||||
|
directories.append(tarinfo)
|
||||||
|
else:
|
||||||
|
tar.extract(tarinfo, '.')
|
||||||
|
|
||||||
|
# Reverse sort directories.
|
||||||
|
directories.sort(lambda a, b: cmp(a.name, b.name))
|
||||||
|
directories.reverse()
|
||||||
|
|
||||||
|
# Set correct owner, mtime and filemode on directories.
|
||||||
|
for tarinfo in directories:
|
||||||
|
try:
|
||||||
|
os.chown(os.path.join('.', tarinfo.name),
|
||||||
|
tarinfo.uid, tarinfo.gid)
|
||||||
|
os.utime(os.path.join('.', tarinfo.name),
|
||||||
|
(0, tarinfo.mtime))
|
||||||
|
except OSError:
|
||||||
|
if __debug__:
|
||||||
|
print "OldModel 103: open(): setting corrext owner,",
|
||||||
|
print "mtime etc"
|
||||||
|
tar.close()
|
||||||
|
self.__connect_to_db()
|
||||||
|
return True
|
||||||
|
|
||||||
|
# private class functions
|
||||||
|
def __connect_to_db(self):
|
||||||
|
"""initialize db connection and store it in class attributes"""
|
||||||
|
self.db_connection = sqlite.connect("%s" % \
|
||||||
|
(self.internal_dirname + '/db.sqlite'),
|
||||||
|
detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
|
||||||
|
self.db_cursor = self.db_connection.cursor()
|
||||||
|
return
|
||||||
|
|
||||||
|
def __close_db_connection(self):
|
||||||
|
"""close db conection"""
|
||||||
|
if self.db_cursor != None:
|
||||||
|
self.db_cursor.close()
|
||||||
|
self.db_cursor = None
|
||||||
|
if self.db_connection != None:
|
||||||
|
self.db_connection.close()
|
||||||
|
self.db_connection = None
|
||||||
|
return
|
||||||
|
|
||||||
|
def __create_internal_dirname(self):
|
||||||
|
"""create temporary directory for working thumb/image files and
|
||||||
|
database"""
|
||||||
|
# TODO: change this stupid rutine into tempfile mkdtemp method
|
||||||
|
self.cleanup()
|
||||||
|
self.internal_dirname = "/tmp/pygtktalog%d" % \
|
||||||
|
datetime.now().microsecond
|
||||||
|
try:
|
||||||
|
os.mkdir(self.internal_dirname)
|
||||||
|
except IOError, (errno, strerror):
|
||||||
|
print "OldModel 138: __create_internal_dirname(): ", strerror
|
||||||
|
return
|
||||||
|
|
||||||
def setup_path():
|
def setup_path():
|
||||||
"""Sets up the python include paths to include needed directories"""
|
"""Sets up the python include paths to include needed directories"""
|
||||||
@@ -57,10 +170,9 @@ if __name__ == "__main__":
|
|||||||
from shutil import copy
|
from shutil import copy
|
||||||
|
|
||||||
from utils.img import Img
|
from utils.img import Img
|
||||||
from models.m_main10rc1 import MainModel
|
|
||||||
from models.m_main import MainModel as NewModel
|
from models.m_main import MainModel as NewModel
|
||||||
|
|
||||||
model = MainModel()
|
model = OldModel()
|
||||||
new_model = NewModel()
|
new_model = NewModel()
|
||||||
if not model.open(os.path.join(execution_dir, sys.argv[1])):
|
if not model.open(os.path.join(execution_dir, sys.argv[1])):
|
||||||
print "cannot open katalog in 1.0RC1 format"
|
print "cannot open katalog in 1.0RC1 format"
|
||||||
@@ -71,6 +183,7 @@ if __name__ == "__main__":
|
|||||||
file_id INTEGER,
|
file_id INTEGER,
|
||||||
filename TEXT);""")
|
filename TEXT);""")
|
||||||
|
|
||||||
|
model.db_cursor.execute("delete from thumbnails")
|
||||||
result = model.db_cursor.execute("select file_id, filename from images")
|
result = model.db_cursor.execute("select file_id, filename from images")
|
||||||
# (id, filename)
|
# (id, filename)
|
||||||
# (4921, u'images/13/39.jpg')
|
# (4921, u'images/13/39.jpg')
|
||||||
@@ -83,6 +196,13 @@ if __name__ == "__main__":
|
|||||||
sql = "insert into images2(file_id, filename) values (?, ?)"
|
sql = "insert into images2(file_id, filename) values (?, ?)"
|
||||||
model.db_cursor.execute(sql, (row[0], image))
|
model.db_cursor.execute(sql, (row[0], image))
|
||||||
|
|
||||||
|
model.db_cursor.execute("select id from thumbnails where file_id=?", (row[0], ))
|
||||||
|
thumb = model.db_cursor.fetchone()
|
||||||
|
if not (thumb and thumb[0]):
|
||||||
|
sql = "insert into thumbnails(file_id, filename) values (?, ?)"
|
||||||
|
model.db_cursor.execute(sql, (row[0], image))
|
||||||
|
|
||||||
|
|
||||||
model.db_connection.commit()
|
model.db_connection.commit()
|
||||||
model.db_cursor.execute("drop table images")
|
model.db_cursor.execute("drop table images")
|
||||||
model.db_cursor.execute("alter table images2 rename to images")
|
model.db_cursor.execute("alter table images2 rename to images")
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user