Esempio n. 1
0
    def assert_exists(self):
        """
        Return True if selector list is not empty.
        """

        if not self.exists():
            args = (self.origin_query, self.origin_selector_class.__name__)
            raise DataNotFound(u'Node does not exists, query: %s, '
                               u'query type: %s' % args)
Esempio n. 2
0
 def node(self, default=NULL):
     try:
         return self.one().node()
     except IndexError:
         if default is NULL:
             m = 'Could not get first item for %s query of class %s'\
                 % (self.origin_query, self.origin_selector_class.__name__)
             raise DataNotFound(m)
         else:
             return default
Esempio n. 3
0
 def attr(self, key, default=NULL):
     if self.is_text_node():
         raise SelectionRuntimeError('Text node selectors do not '
                                     'allow attr method')
     if default is NULL:
         if key in self.node().attrib:
             return self.node().get(key)
         else:
             raise DataNotFound(u'No such attribute: %s' % key)
     else:
         return self.node().get(key, default)
Esempio n. 4
0
    def css_one(self, path, default=NULL):
        """
        Get first element which matches the given css path
            or raise DataNotFound.
        """

        try:
            return self.css_list(path)[0]
        except IndexError:
            if default is NULL:
                raise DataNotFound('CSS path not found: %s' % path)
            else:
                return default
Esempio n. 5
0
def rex_text(body, regexp, flags=0, default=NULL):
    """
    Search `regexp` expression in `body` text and then strip tags in found result.
    """

    match = rex(body, regexp, flags=flags, default=default)
    try:
        return normalize_space(decode_entities(match.group(1)))
    except AttributeError:
        if default is NULL:
            raise DataNotFound('Regexp not found')
        else:
            return default
Esempio n. 6
0
def rex(body, regexp, flags=0, byte=False, default=NULL):
    """
    Search `regexp` expression in `body` text.
    """

    regexp = normalize_regexp(regexp, flags)
    match = regexp.search(body)
    if match:
        return match
    else:
        if default is NULL:
            raise DataNotFound('Could not find regexp: %s' % regexp)
        else:
            return default