コード例 #1
0
ファイル: select.py プロジェクト: CharlesCai930/calibre
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
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
ファイル: select.py プロジェクト: CharlesCai930/calibre
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
コード例 #5
0
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
コード例 #6
0
 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
コード例 #7
0
ファイル: select.py プロジェクト: CharlesCai930/calibre
 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
コード例 #8
0
ファイル: select.py プロジェクト: CharlesCai930/calibre
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
コード例 #9
0
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
コード例 #10
0
ファイル: select.py プロジェクト: CharlesCai930/calibre
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
コード例 #11
0
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
コード例 #12
0
ファイル: select.py プロジェクト: CharlesCai930/calibre
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
コード例 #13
0
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
コード例 #14
0
ファイル: select.py プロジェクト: CharlesCai930/calibre
 def map_tag_name(x):
     return ascii_lower(x.rpartition('}')[2])
コード例 #15
0
ファイル: select.py プロジェクト: CharlesCai930/calibre
 def map_attrib_name(x):
     return ascii_lower(x.rpartition('}')[2])
コード例 #16
0
 def map_tag_name(x):
     return ascii_lower(x.rpartition('}')[2])
コード例 #17
0
ファイル: select.py プロジェクト: CharlesCai930/calibre
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
コード例 #18
0
 def map_attrib_name(x):
     return ascii_lower(x.rpartition('}')[2])