def make(prefix, xmlns): if prefix is not None and not isinstance(prefix, str): raise TypeError(f"prefix must be None or string, not {type(prefix)!r}") xmlns = xsc.nsname(xmlns) if not isinstance(xmlns, str): raise TypeError(f"xmlns must be string, not {type(xmlns)!r}") newprefixes[prefix] = xmlns
def __init__(self, node, indent=None, defaultxmlns=None): """ Create a :class:`TreePresenter` object for the XIST node :obj:`node` using :obj:`indent` for indenting each tree level. If :obj:`indent` is :const:`None` use the value of the environment variable ``LL_XIST_INDENT`` as the indent string (falling back to a tab if the environment variable doesn't exist). If :obj:`defaultxmlns` is not ``None``, elements from this namespace will be output without any namespace name. """ Presenter.__init__(self, node) if indent is None: indent = os.environ.get("LL_XIST_INDENT", "\t") self.indent = indent self.defaultxmlns = xsc.nsname(defaultxmlns)
def __init__(self, data, url=None, defaultxmlns=None): """ Create an :class:`ETree` object. Arguments have the following meaning: :obj:`data` An object that supports the ElementTree API. :obj:`url` The URL of the source. Defaults to ``"ETREE"``. :obj:`defaultxmlns` The namespace name (or a namespace module containing a namespace name) that will be used for all elements that don't have a namespace. """ self.url = url_.URL(url if url is not None else "ETREE") self.data = data self.defaultxmlns = xsc.nsname(defaultxmlns)
def __init__(self, xmlns, xmlname): self.xmlns = xsc.nsname(xmlns) self.xmlname = xmlname
def check(pool, xmlns, xmlname, cls): assert pool.elementclass(xmlns, xmlname) is cls e = pool.element(xmlns, xmlname) assert (e.xmlns, e.xmlname) == (xsc.nsname(xmlns), xmlname)
def selector(selectors, prefixes=None): """ Create a :class:`xfind.Selector` object that matches all nodes that match the specified CSS selector expression. :obj:`selectors` can be a string or a :class:`cssutils.css.selector.Selector` object. :obj:`prefixes` may be a mapping mapping namespace prefixes to namespace names. """ if isinstance(selectors, str): if prefixes is not None: prefixes = dict((key, xsc.nsname(value)) for (key, value) in prefixes.items()) namespaces = "\n".join(f"@namespace {key if key is not None else ''} {value!r};" for (key, value) in prefixes.items()) selectors = f"{namespaces}\n{selectors}{{}}" else: selectors = f"{selectors}{{}}" for rule in cssutils.CSSParser().parseString(selectors).cssRules: if isinstance(rule, css.CSSStyleRule): selectors = rule.selectorList.seq break else: raise ValueError("can't happen") elif isinstance(selectors, css.CSSStyleRule): selectors = selectors.selectorList.seq elif isinstance(selectors, css.Selector): selectors = [selectors] else: raise TypeError(f"can't handle {type(selectors)!r}") orcombinators = [] for selector in selectors: rule = root = CSSTypeSelector() attributename = None attributevalue = None combinator = None for item in selector.seq: t = item.type v = item.value if t == "type-selector": rule.xmlns = v[0] if v[0] != -1 else None rule.type = v[1] if t == "universal": rule.xmlns = v[0] if v[0] != -1 else None rule.type = None elif t == "id": rule.selectors.append(xfind.hasid(v.lstrip("#"))) elif t == "class": rule.selectors.append(xfind.hasclass(v.lstrip("."))) elif t == "attribute-start": combinator = None elif t == "attribute-end": if combinator is None: rule.selectors.append(CSSHasAttributeSelector(attributename)) else: rule.selectors.append(combinator(attributename, attributevalue)) elif t == "attribute-selector": attributename = v elif t == "equals": combinator = xfind.attrhasvalue elif t == "includes": combinator = CSSAttributeListSelector elif t == "dashmatch": combinator = CSSAttributeLangSelector elif t == "prefixmatch": combinator = xfind.attrstartswith elif t == "suffixmatch": combinator = xfind.attrendswith elif t == "substringmatch": combinator = xfind.attrcontains elif t == "pseudo-class": if v.endswith("("): try: rule.selectors.append(_function2class[v.lstrip(":").rstrip("(")]()) except KeyError: raise ValueError(f"unknown function {v}") rule.function = v else: try: rule.selectors.append(_pseudoname2class[v.lstrip(":")]()) except KeyError: raise ValueError(f"unknown pseudo-class {v}") elif t == "NUMBER": # can only appear in a function => set the function value rule.selectors[-1].value = v elif t == "STRING": # can only appear in a attribute selector => set the attribute value attributevalue = v elif t == "child": rule = CSSTypeSelector() root = xfind.ChildCombinator(root, rule) elif t == "descendant": rule = CSSTypeSelector() root = xfind.DescendantCombinator(root, rule) elif t == "adjacent-sibling": rule = CSSTypeSelector() root = CSSAdjacentSiblingCombinator(root, rule) elif t == "following-sibling": rule = CSSTypeSelector() root = CSSGeneralSiblingCombinator(root, rule) orcombinators.append(root) return orcombinators[0] if len(orcombinators) == 1 else xfind.OrCombinator(*orcombinators)
def __init__(self, type=None, xmlns=None, *selectors): self.type = type self.xmlns = xsc.nsname(xmlns) self.selectors = [] # id, class, attribute etc. selectors for this node
def selector(selectors, prefixes=None): """ Create a :class:`xfind.Selector` object that matches all nodes that match the specified CSS selector expression. :obj:`selectors` can be a string or a :class:`cssutils.css.selector.Selector` object. :obj:`prefixes` may be a mapping mapping namespace prefixes to namespace names. """ if isinstance(selectors, str): if prefixes is not None: prefixes = dict( (key, xsc.nsname(value)) for (key, value) in prefixes.items()) selectors = "{}\n{}{{}}".format( "\n".join("@namespace {} {!r};".format( key if key is not None else "", value) for (key, value) in prefixes.items()), selectors) else: selectors = "{}{{}}".format(selectors) for rule in cssutils.CSSParser().parseString(selectors).cssRules: if isinstance(rule, css.CSSStyleRule): selectors = rule.selectorList.seq break else: raise ValueError("can't happen") elif isinstance(selectors, css.CSSStyleRule): selectors = selectors.selectorList.seq elif isinstance(selectors, css.Selector): selectors = [selectors] else: raise TypeError("can't handle {!r}".format(type(selectors))) orcombinators = [] for selector in selectors: rule = root = CSSTypeSelector() attributename = None attributevalue = None combinator = None for item in selector.seq: t = item.type v = item.value if t == "type-selector": rule.xmlns = v[0] if v[0] != -1 else None rule.type = v[1] if t == "universal": rule.xmlns = v[0] if v[0] != -1 else None rule.type = None elif t == "id": rule.selectors.append(xfind.hasid(v.lstrip("#"))) elif t == "class": rule.selectors.append(xfind.hasclass(v.lstrip("."))) elif t == "attribute-start": combinator = None elif t == "attribute-end": if combinator is None: rule.selectors.append( CSSHasAttributeSelector(attributename)) else: rule.selectors.append( combinator(attributename, attributevalue)) elif t == "attribute-selector": attributename = v elif t == "equals": combinator = xfind.attrhasvalue elif t == "includes": combinator = CSSAttributeListSelector elif t == "dashmatch": combinator = CSSAttributeLangSelector elif t == "prefixmatch": combinator = xfind.attrstartswith elif t == "suffixmatch": combinator = xfind.attrendswith elif t == "substringmatch": combinator = xfind.attrcontains elif t == "pseudo-class": if v.endswith("("): try: rule.selectors.append( _function2class[v.lstrip(":").rstrip("(")]()) except KeyError: raise ValueError("unknown function {}".format(v)) rule.function = v else: try: rule.selectors.append( _pseudoname2class[v.lstrip(":")]()) except KeyError: raise ValueError("unknown pseudo-class {}".format(v)) elif t == "NUMBER": # can only appear in a function => set the function value rule.selectors[-1].value = v elif t == "STRING": # can only appear in a attribute selector => set the attribute value attributevalue = v elif t == "child": rule = CSSTypeSelector() root = xfind.ChildCombinator(root, rule) elif t == "descendant": rule = CSSTypeSelector() root = xfind.DescendantCombinator(root, rule) elif t == "adjacent-sibling": rule = CSSTypeSelector() root = CSSAdjacentSiblingCombinator(root, rule) elif t == "following-sibling": rule = CSSTypeSelector() root = CSSGeneralSiblingCombinator(root, rule) orcombinators.append(root) return orcombinators[0] if len(orcombinators) == 1 else xfind.OrCombinator( *orcombinators)
def __init__(self, type=None, xmlns=None, *selectors): self.type = type self.xmlns = xsc.nsname(xmlns) self.selectors = [ ] # id, class, attribute etc. selectors for this node