Applied patch from Frederic Peters for bug https://bugs.launchpad.net/appy/+bug/485815 and another bugfix.

This commit is contained in:
Gaetan Delannay 2010-03-25 16:34:37 +01:00
parent 1227f0ed5e
commit 3a7b5be03b
6 changed files with 78 additions and 24 deletions

View file

@ -1,3 +1,6 @@
0.5.4 (2010-03-24)
- Improved gen search screens and many more minor bugfixes and features.
0.5.3 (2010-02-15)
- Improved gen/pod integration by adding a Pod field for appy.gen applications.

View file

@ -2,11 +2,11 @@
developer the real classes used by the underlying web framework.'''
# ------------------------------------------------------------------------------
import os, os.path, time, mimetypes, unicodedata, random
import os, os.path, time, mimetypes, random
import appy.pod
from appy.gen import Search
from appy.gen.utils import sequenceTypes
from appy.shared.utils import getOsTempFolder, executeCommand
from appy.shared.utils import getOsTempFolder, executeCommand, normalizeString
from appy.shared.xml_parser import XmlMarshaller
# Some error messages ----------------------------------------------------------
@ -256,18 +256,7 @@ class AbstractWrapper:
def normalize(self, s, usage='fileName'):
'''Returns a version of string p_s whose special chars have been
replaced with normal chars.'''
# We work in unicode. Convert p_s to unicode if not unicode.
if isinstance(s, str): s = s.decode('utf-8')
elif not isinstance(s, unicode): s = unicode(s)
if usage == 'fileName':
# Remove any char that can't be found within a file name under
# Windows.
res = ''
for char in s:
if char not in self.unwantedChars:
res += char
s = res
return unicodedata.normalize('NFKD', s).encode("ascii","ignore")
return normalizeString(s, usage)
def search(self, klass, sortBy='', maxResults=None, noSecurity=False,
**fields):

View file

@ -196,4 +196,37 @@ class SomeObjects:
if self.noSecurity: getMethod = '_unrestrictedGetObject'
else: getMethod = 'getObject'
self.objects = [getattr(b, getMethod)() for b in brains]
# ------------------------------------------------------------------------------
class Keywords:
'''This class allows to handle keywords that a user enters and that will be
used as basis for performing requests in a Zope ZCTextIndex.'''
toRemove = '?-+*()'
def __init__(self, keywords, operator='AND'):
# Clean the p_keywords that the user has entered.
words = keywords.strip()
if words == '*': words = ''
for c in self.toRemove: words = words.replace(c, ' ')
self.keywords = words.split()
# Store the operator to apply to the keywords (AND or OR)
self.operator = operator
def merge(self, other, append=False):
'''Merges our keywords with those from p_other. If p_append is True,
p_other keywords are appended at the end; else, keywords are appended
at the begin.'''
for word in other.keywords:
if word not in self.keywords:
if append:
self.keywords.append(word)
else:
self.keywords.insert(0, word)
def get(self):
'''Returns the keywords as needed by the ZCTextIndex.'''
if self.keywords:
op = ' %s ' % self.operator
return op.join(self.keywords)+'*'
return ''
# ------------------------------------------------------------------------------

View file

@ -23,6 +23,7 @@ from UserDict import UserDict
import appy.pod
from appy.pod import PodError
from appy.shared import mimeTypesExts
from appy.shared.xml_parser import XmlElement
from appy.shared.utils import FolderDeleter, executeCommand
from appy.pod.pod_parser import PodParser, PodEnvironment, OdInsert
@ -230,11 +231,6 @@ class Renderer:
return ifFalse
imageFormats = ('png', 'jpeg', 'jpg', 'gif')
mimeTypes = {
'application/vnd.oasis.opendocument.text': 'odt',
'application/msword': 'doc', 'text/rtf': 'rtf',
'application/pdf' : 'pdf', 'image/png': 'png',
'image/jpeg': 'jpg', 'image/gif': 'gif'}
ooFormats = ('odt',)
def importDocument(self, content=None, at=None, format=None,
anchor='as-char'):
@ -256,8 +252,8 @@ class Renderer:
format = os.path.splitext(at)[1][1:]
else:
# If format is a mimeType, convert it to an extension
if self.mimeTypes.has_key(format):
format = self.mimeTypes[format]
if mimeTypesExts.has_key(format):
format = mimeTypesExts[format]
isImage = False
if format in self.ooFormats:
importer = OdtImporter
@ -322,6 +318,8 @@ class Renderer:
stylesMapping = self.stylesManager.checkStylesMapping(stylesMapping)
self.stylesManager.stylesMapping = stylesMapping
except PodError, po:
self.contentParser.env.currentBuffer.content.close()
self.stylesParser.env.currentBuffer.content.close()
if os.path.exists(self.tempFolder):
FolderDeleter.delete(self.tempFolder)
raise po
@ -395,7 +393,10 @@ class Renderer:
except Exception, e:
print WARNING_FINALIZE_ERROR % str(e)
resultOdtName = os.path.join(self.tempFolder, 'result.odt')
resultOdt = zipfile.ZipFile(resultOdtName, 'w')
try:
resultOdt = zipfile.ZipFile(resultOdtName,'w', zipfile.ZIP_DEFLATED)
except RuntimeError:
resultOdt = zipfile.ZipFile(resultOdtName,'w')
os.chdir(self.unzipFolder)
for dir, dirnames, filenames in os.walk('.'):
for f in filenames:

View file

@ -7,7 +7,17 @@ appyPath = os.path.realpath(os.path.dirname(appy.__file__))
mimeTypes = {'odt': 'application/vnd.oasis.opendocument.text',
'doc': 'application/msword',
'rtf': 'text/rtf',
'pdf': 'application/pdf'}
'pdf': 'application/pdf'
}
mimeTypesExts = {
'application/vnd.oasis.opendocument.text': 'odt',
'application/msword' : 'doc',
'text/rtf' : 'rtf',
'application/pdf' : 'pdf',
'image/png' : 'png',
'image/jpeg' : 'jpg',
'image/gif' : 'gif'
}
# ------------------------------------------------------------------------------
class UnmarshalledObject:

View file

@ -17,7 +17,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA.
# ------------------------------------------------------------------------------
import os, os.path, sys, traceback
import os, os.path, sys, traceback, unicodedata
# ------------------------------------------------------------------------------
class FolderDeleter:
@ -76,4 +76,22 @@ def executeCommand(cmd, ignoreLines=None):
res = '\n'.join(keptLines)
childStdIn.close(); childStdOut.close(); childStdErr.close()
return res
# ------------------------------------------------------------------------------
unwantedChars = ('\\', '/', ':', '*', '?', '"', '<', '>', '|', ' ')
def normalizeString(s, usage='fileName'):
'''Returns a version of string p_s whose special chars have been
replaced with normal chars.'''
# We work in unicode. Convert p_s to unicode if not unicode.
if isinstance(s, str): s = s.decode('utf-8')
elif not isinstance(s, unicode): s = unicode(s)
if usage == 'fileName':
# Remove any char that can't be found within a file name under
# Windows.
res = ''
for char in s:
if char not in unwantedChars:
res += char
s = res
return unicodedata.normalize('NFKD', s).encode("ascii","ignore")
# ------------------------------------------------------------------------------