m
9Cc           @   sS  d  Z  d Z d Z d Z d Z d k Z d k Z d k Z d k l	 Z	 d k
 Z
 d k Z d k l Z d k
 l Z l Z y d k Z WnC e j
 o7 y d k Z Wq e j
 o d	 k l Z q Xn Xd d
  Z d d d  Z e d d e   d  Z e d d e   d  Z d   Z e
 i i  e _  d   Z e d j o e   n d S(   s<  oodoctest - a utility to run python code samples from an OOo document

By Olivier Grisel <olivier.grisel@ensta.org>

oodoctest extracts and run code samples found in an OpenOffice.org document to
test their validity using the standard python doctest machinery. The goal is
to make it easy to write runnable python documentation with OpenOffice.org.

This script can be both used a command line tool (oodoctest.py --help) or as the
provider of the OODocFileTest class to integration OO documents as part of
unittest suite.

OpenOffice.org is not required to run oodoctest.
s  
(C) Copyright 2005 Olivier Grisel <olivier.grisel@ensta.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as published
by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
s   0.2.0s)   Olivier Grisel <olivier.grisel@ensta.org>s   - use iterparse instead of parse
- add support for the OpenDocument format (00 2.0 and 1.1.5)
- find a way to plug it in OOo as a macro to inline test the document
N(   s   StringIO(   s   copy(   s   DocFileCases   DocTestParser(   s   ElementTreec         C   s   |  d j o
 d }  n d |  } d | } d |  } h  d | <d | <d | <d d	 | <d
 d | <d d | <d d | <d d | <d d <} | S(   s   Build an openoffice document namespace

    OpenOffice 1.1 and OpenDocument share a similar namespace structure. Only
    the base url changes.
    s   http://openoffice.org/2000s	   %s/offices   {%s}bodys   %s/textt   officet   bodyt   textt   ps   {%s}pt   ss   {%s}st   cs   {%s}ct   hs   {%s}ht   levels	   {%s}levelt   xlinks   http://www.w3.org/1999/xlinkN(   t   baset   Nonet	   oo_officet   oo_bodyt   oo_textt   ns(   R	   R   R   R   R   (    (    tN   /home/ogrisel/coding/oodoctest-download.gna.org/oodoctest.og.main/oodoctest.pyt   _build_namespaceB   s     



kt    c         C   s   | d j o t   } n |  i o | |  i 7} n |  i | d j o6 |  i | d  } | o | d t |  7} q n x, |  i	   D] } t | d | d | } q W|  i | d j o | d 7} n |  i d j	 o | |  i 7} n | S(	   s  Recursively extract text from an tree structured document

    >>> e = ElementTree.XML('''\
    ... <?xml version="1.0" encoding="UTF-8"?>
    ... <office:document-content
    ...   xmlns:office="%(office)s"
    ...   xmlns:text="%(text)s"
    ...   xmlns:xlink="%(xlink)s"
    ...   >\
    ... <office:body>\
    ... <text:p>This is a <text:a xlink:href="http://example.org/">\
    ... paragraph</text:a></text:p>\
    ... <text:p>3 spaces<text:s text:c="3"/>endSpaces</text:p>\
    ... </office:body>\
    ... </office:document-content>
    ... ''' % _build_namespace())
    >>> print _extract_text(e)
    This is a paragraph
    3 spaces   endSpaces
    <BLANKLINE>
    R   R   s    R   R   R   s   
N(   R   R
   R   t   elementR   t   tagt   gett   countert   intt   getchildrent   childt   _extract_textt   tail(   R   R   R   R   R   (    (    R   R   ^   s"     
 c         K   s  | d j o
 h  } n | o | o t d   n | o% t i |  } t i | |   }  n t	 i i
 |   } t i |   } | i d  }
 t i t |
  d d } t |  } t   } t } x< | p4 | i   \ } } | i | d j o
 t } q q Wg  } d }	 x | D] \ } } | d j o qn | i | d j o Pn | i | d j o% d	 |	 j o
 d }	 n | i#   q| i | d
 j o$ |	 t$ | d | 7}	 | i#   qqWd	 |	 j o& | i% | i' |	 | | |  d   n | S(   s\   extract doctests from an OO document at 'path'

    return a list of doctests instances
    s8   Package may only be specified for module-relative paths.s   content.xmlt   eventst   startt   endR   R   R   s   >>>R   R   i    N(   s   starts   end((   t   globsR
   t   packaget   module_relativet
   ValueErrort   doctestt   _normalize_modulet   _module_relative_patht   patht   ost   basenamet   namet   zipfilet   ZipFilet   oofilet   readt   contentt   ElementTreet	   iterparset   StringIOt
   iterparsert   itert   contextR   R   t   Falset   in_bodyt   nextt   _R   R   t   Truet   testst   collected_textt   eventt   clearR   t   appendt   parsert   get_doctest(   R%   R    R   R   R>   t   optionsR9   R;   R+   R:   R-   R   R1   R7   R5   R(   R   R3   (    (    R   t   extract_doctests   sN     
	  
&c      
   K   s4   t  |  d | d | d | d | \ } t | |  S(   s   Build a DocFileCase out of an OpenOffice document.

    Arguments are similar to those of the doctest.DocFileTest function
    R    R   R   R>   N(	   RA   R%   R    R   R   R>   t   testt   DocFileCaseR@   (   R%   R    R   R   R>   R@   RB   (    (    R   t   OODocFileTest   s     c          O   s   t  i   } | i d t  o  t i | i d   | d <n | i   } x) d D]! } | i |  o | | =qR qR Wx> |  D]6 } x- t | |  D] } | i t | |   q Wq~ W| S(   NR    R   R   R>   (   s   module_relatives   packages   globss   parser(   t   unittestt	   TestSuitet   suitet   kwR   R8   R"   R#   t   copyt   kw2t   keyt   has_keyt   pathsR%   RA   RB   t   addTestRC   (   RM   RH   RJ   RK   R%   RB   RG   (    (    R   t   OODocFileSuite   s        c          C   s  d k  l } |   } | i d  | i d d d d d d d	 d
 d d d d | i d d d	 d d d d d d d d g d d | i   \ } } t i	   } g  } | D] } | t i i | |  q ~ }  | p  d GH| i   t i d  n h  d t <}	 d } | i d j o | t i B} n g  } x! |  D] }
 | t |
 |	  7} q=Wt i d |  } x | D] } | i |  qsW| i d | i   d S(   s/   Run oodoctest.py as a command line utility
    (   s   OptionParsers$   %prog [options] oofile [oofile2 ...]s   -vs	   --verboset   actiont   store_constt   consti   t   destt	   verbosityt   helps)   show extracted doctests and their resultst   defaulti    s   -es
   --ellipsist   ellipsist   typet   choices    enaable ellipsis (default is on)t   choicest   ont   offs2   You should provide an OpenOffice file as argument:R    t   optionflagst   verboseN(!   t   optparset   OptionParserR>   t	   set_usaget
   add_optiont
   parse_argsR@   t   argsR&   t   getcwdt   cwdt   _[1]t   argR%   t   joinRM   t   print_usaget   syst   exitR4   RH   R]   RW   R"   t   ELLIPSISR9   RA   t   DocTestRunnert   runnerRB   t   runt	   summarizeRT   (   RM   R9   R>   Ro   Rd   Rf   Rg   Rh   RB   RH   R%   R]   R@   R`   (    (    R   t   main   s@     	

0
  t   __main__(   t   __doc__t   __copyright__t   __version__t
   __author__t   __TODO__R&   Rk   R)   t	   cStringIOR0   R"   RE   RI   RC   t   DocTestParsert   cElementTreeR.   t   ImportErrort   elementtreeR
   R   R   R8   RA   RD   RO   t   DocFileSuiteRr   t   __name__(   RE   R.   Rx   Rv   R   RC   R   R)   Rw   Rk   RI   RD   Ru   Rr   Rz   R0   RO   RA   R"   R&   (    (    R   t   ?   s8   					*E
		)
