ó
²Í[Mc           @   s³  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d Z i  a i  Z e e d ƒ Z i  Z d d l Z d „  Z d Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d e d d „ Z  d e d d „ Z! d e d d „ Z" d „  Z# d e e d d „ Z$ d d „ Z% d e d „ Z& d „  Z' d „  Z( d d „ Z) d „  Z* e ƒ  d „  Z+ e, d k r¯d d l- Z- y# e- j- e j. d d ƒ \ Z/ Z0 Wn" e- j1 k
 r$Z2 e2 GHe+ ƒ  n Xe3 e j. ƒ d k s@e0 rKe+ ƒ  GHn  d Z4 x[ e/ D]P \ Z5 Z6 e5 d k rze* ƒ  n  e5 d  k r“e) e4 ƒ n  e5 d! k rXd Z4 qXqXWn  d S("   sg  Manages the cache of generated Python code.

Description
  This file manages the cache of generated Python code.  When run from the 
  command line, it also provides a number of options for managing that cache.
  
Implementation
  Each typelib is generated into a filename of format "{guid}x{lcid}x{major}x{minor}.py"
  
  An external persistant dictionary maps from all known IIDs in all known type libraries
  to the type library itself.
  
  Thus, whenever Python code knows the IID of an object, it can find the IID, LCID and version of
  the type library which supports it.  Given this information, it can find the Python module
  with the support.
  
  If necessary, this support can be generated on the fly.
  
Hacks, to do, etc
  Currently just uses a pickled dictionary, but should used some sort of indexed file.
  Maybe an OLE2 compound file, or a bsddb file?
iÿÿÿÿNi    t
   __loader__c           C   s*   y t  ƒ  Wn t k
 r% t ƒ  n Xd  S(   N(   t
   _LoadDictst   IOErrort   Rebuild(    (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   __init__5   s    i   c          C   s|   t  r t d t j ƒ ‚ n  t t j j t ƒ  d ƒ d ƒ }  z- t	 j
 |  ƒ } | j t ƒ | j t ƒ Wd  |  j ƒ  Xd  S(   Ns.   Trying to write to a readonly gencache ('%s')!s	   dicts.datt   wb(   t   is_readonlyt   RuntimeErrort   win32comt   __gen_path__t   opent   ost   patht   joint   GetGeneratePatht   picklet   Picklert   dumpt   pickleVersiont   clsidToTypelibt   close(   t   ft   p(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt
   _SaveDicts=   s    !c          C   s"  t  t d ƒ rº d d  l }  t j } | j } t j j t j d ƒ } | j	 | ƒ rk | t
 | ƒ d } n d  Sy | j | ƒ } Wn# t k
 r– d  St k
 r§ d  SX|  j | ƒ } n! t t j j t j d ƒ d ƒ } z5 t j | ƒ } | j ƒ  } | j ƒ  a t j ƒ  Wd  | j ƒ  Xd  S(   NR    iÿÿÿÿs	   dicts.dati   t   rb(   t   hasattrR   t	   cStringIOR    t   archiveR   R   R   R	   t
   startswitht   lent   get_datat   AttributeErrorR   t   StringIOR
   R   t	   Unpicklert   loadR   t   versionRedirectMapt   clearR   (   t   iot   loadert   arc_patht
   dicts_patht   dataR   R   t   version(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyR   I   s,    		!c         C   s(   t  |  ƒ j ƒ  d d !d | | | f S(   sv   Given the clsid, lcid, major and  minor for a type lib, return
	the file name (no extension) providing this support.
	i   iÿÿÿÿs	   x%sx%sx%s(   t   strt   upper(   t   clsidt   lcidt   majort   minor(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   GetGeneratedFileNamet   s    c         C   s   t  |  j d d ƒ ƒ S(   s#   Reverse of GetGeneratedFileName()
	t   xi   (   t   tuplet   split(   t   fname(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   SplitGeneratedFileNamez   s    c          C   sÂ   t  s t d ‚ y t j t j ƒ Wn t j k
 r: n Xy) t j j t j d ƒ }  t j	 |  ƒ WnT t j k
 rº t
 |  d ƒ } | j d ƒ | j d ƒ | j d ƒ | j ƒ  n Xt j S(   sK   Returns the name of the path to generate to.
	Checks the directory is OK.
	s1   Why do you want the genpath for a readonly store?s   __init__.pyt   wsK   # Generated file - this directory may be deleted to reset the COM cache...
s   import win32com
sR   if __path__[:-1] != win32com.__gen_path__: __path__.append(win32com.__gen_path__)
(   R   t   AssertionErrorR   t   makedirsR   R	   t   errorR   R   t   statR
   t   writeR   (   R5   R   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyR      s    c         C   s   t  j |  ƒ } t | ƒ S(   sò   Get a Python class for a Program ID
	
	Given a Program ID, return a Python class which wraps the COM object
	
	Returns the Python class, or None if no module is available.
	
	Params
	progid -- A COM ProgramID or IID (eg, "Word.Application")
	(   t
   pywintypest   IIDt   GetClassForCLSID(   t   progidR-   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   GetClassForProgID˜   s    
c         C   sn   t  |  ƒ }  t j |  ƒ r( t j |  ƒ St |  ƒ } | d k rD d Sy t j |  ƒ SWn t k
 ri d SXd S(   sÛ   Get a Python class for a CLSID
	
	Given a CLSID, return a Python class which wraps the COM object
	
	Returns the Python class, or None if no module is available.
	
	Params
	clsid -- A COM CLSID (or string repr of one)
	N(   R+   t   CLSIDToClasst   HasClasst   GetClasst   GetModuleForCLSIDt   Nonet   KeyError(   R-   t   mod(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyR?   ¥   s    c         C   s5   y t  j |  ƒ } Wn t  j k
 r* d SXt | ƒ S(   s  Get a Python module for a Program ID
	
	Given a Program ID, return a Python module which contains the
	class which wraps the COM object.
	
	Returns the Python module, or None if no module is available.
	
	Params
	progid -- A COM ProgramID or IID (eg, "Word.Application")
	N(   R=   R>   t	   com_errorRF   RE   (   R@   t   iid(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   GetModuleForProgID»   s
    c         C   sJ  t  |  ƒ } y t | \ } } } } Wn t k
 r: d SXy t | | | | ƒ } Wn t k
 rm d } n X| d k	 rF| j j | ƒ } | d k r­ | j j | ƒ } n  | d k	 rF| j	 d | } y t
 | ƒ WnX t k
 r2| | | | f }	 |	 t k rt |	 }	 n  d d l }
 |
 j | |	 ƒ n Xt j | } qFn  | S(   sü   Get a Python module for a CLSID
	
	Given a CLSID, return a Python module which contains the
	class which wraps the COM object.
	
	Returns the Python module, or None if no module is available.
	
	Params
	progid -- A COM CLSID (ie, not the description)
	t   .iÿÿÿÿN(   R+   R   RG   RF   t   GetModuleForTypelibt   ImportErrort   CLSIDToPackageMapt   gett   VTablesToPackageMapt   __name__t
   __import__t   demandGeneratedTypeLibrariest   makepyt   GenerateChildFromTypeLibSpect   syst   modules(   R-   t	   clsid_strt   typelibCLSIDR.   R/   R0   RH   t   sub_modt   sub_mod_namet   infoRU   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyRE   Ì   s2    
c         C   s_   t  |  | | | ƒ } t | ƒ } d | j k r[ t |  | | | ƒ d | j k s[ t ‚ n  | S(   s  Get a Python module for a type library ID
	
	Given the CLSID of a typelibrary, return an imported Python module, 
	else None
	
	Params
	typelibCLSID -- IID of the type library.
	major -- Integer major version.
	minor -- Integer minor version
	lcid -- Integer LCID for the library.
	t   _in_gencache_(   R1   t
   _GetModulet   __dict__t   AddModuleToCacheR8   (   RZ   R.   R/   R0   t   modNameRH   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyRM   õ   s    c   	      C   sz   | d k	 r d GHn  d d l } y/ | j |  | | | f d | d | d | ƒWn t j k
 rf d SXt |  | | | ƒ S(   sæ  Generate support for a type library.
	
	Given the IID, LCID and version information for a type library, generate
	and import the necessary support files.
	
	Returns the Python module.  No exceptions are caught.

	Params
	typelibCLSID -- IID of the type library.
	major -- Integer major version.
	minor -- Integer minor version.
	lcid -- Integer LCID for the library.
	progressInstance -- Instance to use as progress indicator, or None to
	                    use the GUI progress bar.
	s?   The 'bGuiProgress' param to 'MakeModuleForTypelib' is obsolete.iÿÿÿÿNt   progressInstancet
   bForDemandt   bBuildHidden(   RF   RU   t   GenerateFromTypeLibSpecR=   RI   RM   (	   RZ   R.   R/   R0   Rc   t   bGUIProgressRd   Re   RU   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   MakeModuleForTypelib
  s    /c   
      C   sŽ   d d l  } y# | j |  d | d t d | ƒWn t j k
 rF d SX|  j ƒ  } | d } | d } | d } | d	 }	 t | | | |	 ƒ S(
   sò  Generate support for a type library.
	
	Given a PyITypeLib interface generate and import the necessary support files.  This is useful
	for getting makepy support for a typelibrary that is not registered - the caller can locate
	and load the type library itself, rather than relying on COM to find it.
	
	Returns the Python module.

	Params
	typelib_ob -- The type library itself
	progressInstance -- Instance to use as progress indicator, or None to
	                    use the GUI progress bar.
	iÿÿÿÿNRc   Rd   Re   i    i   i   i   (   RU   Rf   t   bForDemandDefaultR=   RI   RF   t
   GetLibAttrRM   (
   t
   typelib_obRc   Rd   Re   RU   t   tlat   guidR.   R/   R0   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   MakeModuleForTypelibInterface$  s    #



c   	      C   s—   |  j  ƒ  } | d } | d } | d } | d } | rY |  t t | ƒ | | | f <n  y t | | | | ƒ SWn t k
 rƒ n Xt |  | | | ƒ S(   s  Check we have support for a type library, generating if not.
	
	Given a PyITypeLib interface generate and import the necessary
	support files if necessary. This is useful for getting makepy support
	for a typelibrary that is not registered - the caller can locate and
	load the type library itself, rather than relying on COM to find it.
	
	Returns the Python module.

	Params
	typelib_ob -- The type library itself
	progressInstance -- Instance to use as progress indicator, or None to
	                    use the GUI progress bar.
	i    i   i   i   (   Rj   RT   R+   RM   RN   Rn   (	   Rk   Rc   Rd   Re   Rl   Rm   R.   R/   R0   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   EnsureModuleForTypelibInterface>  s    



c   	      C   s´   |  j  ƒ  } | d } | d } | d } | d } t | ƒ | | | f } y t | =Wn t k
 rv d | f GHn Xx6 t t j ƒ  ƒ D]" \ } } | | k rŠ t | =qŠ qŠ Wd S(   sd   Drop any references to a typelib previously added with EnsureModuleForTypelibInterface and forDemandi    i   i   i   sZ   ForgetAboutTypelibInterface:: Warning - type library with info %s is not being remembered!N(   Rj   R+   RT   RG   t   listR#   t   items(	   Rk   Rl   Rm   R.   R/   R0   R]   t   keyt   val(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   ForgetAboutTypelibInterface^  s    



c         C   sN  d } yØy t  |  | | | ƒ }	 Wn¶ t k
 rÚ d }	 yˆ t j |  | | | ƒ j ƒ  }
 |
 d | k sy |
 d | k r¿ y% t  |  |
 d |
 d |
 d ƒ }	 Wq¿ t k
 r» |
 d } q¿ Xn  WqÛ t j k
 rÖ qÛ Xn X|	 d k	 rË| rËt sý t d ‚ y® t j	 |  | | | ƒ } | d d k r5| d  } n  t
 t j d d ƒ } | sŒy | j t j ƒ  ƒ } WqŒt k
 rˆt | ƒ } qŒXn  t j |  | | | ƒ j ƒ  } WqËt j k
 rÇd } qËXn  |	 d k	 rÝ| rÝt sít d ‚ d	 t ƒ  t |  | | | ƒ f } | d
 } | d
 } | d } d d l } |	 j | d k s^| j |	 j k rõy t j | ƒ Wn t j k
 r…n Xy t j | ƒ Wn t j k
 r­n Xt j j | ƒ rÜd d l } | j | ƒ n  | d } d }	 d } qÝ|	 j } d	 t ƒ  t |  | | | ƒ f } | d
 } | d } d } y t j | ƒ d } d } WnL t j k
 r¥} y t j | ƒ d } d } Wq¦t j k
 r¡} q¦Xn Xt j | ƒ d } | rÝ| | k rÝd } d }	 qÝn  Wn  t t j f k
 r d }	 n X|	 d k rJt rût |  ƒ | | | f } y t | SWn t k
 rJn Xg  } x] t ƒ  D]R } | d | d k r[| d | d k r[| d | d k r[| j  | ƒ q[q[W| rç| j! ƒ  | d d } t  |  | | | ƒ } n d } | t | <| St" |  | | | | d | d | ƒ}	 | rJt# |	 ƒ }	 t$ |  | | | ƒ qJn  |	 S(   sê  Ensure Python support is loaded for a type library, generating if necessary.
	
	Given the IID, LCID and version information for a type library, check and if
	necessary (re)generate, then import the necessary support files. If we regenerate the file, there
	is no way to totally snuff out all instances of the old module in Python, and thus we will regenerate the file more than necessary,
	unless makepy/genpy is modified accordingly.
	
	
	Returns the Python module.  No exceptions are caught during the generate process.

	Params
	typelibCLSID -- IID of the type library.
	major -- Integer major version.
	minor -- Integer minor version
	lcid -- Integer LCID for the library.
	progressInstance -- Instance to use as progress indicator, or None to
	                    use the GUI progress bar.
	bValidateFile -- Whether or not to perform cache validation or not
	bForDemand -- Should a complete generation happen now, or on demand?
	bBuildHidden -- Should hidden members/attributes etc be generated?
	i    i   i   i   s&   Can't validate in a read-only gencacheiÿÿÿÿt    t   supports_unicode_filenamess   %s\%ss   .pyt   cNs   .pyci   i   Rd   Re   (%   RM   RN   RF   t	   pythoncomt   LoadRegTypeLibRj   RI   R   R8   t   QueryPathOfRegTypeLibt   getattrR   R   t   encodeRW   t   getfilesystemencodingR   R+   R   R1   t   genpyt   MinorVersiont   makepy_versiont   unlinkR:   t   isdirt   shutilt   rmtreeR;   R#   RG   t   GetGeneratedInfost   appendt   sortRh   t   reloadRa   (   RZ   R.   R/   R0   Rc   t   bValidateFileRd   Re   t   bReloadNeededt   modulet   tlbAttrt
   typLibPatht   suft   tlbAttributest   filePathPrefixt   filePatht   filePathPycR~   Rƒ   t   fModTimeSett	   pyModTimet   et   typLibModTimeRr   Rq   t   desct	   new_minort   ret(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   EnsureModulep  s¾     %""


%
		"




<

$c         C   sô   t  j j |  ƒ } | j j d ƒ sð y¦ | j j ƒ  } | j ƒ  d } | j ƒ  \ } } | j	 ƒ  } t
 | d | d | d | d d | ƒ} t | ƒ d d l }	 |	 j t | ƒ ƒ }
 |
 | j ƒ } Wqð t j k
 rì t d	 ƒ ‚ qð Xn  | S(
   sY   Given a COM prog_id, return an object that is using makepy support, building if necessaryt   CLSIDi    i   i   i   Rd   iÿÿÿÿNs`   This COM object can not automate the makepy process - please run makepy manually for this object(   R   t   clientt   DispatchR`   RP   t   _oleobj_t   GetTypeInfot   GetTypeAttrt   GetContainingTypeLibRj   Rš   RE   RB   RD   R+   Rx   RI   t	   TypeError(   t   prog_idRd   t   dispt   tit
   disp_clsidt   tlbt   indexRl   RH   RB   t
   disp_class(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   EnsureDispatch  s    +
c         C   s,  t  |  | | | ƒ } t | ƒ } d | _ | j } t |  ƒ | | | f }	 x$ | j ƒ  D] \ }
 } |	 t |
 <qX W| j } x$ | j ƒ  D] \ }
 } |	 t |
 <qˆ W| j } x$ | j ƒ  D] \ }
 } |	 t |
 <q¸ W| j	 } x$ | j ƒ  D] \ }
 } |	 t |
 <qè W|	 t
 k rt
 |	 =n  | r(t ƒ  n  d S(   s5   Add a newly generated file to the cache dictionary.
	i   N(   R1   R_   R^   t   CLSIDToClassMapR+   t	   iteritemsR   RO   t   VTablesToClassMapRQ   R#   R   (   t   typelibclsidR.   R/   R0   t   verboset	   bFlushNowR5   RH   t   dictR]   R-   t   clst   name(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyRa   "  s(    					
c          C   s¢  t  j j d ƒ }  |  d k rqd d  l } t  j |  d  } t  j |  d j d d ƒ } | j | ƒ } i  } xç | j ƒ  D]Ù } | j | ƒ s• qz n  | t | ƒ d j	 d ƒ d } yZ | j	 d	 ƒ \ } }	 }
 } t
 |	 ƒ }	 t
 |
 ƒ }
 t
 | ƒ } t j d
 | d ƒ } Wn* t k
 r&qz n t j k
 r<qz n Xd | | |	 |
 | f <qz W| j ƒ  t | j ƒ  ƒ St j t  j d ƒ } g  } x
