mò
­fIc           @   s‚  d  Z  d k Z d k Z d k Z d k Z d k Z d k l Z l Z d k	 Z
 y d k TWn e j
 o d k Tn Xd e f d „  ƒ  YZ d e f d „  ƒ  YZ d f  d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ 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 f  d „  ƒ  YZ" d S(    s5   $Id: dbtables.py 36901 2004-08-08 00:54:21Z tim_one $N(   s   ListTypes
   StringType(   t   *t   TableDBErrorc           B   s   t  Z RS(   N(   t   __name__t
   __module__(    (    (    t+   /mit/python/lib/python2.4/bsddb/dbtables.pyR   $   s   t   TableAlreadyExistsc           B   s   t  Z RS(   N(   R   R   (    (    (    R   R   &   s   t   Condc           B   s   t  Z d  Z d „  Z RS(   s!   This condition matches everythingc         C   s   d S(   Ni   (    (   t   selft   s(    (    R   t   __call__,   s    (   R   R   t   __doc__R	   (    (    (    R   R   *   s    t	   ExactCondc           B   s    t  Z d  Z d „  Z d „  Z RS(   s)   Acts as an exact match condition functionc         C   s   | |  _  d  S(   N(   t
   strtomatchR   (   R   R   (    (    R   t   __init__1   s    c         C   s   | |  i j S(   N(   R   R   R   (   R   R   (    (    R   R	   3   s    (   R   R   R
   R   R	   (    (    (    R   R   /   s    	t
   PrefixCondc           B   s    t  Z d  Z d „  Z d „  Z RS(   s9   Acts as a condition function for matching a string prefixc         C   s   | |  _  d  S(   N(   t   prefixR   (   R   R   (    (    R   R   8   s    c         C   s   | t |  i ƒ  |  i j S(   N(   R   t   lenR   R   (   R   R   (    (    R   R	   :   s    (   R   R   R
   R   R	   (    (    (    R   R   6   s    	t   PostfixCondc           B   s    t  Z d  Z d „  Z d „  Z RS(   s:   Acts as a condition function for matching a string postfixc         C   s   | |  _  d  S(   N(   t   postfixR   (   R   R   (    (    R   R   ?   s    c         C   s   | t |  i ƒ |  i j S(   N(   R   R   R   R   (   R   R   (    (    R   R	   A   s    (   R   R   R
   R   R	   (    (    (    R   R   =   s    	t   LikeCondc           B   s&   t  Z d  Z e i d „ Z d „  Z RS(   sÇ   
    Acts as a function that will match using an SQL 'LIKE' style
    string.  Case insensitive and % signs are wild cards.
    This isn't perfect but it should work for the simple common cases.
    c         C   sf   d } x$ | D] } | i | d | ƒ } q W| i d d ƒ |  _ t i d |  i d | ƒ |  _ d  S(   Ns   .*+()[]?s   \t   %s   .*t   ^t   $(   t   chars_to_escapet   chart   likestrt   replaceR   t   ret   compilet   re_flags(   R   R   R   R   R   (    (    R   R   J   s     c         C   s   |  i i | ƒ S(   N(   R   R   t   matchR   (   R   R   (    (    R   R	   R   s    (   R   R   R
   R   t
   IGNORECASER   R	   (    (    (    R   R   D   s    t   __TABLE_NAMES__s   ._COLUMNS__c         C   s   |  t S(   N(   t   tablet   _columns(   R!   (    (    R   t   _columns_key[   s    s   ._DATA_.s	   ._ROWID_.i   c         C   s   |  t | t | S(   N(   R!   t   _datat   colt   rowid(   R!   R%   R&   (    (    R   t	   _data_keyf   s    c         C   s   |  t | t S(   N(   R!   R$   R%   (   R!   R%   (    (    R   t   _search_col_data_keyi   s    c         C   s   |  t S(   N(   R!   R$   (   R!   (    (    R   t   _search_all_data_keyl   s    c         C   s   |  t | t S(   N(   R!   t   _rowidR&   (   R!   R&   (    (    R   t
   _rowid_keyo   s    c         C   s   |  t S(   N(   R!   R*   (   R!   (    (    R   t   _search_rowid_keyr   s    c         C   sh   |  i t ƒ d j pB |  i t ƒ d j p, |  i t ƒ d j p |  i t ƒ d j o d Sn d Sd S(   s‚   Verify that the given string does not contain any
    metadata strings that might interfere with dbtables database operation.
    i    i   N(   R   t   findt   _table_names_keyR"   R$   R*   (   R   (    (    R   t   contains_metastringsu   s     Xt
   bsdTableDBc           B   sÈ   t  Z d d d d d d „ Z d „  Z d „  Z d d „ Z d „  Z d „  Z d	 „  Z d
 „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z h  h  d „ Z h  d „ Z h  d „ Z d „  Z d „  Z RS(   Ni    i€  c         C   s“  d |  _ t } | o | t O} n t t Bt	 Bt
 B| B}
 y | t O} Wn t j
 o n X| o |
 t B}
 n t ƒ  |  _ |  i i t ƒ |  i i | | |
 Bƒ | o | t O} n t |  i ƒ |  _ |  i i d ƒ |  i i t ƒ |  i i | t | | B| ƒ | |  _  |  i i! ƒ  }	 yC |  i i# t$ |	 ƒ p) |  i i% t$ t& i' g  d ƒ d |	 ƒn Wn |	 i( ƒ  ‚  n X|	 i) ƒ  h  |  _* d S(   sÇ   bsdTableDB.open(filename, dbhome, create=0, truncate=0, mode=0600)
        Open database name in the dbhome BerkeleyDB directory.
        Use keyword arguments when calling this constructor.
        i   t   txnN(+   t   NoneR   t   dbt	   DB_THREADt   myflagst   createt	   DB_CREATEt   DB_INIT_MPOOLt   DB_INIT_LOCKt   DB_INIT_LOGt   DB_INIT_TXNt   dbflagst   flagsforenvt   DB_AUTO_COMMITt   AttributeErrort   recovert
   DB_RECOVERt   DBEnvt   envt   set_lk_detectt   DB_LOCK_DEFAULTt   opent   dbhomet   truncatet   DB_TRUNCATEt   DBt   set_get_returns_nonet	   set_flagst   DB_DUPt   filenamet   DB_BTREEt   modet
   dbfilenamet	   txn_beginR1   t   has_keyR.   t   putt   picklet   dumpst   abortt   committ   _bsdTableDB__tablecolumns(   R   RN   RG   R6   RH   RP   R@   R<   R5   R1   R=   (    (    R   R   „   s>     		-

c         C   s   |  i ƒ  d  S(   N(   R   t   close(   R   (    (    R   t   __del__³   s    c         C   sX   |  i d  j	 o |  i i ƒ  d  |  _ n |  i d  j	 o |  i i ƒ  d  |  _ n d  S(   N(   R   R3   R2   RZ   RC   (   R   (    (    R   RZ   ¶   s    c         C   s.   y |  i i | ƒ Wn t j
 o n Xd  S(   N(   R   RC   t   txn_checkpointt   minst   DBIncompleteError(   R   R]   (    (    R   t
   checkpoint¾   s    c         C   s+   y |  i i ƒ  Wn t j
 o n Xd  S(   N(   R   R3   t   syncR^   (   R   (    (    R   R`   Ä   s    c         C   s›   d GH|  i i ƒ  } yc | i ƒ  \ } } xJ t h  | | <ƒ GH| i ƒ  } | o | \ } } q, | i	 ƒ  d Sq, WWn t
 j
 o | i	 ƒ  n Xd S(   s*   Print the database to stdout for debuggings5   ******** Printing raw database for debugging ********i   N(   R   R3   t   cursort   curt   firstt   keyt   datat   reprt   nextRZ   t   DBNotFoundError(   R   Rb   Rg   Rd   Re   (    (    R   t	   _db_printÊ   s       
c         C   so  d } y+t | ƒ o t d ƒ ‚ n x+ | D]# } t | ƒ o t d ƒ ‚ q- q- Wt | ƒ } |  i
 i | ƒ o t d ‚ n |  i i ƒ  } |  i
 i | t i | d ƒ d | ƒt i |  i
 i t d | d t ƒƒ } | i | ƒ |  i
 i t | ƒ |  i
 i t t i | d ƒ d | ƒ| i ƒ  d } Wn7 t j
 o+ } | o | i ƒ  n t | d ‚ n Xd S(   s’   CreateTable(table, columns) - Create a new table in the database
        raises TableDBError if it already exists or for other DB errors.
        s-   bad table name: contains reserved metastringss.   bad column name: contains reserved metastringss   table already existsi   R1   t   flagsN(   R2   R1   R/   R!   t
   ValueErrort   columnst   columnR#   t   columnlist_keyR   R3   RS   R   RC   RR   RT   RU   RV   t   loadst   getR.   t   DB_RMWt	   tablelistt   appendt   deleteRX   t   DBErrort   dberrorRW   R   (   R   R!   Rl   R1   Rv   Rm   Rn   Rr   (    (    R   t   CreateTableÜ   s4       %%

c         C   ss   t  | ƒ o t d ‚ n t | ƒ } |  i i | ƒ p g  Sn |  i i | ƒ } | o t
 i | ƒ Sn g  Sd S(   s\   Return a list of columns in the given table.
        [] if the table doesn't exist.
        s-   bad table name: contains reserved metastringsN(   R/   R!   Rk   R#   Rn   R   R3   RS   Rp   t   pickledcolumnlistRU   Ro   (   R   R!   Rn   Rx   (    (    R   t   ListTableColumns  s      c         C   s2   |  i i t ƒ } | o t i | ƒ Sn g  Sd S(   s)   Return a list of tables in this database.N(   R   R3   Rp   R.   t   pickledtablelistRU   Ro   (   R   Rz   (    (    R   t
   ListTables  s
     c   
      C   s|  y |  i | | ƒ Wnat j
 oUd } yt | ƒ } |  i	 i
 ƒ  } t i |  i i | d | d t ƒƒ } h  } x | D] } | | | <q} Wt i | ƒ }	 x/ | D]' } | i | ƒ p |	 i | ƒ q§ q§ W|	 | j o< |  i i | | ƒ |  i i | t i |	 d ƒ d | ƒn | i ƒ  d } |  i | ƒ Wqxt j
 o+ } | o | i ƒ  n t | d ‚ qxXn Xd S(   s  CreateOrExtendTable(table, columns)

        - Create a new table in the database.
        If a table of this name already exists, extend it to have any
        additional columns present in the given list as well as
        all of its current columns.
        R1   Rj   i   N(    R   Rw   R!   Rl   R   R2   R1   R#   Rn   RC   RR   RU   Ro   R3   Rp   Rq   t   oldcolumnlistt   oldcolumnhasht   ct   copyt   newcolumnlistRS   Rs   Rt   RT   RV   RX   t   _bsdTableDB__load_column_infoRu   Rv   RW   R   (
   R   R!   Rl   R1   Rn   R~   R}   R|   Rv   R€   (    (    R   t   CreateOrExtendTable  s>      '  
c         C   sw   y |  i i t | ƒ ƒ } Wn# t j
 o t d | f ‚ n X| p t d | f ‚ n t i	 | ƒ |  i
 | <d S(   s'   initialize the self.__tablecolumns dicts   unknown table: %rN(   R   R3   Rp   R#   R!   t   tcolpicklesRh   R   RU   Ro   RY   (   R   R!   Rƒ   (    (    R   t   __load_column_infoR  s     c         C   s³   d } x¦ | pž t i ƒ  } | i t t i ƒ  d ƒ ƒ | i t t i ƒ  d ƒ ƒ | i ƒ  } y, |  i
 i t | | ƒ d d | d t ƒWn t j
 o q	 Xd } q	 W| S(   s"   Create a new unique row identifieri    iÿÿÿR1   Rj   i   N(   t   uniquet   xdrlibt   Packert   pt   pack_intt   intt   randomt
   get_buffert   newidR   R3   RT   R+   R!   R2   R1   t   DB_NOOVERWRITEt   DBKeyExistError(   R   R!   R1   Rˆ   R   R…   (    (    R   t   __new_rowid]  s      c   	      C   sv  d } y|  i i t | ƒ ƒ p t d ‚ n |  i i | ƒ p |  i	 | ƒ n x? | i ƒ  D]1 } |  i | i | ƒ p t d | f ‚ q` q` W|  i i ƒ  } |  i | d | ƒ} x? | i ƒ  D]1 \ } } |  i i t | | | ƒ | d | ƒqÆ W| i ƒ  d } Wnc t j
 oW } t i ƒ  } | o' | i ƒ  |  i i t | | ƒ ƒ n t | d | d ‚ n Xd S(   sƒ   Insert(table, datadict) - Insert a new row into the table
        using the keys+values from rowdict as the column values.
        s   unknown tables   unknown column: %rR1   i   i   N(   R2   R1   R   R3   RS   R#   R!   R   RY   R   t   rowdictt   keysRm   t   countRC   RR   t   _bsdTableDB__new_rowidR&   t   itemst   dataitemRT   R'   RX   Ru   Rv   t   syst   exc_infot   infoRW   Rt   R+   (	   R   R!   R‘   R™   R1   R–   Rm   Rv   R&   (    (    R   t   Insertu  s0       )


c         C   su  yL|  i | g  | ƒ }
 | i ƒ  }	 x$|
 i ƒ  D]} d } yÚ xÓ |	 D]Ë } |  i i ƒ  } yD |  i i t | | | ƒ | ƒ } |  i i t | | | ƒ | ƒ Wn t j
 o d } n X| | | ƒ } | d j o) |  i i t | | | ƒ | d | ƒn | i ƒ  d } qG WWq1 t j
 o! } | o | i ƒ  n ‚  q1 Xq1 WWn" t j
 o } t | d ‚ n Xd S(   sâ  Modify(table, conditions) - Modify in rows matching 'conditions'
        using mapping functions in 'mappings'
        * conditions is a dictionary keyed on column names
        containing condition functions expecting the data string as an
        argument and returning a boolean.
        * mappings is a dictionary keyed on column names containint condition
        functions expecting the data string as an argument and returning the
        new string for that column.
        R1   i   N(   R   t   _bsdTableDB__SelectR!   t
   conditionst   matching_rowidst   mappingsR’   Rl   R&   R2   R1   Rm   RC   RR   R3   Rp   R'   R–   Rt   Rh   RT   RX   Ru   Rv   RW   R   (   R   R!   Rœ   Rž   R1   Rm   R–   Rv   R&   Rl   R   (    (    R   t   Modify  s<    	   	
c   	      C   s?  y|  i | g  | ƒ } |  i | } xí | i ƒ  D]ß } d } y£ |  i i ƒ  } xG | D]? } y# |  i i t | | | ƒ | ƒ WqW t j
 o qW XqW Wy  |  i i t | | ƒ | ƒ Wn t j
 o n X| i ƒ  d } Wq2 t j
 o! } | o | i ƒ  n ‚  q2 Xq2 WWn" t j
 o } t | d ‚ n Xd S(   s  Delete(table, conditions) - Delete items matching the given
        conditions from the table.
        * conditions is a dictionary keyed on column names
        containing condition functions expecting the data string as an
        argument and returning a boolean.
        i   N(   R   R›   R!   Rœ   R   RY   Rl   R’   R&   R2   R1   RC   RR   Rm   R3   Rt   R'   Rh   R+   RX   Ru   Rv   RW   R   (	   R   R!   Rœ   R1   R   Rm   Rv   R&   Rl   (    (    R   t   DeleteÍ  s8       	 

c         C   sŠ   y[ |  i i | ƒ p |  i | ƒ n | d j o |  i | } n |  i | | | ƒ } Wn" t
 j
 o } t | d ‚ n X| i ƒ  S(   sª  Select(table, conditions) - retrieve specific row data
        Returns a list of row column->value mapping dictionaries.
        * columns is a list of which column data to return.  If
          columns is None, all columns will be returned.
        * conditions is a dictionary keyed on column names
          containing callable conditions expecting the data string as an
          argument and returning a boolean.
        i   N(   R   RY   RS   R!   R   Rl   R2   R›   Rœ   R   Ru   Rv   R   t   values(   R   R!   Rl   Rœ   R   Rv   (    (    R   t   Selectõ  s     c         C   sö  |  i i | ƒ p |  i | ƒ n | d j o |  i | } n xC | | i	 ƒ  D]1 } |  i | i | ƒ p t d | f ‚ qS qS Wh  } h  } d „  }
 | i ƒ  } | i |
 ƒ |  i i ƒ  } d } x[| D]S\ } } | d } t | | ƒ } | | j o
 d } n d } yÞ | i | ƒ \ } }	 xÂ | t | ƒ  | j oª | t } | i | ƒ py | p | |	 ƒ o; | i | ƒ p h  | | <n | o |	 | | | <qÖqÚ| i | ƒ o | | =n | | | <n | i! ƒ  \ } }	 q/WWqÒ t" j
 o$ } | d t$ j o ‚  qÒ qÒ qÒ XqÒ W| i% ƒ  ~ t | ƒ d j o© x¦ | i ƒ  D]” \ } } x… | D]} } | i | ƒ o qin y& |  i i' t( | | | ƒ ƒ | | <Wqit" j
 o+ } | d t$ j o ‚  n d | | <qiXqiWqVWn | S(   s™  __Select() - Used to implement Select and Delete (above)
        Returns a dictionary keyed on rowids containing dicts
        holding the row data for columns listed in the columns param
        that match the given conditions.
        * conditions is a dictionary keyed on column names
        containing callable conditions expecting the data string as an
        argument and returning a boolean.
        s   unknown column: %rc         C   s  |  d } | d } t | ƒ t | ƒ j oŽ t | t ƒ o3 t | t ƒ o# t t | i	 ƒ t | i	 ƒ ƒ Sn t | t
 ƒ o3 t | t
 ƒ o# t t | i ƒ t | i ƒ ƒ Sn d Sn t | t ƒ o d Sn t | t ƒ o d Sn t | t ƒ o d Sn t | t ƒ o d Sn d S(   Ni   i    iÿÿÿÿ(   t   atuplet   at   btuplet   bt   typet
   isinstanceR   t   cmpR   R   R   R   R   (   R£   R¥   R¤   R¦   (    (    R   t   cmp_conditions$  s"    

 # #iÿÿÿÿi   i    N()   R   RY   RS   R!   R   Rl   R2   t   tablecolumnsRœ   R’   Rm   R“   R   R   t   rejected_rowidsRª   R•   t   conditionlistt   sortR3   Ra   Rb   t
   column_numt	   conditionR(   t	   searchkeyt   savethiscolumndatat	   set_rangeRd   Re   R   t   _rowid_str_lenR&   Rg   Ru   Rv   t   DB_NOTFOUNDRZ   t   rowdataRp   R'   (   R   R!   Rl   Rœ   R¶   R&   R²   R±   R   Re   Rª   R­   R¬   Rv   Rd   R°   Rb   Rm   R¯   (    (    R   t   __Select
  st      	 

 
  &c   	      C   s  d } yÓ|  i i ƒ  } |  i i t | ƒ | ƒ |  i i	 | ƒ } t | ƒ } x[ y | i | ƒ \ } } Wn t j
 o Pn X| t | ƒ  | j o Pn | i ƒ  qR Wt | ƒ } x[ y | i | ƒ \ } } Wn t j
 o Pn X| t | ƒ  | j o Pn | i ƒ  q¼ W| i ƒ  t i |  i i t d | d t ƒƒ } y | i | ƒ Wn t j
 o n X|  i i t | ƒ |  i i t t i | d ƒ d | ƒ| i ƒ  d } |  i i  | ƒ o |  i | =n Wn7 t! j
 o+ } | o | i# ƒ  n t$ | d ‚ n Xd S(   s(   Remove an entire table from the databasei   R1   Rj   N(%   R2   R1   R   RC   RR   R3   Rt   R#   R!   Ra   Rb   R)   t	   table_keyR³   Rd   Re   Rh   R   R,   RZ   RU   Ro   Rp   R.   Rq   Rr   t   removeRk   RT   RV   RX   RY   RS   Ru   Rv   RW   R   (	   R   R!   Rd   R1   Rb   R¸   Rr   Rv   Re   (    (    R   t   Drop{  sT         
'%
(   R   R   R   R[   RZ   R_   R`   Ri   Rw   Ry   R{   R‚   R   R”   Rš   RŸ   R    R¢   R›   Rº   (    (    (    R   R0   ƒ   s$   /					)			4			(0(	q(#   t   _cvsidR   R—   R   R†   R‹   t   typest   ListTypet
   StringTypet   cPickleRU   t	   bsddb3.dbt   ImportErrort   bsddb.dbt   StandardErrorR   R   R   R   R   R   R   R.   R"   R#   R$   R*   R´   R'   R(   R)   R+   R,   R/   R0   (   R+   R   R‹   R.   R)   R/   R¾   R"   R»   R*   R   R   R   R   R†   R—   R#   R'   R½   R´   R$   R   R   R0   R(   R   R,   R   RU   (    (    R   t   ?   s>   													