CMS 3D CMS Logo

Public Member Functions | Public Attributes | Private Member Functions

BeautifulSoup::SoupStrainer Class Reference

List of all members.

Public Member Functions

def __init__
def __init__
def __str__
def __str__
def search
def search
def searchTag
def searchTag

Public Attributes

 attrs
 name
 text

Private Member Functions

def _matches
def _matches

Detailed Description

Encapsulates a number of ways of matching a markup element (tag or
text).

Definition at line 841 of file BeautifulSoup.py.


Constructor & Destructor Documentation

def BeautifulSoup::SoupStrainer::__init__ (   self,
  name = None,
  attrs = {},
  text = None,
  kwargs 
)

Definition at line 845 of file BeautifulSoup.py.

00845                                         {}, text=None, **kwargs):
00846         self.name = name
00847         if isString(attrs):
00848             kwargs['class'] = attrs
00849             attrs = None
00850         if kwargs:
00851             if attrs:
00852                 attrs = attrs.copy()
00853                 attrs.update(kwargs)
00854             else:
00855                 attrs = kwargs
00856         self.attrs = attrs
00857         self.text = text
00858 
def BeautifulSoup::SoupStrainer::__init__ (   self,
  name = None,
  attrs = {},
  text = None,
  kwargs 
)

Definition at line 845 of file BeautifulSoup.py.

00845                                         {}, text=None, **kwargs):
00846         self.name = name
00847         if isString(attrs):
00848             kwargs['class'] = attrs
00849             attrs = None
00850         if kwargs:
00851             if attrs:
00852                 attrs = attrs.copy()
00853                 attrs.update(kwargs)
00854             else:
00855                 attrs = kwargs
00856         self.attrs = attrs
00857         self.text = text
00858 

Member Function Documentation

def BeautifulSoup::SoupStrainer::__str__ (   self)

Definition at line 859 of file BeautifulSoup.py.

00860                      :
00861         if self.text:
00862             return self.text
00863         else:
00864             return "%s|%s" % (self.name, self.attrs)

def BeautifulSoup::SoupStrainer::__str__ (   self)

Definition at line 859 of file BeautifulSoup.py.

00860                      :
00861         if self.text:
00862             return self.text
00863         else:
00864             return "%s|%s" % (self.name, self.attrs)

def BeautifulSoup::SoupStrainer::_matches (   self,
  markup,
  matchAgainst 
) [private]

Definition at line 928 of file BeautifulSoup.py.

00929                                             :
00930         #print "Matching %s against %s" % (markup, matchAgainst)
00931         result = False
00932         if matchAgainst == True and type(matchAgainst) == types.BooleanType:
00933             result = markup != None
00934         elif callable(matchAgainst):
00935             result = matchAgainst(markup)
00936         else:
00937             #Custom match methods take the tag as an argument, but all
00938             #other ways of matching match the tag name as a string.
00939             if isinstance(markup, Tag):
00940                 markup = markup.name
00941             if markup is not None and not isString(markup):
00942                 markup = unicode(markup)
00943             #Now we know that chunk is either a string, or None.
00944             if hasattr(matchAgainst, 'match'):
00945                 # It's a regexp object.
00946                 result = markup and matchAgainst.search(markup)
00947             elif (isList(matchAgainst)
00948                   and (markup is not None or not isString(matchAgainst))):
00949                 result = markup in matchAgainst
00950             elif hasattr(matchAgainst, 'items'):
00951                 result = markup.has_key(matchAgainst)
00952             elif matchAgainst and isString(markup):
00953                 if isinstance(markup, unicode):
00954                     matchAgainst = unicode(matchAgainst)
00955                 else:
00956                     matchAgainst = str(matchAgainst)
00957 
00958             if not result:
00959                 result = matchAgainst == markup
00960         return result

def BeautifulSoup::SoupStrainer::_matches (   self,
  markup,
  matchAgainst 
) [private]

Definition at line 928 of file BeautifulSoup.py.

00929                                             :
00930         #print "Matching %s against %s" % (markup, matchAgainst)
00931         result = False
00932         if matchAgainst == True and type(matchAgainst) == types.BooleanType:
00933             result = markup != None
00934         elif callable(matchAgainst):
00935             result = matchAgainst(markup)
00936         else:
00937             #Custom match methods take the tag as an argument, but all
00938             #other ways of matching match the tag name as a string.
00939             if isinstance(markup, Tag):
00940                 markup = markup.name
00941             if markup is not None and not isString(markup):
00942                 markup = unicode(markup)
00943             #Now we know that chunk is either a string, or None.
00944             if hasattr(matchAgainst, 'match'):
00945                 # It's a regexp object.
00946                 result = markup and matchAgainst.search(markup)
00947             elif (isList(matchAgainst)
00948                   and (markup is not None or not isString(matchAgainst))):
00949                 result = markup in matchAgainst
00950             elif hasattr(matchAgainst, 'items'):
00951                 result = markup.has_key(matchAgainst)
00952             elif matchAgainst and isString(markup):
00953                 if isinstance(markup, unicode):
00954                     matchAgainst = unicode(matchAgainst)
00955                 else:
00956                     matchAgainst = str(matchAgainst)
00957 
00958             if not result:
00959                 result = matchAgainst == markup
00960         return result

