示例#1
0
def generate_runtrace(rules, error_log, rules_parser=None):
    """Annotate a rules file with the results of a transformation"""
    def condition_name(trace):
        """Generate attribute name for this entry"""
        for k in trace.attrib.keys():
            if(k == 'theme_xmlid'): continue
            if(k.startswith('{http://namespaces.plone.org/diazo/css}')): continue
            return "runtrace-"+k
    
    rules_doc = process_rules(rules, rules_parser=rules_parser,
                              stop='add_identifiers')
    trace_doc = etree.XML(log_to_xml_string(error_log))
    
    for trace in trace_doc.xpath('/runtrace/runtrace'):
        for el in rules_doc.xpath("id('"+trace.attrib['theme_xmlid']+"')"):
            el.set(condition_name(trace),trace.text or '')
    return rules_doc
示例#2
0
def generate_runtrace(rules, error_log, rules_parser=None):
    """Annotate a rules file with the results of a transformation"""
    def condition_name(trace):
        """Generate attribute name for this entry"""
        for k in trace.attrib.keys():
            if (k == 'theme_xmlid'):
                continue
            if (k.startswith('{http://namespaces.plone.org/diazo/css}')):
                continue
            return "runtrace-" + k

    rules_doc = process_rules(rules,
                              rules_parser=rules_parser,
                              stop='add_identifiers')
    trace_doc = etree.XML(log_to_xml_string(error_log))

    for trace in trace_doc.xpath('/runtrace/runtrace'):
        for el in rules_doc.xpath("id('" + trace.attrib['theme_xmlid'] + "')"):
            el.set(condition_name(trace), trace.text or '')
    return rules_doc
示例#3
0
文件: compiler.py 项目: garbas/diazo
def compile_theme(rules,
                  theme=None,
                  extra=None,
                  css=True,
                  xinclude=True,
                  absolute_prefix=None,
                  update=True,
                  trace=False,
                  includemode=None,
                  parser=None,
                  compiler_parser=None,
                  rules_parser=None,
                  access_control=None,
                  read_network=False,
                  indent=None,
                  xsl_params=None,
                  runtrace=False):
    """Invoke the diazo compiler.

    * ``rules`` is the rules file
    * ``theme`` is the theme file
    * ``extra`` is an optional XSLT file with Diazo extensions (depracated, use
      inline xsl in the rules instead)
    * ``css``   can be set to False to disable CSS syntax support (providing a
      moderate speed gain)
    * ``xinclude`` can be set to False to disable XInclude support during the
      compile phase (providing a moderate speed gain)
    * ``absolute_prefix`` can be set to a string that will be prefixed to any
      *relative* URL referenced in an image, link or stylesheet in the theme
      HTML file before the theme is passed to the compiler. This allows a
      theme to be written so that it can be opened and views standalone on the
      filesystem, even if at runtime its static resources are going to be
      served from some other location. For example, an
      ``<img src="images/foo.jpg" />`` can be turned into
      ``<img src="/static/images/foo.jpg" />`` with an ``absolute_prefix`` of
      "/static".
    * ``update`` can be set to False to disable the automatic update support
      for the old Deliverance 0.2 namespace (for a moderate speed gain)
    * ``trace`` can be set to True to enable compiler trace information
    * ``runtrace`` can be set to True to add tracing into the XSL output
    * ``includemode`` can be set to 'document', 'esi' or 'ssi' to change the
      way in which includes are processed
    * ``parser`` can be set to an lxml parser instance; the default is an
      HTMLParser
    * ``compiler_parser``` can be set to an lxml parser instance; the default
      is a XMLParser
    * ``rules_parser`` can be set to an lxml parser instance; the default is a
      XMLParser.
    * ``xsl_params`` can be set to a dictionary of parameters that will be
      known to the compiled theme transform. The keys should be the parameter
      names. Values are default values.
    """
    if access_control is not None:
        read_network = access_control.options['read_network']
    rules_doc = process_rules(
        rules=rules,
        theme=theme,
        extra=extra,
        css=css,
        xinclude=xinclude,
        absolute_prefix=absolute_prefix,
        update=update,
        trace=trace,
        includemode=includemode,
        parser=parser,
        rules_parser=rules_parser,
        read_network=read_network,
    )

    # Build a document with all the <xsl:param /> values to set the defaults
    # for every value passed in as xsl_params
    known_params = build_xsl_params_document(xsl_params)

    # Create a pseudo resolver for this
    known_params_url = 'file:///__diazo_known_params__'
    emit_stylesheet_resolver = CustomResolver(
        {known_params_url: etree.tostring(known_params)})
    emit_stylesheet_parser = etree.XMLParser()
    emit_stylesheet_parser.resolvers.add(emit_stylesheet_resolver)

    # Set up parameters
    params = {}
    if indent is not None:
        params['indent'] = indent and "'yes'" or "'no'"
    params['known_params_url'] = quote_param(known_params_url)
    params['runtrace'] = '1' if runtrace else '0'

    # Run the final stage compiler
    emit_stylesheet = pkg_xsl('emit-stylesheet.xsl',
                              parser=emit_stylesheet_parser)
    compiled_doc = emit_stylesheet(rules_doc, **params)
    compiled_doc = set_parser(etree.tostring(compiled_doc), parser,
                              compiler_parser)

    return compiled_doc
