示例#1
0
文件: db_app.py 项目: comlorda/nive
def createObj1file(c):
    type = "type1"
    data = data1_1.copy()
    data["file1"] = File(**file2_1)
    data["file2"] = File(**file2_2)
    user = User(u"test")
    o = c.Create(type, data=data, user=user)
    #o.Commit()
    return o
示例#2
0
    def test_create(self):
        #   File( filekey="",
        #         filename="",
        #         file=None,
        #         size=0,
        #         path="",
        #         extension="",
        #         fileid=0,
        #         uid="",
        #         tempfile=False,
        #         filedict=None,
        #         fileentry=None)
        file = File(filekey="aaa", filename="qqqq.png", file="0123456789")
        self.assert_(file.filekey == "aaa")
        self.assert_(file.filename == "qqqq.png")
        self.assert_(file.size == 10)
        self.assert_(file.extension == "png")

        file = File(filekey="aaa",
                    filename="qqqq.png",
                    file="0123456789",
                    fileentry=fileentrytest())
        self.assert_(file.filekey == "aaa")
        self.assert_(file.filename == "qqqq.png")
        self.assert_(file.size == 10)
        self.assert_(file.extension == "png")

        file = File(filekey="aaa", path="/tmp/qqqq.png")
        self.assert_(file.filekey == "aaa")
        self.assert_(file.filename == "qqqq.png")
        self.assert_(file.extension == "png")

        file = File(
            filedict={
                "filekey": "aaa",
                "filename": "qqqq.png",
                "file": "0123456789",
                "fileentry": fileentrytest()
            })
        self.assert_(file.filekey == "aaa")
        self.assert_(file.filename == "qqqq.png")
        self.assert_(file.size == 10)
        self.assert_(file.extension == "png")
示例#3
0
 def __setitem__(self, key, filedata):
     """
     filedata can be a dictionary, File object or file path
     """
     if not filedata:
         if key in self._temp_:
             del self._temp_[key]
         elif self._content_ and key in self._content_:
             del self._content_[key]
         return
     if isinstance(filedata, dict):
         file = File(key, filedict=filedata, fileentry=self._entry_())
         filedata = file
     elif isinstance(filedata, bytes):
         # load from temp path
         file = File(key, fileentry=self._entry_())
         file.fromPath(filedata)
         filedata = file
     filedata.tempfile = True
     self._temp_[key] = filedata
示例#4
0
    def test_dict(self):
        file = File(filekey="aaa",
                    filename="qqqq.png",
                    file="0123456789",
                    fileentry=fileentrytest())
        self.assert_(file.get("filekey") == "aaa")
        self.assert_(file.get("filename") == "qqqq.png")
        self.assert_(file.get("size") == 10)

        self.assert_(file["filekey"] == "aaa")
        self.assert_(file["filename"] == "qqqq.png")
        self.assert_(file["size"] == 10)

        self.assert_(file.get("none", 123) == 123)

        file.update({"filekey": "bbb", "filename": "oooo.png"})
        self.assert_(file.get("filekey") == "bbb")
        self.assert_(file.get("filename") == "oooo.png")

        for k in file:
            self.assert_(k)
示例#5
0
    def test_dict(self):
        file = File(filekey="aaa",
                    filename="qqqq.png", 
                    file="0123456789",
                    fileentry=fileentrytest()
                    )
        self.assert_(file.get("filekey")=="aaa")
        self.assert_(file.get("filename")=="qqqq.png")
        self.assert_(file.get("size")==10)

        self.assert_(file["filekey"]=="aaa")
        self.assert_(file["filename"]=="qqqq.png")
        self.assert_(file["size"]==10)

        self.assert_(file.get("none",123)==123)
        
        file.update({"filekey":"bbb", "filename":"oooo.png"})
        self.assert_(file.get("filekey")=="bbb")
        self.assert_(file.get("filename")=="oooo.png")
        
        for k in file:
            self.assert_(k)
示例#6
0
 def __setitem__(self, key, filedata):
     """
     filedata can be a dictionary, File object or file path
     """
     if not filedata:
         if key in self._temp_:
             del self._temp_[key]
         elif self._content_ and key in self._content_:
             del self._content_[key]
         return
     if isinstance(filedata, dict):
         file = File(key, filedict=filedata, fileentry=self._entry_())
         filedata = file
     elif isinstance(filedata, bytes):
         # load from temp path
         file = File(key, fileentry=self._entry_())
         file.fromPath(filedata)
         filedata = file
     filedata.tempfile = True
     self._temp_[key] = filedata
示例#7
0
    def test_read(self):
        file = File("aaa", filename="import.zip", tempfile=True)
        self.assert_(file.isTempFile())
        self.assert_(file.filename=="import.zip")
        self.assert_(file.extension=="zip")
        self.assertRaises(IOError, file.read)

        file = File(filekey="aaa",
                    filename="qqqq.png", 
                    file="0123456789",
                    fileentry=fileentrytest()
                    )
        self.assert_(file.read()=="0123456789")

        file = File(filekey="aaa",
                    filename="qqqq.png", 
                    file="0123456789",
                    fileentry=fileentrytest()
                    )
        self.assert_(file.read(5)=="01234")
        self.assert_(file.read(5)=="56789")
        self.assert_(file.read(5)=="")
        self.assert_(file.tell()==10)
        file.seek(0)
        self.assert_(file.tell()==0)

        file = File(filekey="aaa",
                    file=None,
                    fileentry=fileentrytest()
                    )
        self.assertRaises(IOError, file.read)
        file.close()
        self.assertFalse(file.isTempFile())
示例#8
0
    def test_path(self):
        root = resource_filename('nive.utils.dataPool2', 'tests/')
        file = File("aaa")
        file.fromPath(root+"test_db.py")
        self.assert_(file.filename=="test_db.py")
        self.assert_(file.extension=="py")
        self.assertFalse(file.abspath())

        file = File(filekey="aaa",
                    file=None
                    )
        file.fileentry=fileentrytest
        file.path = root+"test_db.py"
        self.assert_(file.abspath().startswith(root))
        
        file = File("aaa", filename="qqqq.png", size=10)
        file.fromPath(root+"test_db.py")
        self.assert_(file.filename=="qqqq.png")
        self.assert_(file.extension=="png")
示例#9
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]

        try:
            source.file.seek(0)
        except:
            pass
        try:
            iObj = Image.open(source)
        except IOError:
            # no file to be converted
            return False, ()

        # skip conversion if image is smaller
        if profile.width > iObj.size[0]:
            # copy file
            filename = DvPath(profile.dest + "_" + source.filename)
            file = File(filekey=profile.dest,
                        filename=str(filename),
                        file=source.file,
                        size=source.size,
                        path=source.path,
                        extension=source.extension,
                        tempfile=True)
            self.files.set(profile.dest, file)
            return True, []

        p = DvPath()
        p.SetUniqueTempFileName()
        p.SetExtension(profile.extension)
        destPath = str(p)

        # 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.convert("RGB")
        iObj = iObj.resize(size, Image.ANTIALIAS)
        iObj.save(destPath, profile.format or image.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)
        return True, []
示例#10
0
    def test_read(self):
        file = File("aaa", filename="import.zip", tempfile=True)
        self.assert_(file.isTempFile())
        self.assert_(file.filename == "import.zip")
        self.assert_(file.extension == "zip")
        self.assertRaises(IOError, file.read)

        file = File(filekey="aaa",
                    filename="qqqq.png",
                    file="0123456789",
                    fileentry=fileentrytest())
        self.assert_(file.read() == "0123456789")

        file = File(filekey="aaa",
                    filename="qqqq.png",
                    file="0123456789",
                    fileentry=fileentrytest())
        self.assert_(file.read(5) == "01234")
        self.assert_(file.read(5) == "56789")
        self.assert_(file.read(5) == "")
        self.assert_(file.tell() == 10)
        file.seek(0)
        self.assert_(file.tell() == 0)

        file = File(filekey="aaa", file=None, fileentry=fileentrytest())
        self.assertRaises(IOError, file.read)
        file.close()
        self.assertFalse(file.isTempFile())
示例#11
0
    def test_path(self):
        root = resource_filename('nive.utils.dataPool2', 'tests/')
        file = File("aaa")
        file.fromPath(root + "test_db.py")
        self.assert_(file.filename == "test_db.py")
        self.assert_(file.extension == "py")
        self.assertFalse(file.abspath())

        file = File(filekey="aaa", file=None)
        file.fileentry = fileentrytest
        file.path = root + "test_db.py"
        self.assert_(file.abspath().startswith(root))

        file = File("aaa", filename="qqqq.png", size=10)
        file.fromPath(root + "test_db.py")
        self.assert_(file.filename == "qqqq.png")
        self.assert_(file.extension == "png")
示例#12
0
 def test_jsonencoder(self):
     self.assert_(JsonDataEncoder().encode({}) == "{}")
     self.assert_(JsonDataEncoder().encode({"a": "a", "b": 1}))
     self.assert_(JsonDataEncoder().encode({"a": datetime.now()}))
     self.assert_(JsonDataEncoder().encode({"a": File()}))