def __str__(self): if hasattr(self, '__unicode__'): if PY3: return self.__unicode__() else: return unicode(self).encode('utf-8') return txt_type('%s object' % self.__class__.__name__)
def get_attr(self, attrname): """ Get an Attribute object by the name `attrname`. """ if attrname in self.elem.attrs: return self.elem.attrs[attrname] else: msg = txt_type(u'{} attribute does not exist in {} {}'.format( attrname, self.elem.type.lower(), self.elem.name)) raise ActionError(msg)
def _make_attrs(self, elem, **kwargs): buf = [] if self.momo_configs['momo_attr_css']: name_fmt = ('<span class="momo-attr-name ' 'momo-attr-name-{name}">' '{name}</span>') else: name_fmt = '{name}' if self.momo_configs['momo_attr_css']: content_fmt = ('<span class="momo-attr-content ' 'momo-attr-content-{name}">' '{content}</span>') else: content_fmt = '{content}' if self.momo_configs['momo_attr_table']: if elem.attr_svals: buf.append('') buf.append('|') buf.append('- | -') for attr in self._filter_control_attrs(elem.attr_svals): buf.append( txt_type(name_fmt + ' | ' + content_fmt).format( name=attr.name, content=self._make_attr_content(attr, **kwargs).strip() ) ) buf.append('') else: for attr in self._filter_control_attrs(elem.attr_svals): buf.append('\n') buf.append( '- %s:%s' % (attr.name, self._make_attr_content(attr, **kwargs))) buf.append( txt_type('- ' + name_fmt + ':' + content_fmt).format( name=attr.name, content=self._make_attr_content(attr, **kwargs).strip() ) ) return '\n'.join(buf)
def _make_attrs(self, elem, **kwargs): buf = [] if self.momo_configs['momo_attr_css']: name_fmt = ('<span class="momo-attr-name ' 'momo-attr-name-{name}">' '{name}</span>') else: name_fmt = '{name}' if self.momo_configs['momo_attr_css']: content_fmt = ('<span class="momo-attr-content ' 'momo-attr-content-{name}">' '{content}</span>') else: content_fmt = '{content}' if self.momo_configs['momo_attr_table']: if elem.attr_svals: buf.append('') buf.append('|') buf.append('- | -') for attr in self._filter_control_attrs(elem.attr_svals): buf.append( txt_type(name_fmt + ' | ' + content_fmt).format( name=attr.name, content=self._make_attr_content(attr, **kwargs).strip())) buf.append('') else: for attr in self._filter_control_attrs(elem.attr_svals): buf.append('\n') buf.append( '- %s:%s' % (attr.name, self._make_attr_content(attr, **kwargs))) buf.append( txt_type('- ' + name_fmt + ':' + content_fmt).format( name=attr.name, content=self._make_attr_content(attr, **kwargs).strip())) return '\n'.join(buf)
def match_value(value, s, exact=False, without=False, case_insensitive=False, sep=None): """ Test whether the value of a node attribute or attr content matches the given string s, which is retrieved from parsed term. :param value: value of a node attribute or attr content. :param s: a string. :param exact: whether to do exact matches. :param without: if True, match if value is None (meaning value does not exist in the node). :param case_insensitive: whether do case-insensitive matching. :param sep: if not None, the separator is used to treat a string as a list separated by this separator. """ def with_case(a): if case_insensitive: return a.lower() else: return a def match_list(s, values): txt_values = map(with_case, map(txt_type, values)) if exact: return with_case(s) in txt_values else: for txt_value in txt_values: if with_case(s) in with_case(txt_value): return True return False if value is None: if without: return True else: return False else: if without: return False s = txt_type(s) if isinstance(value, (txt_type, bin_type, bool, int, float, Node)): if isinstance(value, bool): return match_bool(value, s) else: if (sep is not None and isinstance(value, (txt_type, bin_type)) and sep in value): values = split_by(value, sep) return match_list(s, values) else: if isinstance(value, Node): value = value.name if exact: return with_case(txt_type(value)) == with_case(s) else: return with_case(s) in with_case(txt_type(value)) else: return match_list(s, value)
def __unicode__(self): return txt_type(self.name)