示例#4
0
def compile_theme(rules, theme=None, extra=None, css=True, xinclude=True,
                  absolute_prefix=None, update=True, trace=False,
                  includemode=None, parser=None, compiler_parser=None,
                  rules_parser=None, access_control=None, read_network=False,
                  indent=None, xsl_params=None, runtrace=False):
    """Invoke the diazo compiler.

    * ``rules`` is the rules file
    * ``theme`` is the theme file
    * ``extra`` is an optional XSLT file with Diazo extensions (depracated, use
      inline xsl in the rules instead)
    * ``css``   can be set to False to disable CSS syntax support (providing a
      moderate speed gain)
    * ``xinclude`` can be set to False to disable XInclude support during the
      compile phase (providing a moderate speed gain)
    * ``absolute_prefix`` can be set to a string that will be prefixed to any
      *relative* URL referenced in an image, link or stylesheet in the theme
      HTML file before the theme is passed to the compiler. This allows a
      theme to be written so that it can be opened and views standalone on the
      filesystem, even if at runtime its static resources are going to be
      served from some other location. For example, an
      ``<img src="images/foo.jpg" />`` can be turned into
      ``<img src="/static/images/foo.jpg" />`` with an ``absolute_prefix`` of
      "/static".
    * ``update`` can be set to False to disable the automatic update support
      for the old Deliverance 0.2 namespace (for a moderate speed gain)
    * ``trace`` can be set to True to enable compiler trace information
    * ``runtrace`` can be set to True to add tracing into the XSL output
    * ``includemode`` can be set to 'document', 'esi' or 'ssi' to change the
      way in which includes are processed
    * ``parser`` can be set to an lxml parser instance; the default is an
      HTMLParser
    * ``compiler_parser``` can be set to an lxml parser instance; the default
      is a XMLParser
    * ``rules_parser`` can be set to an lxml parser instance; the default is a
      XMLParser.
    * ``xsl_params`` can be set to a dictionary of parameters that will be
      known to the compiled theme transform. The keys should be the parameter
      names. Values are default values.
    """
    if access_control is not None:
        read_network = access_control.options['read_network']
    rules_doc = process_rules(
        rules=rules,
        theme=theme,
        extra=extra,
        css=css,
        xinclude=xinclude,
        absolute_prefix=absolute_prefix,
        update=update,
        trace=trace,
        includemode=includemode,
        parser=parser,
        rules_parser=rules_parser,
        read_network=read_network,
    )

    # Build a document with all the <xsl:param /> values to set the defaults
    # for every value passed in as xsl_params
    known_params = build_xsl_params_document(xsl_params)

    # Create a pseudo resolver for this
    known_params_url = 'file:///__diazo_known_params__'
    emit_stylesheet_resolver = CustomResolver({
        known_params_url: etree.tostring(known_params)})
    emit_stylesheet_parser = etree.XMLParser()
    emit_stylesheet_parser.resolvers.add(emit_stylesheet_resolver)

    # Set up parameters
    params = {}
    if indent is not None:
        params['indent'] = indent and "'yes'" or "'no'"
    params['known_params_url'] = quote_param(known_params_url)
    params['runtrace'] = '1' if runtrace else '0'

    # Run the final stage compiler
    emit_stylesheet = pkg_xsl(
        'emit-stylesheet.xsl', parser=emit_stylesheet_parser)
    compiled_doc = emit_stylesheet(rules_doc, **params)
    compiled_doc = set_parser(etree.tostring(compiled_doc), parser,
                              compiler_parser)

    return compiled_doc