def BeautifulSoup::SoupStrainer::search (   self,
  markup 
)

Definition at line 902 of file BeautifulSoup.py.

00903                             :
00904         #print 'looking for %s in %s' % (self, markup)
00905         found = None
00906         # If given a list of items, scan it for a text element that
00907         # matches.
00908         if isList(markup) and not isinstance(markup, Tag):
00909             for element in markup:
00910                 if isinstance(element, NavigableString) \
00911                        and self.search(element):
00912                     found = element
00913                     break
00914         # If it's a Tag, make sure its name or attributes match.
00915         # Don't bother with Tags if we're searching for text.
00916         elif isinstance(markup, Tag):
00917             if not self.text:
00918                 found = self.searchTag(markup)
00919         # If it's text, make sure the text matches.
00920         elif isinstance(markup, NavigableString) or \
00921                  isString(markup):
00922             if self._matches(markup, self.text):
00923                 found = markup
00924         else:
00925             raise Exception, "I don't know how to match against a %s" \
00926                   % markup.__class__
00927         return found

def BeautifulSoup::SoupStrainer::search (   self,
  markup 
)

Definition at line 902 of file BeautifulSoup.py.

00903                             :
00904         #print 'looking for %s in %s' % (self, markup)
00905         found = None
00906         # If given a list of items, scan it for a text element that
00907         # matches.
00908         if isList(markup) and not isinstance(markup, Tag):
00909             for element in markup:
00910                 if isinstance(element, NavigableString) \
00911                        and self.search(element):
00912                     found = element
00913                     break
00914         # If it's a Tag, make sure its name or attributes match.
00915         # Don't bother with Tags if we're searching for text.
00916         elif isinstance(markup, Tag):
00917             if not self.text:
00918                 found = self.searchTag(markup)
00919         # If it's text, make sure the text matches.
00920         elif isinstance(markup, NavigableString) or \
00921                  isString(markup):
00922             if self._matches(markup, self.text):
00923                 found = markup
00924         else:
00925             raise Exception, "I don't know how to match against a %s" \
00926                   % markup.__class__
00927         return found

def BeautifulSoup::SoupStrainer::searchTag (   self,
  markupName = None,
  markupAttrs = {} 
)

Definition at line 865 of file BeautifulSoup.py.

00865                                                      {}):
00866         found = None
00867         markup = None
00868         if isinstance(markupName, Tag):
00869             markup = markupName
00870             markupAttrs = markup
00871         callFunctionWithTagData = callable(self.name) \
00872                                 and not isinstance(markupName, Tag)
00873 
00874         if (not self.name) \
00875                or callFunctionWithTagData \
00876                or (markup and self._matches(markup, self.name)) \
00877                or (not markup and self._matches(markupName, self.name)):
00878             if callFunctionWithTagData:
00879                 match = self.name(markupName, markupAttrs)
00880             else:
00881                 match = True
00882                 markupAttrMap = None
00883                 for attr, matchAgainst in self.attrs.items():
00884                     if not markupAttrMap:
00885                          if hasattr(markupAttrs, 'get'):
00886                             markupAttrMap = markupAttrs
00887                          else:
00888                             markupAttrMap = {}
00889                             for k,v in markupAttrs:
00890                                 markupAttrMap[k] = v
00891                     attrValue = markupAttrMap.get(attr)
00892                     if not self._matches(attrValue, matchAgainst):
00893                         match = False
00894                         break
00895             if match:
00896                 if markup:
00897                     found = markup
00898                 else:
00899                     found = markupName
00900         return found
00901 
def BeautifulSoup::SoupStrainer::searchTag (   self,
  markupName = None,
  markupAttrs = {} 
)

Definition at line 865 of file BeautifulSoup.py.

00865                                                      {}):
00866         found = None
00867         markup = None
00868         if isinstance(markupName, Tag):
00869             markup = markupName
00870             markupAttrs = markup
00871         callFunctionWithTagData = callable(self.name) \
00872                                 and not isinstance(markupName, Tag)
00873 
00874         if (not self.name) \
00875                or callFunctionWithTagData \
00876                or (markup and self._matches(markup, self.name)) \
00877                or (not markup and self._matches(markupName, self.name)):
00878             if callFunctionWithTagData:
00879                 match = self.name(markupName, markupAttrs)
00880             else:
00881                 match = True
00882                 markupAttrMap = None
00883                 for attr, matchAgainst in self.attrs.items():
00884                     if not markupAttrMap:
00885                          if hasattr(markupAttrs, 'get'):
00886                             markupAttrMap = markupAttrs
00887                          else:
00888                             markupAttrMap = {}
00889                             for k,v in markupAttrs:
00890                                 markupAttrMap[k] = v
00891                     attrValue = markupAttrMap.get(attr)
00892                     if not self._matches(attrValue, matchAgainst):
00893                         match = False
00894                         break
00895             if match:
00896                 if markup:
00897                     found = markup
00898                 else:
00899                     found = markupName
00900         return found
00901 

Member Data Documentation

Definition at line 845 of file BeautifulSoup.py.

Definition at line 845 of file BeautifulSoup.py.

Definition at line 845 of file BeautifulSoup.py.