ó
a#nSc           @   s¶   d  d l  m Z d  d l Z d  d l Z e j Z e j Z e j Z e j Z d d d d d d „ Z	 d d d d d	 „ Z
 d
 „  Z d d d d d d „ Z d d d d d d „ Z d S(   iÿÿÿÿ(   t   RDConfigNt   sysdbat	   masterkeyt   .s   ::template1c   
      C   s   t  j rÑ | s? y t  j | |  | ƒ } Wq? d | GHg  SXn  | j ƒ  } | j t  j ƒ t j r” g  | j ƒ  D] } d t | d ƒ ^ qq } n- g  | j ƒ  D] } d t | d ƒ ^ q¡ } | j	 | ƒ nK t  j
 rd d l } d d l }	 |	 j | j j | t  j
 ƒ ƒ } n g  } | S(   sà    returns a list of databases that are available

    **Arguments**

      - user: the username for DB access

      - password: the password to be used for DB access

    **Returns**

      - a list of db names (strings)

  s   Problems opening database: %ss   ::i    iÿÿÿÿN(   t   DbModulet   getDbSqlt   connectt   cursort   executeR    t   usePgSQLt   fetchallt   strt   removet   fileWildcardt   os.patht   globt   patht   join(
   t   usert   passwordt   dirNamet   dBaset   cnt   ct   xt   namest   osR   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbInfo.pyt
   GetDbNames   s$    			0-	$i    c   	      C   sÂ   | s6 y t  j |  | | ƒ } Wq6 d |  GHg  SXn  | j ƒ  } | sT t  j } n	 t  j } | j | ƒ g  | j ƒ  D] } t | d ƒ j ƒ  ^ qw } t	 j
 r¾ d | k r¾ | j d ƒ n  | S(   sv   returns a list of tables available in a database

    **Arguments**

      - dBase: the name of the DB file to be used

      - user: the username for DB access

      - password: the password to be used for DB access

      - includeViews: if this is non-null, the views in the db will
        also be returned

    **Returns**

      - a list of table names (strings)

  s   Problems opening database: %si    t   PG_LOGDIR_LS(   R   R   R   t   getTablesSqlt   getTablesAndViewsSqlR   R
   R   t   upperR    R	   R   (	   R   R   R   t   includeViewsR   R   t   commR   R   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbInfo.pyt   GetTableNames8   s    		/c         C   sÍ  |  d  k s |  j d  k r g  Sg  } t j sà x˜|  j D]¡ } | d } | d } | t k rg d } n_ | t k r| d } nJ | t k r‘ d } n5 | t k r¦ d } n  t j	 j
 d | t | ƒ f ƒ | j | | f ƒ q8 Wné d d  l } |  j ƒ  } | s| SxÄ t | ƒ D]¶ \ } }	 |  j | d } t |	 ƒ }
 |
 | j k rPd } nb |
 | j k rhd } nJ |
 | j k r€d } n2 |
 | j k r˜d } n t j	 j
 d | |
 f ƒ | j | | f ƒ qW| S(	   Ni    i   t   stringt   integert   floatt   binarys   odd type in col %s: %s
iÿÿÿÿ(   t   Nonet   descriptionR    t
   useSqlLitet   sqlTextTypest   sqlIntTypest   sqlFloatTypest   sqlBinTypest   syst   stderrt   writeR   t   appendt   typest   fetchonet	   enumeratet   typet   StringTypest   IntTypet	   FloatTypet
   BufferType(   R   t   resultst   itemt   cNamet   cTypet   typeStrR2   t   rt   it   vt   typ(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbInfo.pyt   GetColumnInfoFromCursor_   sF     	

				  				t    t   *c   	      C   sh   | s t  j |  | | ƒ } n  | j ƒ  } d | | f } | rQ | d | 7} n  | j | ƒ t | ƒ S(   s	   gets a list of columns available in a DB table along with their types

    **Arguments**

      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - user: the username for DB access

      - password: the password to be used for DB access

      - join: an optional join clause (omit the verb 'join')

      - what: an optional clause indicating what to select

    **Returns**

      - a list of 2-tuples containing:

          1) column name

          2) column type

  s   select %s from %ss    join %s(   R   R   R   R   RC   (	   R   t   tableR   R   R   t   whatR   R   t   cmd(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbInfo.pyt   GetColumnNamesAndTypes…   s    c         C   s¯   | s t  j |  | | ƒ } n  | j ƒ  } d | | f } | ry | j ƒ  j d ƒ d k rh d | } n  | d | 7} n  | j | ƒ | j ƒ  | j }	 t d „  |	 ƒ }
 |
 S(   s¸   gets a list of columns available in a DB table

    **Arguments**

      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - user: the username for DB access

      - password: the password to be used for DB access

      - join: an optional join clause  (omit the verb 'join')

      - what: an optional clause indicating what to select

    **Returns**

      -  a list of column names

  s   select %s from %sR   i    s   join %st    c         S   s   t  |  d ƒ S(   Ni    (   R   (   R   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbInfo.pyt   <lambda>Ì   s    (	   R   R   R   t   stript   findR   R3   R(   t   map(   R   RF   R   R   R   RG   R   R   RH   t   desct   res(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbInfo.pyt   GetColumnNamesª   s    
	(   t   rdkitR    R   R.   R*   R+   R,   R-   R'   R   R"   RC   RI   RQ   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbInfo.pyt   <module>   s   				$&	'#