Back to index

plone3  3.1.7
Public Member Functions | Public Attributes | Private Member Functions
kss.core.BeautifulSoup.SoupStrainer Class Reference

List of all members.

Public Member Functions

def __init__
def __str__
def searchTag
def search

Public Attributes

 name
 attrs
 text

Private Member Functions

def _matches

Detailed Description

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

Definition at line 722 of file BeautifulSoup.py.


Constructor & Destructor Documentation

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

Definition at line 726 of file BeautifulSoup.py.

00726 
00727     def __init__(self, name=None, attrs={}, text=None, **kwargs):
00728         self.name = name
00729         if isString(attrs):
00730             kwargs['class'] = attrs
00731             attrs = None
00732         if kwargs:
00733             if attrs:
00734                 attrs = attrs.copy()
00735                 attrs.update(kwargs)
00736             else:
00737                 attrs = kwargs
00738         self.attrs = attrs
00739         self.text = text

Here is the caller graph for this function:


Member Function Documentation

Definition at line 740 of file BeautifulSoup.py.

00740 
00741     def __str__(self):
00742         if self.text:
00743             return self.text
00744         else:
00745             return "%s|%s" % (self.name, self.attrs)
    
def kss.core.BeautifulSoup.SoupStrainer._matches (   self,
  markup,
  matchAgainst 
) [private]

Definition at line 809 of file BeautifulSoup.py.

00809 
00810     def _matches(self, markup, matchAgainst):    
00811         #print "Matching %s against %s" % (markup, matchAgainst)
00812         result = False
00813         if matchAgainst == True and type(matchAgainst) == types.BooleanType:
00814             result = markup != None
00815         elif callable(matchAgainst):
00816             result = matchAgainst(markup)
00817         else:
00818             #Custom match methods take the tag as an argument, but all
00819             #other ways of matching match the tag name as a string.
00820             if isinstance(markup, Tag):
00821                 markup = markup.name
00822             if markup and not isString(markup):
00823                 markup = unicode(markup)
00824             #Now we know that chunk is either a string, or None.
00825             if hasattr(matchAgainst, 'match'):
00826                 # It's a regexp object.
00827                 result = markup and matchAgainst.search(markup)
00828             elif isList(matchAgainst):
00829                 result = markup in matchAgainst
00830             elif hasattr(matchAgainst, 'items'):
00831                 result = markup.has_key(matchAgainst)
00832             elif matchAgainst and isString(markup):
00833                 if isinstance(markup, unicode):
00834                     matchAgainst = unicode(matchAgainst)
00835                 else:
00836                     matchAgainst = str(matchAgainst)
00837 
00838             if not result:
00839                 result = matchAgainst == markup
00840         return result

Here is the call graph for this function:

Here is the caller graph for this function:

def kss.core.BeautifulSoup.SoupStrainer.search (   self,
  markup 
)

Definition at line 783 of file BeautifulSoup.py.

00783 
00784     def search(self, markup):
00785         #print 'looking for %s in %s' % (self, markup)
00786         found = None
00787         # If given a list of items, scan it for a text element that
00788         # matches.        
00789         if isList(markup) and not isinstance(markup, Tag):
00790             for element in markup:
00791                 if isinstance(element, NavigableString) \
00792                        and self.search(element):
00793                     found = element
00794                     break
00795         # If it's a Tag, make sure its name or attributes match.
00796         # Don't bother with Tags if we're searching for text.
00797         elif isinstance(markup, Tag):
00798             if not self.text:
00799                 found = self.searchTag(markup)
00800         # If it's text, make sure the text matches.
00801         elif isinstance(markup, NavigableString) or \
00802                  isString(markup):
00803             if self._matches(markup, self.text):
00804                 found = markup
00805         else:
00806             raise Exception, "I don't know how to match against a %s" \
00807                   % markup.__class__
00808         return found
        

Here is the call graph for this function:

Here is the caller graph for this function:

def kss.core.BeautifulSoup.SoupStrainer.searchTag (   self,
  markupName = None,
  markupAttrs = {} 
)

Definition at line 746 of file BeautifulSoup.py.

00746 
00747     def searchTag(self, markupName=None, markupAttrs={}):
00748         found = None
00749         markup = None
00750         if isinstance(markupName, Tag):
00751             markup = markupName
00752             markupAttrs = markup
00753         callFunctionWithTagData = callable(self.name) \
00754                                 and not isinstance(markupName, Tag)
00755 
00756         if (not self.name) \
00757                or callFunctionWithTagData \
00758                or (markup and self._matches(markup, self.name)) \
00759                or (not markup and self._matches(markupName, self.name)):
00760             if callFunctionWithTagData:
00761                 match = self.name(markupName, markupAttrs)
00762             else:
00763                 match = True            
00764                 markupAttrMap = None
00765                 for attr, matchAgainst in self.attrs.items():
00766                     if not markupAttrMap:
00767                          if hasattr(markupAttrs, 'get'):
00768                             markupAttrMap = markupAttrs
00769                          else:
00770                             markupAttrMap = {}
00771                             for k,v in markupAttrs:
00772                                 markupAttrMap[k] = v
00773                     attrValue = markupAttrMap.get(attr)
00774                     if not self._matches(attrValue, matchAgainst):
00775                         match = False
00776                         break
00777             if match:
00778                 if markup:
00779                     found = markup
00780                 else:
00781                     found = markupName
00782         return found

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 737 of file BeautifulSoup.py.

Definition at line 727 of file BeautifulSoup.py.

Definition at line 738 of file BeautifulSoup.py.


The documentation for this class was generated from the following file: