From 47bcf87a5c0a14acdd7dc265af85f38048e6f5e1 Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Wed, 23 Jan 2013 11:47:50 +0100 Subject: [PATCH] [pod] Use, when available, Python's uuid module to generate images and file names to be included in pod results. --- pod/doc_importers.py | 21 +++++++++++++++++---- pod/renderer.py | 9 +++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/pod/doc_importers.py b/pod/doc_importers.py index 1fb83f8..623af46 100644 --- a/pod/doc_importers.py +++ b/pod/doc_importers.py @@ -23,6 +23,11 @@ from appy.pod.odf_parser import OdfEnvironment from appy.shared import mimeTypesExts from appy.shared.utils import FileWrapper from appy.shared.dav import Resource +# The uuid module is there only if python >= 2.5 +try: + import uuid +except ImportError: + uuid = None # ------------------------------------------------------------------------------ FILE_NOT_FOUND = "'%s' does not exist or is not a file." @@ -81,6 +86,15 @@ class DocImporter: # ImageImporter adds image-specific attrs, through # ImageImporter.setImageInfo. + def getUuid(self): + '''Creates a unique id for images/documents to be imported into an + ODT document.''' + if uuid: + return uuid.uuid4().hex + else: + # The uuid module is not there. Generate a UUID based on random. + return 'f%d.%f' % (random.randint(0,1000), time.time()) + def getImportFolder(self): '''This method must be overridden and gives the path where to dump the content of the document or image. In the case of a document it is a @@ -95,7 +109,7 @@ class DocImporter: format = '' # We will know it only after the HTTP GET. else: format = os.path.splitext(at)[1][1:] - fileName = 'f.%d.%f.%s' % (random.randint(0,1000), time.time(), format) + fileName = '%s.%s' % (self.getUuid(), format) return os.path.abspath('%s/%s' % (self.importFolder, fileName)) def moveFile(self, at, importPath): @@ -288,7 +302,6 @@ class ImageImporter(DocImporter): t = self.textNs x = self.linkNs s = self.svgNs - imageName = 'Image%f.%d' % (time.time(), random.randint(0,1000)) # Compute path to image i = self.importPath.rfind(self.pictFolder) imagePath = self.importPath[i+1:].replace('\\', '/') @@ -332,8 +345,8 @@ class ImageImporter(DocImporter): image = '<%s:frame %s%s:name="%s" %s:z-index="0" ' \ '%s:anchor-type="%s"%s><%s:image %s:type="simple" ' \ '%s:show="embed" %s:href="%s" %s:actuate="onLoad"/>' \ - '' % (d, styleInfo, d, imageName, d, t, self.anchor, - size, d, x, x, x, imagePath, x, d) + '' % (d, styleInfo, d, self.getUuid(), d, t, + self.anchor, size, d, x, x, x, imagePath, x, d) if hasattr(self, 'wrapInPara') and self.wrapInPara: image = '<%s:p>%s' % (t, image, t) self.res += image diff --git a/pod/renderer.py b/pod/renderer.py index 38e8188..24717c4 100644 --- a/pod/renderer.py +++ b/pod/renderer.py @@ -501,12 +501,9 @@ class Renderer: resultOdt = zipfile.ZipFile(resultOdtName,'w') # Insert first the file "mimetype" (uncompressed), in order to be # compliant with the OpenDocument Format specification, section 17.4, - # that has a restriction when it comes to zip containers: the file - # called "mimetype" must be at the beginning of the zip file, it must be - # uncompressed and it must be stored without any additional file - # attributes. Else, libraries like "magic", under Linux/Unix, are unable - # to detect the correct mimetype for POD results (it simply recognizes - # it as a "application/zip" and not a + # that expresses this restriction. Else, libraries like "magic", under + # Linux/Unix, are unable to detect the correct mimetype for a pod result + # (it simply recognizes it as a "application/zip" and not a # "application/vnd.oasis.opendocument.text)". resultOdt.write(os.path.join(self.unzipFolder, 'mimetype'), 'mimetype', zipfile.ZIP_STORED)