Back to index

plone3  3.1.7
Classes | Functions | Variables
CMFDefault.utils Namespace Reference

Classes

class  SimpleHTMLParser
class  StrippingParser

Functions

def formatRFC822Headers
def parseHeadersBody
def semi_split
def comma_split
def seq_strip
def tuplize
def scrubHTML
def isHTMLSafe
def bodyfinder
def html_headcheck
def html_marshal
def toUnicode
def decode
def translate
def getBrowserCharset
def makeEmail

Variables

tuple security = ModuleSecurityInfo( 'Products.CMFDefault.utils' )
tuple _dtmldir = os.path.join( package_home( globals() ), 'dtml' )
tuple _wwwdir = os.path.join( package_home( globals() ), 'www' )
dictionary VALID_TAGS
dictionary NASTY_TAGS
tuple _htfinder

Function Documentation

Return body or unchanged text if no body tags found.

Always use html_headcheck() first.

Definition at line 398 of file utils.py.

00398 
00399 def bodyfinder(text):
00400     """ Return body or unchanged text if no body tags found.
00401 
00402     Always use html_headcheck() first.
00403     """
00404     lowertext = text.lower()
00405     bodystart = lowertext.find('<body')
00406     if bodystart == -1:
00407         return text
00408     bodystart = lowertext.find('>', bodystart) + 1
00409     if bodystart == 0:
00410         return text
00411     bodyend = lowertext.rfind('</body>', bodystart)
00412     if bodyend == -1:
00413         return text
00414     return text[bodystart:bodyend]
00415 
security.declarePrivate('_htfinder')

Here is the caller graph for this function:

Split 's' on commas.

Definition at line 129 of file utils.py.

00129 
00130 def comma_split(s):
00131 
00132     """ Split 's' on commas.
00133     """
00134     return map(lambda x: x.strip(), s.split( ',') )
00135 
security.declarePublic('seq_strip')
def CMFDefault.utils.decode (   value,
  context 
)
Decode value using default_charset.

Definition at line 459 of file utils.py.

00459 
00460 def decode(value, context):
00461     """ Decode value using default_charset.
00462     """
00463     ptool = getUtility(IPropertiesTool)
00464     default_charset = ptool.getProperty('default_charset', None)
00465     return toUnicode(value, default_charset)
00466 
security.declarePublic('translate')

Here is the call graph for this function:

Here is the caller graph for this function:

Convert the key-value pairs in 'headers' to valid RFC822-style
    headers, including adding leading whitespace to elements which
    contain newlines in order to preserve continuation-line semantics.

Definition at line 52 of file utils.py.

00052 
00053 def formatRFC822Headers( headers ):
00054 
00055     """ Convert the key-value pairs in 'headers' to valid RFC822-style
00056         headers, including adding leading whitespace to elements which
00057         contain newlines in order to preserve continuation-line semantics.
00058     """
00059     munged = []
00060     linesplit = re.compile( r'[\n\r]+?' )
00061 
00062     for key, value in headers:
00063 
00064         vallines = linesplit.split( value )
00065         while vallines:
00066             if vallines[-1].rstrip() == '':
00067                 vallines = vallines[:-1]
00068             else:
00069                 break
00070         munged.append( '%s: %s' % ( key, '\r\n  '.join( vallines ) ) )
00071 
00072     return '\r\n'.join( munged )
00073 
00074 
security.declarePublic('parseHeadersBody')
Get charset preferred by the browser.

Definition at line 479 of file utils.py.

00479 
00480 def getBrowserCharset(request):
00481     """ Get charset preferred by the browser.
00482     """
00483     envadapter = IUserPreferredCharsets(request)
00484     charsets = envadapter.getPreferredCharsets() or ['utf-8']
00485     return charsets[0]
00486 
security.declarePublic('makeEmail')

Here is the caller graph for this function:

Return 'true' if document looks HTML-ish enough.

If true bodyfinder() will be able to find the HTML body.

Definition at line 420 of file utils.py.

00420 
00421 def html_headcheck(html):
00422     """ Return 'true' if document looks HTML-ish enough.
00423 
00424     If true bodyfinder() will be able to find the HTML body.
00425     """
00426     lowerhtml = html.lower()
00427     if lowerhtml.find('<html') == -1:
00428         return 0
00429     elif _htfinder.match(lowerhtml):
00430         return 1
00431     else:
00432         return 0
00433 
security.declarePublic('html_marshal')

