Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
Bio.ParserSupport.EventGenerator Class Reference

List of all members.

Public Member Functions

def __init__
def startElement
def characters
def endElement
def endDocument

Public Attributes

 interest_tags
 info

Private Member Functions

def _make_callback

Private Attributes

 _consumer
 _finalizer
 _exempt_tags
 _previous_tag
 _cur_content
 _collect_characters

Detailed Description

Handler to generate events associated with a Martel parsed file.

This acts like a normal SAX handler, and accepts XML generated by
Martel during parsing. These events are then converted into
'Biopython events', which can then be caught by a standard
biopython consumer.

Note that Martel is now DEPRECATED.

Definition at line 162 of file ParserSupport.py.


Constructor & Destructor Documentation

def Bio.ParserSupport.EventGenerator.__init__ (   self,
  consumer,
  interest_tags,
  callback_finalizer = None,
  exempt_tags = [] 
)
Initialize to begin catching and firing off events.

Arguments:
o consumer - The consumer that we'll send Biopython events to.

o interest_tags - A listing of all the tags we are interested in.

o callback_finalizer - A function to deal with the collected
information before passing it on to the consumer. By default
the collected information is a list of all of the lines read
for a particular tag -- if there are multiple tags in a row
like:

<some_info>Spam<some_info>
<some_info>More Spam<some_info>

In this case the list of information would be:

['Spam', 'More Spam']

This list of lines will be passed to the callback finalizer if
it is present. Otherwise the consumer will be called with the
list of content information.

o exempt_tags - A listing of particular tags that are exempt from
being processed by the callback_finalizer. This allows you to
use a finalizer to deal with most tags, but leave those you don't
want touched.

Definition at line 173 of file ParserSupport.py.

00173 
00174                      exempt_tags = []):
00175             """Initialize to begin catching and firing off events.
00176 
00177             Arguments:
00178             o consumer - The consumer that we'll send Biopython events to.
00179             
00180             o interest_tags - A listing of all the tags we are interested in.
00181 
00182             o callback_finalizer - A function to deal with the collected
00183             information before passing it on to the consumer. By default
00184             the collected information is a list of all of the lines read
00185             for a particular tag -- if there are multiple tags in a row
00186             like:
00187 
00188             <some_info>Spam<some_info>
00189             <some_info>More Spam<some_info>
00190 
00191             In this case the list of information would be:
00192 
00193             ['Spam', 'More Spam']
00194             
00195             This list of lines will be passed to the callback finalizer if
00196             it is present. Otherwise the consumer will be called with the
00197             list of content information.
00198 
00199             o exempt_tags - A listing of particular tags that are exempt from
00200             being processed by the callback_finalizer. This allows you to
00201             use a finalizer to deal with most tags, but leave those you don't
00202             want touched.
00203             """
00204             self._consumer = consumer
00205             self.interest_tags = interest_tags
00206             self._finalizer = callback_finalizer
00207             self._exempt_tags = exempt_tags
00208 
00209             # a dictionary of content for each tag of interest
00210             # the information for each tag is held as a list of the lines.
00211             # This allows us to collect information from multiple tags
00212             # in a row, and return it all at once.
00213             self.info = {}
00214             for tag in self.interest_tags:
00215                 self.info[tag] = []
00216 
00217             # the previous tag we were collecting information for.
00218             # We set a delay in sending info to the consumer so that we can
00219             # collect a bunch of tags in a row and append all of the info
00220             # together.
00221             self._previous_tag = ''
00222 
00223             # the current character information for a tag
00224             self._cur_content = []
00225             # whether we should be collecting information
00226             self._collect_characters = 0


Member Function Documentation

def Bio.ParserSupport.EventGenerator._make_callback (   self,
  name 
) [private]
Call the callback function with the info with the given name.

Definition at line 265 of file ParserSupport.py.

00265 
00266         def _make_callback(self, name):
00267             """Call the callback function with the info with the given name.
00268             """
00269             # strip off whitespace and call the consumer
00270             callback_function = getattr(self._consumer, name)
00271 
00272             # --- pass back the information
00273             # if there is a finalizer, use that
00274             if self._finalizer is not None and name not in self._exempt_tags:
00275                 info_to_pass = self._finalizer(self.info[name])
00276             # otherwise pass back the entire list of information
00277             else:
00278                 info_to_pass = self.info[name]
00279             
00280             callback_function(info_to_pass)
00281 
00282             # reset the information for the tag
00283             self.info[name] = []

Here is the caller graph for this function:

def Bio.ParserSupport.EventGenerator.characters (   self,
  content 
)
Extract the information if we are interested in it.

Definition at line 233 of file ParserSupport.py.

00233 
00234         def characters(self, content):
00235             """Extract the information if we are interested in it.
00236             """
00237             if self._collect_characters:
00238                 self._cur_content.append(content)

Make sure all of our information has been passed.

This just flushes out any stored tags that need to be passed.

Definition at line 284 of file ParserSupport.py.

00284 
00285         def endDocument(self):
00286             """Make sure all of our information has been passed.
00287 
00288             This just flushes out any stored tags that need to be passed.
00289             """
00290             if self._previous_tag:
00291                 self._make_callback(self._previous_tag)

Here is the call graph for this function:

def Bio.ParserSupport.EventGenerator.endElement (   self,
  name 
)
Send the information to the consumer.

Once we've got the end element we've collected up all of the
character information we need, and we need to send this on to
the consumer to do something with it.

We have a delay of one tag on doing this, so that we can collect
all of the info from multiple calls to the same element at once.

Definition at line 239 of file ParserSupport.py.

00239 
00240         def endElement(self, name):
00241             """Send the information to the consumer.
00242 
00243             Once we've got the end element we've collected up all of the
00244             character information we need, and we need to send this on to
00245             the consumer to do something with it.
00246 
00247             We have a delay of one tag on doing this, so that we can collect
00248             all of the info from multiple calls to the same element at once.
00249             """
00250             # only deal with the tag if it is something we are
00251             # interested in and potentially have information for
00252             if self._collect_characters:
00253                 # add all of the information collected inside this tag
00254                 self.info[name].append("".join(self._cur_content))
00255                 # reset our information and flags
00256                 self._cur_content = []
00257                 self._collect_characters = 0
00258                 
00259                 # if we are at a new tag, pass on the info from the last tag
00260                 if self._previous_tag and self._previous_tag != name:
00261                     self._make_callback(self._previous_tag)
00262 
00263                 # set this tag as the next to be passed
00264                 self._previous_tag = name

Here is the call graph for this function:

def Bio.ParserSupport.EventGenerator.startElement (   self,
  name,
  attrs 
)
Determine if we should collect characters from this tag.

Definition at line 227 of file ParserSupport.py.

00227 
00228         def startElement(self, name, attrs):
00229             """Determine if we should collect characters from this tag.
00230             """
00231             if name in self.interest_tags:
00232                 self._collect_characters = 1


Member Data Documentation

Definition at line 225 of file ParserSupport.py.

Definition at line 203 of file ParserSupport.py.

Definition at line 223 of file ParserSupport.py.

Definition at line 206 of file ParserSupport.py.

Definition at line 205 of file ParserSupport.py.

Definition at line 220 of file ParserSupport.py.

Definition at line 212 of file ParserSupport.py.

Definition at line 204 of file ParserSupport.py.


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