| D]} t j j | ƒ rÐt j j | ƒ d d k rÐq”n  t j j t j j	 | ƒ d ƒ d } yZ | j	 d	 ƒ \ } }	 }
 } t j d
 | d ƒ } t
 |	 ƒ }	 t
 |
 ƒ }
 t
 | ƒ } Wn* t k
 rfq”n t j k
 r|q”n X| j | |	 |
 | f ƒ q”W| Sd  S(   Ns   .zip\i    iÿÿÿÿi   i   s   \t   /i   R2   t   {t   }s   \*s   .py(   R   R	   t   findt   zipfilet   replacet   ZipFilet   namelistR   R   R4   t   intR=   R>   t
   ValueErrorRI   R   Rp   t   keyst   globR   R   R‚   t   splitextR†   (   t   zip_posR¸   t   zip_filet   zip_patht   zft   infost   nt   baseRJ   R.   R/   R0   t   filesR™   t   fileR³   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyR…   A  sT    !
0&c         C   s!   d |  } t  | ƒ } t j | S(   sK   Given the name of a module in the gen_py directory, import and return it.
	s   win32com.gen_py.%s(   RS   RW   RX   (   R5   t   mod_nameRH   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyR_   q  s    
c      	   C   sÚ   t  j ƒ  t ƒ  } |  r- t | ƒ r- d GHn  x… | D]} } | \ } } } } |  rd d Gt | Œ  GHn  y t | | | | |  d ƒ Wq4 d | t j ƒ  d t j ƒ  d f GHq4 Xq4 W|  rÏ t | ƒ rÏ d GHn  t ƒ  d S(   s1   Rebuild the cache indexes from the file system.
	s6   Rebuilding cache of generated files for COM support...t   Checkingi    s    Could not add module %s - %s: %si   s   Done.N(	   R   R$   R…   R   R1   Ra   RW   t   exc_infoR   (   R¯   RÅ   R]   RJ   R.   R/   R0   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyR   x  s    
	.c          C   sŸ   d Gt  j GHi  }  x< t j ƒ  D]. \ } \ } } } } d  |  | | | | f <q WxG |  j ƒ  D]9 \ } } } } t | | | | ƒ } d | j | f GHq^ Wd  S(   Ns   Cache is in directorys   %s - %s(   R   R	   R   R¬   RF   t   iterkeysRM   t   __doc__(   t   dR-   RZ   R.   R/   R0   RH   (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   _Dump‹  s    %c          C   s   d }  |  GHt  j d ƒ d  S(   NsÐ   	  Usage: gencache [-q] [-d] [-r]
	  
			 -q         - Quiet
			 -d         - Dump the cache (typelibrary description and filename).
			 -r         - Rebuild the cache dictionary from the existing .py files
	i   (   RW   t   exit(   t   usageString(    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   usage˜  s    t   __main__t   qrds   -ds   -rs   -q(7   RÎ   R=   R   RW   Rx   R   t   win32com.clientR¿   t	   tracebackRB   t   operatorRi   R   R#   R   R   RT   t   cPickleR   R   R   R   R   R1   R6   R   RA   R?   RK   RE   RM   RF   Rh   Rn   Ro   Rt   Rš   Rª   Ra   R…   R_   R   RÐ   RÓ   RR   t   getoptt   argvt   optst   argsR:   t   messageR   R¯   t   optRs   (    (    (    s;   C:\RCS\Python\lib\site-packages\win32com\client\gencache.pyt   <module>   sn   $			+							)	 	Ÿ	0			#