Here is the caller graph for this function:

Marshal variables for html forms.

Definition at line 434 of file utils.py.

00434 
00435 def html_marshal(**kw):
00436     """ Marshal variables for html forms.
00437     """
00438     vars = [ (key + converter, value)
00439              for key, converter, value in complex_marshal(kw.items()) ]
00440     return tuple(vars)
00441 
security.declarePublic('toUnicode')

Here is the caller graph for this function:

Would current HTML be permitted to be saved?

Definition at line 386 of file utils.py.

00386 
00387 def isHTMLSafe( html ):
00388 
00389     """ Would current HTML be permitted to be saved?
00390     """
00391     try:
00392         scrubHTML( html )
00393     except IllegalHTML:
00394         return 0
00395     else:
00396         return 1
00397 
security.declarePublic('bodyfinder')

Here is the call graph for this function:

def CMFDefault.utils.makeEmail (   mtext,
  context,
  headers = {} 
)
Make email message.

Definition at line 487 of file utils.py.

00487 
00488 def makeEmail(mtext, context, headers={}):
00489     """ Make email message.
00490     """
00491     ptool = getUtility(IPropertiesTool)
00492     email_charset = ptool.getProperty('email_charset', None) or 'utf-8'
00493     try:
00494         msg = MIMEText(mtext.encode(), 'plain')
00495     except UnicodeEncodeError:
00496         msg = MIMEText(mtext.encode(email_charset), 'plain', email_charset)
00497     for k, val in headers.items():
00498         if isinstance(val, str):
00499             val = decode(val, context)
00500         if isinstance(val, i18n.Message):
00501             val = translate(val, context)
00502         header = make_header([ (w, email_charset) for w in val.split(' ') ])
00503         msg[k] = str(header)
00504     return msg.as_string()
00505 
00506 # RFC 2822 local-part: dot-atom or quoted-string
00507 # characters allowed in atom: A-Za-z0-9!#$%&'*+-/=?^_`{|}~
# RFC 2821 domain: max 255 characters

Here is the call graph for this function:

def CMFDefault.utils.parseHeadersBody (   body,
  headers = None,
  rc = re.compile( r'\n|\r\n' ) 
)
Parse any leading 'RFC-822'-ish headers from an uploaded
    document, returning a dictionary containing the headers
    and the stripped body.

    E.g.::

        Title: Some title
        Creator: Tres Seaver
        Format: text/plain
        X-Text-Format: structured

        Overview

        This document .....

        First Section

        ....


    would be returned as::

        { 'Title' : 'Some title'
        , 'Creator' : 'Tres Seaver'
        , 'Format' : 'text/plain'
        , 'text_format': 'structured'
        }

    as the headers, plus the body, starting with 'Overview' as
    the first line (the intervening blank line is a separator).

    Allow passing initial dictionary as headers.

Definition at line 75 of file utils.py.

00075 
00076 def parseHeadersBody( body, headers=None, rc=re.compile( r'\n|\r\n' ) ):
00077 
00078     """ Parse any leading 'RFC-822'-ish headers from an uploaded
00079         document, returning a dictionary containing the headers
00080         and the stripped body.
00081 
00082         E.g.::
00083 
00084             Title: Some title
00085             Creator: Tres Seaver
00086             Format: text/plain
00087             X-Text-Format: structured
00088 
00089             Overview
00090 
00091             This document .....
00092 
00093             First Section
00094 
00095             ....
00096 
00097 
00098         would be returned as::
00099 
00100             { 'Title' : 'Some title'
00101             , 'Creator' : 'Tres Seaver'
00102             , 'Format' : 'text/plain'
00103             , 'text_format': 'structured'
00104             }
00105 
00106         as the headers, plus the body, starting with 'Overview' as
00107         the first line (the intervening blank line is a separator).
00108 
00109         Allow passing initial dictionary as headers.
00110     """
00111     buffer = StringIO.StringIO(body)
00112     message = rfc822.Message(buffer)
00113 
00114     headers = headers and headers.copy() or {}
00115 
00116     for key in message.keys():
00117         headers[key.capitalize()] = '\n'.join(message.getheaders(key))
00118 
00119     return headers, buffer.read()
00120 
00121 
security.declarePublic('semi_split')

Here is the caller graph for this function:

Strip illegal HTML tags from string text.

o Prefer a utility, if registered.

Definition at line 369 of file utils.py.

