[pod] Use, when available, Python's uuid module to generate images and file names to be included in pod results.

This commit is contained in:
Gaetan Delannay 2013-01-23 11:47:50 +01:00
parent 5d31bc10db
commit 47bcf87a5c
2 changed files with 20 additions and 10 deletions

View file

@ -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"/>' \
'</%s:frame>' % (d, styleInfo, d, imageName, d, t, self.anchor,
size, d, x, x, x, imagePath, x, d)
'</%s:frame>' % (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</%s:p>' % (t, image, t)
self.res += image

View file

@ -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)