ó
[łXMc           @   sa  d  Z  d d l Z d d l Z y d d l Z Wn e k
 rM d d l Z n Xy d d l Z Wn e k
 r} d d l Z n Xd d l m	 Z	 m
 Z
 d d l m Z d d l m Z d d l m Z m Z d   Z d   Z d	 e f d
     YZ d d d     YZ e Z d e j f d     YZ d d  Z d d  Z d   Z d d d d d	 d g Z d S(   s`   
Save and load Small OBjects to and from files, using various formats.

Maintainer: Moshe Zadka
i˙˙˙˙N(   t   logt   runtime(   t   md5(   t   styles(   t
   implementst	   Interfacec         C   sg   d d l  m } t |  | j } | rA | d | j | 7} n  | j t |   j   d   j |  S(   Ni˙˙˙˙(   t   AESt    i   (   t   Crypto.CipherR   t   lent
   block_sizet   newR   t   digestt   encrypt(   t
   passphraset   datat   ciphert   leftover(    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt   _encrypt   s
    c         C   s6   d d l  m } | j t |   j   d   j |  S(   Ni˙˙˙˙(   R   i   (   R   R   R   R   R   t   decrypt(   R   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt   _decrypt%   s    t   IPersistablec           B   s)   e  Z d  Z d   Z d d d d  Z RS(   s9   An object which can be saved in several formats to a filec         C   s   d S(   sW   Set desired format.

        @type style: string (one of 'pickle' or 'source')
        N(    (   t   style(    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt   setStyle.   s    c         C   s   d S(   sx   Save object to file.

        @type tag: string
        @type filename: string
        @type passphrase: string
        N(    (   t   tagt   filenameR   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt   save4   s    N(   t   __name__t
   __module__t   __doc__R   t   NoneR   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyR   *   s   	t
   Persistentc           B   sW   e  Z e e  d  Z d   Z d   Z d   Z d   Z d   Z	 d d d d  Z RS(   t   picklec         C   s   | |  _  | |  _ d  S(   N(   t   originalt   name(   t   selfR!   R"   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt   __init__C   s    	c         C   s   | |  _  d S(   sW   Set desired format.

        @type style: string (one of 'pickle' or 'source')
        N(   R   (   R#   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyR   G   s    c         C   s~   | r | } | d } n[ | rN d |  j  | | f } d |  j  | | f } n& d |  j  | f } d |  j  | f } | | f S(   Ns   -2s
   %s-%s-2.%ss   %s-%s.%ss   %s-2.%ss   %s.%s(   R"   (   R#   R   t   extR   t	   finalname(    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt   _getFilenameN   s    c         C   st   t  | d  } | d  k r. | |  j |  n8 t j   } | |  j |  | j t | | j     | j   d  S(   Nt   wb(   t   openR   R!   t   StringIOt   writeR   t   getvaluet   close(   R#   R   R   t   dumpFunct   ft   s(    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt	   _saveTempZ   s    c         C   sA   |  j  d k r( d d l m } d } n d   } d } | | f S(   Nt   sourcei˙˙˙˙(   t   jellyToSourcet   tasc         S   s   t  j |  | d  d  S(   Ni   (   R    t   dump(   t   objt   file(    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyR.   i   s    t   tap(   R   t   twisted.persisted.aotR3   (   R#   R.   R%   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt	   _getStyled   s    		c         C   sĹ   |  j    \ } } | r% d | } n  |  j | | |  \ } } t j d |  j d | d  |  j | | |  t j d k r¤ t j	 j
 |  r¤ t j |  n  t j | |  t j d  d S(   sx   Save object to file.

        @type tag: string
        @type filename: string
        @type passphrase: string
        t   es   Saving s    application to s   ...t   win32s   Saved.N(   R:   R'   R    t   msgR"   R1   R   t   platformTypet   ost   patht   isfilet   removet   rename(   R#   R   R   R   R%   R.   R&   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyR   n   s     !N(   R   R   R   R   R   R$   R   R'   R1   R:   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyR   =   s   
				
	
t   _EverythingEphemeralc           B   s    e  Z d  Z d   Z d   Z RS(   i    c         C   s   | |  _  d S(   sS   
        @param mainMod: The '__main__' module that this class will proxy.
        N(   t   mainMod(   R#   RE   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyR$      s    c         C   sV   y t  |  j |  SWn; t k
 rQ |  j r3   qR t j d |  t j   Sn Xd  S(   Ns#   Warning!  Loading from __main__: %s(   t   getattrRE   t   AttributeErrort   initRunR    R=   R   t	   Ephemeral(   R#   t   key(    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt   __getattr__   s    	(   R   R   RH   R$   RK   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyRD      s   	c   	      C   s  d } | d k r% d d l  m } n t j d } } | re t j t | t |  d  j     } n t |  |  } t t	 j
 d  } | t	 j
 d <d | _ z | |  } Wd | j t	 j
 d <Xt j   d	 | _ t | d  } | d k	 r˙ | j |  n  | S(
   sŐ   Load an object from a file.

    Deserialize an object from a file. The file can be encrypted.

    @param filename: string
    @param style: string (one of 'pickle' or 'source')
    @param passphrase: string
    t   rR2   i˙˙˙˙(   t   unjellyFromSourcet   rbt   __main__i   Ni    (   R9   RM   R    t   loadR*   R   R)   t   readRD   t   syst   modulesRH   RE   R   t	   doUpgradeR   R   R   (	   R   R   R   t   modet   _loadt   fpt   eet   valuet   persistable(    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyRP      s(    		
	c         B   sw   | r d } n d } e  |  |  } i |  d 6} | r_ | j   } e | |  } | | | Un
 | | | U| | } | S(   s)  Load the value of a variable in a Python file.

    Run the contents of the file, after decrypting if C{passphrase} is
    given, in a namespace and return the result of the variable
    named C{variable}.

    @param filename: string
    @param variable: string
    @param passphrase: string
    RN   RL   t   __file__(   R)   RQ   R   (   R   t   variableR   RU   t   fileObjt   dR   RY   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt   loadValueFromFileź   s    	

c         C   sO   t  j j |   d } i d d 6d d 6d d 6d d 6d d 6d	 d
 6d	 d 6| S(   Ni   t   pythons   .tacs   .etacs   .pyR    s   .taps   .etapR2   s   .tass   .etas(   R?   R@   t   splitext(   R   R%   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt	   guessTypeÖ   s    R_   RP   t
   PersistantRb   (    (   R   R?   RR   t   cPickleR    t   ImportErrort	   cStringIOR*   t   twisted.pythonR    R   t   twisted.python.hashlibR   t   twisted.persistedR   t   zope.interfaceR   R   R   R   R   R   Rc   RI   RD   R   RP   R_   Rb   t   __all__(    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/persisted/sob.pyt   <module>
   s0   		D$	