00369 
00370 def scrubHTML( html ):
00371 
00372     """ Strip illegal HTML tags from string text.
00373 
00374     o Prefer a utility, if registered.
00375     """
00376     scrubber = queryUtility(IHTMLScrubber)
00377 
00378     if scrubber is not None:
00379         return scrubber.scrub(html)
00380 
00381     parser = StrippingParser()
00382     parser.feed( html )
00383     parser.close()
00384     return parser.result
00385 
security.declarePublic('isHTMLSafe')

Here is the caller graph for this function:

Split 's' on semicolons.

Definition at line 122 of file utils.py.

00122 
00123 def semi_split(s):
00124 
00125     """ Split 's' on semicolons.
00126     """
00127     return map(lambda x: x.strip(), s.split( ';' ) )
00128 
security.declarePublic('comma_split')
def CMFDefault.utils.seq_strip (   seq,
  stripper = lambda x: x.strip() 
)
Strip a sequence of strings.

Definition at line 136 of file utils.py.

00136 
00137 def seq_strip(seq, stripper=lambda x: x.strip() ):
00138     """ Strip a sequence of strings.
00139     """
00140     if isinstance(seq, list):
00141         return map( stripper, seq )
00142 
00143     if isinstance(seq, tuple):
00144         return tuple( map( stripper, seq ) )
00145 
00146     raise ValueError, "%s of unsupported sequencetype %s" % ( seq, type( seq ) )
00147 
security.declarePublic('tuplize')

Here is the call graph for this function:

Here is the caller graph for this function:

def CMFDefault.utils.toUnicode (   value,
  charset = None 
)
Convert value to unicode.

Definition at line 442 of file utils.py.

00442 
00443 def toUnicode(value, charset=None):
00444     """ Convert value to unicode.
00445     """
00446     if isinstance(value, str):
00447         return charset and unicode(value, charset) or unicode(value)
00448     elif isinstance(value, list):
00449         return [ toUnicode(val, charset) for val in value ]
00450     elif isinstance(value, tuple):
00451         return tuple( [ toUnicode(val, charset) for val in value ] )
00452     elif isinstance(value, dict):
00453         for key, val in value.items():
00454             value[key] = toUnicode(val, charset)
00455         return value
00456     else:
00457         return value
00458 
security.declarePublic('decode')

Here is the caller graph for this function:

def CMFDefault.utils.translate (   message,
  context 
)
Translate i18n message.

Definition at line 467 of file utils.py.

00467 
00468 def translate(message, context):
00469     """ Translate i18n message.
00470     """
00471     GTS = getGlobalTranslationService()
00472     if isinstance(message, Exception):
00473         try:
00474             message = message[0]
00475         except (TypeError, IndexError):
00476             pass
00477     return GTS.translate('cmf_default', message, context=context)
00478 
security.declarePublic('getBrowserCharset')
def CMFDefault.utils.tuplize (   valueName,
  value,
  splitter = lambda x: x.split() 
)

Definition at line 148 of file utils.py.

00148 
00149 def tuplize( valueName, value, splitter=lambda x: x.split() ):
00150 
00151     if isinstance(value, tuple):
00152         return seq_strip( value )
00153 
00154     if isinstance(value, list):
00155         return seq_strip( tuple( value ) )
00156 
00157     if isinstance(value, basestring):
00158         return seq_strip( tuple( splitter( value ) ) )
00159 
00160     raise ValueError, "%s of unsupported type" % valueName
00161 

Here is the call graph for this function:


Variable Documentation

tuple CMFDefault.utils._dtmldir = os.path.join( package_home( globals() ), 'dtml' )

Definition at line 48 of file utils.py.

Initial value:
00001 re.compile(r'(\s|(<[^<>]*?>))*<html.*<body.*?>.*</body>',
00002                        re.DOTALL)

Definition at line 416 of file utils.py.

tuple CMFDefault.utils._wwwdir = os.path.join( package_home( globals() ), 'www' )

Definition at line 49 of file utils.py.

Initial value:
00001 { 'script'     : 1
00002              , 'object'     : 1
00003              , 'embed'      : 1
00004              , 'applet'     : 1
00005              }

Definition at line 285 of file utils.py.

tuple CMFDefault.utils.security = ModuleSecurityInfo( 'Products.CMFDefault.utils' )

Definition at line 45 of file utils.py.

Definition at line 236 of file utils.py.