Example #1
0
File: rules.py Project: plone/diazo
def add_theme(
    rules_doc,
    theme,
    parser=None,
    absolute_prefix=None,
    read_network=False,
):
    if not read_network and \
            isinstance(theme, string_types) and \
            theme[:6] in ('ftp://', 'http:/', 'https:'):
        raise ValueError(
            "Supplied theme '{theme}', but network access denied.".format(
                theme=theme,
            ),
        )
    if absolute_prefix is None:
        absolute_prefix = ''
    if parser is None:
        parser = etree.HTMLParser()
    root = rules_doc.getroot()
    element = root.makeelement(fullname(namespaces['diazo'], 'theme'))
    root.append(element)
    theme_doc = etree.parse(theme, parser=parser)
    expand_theme(element, theme_doc, absolute_prefix)
    return rules_doc
Example #2
0
def add_identifiers(rules_doc):
    """Add identifiers to the rules for debugging"""
    for i, elem in enumerate(rules_doc.xpath(
            '//diazo:rules | //diazo:rules/diazo:*'
            ' | //old1:rules | //old1:rules/old1:*'
            ' | //old2:rules | //old2:rules/old1:*', namespaces=namespaces)):
        elem.set(fullname(namespaces['xml'], 'id'), 'r%s' % i)
    return rules_doc
Example #3
0
def add_identifiers(rules_doc):
    """Add identifiers to the rules for debugging"""
    for i, elem in enumerate(rules_doc.xpath(
            '//diazo:rules | //diazo:rules/diazo:*'
            ' | //old1:rules | //old1:rules/old1:*'
            ' | //old2:rules | //old2:rules/old1:*', namespaces=namespaces)):
        elem.set(fullname(namespaces['xml'], 'id'), 'r%s' % i)
    return rules_doc
Example #4
0
def add_identifiers(rules_doc):
    """Add identifiers to the rules for debugging"""
    for i, elem in enumerate(
        rules_doc.xpath(
            "//diazo:rules | //diazo:rules/diazo:*"
            " | //old1:rules | //old1:rules/old1:*"
            " | //old2:rules | //old2:rules/old1:*",
            namespaces=namespaces,
        )
    ):
        elem.set(fullname(namespaces["xml"], "id"), "r%s" % i)
    return rules_doc
Example #5
0
def add_theme(rules_doc, theme, parser=None, absolute_prefix=None, read_network=False):
    if isinstance(theme, basestring) and theme[:6] in ("ftp://", "http:/", "https:"):
        raise ValueError("Supplied theme '%s', but network access denied." % theme)
    if absolute_prefix is None:
        absolute_prefix = ""
    if parser is None:
        parser = etree.HTMLParser()
    root = rules_doc.getroot()
    element = root.makeelement(fullname(namespaces["diazo"], "theme"))
    root.append(element)
    theme_doc = etree.parse(theme, parser=parser)
    expand_theme(element, theme_doc, absolute_prefix)
    return rules_doc
Example #6
0
def convert_css_selectors(rules):
    """Convert css rules to xpath rules element tree in place
    """
    # XXX: There is a :root pseudo-class  # NOQA: T000
    # http://www.w3.org/TR/css3-selectors/#root-pseudo
    # We may wish to add support to lxml.cssselect for it some day.
    for element in rules.xpath(
        "//@*[namespace-uri()='{nsp}']/..".format(nsp=utils.namespaces['css']),
    ):
        tag_namespace = utils.namespace(element.tag)
        css_prefix = element.attrib.get(
            utils.fullname(
                utils.namespaces['css'],
                'prefix',
            ),
            None,
        )
        for name, value in element.attrib.items():
            if not name.startswith(
                '{%s}' % utils.namespaces['css'],  # NOQA: S001
            ):
                continue
            localname = utils.localname(name)
            if localname == 'prefix':
                continue
            if not value:
                element.attrib[localname] = ''
                continue
            if (
                tag_namespace == utils.namespaces['diazo'] and
                localname in
                (
                    'content',
                    'content-children',
                    'if-content',
                    'if-not-content',
                ) or (
                    tag_namespace == utils.namespaces['xsl'] and
                    localname in ('match',)
                )
            ):
                prefix = css_prefix or '//'
                tr = _location_path_translator
            else:
                prefix = css_prefix or 'descendant-or-self::'
                tr = _generic_translator
            element.attrib[localname] = tr.css_to_xpath(value, prefix=prefix)

    return rules
Example #7
0
def add_theme(rules_doc, theme, parser=None, absolute_prefix=None, read_network=False):
    if isinstance(theme, basestring) and theme[:6] in ('ftp://', 'http:/', 'https:'):
        raise ValueError("Supplied theme '%s', but network access denied." % theme)
    if absolute_prefix is None:
        absolute_prefix = ''
    if parser is None:
        parser = etree.HTMLParser()
    root = rules_doc.getroot()
    element = root.makeelement(fullname(namespaces['diazo'], 'theme'))
    theme_doc = etree.parse(theme, parser=parser)
    prefix = urljoin(absolute_prefix, element.get('prefix', ''))
    apply_absolute_prefix(theme_doc, prefix)
    element.append(theme_doc.getroot())   
    root.append(element)
    return rules_doc
Example #8
0
def add_theme(rules_doc, theme, parser=None, absolute_prefix=None,
              read_network=False):
    if not read_network and \
            isinstance(theme, string_types) and \
            theme[:6] in ('ftp://', 'http:/', 'https:'):
        raise ValueError("Supplied theme '%s', "
                         "but network access denied." % theme)
    if absolute_prefix is None:
        absolute_prefix = ''
    if parser is None:
        parser = etree.HTMLParser()
    root = rules_doc.getroot()
    element = root.makeelement(fullname(namespaces['diazo'], 'theme'))
    root.append(element)
    theme_doc = etree.parse(theme, parser=parser)
    expand_theme(element, theme_doc, absolute_prefix)
    return rules_doc
Example #9
0
def add_identifiers(rules_doc):
    """Add identifiers to the rules for debugging"""
    for index, elem in enumerate(
            rules_doc.xpath(
                '//diazo:rules | //diazo:rules/diazo:*'
                ' | //old1:rules | //old1:rules/old1:*'
                ' | //old2:rules | //old2:rules/old1:*',
                namespaces=namespaces,
            ), ):
        elem.set(
            fullname(
                namespaces['xml'],
                'id',
            ),
            'r{index}'.format(index=index),
        )
    return rules_doc
Example #10
0
File: rules.py Project: plone/diazo
def add_identifiers(rules_doc):
    """Add identifiers to the rules for debugging"""
    for index, elem in enumerate(
        rules_doc.xpath(
            '//diazo:rules | //diazo:rules/diazo:*'
            ' | //old1:rules | //old1:rules/old1:*'
            ' | //old2:rules | //old2:rules/old1:*',
            namespaces=namespaces,
        ),
    ):
        elem.set(
            fullname(
                namespaces['xml'],
                'id',
            ),
            'r{index}'.format(index=index),
        )
    return rules_doc