ó
[³XMc           @   s1  d  Z  d d l m Z d d l Z d d l Z y d d l Z Wn e k
 r] d d l Z n Xd d l m	 Z	 m
 Z
 d d d „  ƒ  YZ d e f d „  ƒ  YZ d	 e
 f d
 „  ƒ  YZ d e
 f d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ e d „ Z d d d „  ƒ  YZ e d „ Z d S(   s)   Basic classes and interfaces for journal.iÿÿÿÿ(   t   nested_scopesN(   t
   implementst	   Interfacet   Journalc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s}   All commands to the system get routed through here.

    Subclasses should implement the actual snapshotting capability.
    c         C   s(   | |  _  | |  _ |  j  j ƒ  |  _ d  S(   N(   t   logt   journaledServicet   getCurrentIndext   latestIndex(   t   selfR   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   __init__   s    		c         C   s\   |  j  ƒ  } | |  j k  rX x: |  j j | d ƒ D] \ } } | j |  j | ƒ q2 Wn  d S(   s™   Run all commands from log that haven't been run yet.

        This method should be run on startup to ensure the snapshot
        is up-to-date.
        i   N(   t   getLastSnapshotR   R   t   getCommandsSincet   executeR   (   R   t   snapshotIndext   cmdtimet   command(    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   updateFromLog$   s    #c         C   s;   t  j  ƒ  } |  j j | | ƒ } | j |  j | | ƒ | S(   s   Log and execute a command.(   t   timeR   t
   logCommandt   addCallbackt   _reallyExecute(   R   R   t   runTimet   d(    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   executeCommand/   s    c         C   s"   | j  |  j | ƒ } | |  _ | S(   s-   Callback called when logging command is done.(   R   R   R   (   R   t   indexR   R   t   result(    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR   6   s    	c         C   s
   t  ‚ d S(   s0   Return command index of the last snapshot taken.N(   t   NotImplementedError(   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR
   <   s    c         O   s
   t  ‚ d S(   s­   Save journal to disk, returns Deferred of finish status.

        Subclasses may choose whatever signature is appropriate, or may
        not implement this at all.
        N(   R   (   R   t   argst   kwargs(    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   sync@   s    (	   t   __name__t
   __module__t   __doc__R	   R   R   R   R
   R   (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR      s   					t   MemoryJournalc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s6   Prevayler-like journal that dumps from memory to disk.c         C   s¦   | |  _  t j  j | ƒ r| y% t j t | d ƒ ƒ \ |  _ } Wn, t t t j	 f k
 rn d \ |  _ } n X| | ƒ n d |  _ | d  ƒ t j |  | | ƒ d  S(   Nt   rbi    (   i    N(   t   patht   ost   existst   picklet   loadt   opent   lastSynct   IOErrort   OSErrort   UnpicklingErrort   NoneR   R	   (   R   R   R   R#   t   loadedCallbackt   obj(    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR	   M   s    	%	
c         C   s   |  j  S(   N(   R)   (   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR
   Z   s    c         C   sH   t  |  j d ƒ } t j |  j | f | d ƒ | j ƒ  |  j |  _ d  S(   Nt   wbi   (   R(   R#   R&   t   dumpR   t   closeR)   (   R   R/   t   f(    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR   ]   s    
(   R   R   R    R	   R
   R   (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR!   J   s   		t   ICommandc           B   s   e  Z d  Z d „  Z RS(   s@   A serializable command which interacts with a journaled service.c         C   s   d S(   s"   Run the command and return result.N(    (   R   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR   h   s    (   R   R   R    R   (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR4   e   s   t   ICommandLogc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s   Interface for command log.c         C   s   d S(   s`   Add a command and its run time to the log.

        @return: Deferred of command index.
        N(    (   R   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR   o   s    c           C   s   d S(   s-   Return index of last command that was logged.N(    (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR   u   s    c         C   s   d S(   s…   Return commands who's index >= the given one.

        @return: list of (time, command) tuples, sorted with ascending times.
        N(    (   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR   x   s    (   R   R   R    R   R   R   (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR5   l   s   		t   LoadingServicec           B   s   e  Z d  Z d „  Z RS(   s7   Base class for journalled service used with Wrappables.c         C   s
   t  ‚ d S(   s'   Return object of specified type and id.N(   R   (   R   t   objTypet   objId(    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt
   loadObject‚   s    (   R   R   R    R9   (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR6      s   t	   Wrappablec           B   s   e  Z d  Z d Z d „  Z RS(   s0   Base class for objects used with LoadingService.c         C   s
   t  ‚ d S(   s5   Return uid for loading with LoadingService.loadObjectN(   R   (   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   getUidŒ   s    N(   R   R   R    R-   t
   objectTypeR;   (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR:   ‡   s   t   WrapperCommandc           B   s3   e  Z e e ƒ d i  d  „ Z d „  Z d „  Z RS(   c         C   sC   | |  _  | j ƒ  |  _ | j |  _ | |  _ | |  _ | |  _ d  S(   N(   R/   R;   R8   R<   R7   t
   methodNameR   R   (   R   R>   R/   R   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR	   •   s    			c         C   sR   t  |  d ƒ s* | j |  j |  j ƒ } n	 |  j } t | |  j ƒ |  j |  j Ž  S(   NR/   (	   t   hasattrR9   R7   R8   R/   t   getattrR>   R   R   (   R   t   svct   commandTimeR/   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR      s    	c         C   s   |  j  j ƒ  } | d =| S(   NR/   (   t   __dict__t   copy(   R   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   __getstate__¤   s    (    (   R   R   R   R4   R	   R   RE   (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR=   ‘   s   
	c            s   ‡ ‡  f d †  } | S(   sý  Wrap a method so it gets turned into command automatically.

    For use with Wrappables.

    Usage::

        | class Foo(Wrappable):
        |     objectType = "foo"
        |     def getUid(self):
        |         return self.id
        |     def _bar(self, x):
        |         return x + 1
        |
        |     bar = command('_bar')

    The resulting callable will have signature identical to wrapped
    function, except that it expects journal as first argument, and
    returns a Deferred.
    c            s   | j  ˆ ˆ  |  | | ƒ ƒ S(   N(   R   (   R/   t   journalR   R   (   R>   t   cmdClass(    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   wrapper¾   s    (    (   R>   RG   RH   (    (   RG   R>   sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR   ª   s    t   ServiceWrapperCommandc           B   s<   e  Z e e ƒ d i  d  „ Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  | |  _ | |  _ d  S(   N(   R>   R   R   (   R   R>   R   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR	   Ç   s    		c         C   s   t  | |  j ƒ |  j |  j Ž  S(   N(   R@   R>   R   R   (   R   RA   RB   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyR   Ì   s    c         C   s   d |  j  |  j |  j f S(   Ns#   <ServiceWrapperCommand: %s, %s, %s>(   R>   R   R   (   R   (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   __repr__Ï   s    c         C   s*   t  | d ƒ r" t |  j | j ƒ Sd Sd  S(   NRC   i    (   R?   t   cmpRC   (   R   t   other(    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   __cmp__Ò   s    (    (   R   R   R   R4   R	   R   RJ   RM   (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyRI   Ã   s
   
		c            s   ‡ ‡  f d †  } | S(   sÚ   Wrap methods into commands for a journalled service.

    The resulting callable will have signature identical to wrapped
    function, except that it expects journal as first argument, and
    returns a Deferred.
    c            s   | j  ˆ ˆ  | | ƒ ƒ S(   N(   R   (   R/   RF   R   R   (   R>   RG   (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyRH   à   s    (    (   R>   RG   RH   (    (   RG   R>   sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   serviceCommandÙ   s    (    (    (    (    (    (   R    t
   __future__R    R$   R   t   cPickleR&   t   ImportErrort   zope.interfaceR   R   R   R!   R4   R5   R6   R:   R=   R   RI   RN   (    (    (    sB   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/base.pyt   <module>	   s"   1
