ó
}c‹Ac           @   sg   d  Z  d d l Z d d l Z d d l Z d d l m Z m Z d d l m Z d d d „  ƒ  YZ	 d S(   sr    SimpleDataStore class
    If you want to implement new DataStore: implement class with interface equal of this. 
iÿÿÿÿN(   t   strftimet	   localtime(   t   Configt   SimpleDataStorec           B   sY   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 d „ Z
 d „  Z RS(
   s‘   SimpleDataStore: uses file system for data storage;
       'content_path' must be added to Config before, instanciating object of this class
    c         C   s   t  j d ƒ |  _ d S(   s   Initilize SimpleDataStore
           'content_path' must be added to Config before, instanciating object of this class
        t   content_pathN(   R   t   getR   (   t   self(    (    s!   lib/medus/data/SimpleDataStore.pyt   __init__   s    c         C   s"   t  j j t  j j |  j | ƒ ƒ S(   s    Return True, if (id) file exists(   t   ost   patht   existst   joinR   (   R   t   id(    (    s!   lib/medus/data/SimpleDataStore.pyR
      s    c         C   s\   t  j j |  j | ƒ } d } t  j j | ƒ rX t | d ƒ } | j ƒ  } | j ƒ  n  | S(   s   Return (id) file contentt    t   r(   R   R	   R   R   t   isfilet   opent   readt   close(   R   R   t	   file_patht   textt   infile(    (    s!   lib/medus/data/SimpleDataStore.pyR      s    c         C   s<   t  t j j |  j | ƒ d ƒ } | j | ƒ | j ƒ  d S(   s&   (id, content) write content to id:filet   wN(   R   R   R	   R   R   t   writeR   (   R   R   t   contentt   outfile(    (    s!   lib/medus/data/SimpleDataStore.pyR   #   s    !c         C   s#   t  j t  j j |  j | ƒ ƒ d S(   s!   Delete file(id) from data storageN(   R   t   removeR	   R   R   (   R   R   (    (    s!   lib/medus/data/SimpleDataStore.pyR   )   s    c         C   sG   g  t  j |  j ƒ D]0 } t  j j t  j j |  j | ƒ ƒ r | ^ q S(   s    Return list of wiki files(names)(   R   t   listdirR   R	   R   R   (   R   t   file(    (    s!   lib/medus/data/SimpleDataStore.pyt   listdata-   s    c         C   s  g  |  j  ƒ  D]0 } t j j t j j |  j | ƒ ƒ | f ^ q } | j ƒ  | j ƒ  | rŽ t | ƒ | k  r~ t | ƒ g p„ | g d } n t | ƒ } d } d } g  t	 d | ƒ D]K } | | d t
 | t | | d ƒ ƒ t
 | t | | d ƒ ƒ f ^ q¶ S(   sË   Return information about (recently modified files) changes in datastore
           (count=None) - number of files (if=None, then all files will be listed)
           -> [(filename, date, time)*]
        i    s   %A, %d %B %Ys   %H:%Mi   (   R   R   R	   t   getmtimeR   R   t   sortt   reverset   lent   rangeR    R   (   R   t   countR   t   itemst   datefmtt   timefmtt   i(    (    s!   lib/medus/data/SimpleDataStore.pyt
   getchanges2   s    @

1c         C   s„   t  j | t  j ƒ } g  } xN |  j ƒ  D]@ } t | j |  j | ƒ ƒ ƒ } | r( | j | | f ƒ q( q( W| j ƒ  | j	 ƒ  | S(   s<   Search data store. (str) -> [(filename, number of matches)*](
   t   ret   compilet
   IGNORECASER   R!   t   findallR   t   appendR   R    (   R   t   strt   str_ret   foundR   R#   (    (    s!   lib/medus/data/SimpleDataStore.pyt   searchG   s    

N(   t   __name__t
   __module__t   __doc__R   R
   R   R   R   R   t   NoneR(   R1   (    (    (    s!   lib/medus/data/SimpleDataStore.pyR      s   			
			(    (
   R4   t   os.pathR   t   timeR)   R    R   t	   medus.cfgR   R   (    (    (    s!   lib/medus/data/SimpleDataStore.pyt   <module>   s   $