Ñò
™ï[Nc           @   s¹   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d	 d d
 „  ƒ  YZ	 e
 d j o d d k Z e i ƒ  n d S(   sÐ   
@modified: August 29 2011
@author: Dale Hamel
@contact: umhameld@cc.umanitoba.ca
The purpose of this class is to Provide support for the various actions that are repeated by several different python scripts
iÿÿÿÿNt   Birchmodc           B   sb   e  Z d  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 d
 „  Z RS(   sÛ   
	Purpose:
	This class provides common safety features to scripts,
	as well as making user interactions more intuitive.
	In particular, this class is intended to provide
	user-readable information when an error occurs
	c         C   s   | |  _  | |  _ d S(   sB   Initializes birchmod to contain the program usage and program nameN(   t   PROGRAMt   USAGE(   t   selft   progt   use(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   __init__   s    	c            s@   d | d | d GHd ‡  f d † } t i | | | ƒ } d S(   sQ   Downloads from url specified to the filename/path specified and displays progresss	   Fetching s    from s   

c            s‹   y t  |  | d | d ƒ } Wn d } n X|  d j oK d } ˆ  i d t | ƒ d d t | | ƒ d } t i i | ƒ n d  S(   Nid   i    i@B s	   Progress:t   %s    of s   MB(   t   minR   t   strt   syst   stdoutt   write(   t	   numblockst	   blocksizet   filesizet   urlt   percentt   MB_CONSTt   out_str(   R   (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt
   reporthook!   s    
1N(   t   Nonet   urllibt   urlretrieve(   R   R   t   nameR   t	   urlStream(    (   R   s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   wget   s    t   .c         C   s*   t  i | ƒ } | i | ƒ | i ƒ  d S(   sS   Extracts the tarfile given by file to current working directory by default, or pathN(   t   tarfilet   opent
   extractallt   close(   R   t   filet   patht   tarball(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   untar0   s    c         C   s(   t  i i | ƒ o t i | ƒ n d S(   s5   Mimics the behavior or unix rm -rf for the given pathN(   t   osR!   t   existst   shutilt   rmtree(   R   R!   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   rmrf=   s    c         C   s   |  i  |  i GHt ‚ d S(   ss   Called when a program performs an illegal operation. Causes program to print its appropriate usage, and exit nicelyN(   R   R   t
   SystemExit(   R   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt
   printusageC   s    c         C   sw   t  | ƒ } |  i d | d GHt i i | ƒ o |  i d | d GHn# |  i d | d t i ƒ  d GHt ‚ d S(   sK   Called when reading a file fails, used to provide more comprehensive outputs2   An error occurred trying to process file named : 's   's
   The file 's"   ' exists, but may not be accessed.s   ' does not exist in 'N(   R	   R   R$   R!   t   isfilet   getcwdR)   (   R   t	   file_name(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt
   file_errorJ   s    "c         C   s   |  i  d GHt ‚ d S(   sR   Used to indicate to the user that a script did execute, and completed successfullys    Successfully completed executionN(   R   R)   (   R   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   exit_successY   s    c         C   s=   t  | t ƒ p |  i d GHn | t i j o t St Sd S(   s?   Returns true if "argId" was a string passed on the command lines/   Argument to be check for not supplied as stringN(   t
   isinstanceR	   R   R
   t   argvt   Truet   False(   R   t   argId(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt	   arg_given_   s
    c         C   s?   |  i  d ƒ o' |  i d GHd d k } | i ƒ  t St Sd S(   st   called by using "pydoc {path to script} pydoc" * note must pass "pydoc" as an argument to toggle documentation mode*s   -pydocs   Generating documentation...iÿÿÿÿN(   R5   R   t   doctestt   testmodR2   R3   (   R   R6   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt
   documentorj   s    
(   t   __name__t
   __module__t   __doc__R   R   R#   R(   R*   R.   R/   R5   R8   (    (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyR       s   							t   Argumentc           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sÖ  
	The purpose of this class is to provide a simplified and common way
	for all scripts retrieve arguments from the command line parameters provided (sys.argv)
	This is done by declaring an Argument variable, and specifying any advanced attributes
	with the various augmentation methods provided.
	
	Examples:
	
	# optional arguments with parameters
	self.AInf = Argument("-inf", str, BM)
	self.AInf.set_optional()
	
        self.AOutf = Argument("-outf", str, BM)
	self.AOutf.set_optional()  	

        # optional argument with no parameters ie. switch
        self.AInvert = Argument("-inv", str, BM)
	self.AInvert.set_is_switch()
	self.AInvert.set_optional()	

        # required arguments at a specific position.
        Ainfile = Argument("", str, BM)
        Ainfile.set_position(-2)
		
        Aoutfile = Argument("", str, BM)	
        Aoutfile.set_position(-1)
		
		
        try:
	    if (BM.arg_given("-inf")):
	       self.InFormat = self.AInf.fetch()
	    if (BM.arg_given("-outf")):
	       self.OutFormat = self.AOutf.fetch()	       
	    self.Invert = BM.arg_given("-inv")	       
            self.Ifn = Ainfile.fetch()
            self.Ofn = Aoutfile.fetch()	    
        except ValueError:
            BM.printusage()
	
	
	c         C   s©   t  | ƒ |  _ t |  _ d |  _ d |  _ t |  _ d |  _	 t
 | t ƒ o | |  _ n* | d j o t |  _ d |  _ n t ‚ t
 | t ƒ o | |  _	 n t ‚ d S(   sš  
		Initializer:
		arg_id: The command line flag that specifies an argument parameter is to follow,
			   ex: "-o outfile"
		type: The type that the argument is. This must be a basic type, such as str, float, bool.
			  if you are unsure if something is a basic type, try running "type(yourtypename)" at interpretter
		BM: a pointer to the Birchmod module for the class using this argument (they are coupled)
		i    N(   R	   t   arg_idR2   t   is_requiredt   positionR   t   excludeR3   t   is_flagt   BMR0   t   typet   arg_typet
   ValueErrorR    (   R   R=   RD   t   BMOD(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyR   ¡   s    							c         C   s   t  | ƒ |  _ d S(   sZ  
		Specify the position on sys.argv where the argument is always found.
		Argument 0 is the name of the program, so argument 1 is the first argument.
		Note that arguments near the end can be specified by len(sys.argv)-X,
		where X is the index from the end. "-1" refers to the last argument, "-2"
		refers to the next to last argument, etc. 	
		N(   t   intR?   (   R   R?   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   set_positionÄ   s    c         C   sW   | |  i  j o |  i i d GHn |  i d j o t ƒ  |  _ n |  i i | ƒ d S(   s`   Add an argument (flag) to the list of mutually exclusive argument flags for this Argument's flags"   An argument may not exclude itselfN(   R=   RB   R   R@   R   t   listt   append(   R   R@   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   add_exclusiveÏ   s
    c         C   s   t  |  _ d S(   s/   Use this if the paramter passed is NOT requiredN(   R3   R>   (   R   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   set_optionalÚ   s    c         C   s   t  |  _ d |  _ d S(   sc   if this argument is just a switch (with no parameters), set this to true (ex ls -l, -l is a switch)N(   R2   RA   R   RD   (   R   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   set_is_switchß   s    	c         C   sò  t  |  i ƒ d j o7 |  i t t i ƒ j  o t i |  i } q‘t ‚ nE|  i oŽ |  i d j oh xe |  i D]V } | t i j o@ |  i	 t i j o- |  i
 i d | d |  i	 d GHt ƒ  qq qq Wn |  i	 t i j } n­ |  i o; |  i	 t i j o t i GHt ‚ q‘|  i |  i	 ƒ } nh |  i d j oE xB |  i D]3 } | t i j o |  i	 t i j o
 t ‚ qDqDWn |  i |  i	 ƒ } |  i d j o& |  i	 t i j o |  i | ƒ } n |  i t j o | d j o
 d } n | S(   s\   
		This method attempts to fetch the argument with the specified attributes from sys.argv
		i    s+   Cannot combine mutually exclusive options "s   " and "s   "t    N(   t   absR?   t   lenR
   R1   RE   RA   R@   R   R=   RB   R   t   exitR>   t   _Argument__get_argRD   R	   (   R   t	   to_returnt   each(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   fetchå   s8    


 #"


 ##
c         C   s€   t  | ƒ } yB t i i | ƒ d } | t t i ƒ j  o t i | St ‚ Wn+ t j
 o |  i i d | d GHd SXd S(   sœ   
		Used as a helper method. Retrieves a flags parameter, assuming it is one index to the right.
		ex "-o outfile", if argId= -o, this will return outfile
		i   s	   Argument s    not supplied at command line.N(	   R	   R
   R1   t   indexRP   RE   RB   R   R   (   R   R4   R?   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt	   __get_arg!  s    
(
   R9   R:   R;   R   RH   RK   RL   RM   RU   RR   (    (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyR<   v   s   )	#					<t
   HTMLWriterc           B   sh   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   s"   Methods for writing html to a filec         C   s   d |  _  d |  _ d |  _ d S(   sI   
	 	  Initializes arguments:
	 		indentwidth=3
	 		col=0
	 		lpad=""
		  i   i    RN   N(   t   indentwidtht   colt   lpad(   R   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyR   5  s    		c         C   s,   |  i  |  i |  _  d i |  i  ƒ |  _ d S(   sj   
		  **indent is not currently used by htmlwriter**
		  decrease indent using identwidth blank spaces
		  t    N(   RZ   RY   t   rjustR[   (   R   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   indentA  s    c         C   sR   |  i  |  i |  _  |  i  d j  o d |  _  d |  _ n d i |  i  ƒ |  _ d S(   sj   
		  **undent is not currently used by htmlwriter**
		  decrease indent using identwidth blank spaces
		  i    RN   R\   N(   RZ   RY   R[   R]   (   R   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   undentJ  s
    	c         C   s   | i  d | | d ƒ d S(   s  
		  Write begin tag, with attributes
		  @param htmlfile: The name of the html file to write the tag for
		  @type htmlfile: str
		  @param tagname: The name of the tag
		  @type tagname: str
		  @param attributes: The tag information itself
		  @type attributes: str
		  t   <s   >
N(   R   (   R   t   htmlfilet   tagnamet
   attributes(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   startW  s    c         C   s   | i  d | d ƒ d S(   s²   
		  Write end tag
		  @param htmlfile: The of the html file to write tag for
		  @type htmlfile: str
		  @param tagname: The name of the tag to write
		  @type tagname: str
		  s   </s   >
N(   R   (   R   Ra   Rb   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   endd  s    	c         C   s   | i  d | d ƒ d S(   sª   
		  Write title
		  @param htmlfile: The name of the html file to add the title to
		  @type htmlfile: str
		  @param title: The title to write
		  @type title: str
		  s   <title>s   birch - s	   </title>
Ns   <title>birch - (   R   (   R   Ra   t   title(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt
   page_titleo  s    	c         C   s)   | i  d | d | d | d ƒ d S(   s¥   
		  FIXME
		  @param htmlfile:
		  @type htmlfile:
		  @param url:
		  @type url:
		  @param attributes:
		  @type attributes:
		  @param text:
		  @type text:
		  s	   <a href="t   "t   >s   </a>N(   R   (   R   Ra   R   Rc   t   text(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   linkz  s    c         C   s°   | i  d ƒ |  i | d d ƒ |  i | | ƒ d } |  i | d | ƒ |  i | d d ƒ d } d } |  i | | d | ƒ | i  d	 | ƒ |  i | d ƒ | i  d
 ƒ d S(   s[   
		  FIXME
		  @param htmlfile:
		  @type htmlfile:
		  @param title:
		  @type title:
		  s9   <!-- this page generated automatically by htmldoc.py -->
t   htmlRN   s-    style ="background-color: rgb(255, 204, 0);"t   bodyt   h1s   ../../index.htmlsO   <img alt="birch - " src="../../images/birch_white.png" height="68" width="100">R\   s   <br>N(   R   Rd   Rg   Rk   Re   (   R   Ra   Rf   Rc   R   Rj   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt
   start_page‰  s    	c         C   s:   d } |  i  | d | ƒ | i | ƒ |  i | d ƒ d S(   sY   
		  FIXME
		  @param htmlfile:
		  @type htmlfile:
		  @param text:
		  @type text:
		  s    style="margin-left: 40px;"t   divN(   Rd   R   Re   (   R   Ra   Rj   Rc   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   indent_text£  s    	c         C   s$   |  i  | d ƒ |  i  | d ƒ d S(   s8   
		  FIXME
		  @param htmlfile:
		  @type htmlfile:
		  Rm   Rl   N(   Re   (   R   Ra   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   end_page±  s    (   R9   R:   R;   R   R^   R_   Rd   Re   Rg   Rk   Ro   Rq   Rr   (    (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyRX   2  s   										t	   Htmlutilsc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  | |  _ d S(   s_   
	 		FIXME
	 		@param CATDICT:
	 		@type CATDICT:
	 		@param PROGDICT:
	 		@type PROGDICT:
	 		N(   t   CATDICTt   PROGDICT(   R   Rt   Ru   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyR   ½  s    	c         C   sl   | i  ƒ  } d } xS | t | ƒ j  o? | | i d ƒ | | <| | i d d ƒ | | <| d } q W| S(   s0   
	 		FIXME
	 		@param line:
	 		@type line:
	 		i   Rh   s   \RN   (   t   splitRP   t   stript   replace(   R   t   linet   tokenst   i(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   tokenizeÉ  s    
 c            s    d t  f ‡  f d †  ƒ  Y} | S(   sç   
			this function simplifies the transition to python 3 by eleiminating the need for the "cmp" function
		this was a recommended workaround for using "cmp"'s in sorts (recommended by: http://wiki.python.org/moin/HowTo/Sorting/)
			t   Kc              sk   e  Z d  „  Z ‡  f d †  Z ‡  f d †  Z ‡  f d †  Z ‡  f d †  Z ‡  f d †  Z ‡  f d †  Z RS(   c         W   s   | |  _  d S(   sh   
			 			FIXME
			  			@param obj:
			  			@type obj:
			  			@param *args:
			  			@type *args:
			  			N(   t   obj(   R   R~   t   args(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyR   å  s    c            s   ˆ  |  i  | i  ƒ d j  S(   sB   
			  			FIXME
			  			@param other:
			  			@type other:
			  			i    (   R~   (   R   t   other(   t   mycmp(    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   __lt__î  s    c            s   ˆ  |  i  | i  ƒ d j S(   sB   
			  			FIXME
			  			@param other:
			  			@type other:
			  			i    (   R~   (   R   R€   (   R   (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   __gt__õ  s    c            s   ˆ  |  i  | i  ƒ d j S(   sB   
			  			FIXME
			  			@param other:
			  			@type other:
			  			i    (   R~   (   R   R€   (   R   (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   __hash__ü  s    c            s   ˆ  |  i  | i  ƒ d j S(   sB   
			  			FIXME
			  			@param other:
			  			@type other:
			  			i    (   R~   (   R   R€   (   R   (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   __le__  s    c            s   ˆ  |  i  | i  ƒ d j S(   sB   
			  			FIXME
			  			@param other:
			  			@type other:
			  			i    (   R~   (   R   R€   (   R   (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   __ge__
  s    c            s   ˆ  |  i  | i  ƒ d j S(   sB   
			  			FIXME
			  			@param other:
			  			@type other:
			  			i    (   R~   (   R   R€   (   R   (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   __ne__  s    (	   R9   R:   R   R‚   Rƒ   R„   R…   R†   R‡   (    (   R   (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyR}   ä  s   		(   t   object(   R   R   R}   (    (   R   s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt
   cmp_to_keyß  s    4c         C   s1   | d d j o | d | d } n | } | S(   s]   
		 	FIXME
		 	@param name:
		 	@type name:
		 	@param doc_prefix:
		 	@type doc_prefix:
		 	i    t   $t   /i   (    (   R   R   t
   doc_prefixR   (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   name_to_url  s    c         C   s£   t  | d ƒ } d } x; | D]3 } | d j o | i d ƒ | _ n | d } q W| i ƒ  | i i d ƒ d j p | i i d ƒ d j o
 t } n t } | S(   sG   
	 		FIXME
	 		@param fn:
	 		@type fn:
	 		@param p:
	 		@type p:
	 		t   ri   i   s   \ss   http://i    s   file:///(   R   Rw   t	   DOCPREFIXR   t   findR2   R3   (   R   t   fnt   pR    R{   Ry   t   okay(    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt
   get_prefix/  s    
 
2
(   R9   R:   R   R|   R‰   R   R”   (    (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyRs   »  s
   			;	t   __main__(    (    (    (    (   R;   R$   R
   R   R   R&   R    R<   RX   Rs   R9   R6   R7   (    (    (    s1   /home/psgendb/BIRCHDEV/script/mrtrans/birchlib.pyt   <module>   s   g¼‰“