<commit_before>#!/usr/bin/env python
"""\
Usage: %prog RULES

RULES is a file defining a set of xdv rules in css syntax, e.g:

<rules xmlns="http://namespaces.plone.org/xdv"
       xmlns:css="http://namespaces.plone.org/xdv+css">
       <copy css:content="#content-wrapper" css:theme="#page-content"/>
</rules>\
"""
usage = __doc__

from optparse import OptionParser
from lxml import etree
from lxml.cssselect import css_to_xpath

import utils

def convert_css_selectors(rules, prefix='//'):
    """Convert css rules to xpath rules element tree in place
    """
    for element in rules.xpath("//@*[namespace-uri()='%s']/.." % utils.namespaces['css']):
        for name, value in element.attrib.items():
            if name.startswith('{%s}' % utils.namespaces['css']):
                element.attrib[utils.fullname(element.nsmap[element.prefix], utils.localname(name))] = css_to_xpath(value, prefix=prefix)

def main():
    """Called from console script
    """
    parser = OptionParser(usage=usage)
    parser.add_option("-o", "--output", metavar="output.html",
                      help="Output filename (instead of stdout)",
                      dest="output", default=sys.stdout)
    parser.add_option("-p", "--pretty-print", action="store_true",
                      help="Pretty print output",
                      dest="pretty_print", default=False)
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error("Invalid number of arguments")
    rules = etree.parse(args[0])
    convert_css_selectors(rules)
    rules.write(options.output, pretty_print=options.pretty_print)

if __name__ == '__main__':
    main()<commit_msg>error checking for empty css attributes<commit_after>#!/usr/bin/env python
"""\
Usage: %prog RULES

RULES is a file defining a set of xdv rules in css syntax, e.g:

<rules xmlns="http://namespaces.plone.org/xdv"
       xmlns:css="http://namespaces.plone.org/xdv+css">
       <copy css:content="#content-wrapper" css:theme="#page-content"/>
</rules>\
"""
usage = __doc__

from optparse import OptionParser
from lxml import etree
from lxml.cssselect import css_to_xpath

import utils

import logging
logger = logging.getLogger('xdv')

def convert_css_selectors(rules, prefix='//'):
    """Convert css rules to xpath rules element tree in place
    """
    for element in rules.xpath("//@*[namespace-uri()='%s']/.." % utils.namespaces['css']):
        for name, value in element.attrib.items():
            if value: 
                if name.startswith('{%s}' % utils.namespaces['css']):
                    element.attrib[utils.fullname(element.nsmap[element.prefix], utils.localname(name))] = css_to_xpath(value, prefix=prefix)
            else:
                logger.error("Missing value for attribute %s" %name)

def main():
    """Called from console script
    """
    parser = OptionParser(usage=usage)
    parser.add_option("-o", "--output", metavar="output.html",
                      help="Output filename (instead of stdout)",
                      dest="output", default=sys.stdout)
    parser.add_option("-p", "--pretty-print", action="store_true",
                      help="Pretty print output",
                      dest="pretty_print", default=False)
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error("Invalid number of arguments")
    rules = etree.parse(args[0])
    convert_css_selectors(rules)
    rules.write(options.output, pretty_print=options.pretty_print)

if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.14792731402612153, 'fraction_numerical': 0.001135718341851221, 'mean_word_length': 3.0775462962962963, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2714775', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1073, 'n_words': 251}