def commitTemp(self, fileentry): """ This functions writes the file to the pool directory. If the file is not marked as tempfile, nothing is written. Files are processed in the following order: - a temp path is created - the file is written to this path - the original file is renamed to be deleted on success and stored as `file.deleteOnSuccess` - the tempfile is renamed to the original path - the original file can be removed by calling `Cleanup()` fileentry is the database entry the file is stored for. """ if not self.isTempFile(): # nothing to write -> return return True if not self.fileentry: self.fileentry = weakref.ref(fileentry) maxFileSize = fileentry.maxFileSize if self.size and self.size > maxFileSize: raise IOError, "File too big" # create temp path for current backupPath = None originalPath = DvPath(self._Path()) newPath = DvPath(self._CreatePath(self.filekey, self.filename)) tempPath = DvPath(str(newPath)) tempPath.SetName(u"_temp_" + unicode(uuid.uuid4())) tempPath.SetExtension(newPath.GetExtension()) if tempPath.Exists(): tempPath.Delete() tempPath.CreateDirectories() size = 0 try: out = open(tempPath.GetStr(), "wb") data = self.read(10000) while data: size += len(data) if maxFileSize and size > maxFileSize: raise IOError, "File too big" out.write(data) data = self.read(10000) out.close() #file.close() except Exception, e: try: self.file.close() except: pass try: out.close() except: pass # reset old file tempPath.Delete() raise Exception, e
def app_db(modules=None): a = ApplicationBase() a.Register(appconf) if modules: for m in modules: a.Register(m) p = Portal() p.Register(a, "nive") a.SetupApplication() dbfile = DvPath(a.dbConfiguration.dbName) if not dbfile.IsFile(): dbfile.CreateDirectories() root = DvPath(a.dbConfiguration.fileRoot) if not root.IsDirectory(): root.CreateDirectories() try: a.Query("select id from pool_meta where id=1") a.Query("select id from data1 where id=1") a.Query("select id from data2 where id=1") a.Query("select id from data3 where id=1") a.Query("select id from pool_files where id=1") a.Query("select id from pool_sys where id=1") a.Query("select id from pool_groups where id=1") a.Query("select title from pool_meta where id=1") except: a.GetTool("nive.tools.dbStructureUpdater")() # disable this to update test tables each time the tests are called #a.GetTool("nive.tools.dbStructureUpdater")() a.Startup(None) # this will reset all testdata #emptypool(a) return a
def _Convert(self, profile): source = self.files.get(profile.source) if not source or not source.tempfile: # convert only if tempfile return False, () if not source: return False, [_(u"Image not found: ") + profile.source] p = DvPath() p.SetUniqueTempFileName() p.SetExtension(profile.extension) destPath = str(p) try: try: source.file.seek(0) except: pass try: iObj = Image.open(source) except IOError: # no file to be converted return False, () iObj = iObj.convert("RGB") # resize size = [profile.width, profile.height] if size[0] != 0 or size[1] != 0: if size[0] == 0: size[0] = size[1] elif size[1] == 0: size[1] = size[0] x, y = iObj.size if x > size[0]: y = y * size[0] / x; x = size[0] if y > size[1]: x = x * size[1] / y; y = size[1] size = x, y iObj = iObj.resize(size, Image.ANTIALIAS) iObj.save(destPath, profile.format) try: source.file.seek(0) except: pass # file meta data imgFile = open(destPath) filename = DvPath(profile.dest+"_"+source.filename) filename.SetExtension(profile.extension) file = File(filekey=profile.dest, filename=str(filename), file=imgFile, size=p.GetSize(), path=destPath, extension=profile.extension, tempfile=True) self.files.set(profile.dest, file) finally: # clean temp file p.Delete() return True, []
def test_Create(self): n = self.name p = DvPath(n) self.assert_(p._path == n) self.assert_(str(p) == n) p = DvPath(DvPath(n)) self.assert_(str(p) == n) p.SetStr(n) self.assert_(str(p) == n) self.assert_(p.GetStr() == n)
def _CreatePath(self, key, filename): """ Create the physical path of the file """ root = str(self.fileentry().pool.root) aP = DvPath(root) aP.AppendSeperator() aP.AppendDirectory(self.fileentry().pool._GetDirectory(self.fileentry().id)) aP.AppendSeperator() aP.SetName(u"%06d_%s_" % (self.fileentry().id, key)) aP.SetExtension(DvPath(filename).GetExtension()) return aP.GetStr()
def test_Dirs(self): n = self.base p = DvPath(n) self.assert_(str(p) == n) p.Append("another_dir" + os.sep + "and.file") self.assert_(str(p) == n + "another_dir" + os.sep + "and.file", str(p)) p = DvPath(n) p.AppendDirectory("the_last") self.assert_(str(p) == n + "the_last" + os.sep, str(p)) p.RemoveLastDirectory() self.assert_(str(p) == n) p = DvPath(n[:-1]) p.AppendSeperator() self.assert_(str(p) == self.base)
def _Path(self, absolute = True): """ Get the physical path of the file. Checks the database. """ if self.tempfile or not self.path: return u"" root = str(self.fileentry().pool.root) if absolute and self.path[:len(root)] != root: path = DvPath(root) path.AppendSeperator() path.Append(self.path) else: path = DvPath(self.path) return path.GetStr()
def myapp(modules=None): a = ApplicationBase() a.Register(appconf) a.Register(dbconfMySql) if modules: for m in modules: a.Register(m) p = Portal() p.Register(a, "nive") a.LoadConfiguration() root = DvPath(a.dbConfiguration.fileRoot) if not root.IsDirectory(): root.CreateDirectories() try: a.Query("select id from pool_meta where id=1") a.Query("select id from data1 where id=1") a.Query("select id from data2 where id=1") a.Query("select id from data3 where id=1") a.Query("select id from pool_files where id=1") a.Query("select id from pool_sys where id=1") a.Query("select id from pool_groups where id=1") except: a.GetTool("nive.components.tools.dbStructureUpdater")() a.Startup(None) return a
def setUp(self): self.pool = getPool() dbfile = DvPath(conn["dbName"]) if not dbfile.IsFile(): dbfile.CreateDirectories() self.checkdb() self.connect()
def test_load(self, **kw): p = DvPath(__file__) p.SetNameExtension("app.json") i, c = ResolveConfiguration(str(p)) self.assert_(c) p.SetNameExtension("db.json") c = LoadConfiguration(str(p)) self.assert_(c)
def exists(self): """ check if the file physically exists """ if not self.path: return True path = DvPath(self._Path()) return path.Exists()
def setUp(self): dbfile = DvPath(conn["dbName"]) if not dbfile.IsFile(): dbfile.CreateDirectories() checkdb() db = Sqlite3(conf, conn) db.GetPoolStructureObj().SetStructure(struct) self.pool = db
def _GetTrashcanDirectory(self, id): aP = DvPath() aP.SetStr(str(self.root)) aP.AppendSeperator() aP.AppendDirectory(self.Trashcan) aP.AppendSeperator() aP.AppendDirectory(self._GetDirectory(id)) aP.AppendSeperator() return aP
def setUp(self): conn = DatabaseConf(DB_CONF) p = Sqlite3(connParam=conn, **test_Base.conf) p.structure.Init(structure=test_Base.struct, stdMeta=test_Base.struct[u"pool_meta"]) dbfile = DvPath(conn["dbName"]) if not dbfile.IsFile(): dbfile.CreateDirectories() self.pool = p self.pool.connection.connect()
def test_fncs(self): temp = tempfile.gettempdir() p = DvPath(temp) p.AppendDirectory("tmp_nivepathtest_000") p.Delete(deleteSubdirs=True) p.CreateDirectories() self.assert_(p.IsDirectory()) p.CreateDirectoriesExcp() p.Rename("tmp_nivepathtest_111") p.AppendDirectory("tmp_nivepathtest_000") p.AppendDirectory("tmp_nivepathtest_000") p.CreateDirectories() self.assert_(p.IsDirectory()) p = DvPath(temp) p.AppendDirectory("tmp_nivepathtest_000") p.Delete(deleteSubdirs=False) self.assert_(p.IsDirectory() == True) p.Delete(deleteSubdirs=True) self.assert_(p.IsDirectory() == False)
def InitFileStorage(self, root, connectionParam): """ Set the local root path for files """ self.root = DvPath() self.root.SetStr(root) if root == u"": return self.root.AppendSeperator() self.root.CreateDirectoriesExcp()
def test_Set(self): n = self.name p = DvPath(n) self.assert_(str(p) == n) p.SetName("new") self.assert_(str(p) == self.base + "new.txt", str(p)) p.SetExtension("png") self.assert_(str(p) == self.base + "new.png") p.SetNameExtension("another.txt") self.assert_(str(p) == self.base + "another.txt")
def delete(self): if not self.path: return True originalPath = DvPath(self._Path()) if not originalPath.IsFile(): #not a file return True if originalPath.Exists() and not self.fileentry().pool._MoveToTrashcan(originalPath, self.fileentry().id): #Delete failed! return False return True
def test_Get(self): n = self.name p = DvPath(n) self.assert_(p.GetPath() == self.base) self.assert_(p.GetName() == "nofile") self.assert_(p.GetExtension() == "txt") self.assert_(p.GetNameExtension() == "nofile.txt") self.assert_(p.GetSize() == -1) self.assert_(p.GetLastDirectory() == "tmp_nivepathtest_000") p.IsFile() p.IsDirectory() p.Exists() p.IsValid()
def fromPath(self, path): """ Set temp file and load file values from file path """ if self.filename == "": self.filename = os.path.basename(path) f, fileExtension = os.path.splitext(path) self.extension = fileExtension[1:6] if self.size == 0: p = DvPath(path) self.size = p.GetSize() self.tempfile = True self.file = open(path, "rb")
def Cleanup(self, files): """ Cleanup tempfiles after succesful writes """ if not isinstance(files, dict): files = {"":files} for key, file in files.items(): if not hasattr(file, "deleteOnSuccess"): continue path = DvPath(file.deleteOnSuccess) try: path.Delete() except: pass
def app_db(confs=None): a = DataStorage() a.Register(appconf) if confs: for c in confs: a.Register(c) p = Portal() p.Register(a) a.Startup(None) dbfile = DvPath(a.dbConfiguration.dbName) if not dbfile.IsFile(): dbfile.CreateDirectories() try: a.Query("select id from pool_meta where id=1") a.Query("select pool_wfp from pool_meta where id=1") a.Query("select id from bookmarks where id=1") a.Query("select id from tracks where id=1") a.Query("select id from pool_files where id=1") except: a.GetTool("nive.tools.dbStructureUpdater")() return a
def __test(): dbfile = DvPath(conn["dbName"]) if not dbfile.IsFile(): dbfile.CreateDirectories() checkdb() db = Sqlite3(conf, conn) db.GetPoolStructureObj().SetStructure(struct) app = Sqlite3ConnMultithreading() app.pool = db app.setUp() threadcnt = 10 ts = [] for i in range(threadcnt): t = ThreadClass() ts.append(t) t.app = app t.start() for t in ts: t.join() print "OK"
def app(confs=[]): a = WebsitePublisher() a.Register(appconf) a.Register(dbconf) for c in confs: a.Register(c) p = Portal() p.Register(a) a.Startup(None) dbfile = DvPath(a.dbConfiguration.dbName) if not dbfile.IsFile(): dbfile.CreateDirectories() try: a.Query("select id from pool_meta where id=1") a.Query("select id from data1 where id=1") a.Query("select id from data2 where id=1") a.Query("select id from data3 where id=1") a.Query("select id from pool_files where id=1") except: a.GetTool("nive.components.tools.dbStructureUpdater")() return a
def app_db(confs=None): appconf = AppConf("nive_cms.app") a = WebsitePublisher() a.Register(appconf) if confs: for c in confs: a.Register(c) p = Portal() p.Register(a) a.Startup(None) dbfile = DvPath(a.dbConfiguration.dbName) if not dbfile.IsFile(): dbfile.CreateDirectories() try: a.Query("select id from pool_meta where id=1") a.Query("select id from box where id=1") a.Query("select id from columnbox where id=1") a.Query("select id from texts where id=1") a.Query("select id from pool_files where id=1") except: a.GetTool("nive.tools.dbStructureUpdater")() return a
def app(extmodules=None): appconf = AppConf("nive.userdb.app") appconf.modules.append("nive.userdb.userview.view") appconf.modules.append("nive.components.tools.sendMail") a = UserDB(appconf) a.dbConfiguration = dbconf p = Portal() p.Register(a) a.Startup(None) dbfile = DvPath(a.dbConfiguration.dbName) if not dbfile.IsFile(): dbfile.CreateDirectories() try: a.Query("select id from pool_meta where id=1") a.Query("select id from data1 where id=1") a.Query("select id from data2 where id=1") a.Query("select id from data3 where id=1") a.Query("select id from pool_files where id=1") except: a.GetTool("nive.components.tools.dbStructureUpdater")() return a
def app_db(confs=None): appconf = AppConf("nive_userdb.app") appconf.modules.append("nive_userdb.userview.view") appconf.modules.append("nive.tools.sendMailTester") a = UserDB() a.Register(appconf) if confs: for c in confs: a.Register(c) p = Portal() p.Register(a) a.Startup(None) dbfile = DvPath(a.dbConfiguration.dbName) if not dbfile.IsFile(): dbfile.CreateDirectories() try: a.Query("select id from pool_meta where id=1") a.Query("select id from users where id=1") a.Query("select id from users where token='1'") a.Query("select id from users where tempcache='1'") a.Query("select id from pool_files where id=1") except: a.GetTool("nive.tools.dbStructureUpdater")() return a
def tearDown(self): self.db.Close() p = DvPath(dbpath) p.Delete() pass
def setUp(self): p = DvPath(dbpath) p.Delete() self.db = Sqlite3Manager() self.db.Connect(dbpath) pass
def test_tempfilename(self): p = DvPath("file.txt") p.SetUniqueTempFileName() self.assert_(p.GetExtension() == "txt", p.GetExtension())