Пример #1
0
def get_all_properties():
    all_properties = list(get_css_dict().keys())

    # раширить парами "свойство значение" (например "position absolute")
    for prop_name in all_properties:
        property_values = css_flat_list(prop_name, get_css_dict())
        extends_sieve = (i for i in property_values if not i[1].startswith('<'))
        unit_sieve = (i for i in extends_sieve if not i[1].startswith('.'))
        all_properties.extend('{0} {1}'.format(prop_name, v[1]) for v in unit_sieve)
    return all_properties
Пример #2
0
def extract(s1):
    """В зависимости от найденных компонент в аббревиатуре применяет функцию extract"""
    # print repr(s1)
    prop_iter = []
    parts = segmentation(s1)
    abbr_value = False
    if 'property-name' in parts:
        if parts['important']:
            s1 = s1[:-1]
        if s1[-1] != ':' and s1 != parts['property-name']:
            abbr_value = True

    if 'color' in parts:
        prop_iter.extend(prop for prop, val in get_flat_css() if val == '<color_values>')

    if isinstance(parts.get('type-value'), int):
        prop_iter.extend(prop for prop, val in get_flat_css() if val == '<integer>')

    if isinstance(parts.get('type-value'), float):
        # TODO: добавить deg, grad, time
        prop_iter.extend(prop for prop, val in get_flat_css() if val in ('<length>', '<number>', 'percentage'))

    if 'keyword-value' in parts and not parts['keyword-value']:
        prop_iter.extend(get_all_properties())

    if 'keyword-value' in parts:
        prop_iter.extend(prop_value(parts['property-name'], parts['keyword-value']))
    elif 'color' not in parts or 'type-value' in parts:
        prop_iter.extend(get_all_properties())

    assert parts.get('property-name', '') or parts.get('property-value', '')
    abbr = ' '.join([
        parts.get('property-name', '') or parts.get('property-value', ''),
        parts.get('keyword-value', ''),
    ])

    # предустановленные правила
    abbr = abbr.strip()
    if abbr in STATIC_ABBR:
        property_ = STATIC_ABBR[abbr]
    else:
        starts_properties = []
        # todo: переделать механизм PAIRS
        # надо вынести константы в css-dict
        # по две буквы (bd, bg, ba)
        pair = PAIRS.get(abbr[:2], None)
        if pair is not None:
            starts_properties = [prop for prop in prop_iter if prop.startswith(pair) and sub_string(prop, abbr)]
        if not starts_properties:
            starts_properties = [prop for prop in prop_iter if prop[0] == abbr[0] and sub_string(prop, abbr)]

        if 'type-value' in parts:
            starts_properties = [i for i in starts_properties if ' ' not in i]

        property_ = hayaku_extract(abbr, starts_properties, PRIORITY_PROPERTIES, string_score)

    property_, value = property_.split(' ') if ' ' in property_ else (property_, None)
    # print property_, value
    if not property_:
        return {}

    parts['property-name'] = property_

    if value is not None:
        parts['keyword-value'] = value

    # Проверка соответствия свойства и значения

    allow_values = [val for prop, val in get_flat_css() if prop == parts['property-name']]

    if 'color' in parts and '<color_values>' not in allow_values:
        del parts['color']
    if 'type-value' in parts and not any((t in allow_values) for t in ['<integer>', 'percentage', '<length>', '<number>', '<alphavalue>']):
        del parts['type-value']
    if 'keyword-value' in parts and parts['keyword-value'] not in allow_values:
        del parts['keyword-value']

    if all([
            'keyword-value' not in parts,
            'type-value' not in parts,
            'color' not in parts,
        ]) and abbr_value:
        return {}

    # Добавить значение по-умолчанию
    if parts['property-name'] in get_css_dict():
        default_value = css_defaults(parts['property-name'], get_css_dict())
        if default_value is not None:
            parts['default-value'] = default_value
        obj = get_css_dict()[parts['property-name']]
        if 'prefixes' in obj:
            parts['prefixes'] = obj['prefixes']
            if 'no-unprefixed-property' in obj:
                parts['no-unprefixed-property'] = obj['no-unprefixed-property']

    if parts['abbr'] == parts.get('property-value'):
        del parts['property-value']

    return parts