def select_class(cache, selector): 'A class selector' items = cache.class_map[ascii_lower(selector.class_name)] if items: for elem in cache.iterparsedselector(selector.selector): if elem in items: yield elem
def select_hash(cache, selector): 'An id selector' items = cache.id_map[ascii_lower(selector.id)] if len(items) > 0: for elem in cache.iterparsedselector(selector.selector): if elem in items: yield elem
def select_attrib(cache, selector): operator = cache.attribute_operator_mapping[selector.operator] items = frozenset(cache.dispatch_map[operator]( cache, ascii_lower(selector.attrib), selector.value)) for item in cache.iterparsedselector(selector.selector): if item in items: yield item
def iterparsedselector(self, parsed_selector): type_name = type(parsed_selector).__name__ try: func = self.dispatch_map[ascii_lower(type_name)] except KeyError: raise ExpressionError('%s is not supported' % type_name) for item in func(self, parsed_selector): yield item
def select_element(cache, selector): """A type or universal selector.""" element = selector.element if not element or element == '*': for elem in cache.itertag(): yield elem else: for elem in cache.element_map[ascii_lower(element)]: yield elem
def select_lang(cache, function): ' Implement :lang() ' if function.argument_types() not in (['STRING'], ['IDENT']): raise ExpressionError("Expected a single string or ident for :lang(), got %r" % function.arguments) lang = function.arguments[0].value if lang: lang = ascii_lower(lang) lp = lang + '-' for tlang, elem_set in cache.lang_map.iteritems(): if tlang == lang or (tlang is not None and tlang.startswith(lp)): for elem in elem_set: yield elem
def select_lang(cache, function): ' Implement :lang() ' if function.argument_types() not in (['STRING'], ['IDENT']): raise ExpressionError("Expected a single string or ident for :lang(), got %r" % function.arguments) lang = function.arguments[0].value if lang: lang = ascii_lower(lang) lp = lang + '-' for tlang, elem_set in iteritems(cache.lang_map): if tlang == lang or (tlang is not None and tlang.startswith(lp)): for elem in elem_set: yield elem
def normalize_language_tag(tag): """Return a list of normalized combinations for a `BCP 47` language tag. Example: >>> normalize_language_tag('de_AT-1901') ['de-at-1901', 'de-at', 'de-1901', 'de'] """ # normalize: tag = ascii_lower(tag).replace('_','-') # split (except singletons, which mark the following tag as non-standard): tag = re.sub(r'-([a-zA-Z0-9])-', r'-\1_', tag) subtags = [subtag.replace('_', '-') for subtag in tag.split('-')] base_tag = (subtags.pop(0),) taglist = {base_tag[0]} # find all combinations of subtags for n in range(len(subtags), 0, -1): for tags in itertools.combinations(subtags, n): taglist.add('-'.join(base_tag + tags)) return taglist
def map_tag_name(x): return ascii_lower(x.rpartition('}')[2])
def map_attrib_name(x): return ascii_lower(x.rpartition('}')[2])
def select_attrib(cache, selector): operator = cache.attribute_operator_mapping[selector.operator] items = frozenset(cache.dispatch_map[operator](cache, ascii_lower(selector.attrib), selector.value)) for item in cache.iterparsedselector(selector.selector): if item in items: yield item