ó
‘ Ic           @   sO  d  Z  d d l Z d d l Z d d l Td d l m Z y d d l m Z d Z Wn+ e	 k
 r e j
 j d d ƒ d	 Z n Xd
 d d „  ƒ  YZ d d d „  ƒ  YZ d e f d „  ƒ  YZ d d d „  ƒ  YZ e rñ d e j f d „  ƒ  YZ n  d „  Z d „  Z d „  Z d „  Z d d d d d d „ Z d	 d „ Z d „  Z d „  Z d S(   s„  Code to support writing parsers.



Classes:
AbstractParser         Base class for parsers.
AbstractConsumer       Base class of all Consumers.
TaggingConsumer        Consumer that tags output with its event.  For debugging
SGMLStrippingConsumer  Consumer that strips SGML tags from output.
EventGenerator         Generate Biopython Events from Martel XML output
                       (note that Martel is now DEPRECATED)

Functions:
safe_readline          Read a line from a handle, with check for EOF.
safe_peekline          Peek at next line, with check for EOF.
read_and_call          Read a line from a handle and pass it to a method.
read_and_call_while    Read many lines, as long as a condition is met.
read_and_call_until    Read many lines, until a condition is met.
attempt_read_and_call  Like read_and_call, but forgiving of errors.
is_blank_line          Test whether a line is blank.

iÿÿÿÿN(   t   *(   t   File(   t   handleri   s4   Warning: Could not import SAX for dealing with XML.
s5   This causes problems with some ParserSupport modules
i    t   AbstractParserc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s#   Base class for other parsers.

    c         C   s   t  d ƒ ‚ d  S(   Ns#   Please implement in a derived class(   t   NotImplementedError(   t   selft   handle(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   parse0   s    c         C   s   |  j  t j | ƒ ƒ S(   N(   R   R   t   StringHandle(   R   t   string(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt	   parse_str3   s    c         C   s1   t  | ƒ } z |  j | ƒ } Wd  | j ƒ  X| S(   N(   t   openR   t   close(   R   t   filenamet   ht   retval(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt
   parse_file6   s
    (   t   __name__t
   __module__t   __doc__R   R
   R   (    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR   ,   s   		t   AbstractConsumerc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sž   Base class for other Consumers.

    Derive Consumers from this class and implement appropriate
    methods for each event that you want to receive.
    
    c         C   s   d  S(   N(    (   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   _unhandled_sectionE   s    c         C   s   d  S(   N(    (   R   t   data(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt
   _unhandledG   s    c         C   s9   | d  d k s  | d  d k r, |  j  } n	 |  j } | S(   Ni   t   start_i   t   end_(   R   R   (   R   t   attrt   method(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   __getattr__I   s     	(   R   R   R   R   R   R   (    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR   >   s   		t   TaggingConsumerc           B   sG   e  Z d  Z d d d d „ Z d „  Z d „  Z d d „ Z d „  Z RS(	   so   A Consumer that tags the data stream with the event and
    prints it to a handle.  Useful for debugging.

    i   iP   c         C   s7   | d k r t j } n  | |  _ | |  _ | |  _ d S(   s<   TaggingConsumer(handle=sys.stdout, colwidth=15, maxwidth=80)N(   t   Nonet   syst   stdoutt   _handlet	   _colwidtht	   _maxwidth(   R   R   t   colwidtht   maxwidth(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   __init__U   s
    		c         C   s   |  j  d ƒ d  S(   Nt   unhandled_section(   t   _print_name(   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR'   b   s    c         C   s   |  j  d | ƒ d  S(   Nt	   unhandled(   R(   (   R   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR)   e   s    c         C   ss   | d  k r0 |  j j d d |  j | f ƒ n? |  j j d |  j | |  j  | |  j |  j d  j ƒ  f ƒ d  S(   Ns   %s %s
R    s	   %-*s: %s
i   (   R   R!   t   writeR"   R#   t   rstrip(   R   t   nameR   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR(   h   s
    $c         C   sE   | d  d k s  | d  d k r2 | |  d „ } n | |  d „ } | S(   Ni   R   i   R   c         S   s   | j  |  ƒ S(   N(   R(   (   t   at   s(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   <lambda>t   s    c         S   s   | j  | |  ƒ S(   N(   R(   (   t   xR-   R.   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR/   v   s    (    (   R   R   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR   r   s     N(	   R   R   R   R   R&   R'   R)   R(   R   (    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR   P   s   		
t   SGMLStrippingConsumerc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sm   A consumer that strips off SGML tags.

    This is meant to be used as a decorator for other consumers.

    c         C   sF   t  | ƒ t k	 r! t d ƒ ‚ n  | |  _ d  |  _ t j ƒ  |  _ d  S(   Ns   consumer should be an instance(	   t   typet   InstanceTypet
   ValueErrort	   _consumerR   t
   _prev_attrR   t   SGMLStrippert	   _stripper(   R   t   consumer(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR&      s
    		c         C   s#   |  j  j | ƒ } |  j | ƒ d  S(   N(   R8   t   stripR6   (   R   R   t   clean(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   _apply_clean_data†   s    c         C   su   | d k r t  |  | ƒ St  |  j | ƒ } t | ƒ t k	 rA | S| d  d k sa | d  d k re | S| |  _ |  j S(   NR6   R8   i   R   i   R   (   R6   R8   (   t   getattrR5   R2   t
   MethodTypeR6   R<   (   R   R,   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR   Š   s     	(   R   R   R   R&   R<   R   (    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR1   y   s   		t   EventGeneratorc           B   sJ   e  Z d  Z d g  d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sd  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.
        c         C   sm   | |  _  | |  _ | |  _ | |  _ i  |  _ x |  j D] } g  |  j | <q7 Wd |  _ g  |  _ d |  _ d S(   s³  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.
            t    i    N(   R5   t   interest_tagst
   _finalizert   _exempt_tagst   infot   _previous_tagt   _cur_contentt   _collect_characters(   R   R9   RA   t   callback_finalizert   exempt_tagst   tag(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR&   ¤   s    							c         C   s   | |  j  k r d |  _ n  d S(   sE   Determine if we should collect characters from this tag.
            i   N(   RA   RG   (   R   R,   t   attrs(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   startElementÛ   s    c         C   s    |  j  r |  j j | ƒ n  d S(   s@   Extract the information if we are interested in it.
            N(   RG   RF   t   append(   R   t   content(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt
   charactersá   s    	c         C   sv   |  j  rr |  j | j d j |  j ƒ ƒ g  |  _ d |  _  |  j rf |  j | k rf |  j |  j ƒ n  | |  _ n  d S(   s“  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.
            R@   i    N(   RG   RD   RM   t   joinRF   RE   t   _make_callback(   R   R,   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt
   endElementç   s    	 		c         C   sq   t  |  j | ƒ } |  j d k	 rI | |  j k rI |  j |  j | ƒ } n |  j | } | | ƒ g  |  j | <d S(   sJ   Call the callback function with the info with the given name.
            N(   R=   R5   RB   R   RC   RD   (   R   R,   t   callback_functiont   info_to_pass(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyRQ     s    
c         C   s    |  j  r |  j |  j  ƒ n  d S(   s‰   Make sure all of our information has been passed.

            This just flushes out any stored tags that need to be passed.
            N(   RE   RQ   (   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   endDocument  s    	N(
   R   R   R   R   R&   RL   RO   RR   RQ   RU   (    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyR?   š   s   	6				c         K   sG   t  |  ƒ } t | f | Ž  } | d k	 r9 t | ƒ ‚ n  | | ƒ d S(   s…  read_and_call(uhandle, method[, start][, end][, contains][, blank][, has_re])

    Read a line from uhandle, check it, and pass it to the method.
    Raises a ValueError if the line does not pass the checks.

    start, end, contains, blank, and has_re specify optional conditions
    that the line must pass.  start and end specifies what the line must
    begin or end with (not counting EOL characters).  contains
    specifies a substring that must be found in the line.  If blank
    is a true value, then the line must be blank.  has_re should be
    a regular expression object with a pattern that the line must match
    somewhere.

    N(   t   safe_readlinet   _fails_conditionsR   R4   (   t   uhandleR   t   keywdst   linet   errmsg(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   read_and_call  s
    c         K   sT   d } xG t  |  ƒ } t | f | Ž  r8 |  j | ƒ Pn  | | ƒ | d } q	 W| S(   sJ  read_and_call_while(uhandle, method[, start][, end][, contains][, blank][, has_re]) -> number of lines

    Read a line from uhandle and pass it to the method as long as
    some condition is true.  Returns the number of lines that were read.

    See the docstring for read_and_call for a description of the parameters.
    
    i    i   (   RV   RW   t   saveline(   RX   R   RY   t   nlinesRZ   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   read_and_call_while1  s    	
c         K   sT   d } xG t  |  ƒ } t | f | Ž  s8 |  j | ƒ Pn  | | ƒ | d } q	 W| S(   sL  read_and_call_until(uhandle, method, 
    start=None, end=None, contains=None, blank=None) -> number of lines

    Read a line from uhandle and pass it to the method until
    some condition is true.  Returns the number of lines that were read.

    See the docstring for read_and_call for a description of the parameters.
    
    i    i   (   RV   RW   R]   (   RX   R   RY   R^   RZ   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   read_and_call_untilE  s    

c         K   sC   t  |  ƒ } t | f | Ž  } | r2 | | ƒ n |  j | ƒ | S(   s  attempt_read_and_call(uhandle, method, **keywds) -> boolean

    Similar to read_and_call, but returns a boolean specifying
    whether the line has passed the checks.  Does not raise
    exceptions.

    See docs for read_and_call for a description of the function
    arguments.

    (   RV   RW   R]   (   RX   R   RY   RZ   t   passed(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   attempt_read_and_callZ  s    c         C   s  | d  k	 r3 |  t | ƒ  | k r3 d | |  f Sn  | d  k	 rm |  j ƒ  t | ƒ | k rm d | |  f Sn  | d  k	 rŸ |  j | ƒ d k rŸ d | |  f Sn  | d  k	 rÛ | rÈ t |  ƒ sØ d |  SqÛ t |  ƒ rÛ d Sn  | d  k	 r| j |  ƒ d  k rd | j |  f Sn  d  S(   Ns!   Line does not start with '%s':
%ss   Line does not end with '%s':
%siÿÿÿÿs   Line does not contain '%s':
%ss    Expected blank line, but got:
%ss,   Expected non-blank line, but got a blank ones"   Line does not match regex '%s':
%s(   R   t   lenR+   t   findt   is_blank_linet   searcht   pattern(   RZ   t   startt   endt   containst   blankt   has_re(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyRW   m  s(    c         C   s>   |  s
 d S| r  |  j  ƒ  d k S|  d d k p= |  d d k S(   s$  is_blank_line(line, allow_spaces=0) -> boolean

    Return whether a line is blank.  allow_spaces specifies whether to
    allow whitespaces in a blank line.  A true value signifies that a
    line containing whitespaces as well as end-of-line characters
    should be considered blank.

    i   R@   i    s   
s   (   R+   (   RZ   t   allow_spaces(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyRe   …  s
    	c         C   s%   |  j  ƒ  } | s! t d ƒ ‚ n  | S(   sš   safe_readline(handle) -> line

    Read a line from an UndoHandle and return it.  If there are no more
    lines to read, I will raise a ValueError.

    s   Unexpected end of stream.(   t   readlineR4   (   R   RZ   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyRV   ”  s    c         C   s%   |  j  ƒ  } | s! t d ƒ ‚ n  | S(   s¦   safe_peekline(handle) -> line

    Peek at the next line in an UndoHandle and return it.  If there are no
    more lines to peek, I will raise a ValueError.
    
    s   Unexpected end of stream.(   t   peeklineR4   (   R   RZ   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   safe_peekline   s    (    (    (    (   R   R   t	   tracebackt   typest   BioR   t   xml.saxR   t   xml_supportt   ImportErrort   stderrR*   R   R   R   R1   t   ContentHandlerR?   R\   R_   R`   Rb   R   RW   Re   RV   Rp   (    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/ParserSupport.pyt   <module>   s4   


) ‚					