³ò
œ›\Kc           @   sÑ  d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k l Z l Z d d k	 Z
 y d d k TWn e j
 o d d k Tn Xy e Wn) e j
 o d e f d „  ƒ  YZ n Xd e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d d# 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! d$ d" „  ƒ  YZ% d S(%   s=   $Id: dbtables.py 58760 2007-11-01 21:22:40Z gregory.p.smith $iÿÿÿÿN(   t   ListTypet
   StringType(   t   *t   DBIncompleteErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   '   s   t   TableDBErrorc           B   s   e  Z RS(    (   R   R   (    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   *   s   t   TableAlreadyExistsc           B   s   e  Z RS(    (   R   R   (    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   ,   s   t   Condc           B   s   e  Z d  Z d „  Z RS(   s!   This condition matches everythingc         C   s   d S(   Ni   (    (   t   selft   s(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   __call__2   s    (   R   R   t   __doc__R   (    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   0   s   t	   ExactCondc           B   s    e  Z d  Z d „  Z d „  Z RS(   s)   Acts as an exact match condition functionc         C   s   | |  _  d  S(   N(   t
   strtomatch(   R	   R   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   __init__7   s    c         C   s   | |  i  j S(   N(   R   (   R	   R
   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   9   s    (   R   R   R   R   R   (    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   5   s   	t
   PrefixCondc           B   s    e  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   prefix(   R	   R   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   >   s    c         C   s   | t  |  i ƒ  |  i j S(   N(   t   lenR   (   R	   R
   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   @   s    (   R   R   R   R   R   (    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   <   s   	t   PostfixCondc           B   s    e  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   postfix(   R	   R   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   E   s    c         C   s   | t  |  i ƒ |  i j S(   N(   R   R   (   R	   R
   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   G   s    (   R   R   R   R   R   (    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   C   s   	t   LikeCondc           B   s&   e  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   replacet   likestrt   ret   compile(   R	   R   t   re_flagst   chars_to_escapet   char(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   P   s     c         C   s   |  i  i | ƒ S(   N(   R   t   match(   R	   R
   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   X   s    (   R   R   R   R   t
   IGNORECASER   R   (    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   J   s   t   __TABLE_NAMES__s   ._COLUMNS__c         C   s   |  t  S(   N(   t   _columns(   t   table(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   _columns_keya   s    s   ._DATA_.s	   ._ROWID_.i   c         C   s   |  t  | t  | S(   N(   t   _data(   R$   t   colt   rowid(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt	   _data_keyl   s    c         C   s   |  t  | t  S(   N(   R&   (   R$   R'   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   _search_col_data_keyo   s    c         C   s   |  t  S(   N(   R&   (   R$   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   _search_all_data_keyr   s    c         C   s   |  t  | t  S(   N(   t   _rowid(   R$   R(   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt
   _rowid_keyu   s    c         C   s   |  t  S(   N(   R,   (   R$   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   _search_rowid_keyx   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(   t   findt   _table_names_keyR#   R&   R,   (   R
   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   contains_metastrings{   s    t
   bsdTableDBc           B   sÈ   e  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(   i    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(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   Nonet   dbt	   DB_THREADt	   DB_CREATEt   DB_INIT_MPOOLt   DB_INIT_LOCKt   DB_INIT_LOGt   DB_INIT_TXNt   DB_AUTO_COMMITt   AttributeErrort
   DB_RECOVERt   DBEnvt   envt   set_lk_detectt   DB_LOCK_DEFAULTt   opent   DB_TRUNCATEt   DBt   set_get_returns_nonet	   set_flagst   DB_DUPt   DB_BTREEt
   dbfilenamet	   txn_begint   has_keyR0   t   putt   picklet   dumpst   abortt   committ   _bsdTableDB__tablecolumns(   R	   t   filenamet   dbhomet   createt   truncatet   modet   recovert   dbflagst   myflagst   flagsforenvR3   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR   Š   s<    		-

c         C   s   |  i  ƒ  d  S(   N(   t   close(   R	   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   __del__º   s    c         C   sX   |  i  d  j	 o |  i  i ƒ  d  |  _  n |  i d  j	 o |  i i ƒ  d  |  _ n d  S(   N(   R5   R4   R\   R@   (   R	   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR\   ½   s    c         C   s.   y |  i  i | ƒ Wn t j
 o n Xd  S(   N(   R@   t   txn_checkpointR   (   R	   t   mins(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt
   checkpointÅ   s    c         C   s+   y |  i  i ƒ  Wn t j
 o n Xd  S(   N(   R5   t   syncR   (   R	   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyRa   Ë   s    c         C   s™   d GH|  i  i ƒ  } ya | i ƒ  \ } } xH t h  | | <ƒ GH| i ƒ  } | o | \ } } q, | i ƒ  d Sq, Wn t j
 o | i ƒ  n Xd S(   s*   Print the database to stdout for debuggings5   ******** Printing raw database for debugging ********N(   R5   t   cursort   firstt   reprt   nextR\   t   DBNotFoundError(   R	   t   curt   keyt   dataRe   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt	   _db_printÑ   s    
c         C   s†  t  | t ƒ p t ‚ d } y+t | ƒ o t d ƒ ‚ n x+ | D]# } t | ƒ o t d ƒ ‚ qD qD 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   R3   t   flagsN(   t
   isinstanceR    t   AssertionErrorR4   R1   t
   ValueErrorR%   R5   RL   R   R@   RK   RM   RN   RO   t   loadst   getR0   t   DB_RMWt   appendt   deleteRQ   t   DBErrorRP   R   (   R	   R$   t   columnsR3   t   columnt   columnlist_keyt	   tablelistt   dberror(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   CreateTableã   s6     %%

c         C   sŠ   t  | t ƒ p t ‚ 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(   Rl   R   Rm   R1   Rn   R%   R5   RL   Rp   RN   Ro   (   R	   R$   Rw   t   pickledcolumnlist(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   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(   R5   Rp   R0   RN   Ro   (   R	   t   pickledtablelist(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt
   ListTables  s    c   
      C   s“  t  | t ƒ p t ‚ 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 | ƒ Wqt j
 o+ }	 | o | i ƒ  n t |	 d ‚ q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.
        R3   Rk   i   N(   Rl   R    Rm   Rz   R   R4   R%   R@   RK   RN   Ro   R5   Rp   Rq   t   copyRL   Rr   Rs   RM   RO   RQ   t   _bsdTableDB__load_column_infoRt   RP   R   (
   R	   R$   Ru   R3   Rw   t   oldcolumnlistt   oldcolumnhasht   ct   newcolumnlistRy   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   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(   R5   Rp   R%   Rf   R   RN   Ro   RR   (   R	   R$   t   tcolpickles(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   __load_column_info[  s    c         C   s­   d } x  | p˜ g  } x- t  t ƒ D] } | i t i d d ƒ ƒ q# Wt i d t | Œ } 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ÿ   t   BR3   Rk   i   N(   t   xranget   _rowid_str_lenRr   t   randomt   randintt   structt   packR5   RM   R-   R4   t   DB_NOOVERWRITEt   DBKeyExistError(   R	   R$   R3   t   uniquet   blistt   xt   newid(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   __new_rowidf  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: %rR3   i   i   N(   R4   R5   RL   R%   R   RR   R€   t   keyst   countR@   RK   t   _bsdTableDB__new_rowidt   itemsRM   R)   RQ   Rt   t   syst   exc_infoRP   Rs   R-   (	   R	   R$   t   rowdictR3   Rv   R(   t   dataitemRy   t   info(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   Insert~  s.      )


c         C   sj  yA|  i  | g  | ƒ } | i ƒ  } x| i ƒ  D]} d } yÝ xÖ | D]Î } |  i i ƒ  } yG |  i i t | | | ƒ d | ƒ}	 |  i i t | | | ƒ | ƒ Wn t	 j
 o d }	 n X| | |	 ƒ }	 |	 d j o) |  i i
 t | | | ƒ |	 d | ƒn | i ƒ  d } qG WWq1 | o | i ƒ  n ‚  q1 Xq1 WWn" t j
 o }
 t |
 d ‚ n Xd S(   s  Modify(table, conditions={}, mappings={}) - Modify items in rows matching 'conditions' using mapping functions in 'mappings'

        * table - the table name
        * conditions - a dictionary keyed on column names containing
          a condition callable expecting the data string as an
          argument and returning a boolean.
        * mappings - a dictionary keyed on column names containing a
          condition callable expecting the data string as an argument and
          returning the new string for that column.
        R3   i   N(   t   _bsdTableDB__SelectR–   R4   R@   RK   R5   Rp   R)   Rs   Rf   RM   RQ   RP   Rt   R   (   R	   R$   t
   conditionst   mappingst   matching_rowidsRu   R(   R3   Rv   R   Ry   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   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 - a dictionary keyed on column names containing
          condition functions expecting the data string as an
          argument and returning a boolean.
        i   N(   R    RR   R–   R4   R@   RK   R5   Rs   R)   Rf   R-   RQ   Rt   RP   R   (	   R	   R$   R¡   R£   Ru   R(   R3   Rv   Ry   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   DeleteØ  s6      	 

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, columns, conditions) - retrieve specific row data
        Returns a list of row column->value mapping dictionaries.

        * columns - a list of which column data to return.  If
          columns is None, all columns will be returned.
        * conditions - a dictionary keyed on column names
          containing callable conditions expecting the data string as an
          argument and returning a boolean.
        i   N(   RR   RL   R€   R4   R    Rt   R   t   values(   R	   R$   Ru   R¡   R£   Ry   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   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         S   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   typeRl   R   t   cmpR   R   R   R   R   (   t   atuplet   btuplet   at   b(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   cmp_conditions1  s"    

 # #iÿÿÿÿi   i    N(   RR   RL   R€   R4   t   tablecolumnsR–   R—   R   R™   t   sortR5   Rb   R*   t	   set_rangeR   RŠ   Re   Rt   t   DB_NOTFOUNDR\   Rp   R)   (   R	   R$   Ru   R¡   Rv   R£   t   rejected_rowidsR®   t   conditionlistRg   t
   column_numt	   conditiont	   searchkeyt   savethiscolumndataRh   Ri   R(   Ry   t   rowdata(    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   __Select  st    
 	 

 
  	c   	      C   s  d } yÏ|  i i ƒ  } |  i i t | ƒ | ƒ |  i i | ƒ } t | ƒ } xY y | i | ƒ \ } } Wn t	 j
 o Pn X| t
 | ƒ  | j o Pn | i ƒ  qR t | ƒ } xY y | i | ƒ \ } } Wn t	 j
 o Pn X| t
 | ƒ  | j o Pn | i ƒ  qº | 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 databaseR3   Rk   i   N(   R4   R@   RK   R5   Rs   R%   Rb   R+   R±   Rf   R   R.   R\   RN   Ro   Rp   R0   Rq   t   removeRn   RM   RO   RQ   RR   RL   Rt   RP   R   (	   R	   R$   R3   Rg   t	   table_keyRh   Ri   Rx   Ry   (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyt   Dropˆ  sP    
!%
(   R   R   R   R]   R\   R`   Ra   Rj   Rz   R|   R~   R…   R€   R˜   RŸ   R¤   R¥   R§   R    R½   (    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pyR2   ‰   s&   	/					*			5			(2)	q(    (    (&   t   _cvsidR   Rš   R   R   R‹   t   typesR    R   t   cPickleRN   t	   bsddb3.dbt   ImportErrort   bsddb.dbR   t	   NameErrort	   Exceptiont   StandardErrorR   R   R   R   R   R   R   R0   R#   R%   R&   R,   RŠ   R)   R*   R+   R-   R.   R1   R2   (    (    (    s$   /usr/lib/python2.5/bsddb/dbtables.pys   <module>   sF   							