Example #1
0
 def _cook_check(self):
     import Globals  # for data
     if self._v_last_read and not Globals.DevelopmentMode:
         return
     __traceback_info__ = self.filename
     try:
         mtime = os.path.getmtime(self.filename)
     except OSError:
         mtime = 0
     if self._v_program is not None and mtime == self._v_last_read:
         return
     f = open(self.filename, "rb")
     try:
         text = f.read(XML_PREFIX_MAX_LENGTH)
     except:
         f.close()
         raise
     t = sniff_type(text)
     if t != "text/xml":
         # For HTML, we really want the file read in text mode:
         f.close()
         f = open(self.filename, 'U')
         text = ''
     text += f.read()
     f.close()
     self.pt_edit(text, t)
     self._cook()
     if self._v_errors:
         LOG.error('Error in template %s' % '\n'.join(self._v_errors))
         return
     self._v_last_read = mtime
Example #2
0
 def _cook_check(self):
     import Globals  # for data
     if self._v_last_read and not Globals.DevelopmentMode:
         return
     __traceback_info__ = self.filename
     try:
         mtime = os.path.getmtime(self.filename)
     except OSError:
         mtime = 0
     if self._v_program is not None and mtime == self._v_last_read:
         return
     f = open(self.filename, "rb")
     try:
         text = f.read(XML_PREFIX_MAX_LENGTH)
     except:
         f.close()
         raise
     t = sniff_type(text)
     if t != "text/xml":
         # For HTML, we really want the file read in text mode:
         f.close()
         f = open(self.filename, 'U')
         text = ''
     text += f.read()
     f.close()
     self.pt_edit(text, t)
     self._cook()
     if self._v_errors:
         LOG.error('Error in template %s' % '\n'.join(self._v_errors))
         return
     self._v_last_read = mtime
Example #3
0
 def _read_file(self):
     __traceback_info__ = self.filename
     with open(self.filename, "rb") as f:
         text = f.read(XML_PREFIX_MAX_LENGTH)
         type_ = sniff_type(text)
         text += f.read()
     if type_ != "text/xml":
         text, type_ = self._prepare_html(text)
     else:
         text, type_ = self._prepare_xml(text)
     f.close()
     return text, type_
Example #4
0
def guess_type(filename, body):
    # check for XML ourself since guess_content_type can't
    # detect text/xml  if 'filename' won't end with .xml
    # XXX: fix this in zope.contenttype

    if body.startswith(b'<?xml'):
        return 'text/xml'

    content_type, ignored_encoding = guess_content_type(filename, body)
    if content_type in ('text/html', 'text/xml'):
        return content_type
    return sniff_type(body) or 'text/html'
Example #5
0
def guess_type(filename, body):
    # check for XML ourself since guess_content_type can't
    # detect text/xml  if 'filename' won't end with .xml
    # XXX: fix this in zope.contenttype

    if body.startswith(b'<?xml'):
        return 'text/xml'

    content_type, ignored_encoding = guess_content_type(filename, body)
    if content_type in ('text/html', 'text/xml'):
        return content_type
    return sniff_type(body) or 'text/html'
Example #6
0
 def _read_file(self):
     __traceback_info__ = self.filename
     with open(self.filename, "rb") as f:
         text = f.read(XML_PREFIX_MAX_LENGTH)
         type_ = sniff_type(text)
         text += f.read()
     if type_ != "text/xml":
         text, type_ = self._prepare_html(text)
     else:
         text, type_ = self._prepare_xml(text)
     f.close()
     return text, type_
Example #7
0
def guess_type(filename, text):
    # check for XML ourself since guess_content_type can't
    # detect text/xml  if 'filename' won't end with .xml
    # XXX: fix this in zope.contenttype

    if text.startswith('<?xml'):
        return 'text/xml'

    content_type, dummy = guess_content_type(filename, text)
    if content_type in ('text/html', 'text/xml'):
        return content_type
    return sniff_type(text) or 'text/html'
Example #8
0
def guess_type(filename, text):

    # check for XML ourself since guess_content_type can't
    # detect text/xml  if 'filename' won't end with .xml
    # XXX: fix this in zope.contenttype

    if text.startswith('<?xml'):
        return 'text/xml'

    content_type, dummy = guess_content_type(filename, text)
    if content_type in ('text/html', 'text/xml'):
        return content_type
    return sniff_type(text) or 'text/html'
Example #9
0
 def _read_file(self):
     __traceback_info__ = self.filename
     f = open(self.filename, "rb")
     try:
         text = f.read(XML_PREFIX_MAX_LENGTH)
     except:
         f.close()
         raise
     type_ = sniff_type(text)
     text += f.read()
     if type_ != "text/xml":
         text, type_ = self._prepare_html(text)
     f.close()
     return text, type_
