ó
ÉÝ{Tc           @   sI   d  Z  d d l Td d l m Z d d l Z d e j f d     YZ d S(   sn  
     python/ui/ccp4i_database.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2008 University of York, CCLRC
     Copyright (C) 2012 STFC

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Lesser General Public License for more details.
i˙˙˙˙(   t   *(   t   QtCoreNt   ccp4i_databasec           B   sŞ   e  Z d  Z d   Z d   Z d  d  d  d  Z d  d  Z d  d  Z d   Z d d d  Z	 d	   Z
 d
   Z d   Z d  d  d  Z d  d  Z d   Z d   Z RS(   t    c         C   sJ   t  j j |  t    |  t _ g  |  _ g  |  _ d |  _ |  j	   } d  S(   NR   (
   R   t   QObjectt   __init__t
   MAINWINDOWR   t   instst   directoriest   projectst   mg_current_projectt   load_ccp4i_directories(   t   selft   rv(    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyR   %   s    				c         K   s:  d d  l  } d d  l } d d  l } t j   } | j j |  sP d d | g S| j d | j  d k rz d } d } n d } d	 } | j j	 | | | d
  }	 | d k r| j j |	  rd d  l
 }
 d d  l } |	 d |
 j d  } | j |	 |  n  t j |	 | d d } | s-d d |	 g Sd | | g S(   Ni˙˙˙˙i   s$   ERROR No HOME/USERPROFILE directory t   win32i    t   CCP4t   windowss   .CCP4t   unixs   directories.deft   wt   .s   %y_%m_%d_%H-%Mt	   overwritet	   Overwrites   ERROR opening (   t   ost   stringt   syst   utilst   get_HOMEt   patht   existst   findt   platformt   joint   timet   shutilt   strftimet   copyfilet   openFile(   R   t   modet   kwR   R   R   t   ccp4_dirt   subdt   opsyst   directories_defR    R!   t   backupt   f(    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   open_directories_file/   s(     	 c   
      K   sH  | r t    j d  d S| d k rDd d l } | j |  j  } |  j   | rDt d  j d  sv | | k rz d Sd d l } t	 j
 | | j j d d   } t   j d	 | d
 d
 d t   j  t	 j
 | | j j d d   } | j j |  sd Sd d l }	 |	 j d d d d  t   j d	 | d d  qDn  d S(   s@   
    Interface to the projects and directories gui in ccp4i
    s   Directories editNt   1i˙˙˙˙t   save_session_optionst   save_projectst   CCP4_DATABASEs   mg_exit_status.pklt   filenameR   t	   save_listt   actiont   deletet   exitt   confirmt   restore_datai   (   t   MGGUIt   Sendt   copyt   deepcopyR
   R   t   PMt   getR   R   t   get_filenameR   R   t   HISTORYt   save_statust   save_to_projectR   t   dataobjt   Applyt   restore_status(
   R   t   change_projectst   updatet   current_projectt   keywordsR;   t   previous_projectR   R2   RC   (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   handle_projectsH   s.    
 		 c         C   sÎ  d d l  } d d l } |  j d d d } | d rD | d GH| S| d \ } } | j   } | j   | j | d  } i  }	 g  }
 g  } g  |  _ xl| D]d} t | j |   d k rÂ q | d d	 k rĺ |  j j	 |  q | j | d
 d  \ } } | j |  } y2 | d d k r@| j | d
 d  \ } } n  Wn n X| j |  } | rŮ| d d k r| d d k r| j | d  } n  d } xA d d d d d d g D] } | j
 |  r´d } q´q´Wn d } | rř|  j j	 |  q | |	 | <q W|	 j d  rÖxž t d t |	 d  d  D] } |	 j d t |   r3|	 j d t |   r3|	 d t |  } | d k rŤ| j d |  rŤ| d } n  | j	 |	 d t |  | g  q3q3Wn  |	 j d  rŚxž t d t |	 d  d  D] } |	 j d t |   r|	 j d t |   r|	 d t |  } | d k r{| j d |  r{| d } n  |
 j	 |	 d t |  | g  qqWn  |
 r¸|
 |  _ n  | rĘ| |  _ n  d S(   s?   
    Load the ccp4i directories.def file into a dictionary
    i˙˙˙˙Nt   rR    R   i    i   s   
t   #t    t   "t
   N_PROJECTSt   PROJECT_ALIASt   PROJECT_PATHt
   N_DEF_DIRSt   DEF_DIR_PATHt   DEF_DIR_ALIASs   PROJECT_ALIAS,s   PROJECT_PATH,R   s   /$t   /s   DEF_DIR_ALIAS,s   DEF_DIR_PATH,(   R   t   reR-   t   readt   closet   splitt   headert   lent   stript   appendt   countt   has_keyt   ranget   intt   strt   searchR   R	   (   R   R    R   RW   R   R,   R)   t   textt   linest   arrayR   R	   t   linet   varnamet   line0t   dtypet   linetmpt   othert   itemt   nR   (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyR   v   sv    
	
	"  $ +$ +c         C   s   x; |  j  D]0 } t | d  t |  k r
 t | d  Sq
 Wx; |  j D]0 } t | d  t |  k rH t | d  SqH Wd S(   Ni    i   R   (   R	   Rc   R   (   R   t   projectt   proj(    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   get_project_dirĆ   s    c         C   s   d S(   NR   (    (   R   (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   get_current_projectŇ   s    i   c         C   s   g  } g  } | r: x% |  j  D] } | j | d  q Wn  | rh x% |  j D] } | j | d  qJ Wn  | j   | j   | | g S(   Ni    (   R	   R^   R   t   sort(   R   t   aliasesR	   t	   proj_listt
   alias_listRq   (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   getProjectsListŘ   s    

c         C   sc   |  j  |  } | d k  r d S| t |  j  k  rC |  j | d S|  j | t |  j  d Sd  S(   Ni    R   (   t   projectDirIndexR\   R	   R   (   R   t   dirnt   ii(    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   aliasForDirč   s    c         C   sx   d d  l  } d } x_ |  j |  j g D]K } xB | D]: \ } } | d } y t j | |  ra | SWq2 q2 Xq2 Wq% Wd S(   Ni˙˙˙˙i   (   R   R	   R   R   t   samefile(   R   t   dirR   R{   t   listingRq   t   proj_dir(    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyRy   ô   s    
c         C   sf   d } xY |  j  |  j g D]E } x< | D]4 \ } } | d } y | | k rO | SWq& q& Xq& Wq Wd S(   Ni˙˙˙˙i   (   R	   R   (   R   t   aliasR{   R   Rq   R   (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   projectAliasIndex  s    
c         C   s¸   |  j    |  j |  } | d k rP | t |  j  k  rP | | g |  j | <nE | d k r | | g |  j | t |  j  <n |  j j | | g  |  j t j d  |  d d g S(   Ni    t   directoriesChangedR   (	   R   R   R\   R	   R   R^   t   emitR   t   SIGNAL(   R   R   R   R{   (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   edit_dir_alias  s    
!#c         C   sŤ   | d k r d | d g S|  j  |  } | d k  rC d | d g S| t |  j  k  rk |  j j |  n |  j j | t |  j   |  j t j d  |  d d g S(   Nt	   TEMPORARYi   s    alias can not be deletedi    s/    alias is not saved in the directories databaseR   R   (   R   R\   R	   t   popR   R   R   R   (   R   R   R{   (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   delete_alias'  s    c         C   s#   | j  d  r d | d S| Sd  S(   NRN   RO   (   R_   (   R   R   (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt
   add_quotes:  s    c   	      C   sŐ  d d  l  } d } x |  j D] } | | d } q W| d d t t |  j   f d } d } xq |  j D]f \ } } | d } | d d	 t |  |  j |  f d d
 d t |  |  j |  f d } qk W| d d t t |  j   f d } d } xq |  j D]f \ } } | d } | d d t |  |  j |  f d d
 d t |  |  j |  f d } qW|  j d d  } | d sË| d } y | j |  | j	   WqËd d g SXn  d d g S(   Ni˙˙˙˙R   s   
s   %-30s_positiveint1      %sRP   i    i   s   %-30s_text      %ss   PROJECT_ALIAS,s   %-30s_dir      %ss   PROJECT_PATH,RS   s   DEF_DIR_ALIAS,s   DEF_DIR_PATH,R%   R   s$   Error saving directories information(
   R   R[   Rc   R\   R	   R   R   R-   t   writeRY   (	   R   R   Re   Rh   Ro   R   R   R   R,   (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   write_ccp4i_directoriesA  s.    '
T'
T

(   t   __name__t
   __module__R   R   R-   RK   R   Rr   Rs   Rx   R|   Ry   R   R   R   R   R   (    (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyR      s   	
	.P					(   t   __doc__t   global_definitionst   PyQt4R   R   R   R   (    (    (    sE   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/ccp4i_database.pyt   <module>   s   
