Back to index

plone3  3.1.7
Classes | Functions | Variables
ATContentTypes.lib.validators Namespace Reference

Classes

class  TALValidator
class  TidyHtmlValidator
class  TidyHtmlWithCleanupValidator
class  NonEmptyFileValidator

Functions

def doTidy
def wrapValueInHTML
def unwrapValueFromHTML
def correctEncoding
 

remove 2 spaces from the beginning of each line

nlines = [] for line in body.split('
'): print line if line[:2] == ' ': nlines.append(line[2:]) else: nlines.append(line)
def parseErrorData

Variables

string __author__ = 'Christian Heimes <tiran@cheimes.de>'
string __docformat__ = 'restructuredtext'
tuple logger = logging.getLogger('ATCT')
tuple RE_MATCH_WARNING = re.compile('^line (\d+) column (\d+) - Warning: (.*)$')
string WARNING_LINE = 'line %d column %d - Warning: %s'
tuple RE_MATCH_ERROR = re.compile('^line (\d+) column (\d+) - Error: (.*)$')
string ERROR_LINE = 'line %d column %d - Error: %s'
tuple RE_BODY = re.compile('<body[^>]*?>(.*)</body>', re.DOTALL )
tuple RE_GET_HTML_ENCODING = re.compile('(<meta.*?content-type.*?charset[\s]*=[\s]*)([^"]*?)("[^>]*?>)', re.S | re.I)
int SUBTRACT_LINES = 11
list validatorList = []

Function Documentation

remove 2 spaces from the beginning of each line

nlines = [] for line in body.split('
'): print line if line[:2] == ' ': nlines.append(line[2:]) else: nlines.append(line)

return '\n'.join(nlines)  @verbatim correct the encoding of a html-page if we know it an mxTidy

expects an other encoding

Definition at line 302 of file validators.py.

00302 
00303 def correctEncoding(value):
00304     """correct the encoding of a html-page if we know it an mxTidy
00305        expects an other encoding 
00306     """
00307 
00308     # we have nothing to do if mxTidy has no
00309     # fixed char_encoding
00310     if not MX_TIDY_OPTIONS.has_key('char_encoding')  \
00311            or ( MX_TIDY_OPTIONS['char_encoding'] == 'raw'):
00312         return value
00313 
00314     match = RE_GET_HTML_ENCODING.search(value)
00315     if match:
00316         groups = match.groups()
00317 
00318         # lookup encodings in the pyhon encodings database
00319         # returns function-pointers that we can compare
00320         # need to normalize encodings a bit before
00321         html_encoding = groups[1].strip().lower()
00322         char_encoding = MX_TIDY_OPTIONS['char_encoding'].lower().strip()
00323         h_enc = encodings.search_function(html_encoding)
00324         c_enc = encodings.search_function(char_encoding)
00325 
00326         # one encoding is missing or they are equal
00327         if not (h_enc and c_enc) or  h_enc == c_enc:
00328             return value
00329         else:
00330             try:
00331                 return unicode(value, html_encoding).encode(char_encoding)
00332             except:
00333                 logger.info("Error correcting encoding from %s to %s" % (html_encoding, char_encoding))
00334     return value

Here is the call graph for this function:

Here is the caller graph for this function:

def ATContentTypes.lib.validators.doTidy (   value,
  field,
  request,
  cleanup = 0 
)
Tidy the data in 'value' for the field in the current request

Optional cleanup:
  * removes header/footer of the output data
  * Removes warnings from the error data

Return None for 'nothing done'
else return (nerrors, nwarnings, outputdata, errordata)

Definition at line 221 of file validators.py.

00221 
00222 def doTidy(value, field, request, cleanup=0):
00223     """Tidy the data in 'value' for the field in the current request
00224 
00225     Optional cleanup:
00226       * removes header/footer of the output data
00227       * Removes warnings from the error data
00228 
00229     Return None for 'nothing done'
00230     else return (nerrors, nwarnings, outputdata, errordata)
00231     """
00232     # we can't use the mimetype from the field because it's updated *after*
00233     # validation so we must get it from the request
00234     tf_name     = '%s_text_format' % field.getName()
00235     text_format = getattr(request, tf_name, '')
00236 
00237     # MX_TIDY_MIMETYPES configuration option isn't empty
00238     # and the current text_format isn't in the list
00239     if MX_TIDY_MIMETYPES and text_format not in MX_TIDY_MIMETYPES:
00240         # do not filter this mime type
00241         return
00242 
00243     # it's a file upload
00244     if isinstance(value, FileUpload):
00245         # *mmh* ok it's a file upload but a file upload could destroy
00246         # the layout, too.
00247         # the validator can be called many times, we have to rewind
00248         # the FileUpload.
00249         value.seek(0)
00250         value = correctEncoding(value.read())
00251     else:
00252         value = wrapValueInHTML(value)
00253 
00254     result = mx_tidy(value, **MX_TIDY_OPTIONS)
00255     nerrors, nwarnings, outputdata, errordata = result
00256 
00257     # parse and change the error data
00258     errordata = parseErrorData(errordata, removeWarnings=cleanup)
00259     if cleanup:
00260         # unwrap tidied output data
00261         outputdata = unwrapValueFromHTML(outputdata)
00262 
00263     return nerrors, nwarnings, outputdata, errordata

Here is the call graph for this function:

Here is the caller graph for this function:

def ATContentTypes.lib.validators.parseErrorData (   data,
  removeWarnings = 0 
)
Parse the error data to change some stuff

Definition at line 335 of file validators.py.

00335 
00336 def parseErrorData(data, removeWarnings=0):
00337     """Parse the error data to change some stuff
00338     """
00339     lines  = data.split('\n')
00340     nlines = []
00341     for line in lines:
00342         # substract 11 lines from line
00343         error = RE_MATCH_ERROR.search(line)
00344         if error:
00345             # an error line
00346             lnum, cnum, text = error.groups()
00347             lnum  = int(lnum) - SUBTRACT_LINES
00348             cnum  = int(cnum)
00349             nlines.append(ERROR_LINE % (lnum, cnum, text))
00350         else:
00351             warning = RE_MATCH_WARNING.search(line)
00352             if warning and not removeWarnings:
00353                 # a warning line and add warnings to output
00354                 lnum, cnum, text = warning.groups()
00355                 lnum  = int(lnum) - SUBTRACT_LINES
00356                 cnum  = int(cnum)
00357                 nlines.append(WARNING_LINE % (lnum, cnum, text))
00358             else:
00359                 # something else
00360                 nlines.append(line)
00361     return '\n'.join(nlines)

Here is the caller graph for this function:

Remove the html stuff around the body

Definition at line 280 of file validators.py.

00280 
00281 def unwrapValueFromHTML(value):
00282     """Remove the html stuff around the body
00283     """
00284     # get the body text
00285     result = RE_BODY.search(value)
00286     if result:
00287         body = result.group(1)
00288     else:
00289         raise ValueError('%s is not a html string' % value)

Here is the caller graph for this function:

Wrap the data in a valid html construct to remove the missing title error

Definition at line 264 of file validators.py.

00264 
00265 def wrapValueInHTML(value):
00266     """Wrap the data in a valid html construct to remove the missing title error
00267     """
00268     return """
00269 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
00270     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
00271 <html xmlns="http://www.w3.org/1999/xhtml">
00272 <head>
00273 <title></title>
00274 </head>
00275 <body>
00276 %s
00277 </body>
00278 </html>
00279 """ % value

Here is the caller graph for this function:


Variable Documentation

string ATContentTypes.lib.validators.__author__ = 'Christian Heimes <tiran@cheimes.de>'

Definition at line 23 of file validators.py.

string ATContentTypes.lib.validators.__docformat__ = 'restructuredtext'

Definition at line 24 of file validators.py.

string ATContentTypes.lib.validators.ERROR_LINE = 'line %d column %d - Error: %s'

Definition at line 57 of file validators.py.

tuple ATContentTypes.lib.validators.logger = logging.getLogger('ATCT')

Definition at line 40 of file validators.py.

tuple ATContentTypes.lib.validators.RE_BODY = re.compile('<body[^>]*?>(.*)</body>', re.DOTALL )

Definition at line 62 of file validators.py.

tuple ATContentTypes.lib.validators.RE_GET_HTML_ENCODING = re.compile('(<meta.*?content-type.*?charset[\s]*=[\s]*)([^"]*?)("[^>]*?>)', re.S | re.I)

Definition at line 67 of file validators.py.

tuple ATContentTypes.lib.validators.RE_MATCH_ERROR = re.compile('^line (\d+) column (\d+) - Error: (.*)$')

Definition at line 56 of file validators.py.

tuple ATContentTypes.lib.validators.RE_MATCH_WARNING = re.compile('^line (\d+) column (\d+) - Warning: (.*)$')

Definition at line 52 of file validators.py.

Definition at line 70 of file validators.py.

Definition at line 72 of file validators.py.

string ATContentTypes.lib.validators.WARNING_LINE = 'line %d column %d - Warning: %s'

Definition at line 53 of file validators.py.