Пример #1
0
    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
Пример #2
0
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
Пример #3
0
 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, []
Пример #4
0
 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)
Пример #5
0
    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()
Пример #6
0
 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)
Пример #7
0
 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()
Пример #8
0
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
Пример #9
0
 def setUp(self):
     self.pool = getPool()
     dbfile = DvPath(conn["dbName"])
     if not dbfile.IsFile():
         dbfile.CreateDirectories()
     self.checkdb()
     self.connect()
Пример #10
0
 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)
Пример #11
0
 def exists(self):
     """
     check if the file physically exists
     """
     if not self.path:
         return True
     path = DvPath(self._Path())
     return path.Exists()
Пример #12
0
 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
Пример #13
0
 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
Пример #14
0
 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()
Пример #15
0
 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)
Пример #16
0
 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()
Пример #17
0
 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")
Пример #18
0
 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
Пример #19
0
 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()
Пример #20
0
 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")
Пример #21
0
 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
Пример #22
0
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
Пример #23
0
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"
Пример #24
0
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
Пример #25
0
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
Пример #26
0
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
Пример #27
0
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
Пример #28
0
 def tearDown(self):
     self.db.Close()
     p = DvPath(dbpath)
     p.Delete()
     pass
Пример #29
0
 def setUp(self):
     p = DvPath(dbpath)
     p.Delete()
     self.db = Sqlite3Manager()
     self.db.Connect(dbpath)
     pass
Пример #30
0
 def test_tempfilename(self):
     p = DvPath("file.txt")
     p.SetUniqueTempFileName()
     self.assert_(p.GetExtension() == "txt", p.GetExtension())