Example #10
0
 def _read_file(self):
     __traceback_info__ = self.filename
     f = open(self.filename, "rb")
     try:
         text = f.read(XML_PREFIX_MAX_LENGTH)
     except:
         f.close()
         raise
     type_ = sniff_type(text)
     text += f.read()
     if type_ != "text/xml":
         text, type_ = self._prepare_html(text)
     f.close()
     return text, type_
Example #11
0
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()):
    """Retrieve all TAL messages from `dir` that are in the `domain`.
    """
    # We import zope.tal.talgettext here because we can't rely on the
    # right sys path until app_dir has run
    from zope.pagetemplate.pagetemplatefile import sniff_type
    from zope.pagetemplate.pagetemplatefile import XML_PREFIX_MAX_LENGTH
    from zope.tal.talgettext import POEngine, POTALInterpreter
    from zope.tal.htmltalparser import HTMLTALParser
    from zope.tal.talparser import TALParser
    engine = POEngine()

    class Devnull(object):
        def write(self, s):
            pass

    filenames = find_files(dir, '*.pt', exclude=tuple(exclude)) \
              + find_files(dir, '*.html', exclude=tuple(exclude)) \
              + find_files(dir, '*.xml', exclude=tuple(exclude))

    for filename in sorted(filenames):
        # This is taken from zope/pagetemplate/pagetemplatefile.py (r40504)
        f = open(filename, "rb")
        try:
            text = f.read(XML_PREFIX_MAX_LENGTH)
        except:
            f.close()
            raise
        type_ = sniff_type(text)
        if type_ == "text/xml":
            text += f.read()
        else:
            # For HTML, we really want the file read in text mode:
            f.close()
            f = open(filename)
            text = f.read()
        f.close()

        try:
            engine.file = filename
            if type_ != "text/xml":
                p = HTMLTALParser()
            else:
                p = TALParser()
            p.parseString(text)
            program, macros = p.getCode()
            POTALInterpreter(program, macros, engine, stream=Devnull(),
                             metal=False)()
        except: # Hee hee, I love bare excepts!
            print 'There was an error processing', filename
            traceback.print_exc()

    # See whether anything in the domain was found
    if not engine.catalog.has_key(domain):
        return {}
    # We do not want column numbers.
    catalog = engine.catalog[domain].copy()
    # When the Domain is 'default', then this means that none was found;
    # Include these strings; yes or no?
    if include_default_domain:
        catalog.update(engine.catalog['zope'])
    for msgid, locations in catalog.items():
        catalog[msgid] = map(lambda l: (l[0], l[1][0]), locations)
    return catalog
Example #12
0
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()):
    """Retrieve all TAL messages from `dir` that are in the `domain`.
    """
    # We import zope.tal.talgettext here because we can't rely on the
    # right sys path until app_dir has run
    from zope.pagetemplate.pagetemplatefile import sniff_type
    from zope.pagetemplate.pagetemplatefile import XML_PREFIX_MAX_LENGTH
    from zope.tal.talgettext import POEngine, POTALInterpreter
    from zope.tal.htmltalparser import HTMLTALParser
    from zope.tal.talparser import TALParser
    engine = POEngine()

    class Devnull(object):
        def write(self, s):
            pass

    filenames = find_files(dir, '*.pt', exclude=tuple(exclude)) \
              + find_files(dir, '*.html', exclude=tuple(exclude)) \
              + find_files(dir, '*.xml', exclude=tuple(exclude))

    for filename in sorted(filenames):
        # This is taken from zope/pagetemplate/pagetemplatefile.py (r40504)
        f = open(filename, "rb")
        try:
            text = f.read(XML_PREFIX_MAX_LENGTH)
        except:
            f.close()
            raise
        type_ = sniff_type(text)
        if type_ == "text/xml":
            text += f.read()
        else:
            # For HTML, we really want the file read in text mode:
            f.close()
            f = open(filename)
            text = f.read()
        f.close()

        try:
            engine.file = filename
            if type_ != "text/xml":
                p = HTMLTALParser()
            else:
                p = TALParser()
            p.parseString(text)
            program, macros = p.getCode()
            POTALInterpreter(program,
                             macros,
                             engine,
                             stream=Devnull(),
                             metal=False)()
        except:  # Hee hee, I love bare excepts!
            print 'There was an error processing', filename
            traceback.print_exc()

    # See whether anything in the domain was found
    if not engine.catalog.has_key(domain):
        return {}
    # We do not want column numbers.
    catalog = engine.catalog[domain].copy()
    # When the Domain is 'default', then this means that none was found;
    # Include these strings; yes or no?
    if include_default_domain:
        defaultCatalog = engine.catalog.get('default')
        if defaultCatalog == None:
            engine.catalog['default'] = {}
        catalog.update(engine.catalog['default'])
    for msgid, locations in catalog.items():
        catalog[msgid] = map(lambda l: (l[0], l[1][0]), locations)
    return catalog