ó
ÉÝ{Tc           @   sx  d  Z  d d l Z d d l Z d d l Tg  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 e d
 „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d d d d d „ Z d g  d d d „ Z d „  Z d „  Z d „  Z d  d! „ Z d d" „ Z d d# d$ „ Z! d d d% „ Z" d& „  Z# d# d' d( „ Z$ d) d* „ Z% d d d d+ „ Z& d d, „ Z' d- „  Z( d. „  Z) i  i  i  d/ „ Z* i  i  i  d0 „ Z+ d d d d1 „ Z, g  d d d2 „ Z- d d3 „ Z. g  d d d d4 „ Z/ d5 „  Z0 d6 „  Z1 d d d7 „ Z2 d8 „  Z3 d9 „  Z4 d: d; g  d< „ Z5 g  d= „ Z6 d> „  Z7 d S(?   s•  
     python/ui/utils.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2008 University of York, CCLRC
     Copyright (C) 2009-2010 University of York
     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ÿÿÿÿN(   t   *c         C   sX   d d  l  } t j j |  ƒ r* | j } n d } | j |  | j | j d  | j	 | d  ƒ S(   Niÿÿÿÿi    (
   t	   win32filet   ost   patht   isdirt   FILE_FLAG_BACKUP_SEMANTICSt
   CreateFilet   GENERIC_READt   FILE_SHARE_READt   Nonet   OPEN_EXISTING(   t   filenameR   t   dwFlagsAndAttributes(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   get_read_handle"   s    c      
   C   sF   d d  l  } | j |  ƒ \
 } } } } } } } }	 }
 } | |
 | f S(   Niÿÿÿÿ(   R   t   GetFileInformationByHandle(   t   hFileR   t
   attributest
   created_att   accessed_att
   written_att   volumet   file_hit   file_lot   n_linkst   index_hit   index_lo(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   get_unique_id2   s    -c         C   su   y t  |  ƒ } Wn t SXy t  | ƒ } Wn t SXy0 t | ƒ t | ƒ k } | j ƒ  | j ƒ  Wn t SX| S(   N(   R   t   TrueR   t   Close(   t	   filename1t	   filename2t   hFile1t   hFile2t	   are_equal(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   files_are_equal>   s    
c         C   s3   t  j d k r" t j j |  | ƒ St |  | ƒ Sd  S(   Nt   win32(   t   syst   platformR   R   t   samefileR"   (   t   f1t   f2(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyR&   O   s    c          C   sm   xf t  D]^ }  y& t j j |  ƒ r2 t j |  ƒ n  Wq t j ƒ  d  \ } } d G|  GH| G| GHq Xq Wd  S(   Ni   s   Failed to remove temporary file(   t   temporaryFilesR   R   t   isfilet   removeR$   t   exc_info(   t   ft   exc_typet	   exc_value(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   deleteTemporaryFilesU   s    	c   	   	   C   s^  t  |  ƒ } | j ƒ  } | j ƒ  i  } g  } d } xb | D]Z } | j d ƒ rˆ | | t | j ƒ  d ƒ <| j d | ƒ | d } q; | j | ƒ q; Wt  | d ƒ } x¥ | D]� } | j d ƒ r?t | d d !ƒ d k rí | j | ƒ qLt | d d !ƒ | k rL| j d	 | d  | t | d d !ƒ | d f ƒ qLq¯ | j | ƒ q¯ W| j ƒ  d
 S(   sT   
  This tries to see if numbering models as in the PDB
  spec. fixes load errors.
  i   s   MODEL s   MODEL     %d
t   wt   CISPEPi+   i.   i    s   %s%3d%sN(   t   opent	   readlinest   closet
   startswitht   intt   splitt   appendt   write(	   t   ifnamet   ofnameR-   t   linest   oldmodelt   lnewt   imodelt   lt   of(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   fixModelNumbers_   s*    
9c         C   s–  d d  l  } | j ƒ  j d ƒ s6 | j ƒ  j d ƒ rC |  j | ƒ S|  j | ƒ } | d k sj | d k rÔ d GHyR | j d d ƒ } | j } | j ƒ  t | | ƒ |  j | ƒ } t j	 | ƒ WqÔ d	 GHd SXn  | rÞ | Sy) | j d d ƒ } | j } | j ƒ  Wn d	 GHd SXd
 } | r^t
 |  d ƒ r^t |  j ƒ r^y |  j ƒ  } Wq^q^Xn  |  j | ƒ } | r|d GH| St j	 | ƒ |  j | ƒ S(   Niÿÿÿÿt   cifs   cif.gzi   i   s'   Dodgy model lines? Attempting renumber.t   suffixs   .pdbs5   Failed to create temporary file in utils.ReadCoorFilei    t   FixElementNamess7   Failed to write to temporary file in utils.ReadCoorFile(   t   tempfilet   lowert   endswitht   ReadCoorFilet   NamedTemporaryFilet   nameR5   RC   R)   R9   t   hasattrt   callableRF   t   WritePDBASCII(   t   molHndR   t   fixElementNamesRG   t   RCt   tfilet   tfnamet   nfixed(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyRJ   …   sJ    *	
	$c         C   s   |  t  d k r | GHn  d  S(   Nt   REPORT_LEVEL(   t   preferences(   t   levelt   message(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   reportError®   s    c         C   s‹   | j  |  ƒ r% d | j |  ƒ g g St |  ƒ } g  } d } x; | D]3 } | d } |  | d | !k rD | j | ƒ qD qD Wt | ƒ | g S(   Ni   iÿÿÿÿi    (   t   countt   indext   lenR9   (   t   inputt   keywordsRA   t   llt   it   kw(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   matchKeywordµ   s    
 c         C   s.   | j  |  ƒ r& | j |  ƒ } | | Sd Sd  S(   Nt    (   R[   R\   (   R^   R_   t   equivsRa   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   equivKeywordÃ   s    c         C   sf   d d  l  } t |  ƒ } | j | d ƒ } | d k r^ t | | d t | ƒ ƒ } | d | !S| Sd  S(   Niÿÿÿÿt   .i   i    (   t   stringt   strt   findt   minR]   (   R^   t   ndRh   t   sst   tt(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   decimalPlacesÌ   s    c         C   s©  d d  l  } d d  l } |  s1 t |  ƒ d k r5 g  Sg  } d } xa| r¤| j |  d ƒ } | d k  r | j t |  ƒ ƒ | S| d k r¨ | j t |  d | !ƒ ƒ n  | t |  ƒ d k rÇ d } qD | j |  | d d ƒ } | d k  rþ |  | d }  qD t | ƒ d k rL| d d d k rL| d |  | d | | d !| d <n  | j |  | d | | d !ƒ | | d t |  ƒ k  r›|  | | d }  qD d } qD W| S(   Niÿÿÿÿi    i   t   "t   =i   (   t   reRh   R]   Rj   t   extendt
   splitWordsR9   (   t   inp_strRr   Rh   t   wordst   continut   fqt   lq(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   splitWords0Ø   s2     		&( 
c         C   s�   d d  l  } d d  l } |  s1 t |  ƒ d k r5 g  Sg  } x? | j d |  ƒ D]+ } | j | ƒ } | rN | j | ƒ qN qN W| S(   Niÿÿÿÿi    s   \s(   Rr   Rh   R]   R8   t   stripR9   (   Ru   Rr   Rh   Rv   R1   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyRt   þ   s      c         C   s1   g  } x$ t  d | ƒ D] } | j |  ƒ q W| S(   Ni    (   t   rangeR9   (   t   valuet   lengtht   mmRa   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   makeList
  s    i    i   c         C   s‡   |  s
 |  Sd d  l  } d d  l } t |  ƒ }  | rk d } x. | d k rg | j d d |  ƒ \ }  } q= Wn  | j d d | j |  ƒ ƒ S(   Niÿÿÿÿi   i    s     t    t   _(   Rr   Rh   t   tclSafet   subnt   subR{   (   R^   t   tcl_safet   compressRr   Rh   t   n(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   oneWord  s     "c         C   s�   |  s
 |  St  |  ƒ }  d d  l } | j d d |  ƒ }  | j d d |  ƒ }  | j d d |  ƒ }  | j d d |  ƒ }  | j d d |  ƒ }  |  S(   Niÿÿÿÿs   \[s   \$s   "s   \"s   \{s   \}(   Ri   Rr   R…   (   R^   Rr   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyRƒ     s     c         C   sF   d d  l  } | j d |  ƒ s" |  Sd „  } d | j d | |  ƒ d S(   Niÿÿÿÿs   [ "]c         S   s9   |  j  d ƒ r$ |  j  d ƒ d d S|  j  d ƒ d Sd  S(   Ni   i   s   "(   t   group(   t   match(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   repl7  s    Rp   s	   (\*)("|$)(   Rr   t   searchR…   (   t   argRr   RŒ   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt
   msvcescape,  s
    	c          C   sÙ   d d l  }  t j j d ƒ } | s9 t j j d ƒ } n  | sÕ t j d k r� t j j d ƒ r{ t j j d ƒ r{ d } qÒ t j j d ƒ } qÕ t j j d ƒ r½ t j j d ƒ r½ d	 } qÕ t j j t	 ƒ  ƒ } n  | S(
   s+   
  Return a reasonable scratch directory
  iÿÿÿÿNt   SCRATCHt   CCP4_SCRR#   s   C:/Ccp4Tempt   USERPROFILEs   /tmps   /tmp/(
   Rr   R   t   environt   getR$   R%   R   t   existsR   t   get_HOME(   Rr   t   scratch(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   environScratch@  s    $	$	c         C   s    t  j j t  j j |  ƒ ƒ d S(   Ni    (   R   R   t   splitextt   basename(   t   file(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   fileRootZ  s    Rd   t   queryc   
      C   s¸  d d  l  } d d  l } | d k r8t j j |  ƒ r8t j j |  ƒ sm | ri t ƒ  j |  d | ƒ n  d  S| d k r8t ƒ  j d d d d |  g d	 d
 d d g g d d g d d d d d |  d g d d d g g g g g d d ƒ} | j	 d ƒ r| j
 | d ƒ } n  | d k r#d  S| d k r5q5q8n  d g j t j ƒ ru| j d | ƒ d k  ru| d } qun  y t |  | ƒ }	 Wn) | r¯t ƒ  j d |  | ƒ n  d SX|	 S(   NiÿÿÿÿR1   s    already exists. R�   t	   overwriteRd   t   TITLEs
   Overwrite t   RETURNs   utils.openFilet   RETURN_LISTR   t   CENTREt   SCREENt   DISPLAYt   CHOOSEOPTIONt   confirmt   TEXTs   File exits: s   \nDo you want to overwrite it?t   OPTIONSt   Quitt	   Overwritet   blocki   t   exitR#   t   bi    s   Error opening file (   Rh   Rr   R   R   R•   R*   t   MGGUIt   WarningMessaget   CreateGUIWindowt   has_keyRH   R[   R$   R%   R�   R3   (
   R   t   modet   warningRY   Rž   R¬   Rh   Rr   t   rvR-   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   openFile_  sB    			 	c         C   s¬   t  j j |  ƒ r d } n d } t |  | d | d | ƒ} | sF d Sy: | r_ | j | ƒ n | ru | j | ƒ n  | j ƒ  Wn% | r£ t ƒ  j d |  ƒ n  d SXd S(   Ns   w+R1   R³   Rž   i   s   Error writing file i    (	   R   R   R•   Rµ   t
   writelinesR:   R5   R®   R¯   (   R   t   textt	   text_listR³   Rž   R²   R-   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   saveFileˆ  s"    	 c         C   sX   d d  l  } t j j |  ƒ s" d St j |  ƒ } t | ƒ d k rG d S| j | d ƒ S(   NiÿÿÿÿRd   i   (   t   timeR   R   R•   t   statR]   t   ctime(   R   Rº   R»   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   fileDate¢  s      c          C   sn   y: t  j d k r' t j j d ƒ }  n t j j d ƒ }  Wn d }  n X|  rf t j j |  ƒ rf |  Sd Sd S(   s|  
  print "HOME", os.environ.get('HOME')
  print "HOMEDRIVE", os.environ.get('HOMEDRIVE')
  print "HOMEPATH",  os.environ.get('HOMEPATH')
  print "USERPROFILE", os.environ.get('USERPROFILE')

  # Expect HOME defined in LINUX and use it if exists in windows
  #homedir = os.environ.get('HOME')
  #if homedir and homedir != '' and os.path.exists(homedir):
  #  return homedir
  if sys.platform == 'win32':
    # Try using HOMEDRIVE & HOMEPATH
    if os.environ.get('HOMEDRIVE'):
      if  os.environ.get('HOMEPATH'):
        homedir = os.path.join(os.environ.get('HOMEDRIVE'), os.environ.get('HOMEPATH'))
      else:
        homedir = os.environ.get('HOMEDRIVE')
    else: 
      # Last ditch effort - use USERPROFILE
      homedir = os.environ.get('USERPROFILE')
    if homedir and os.path.exists(homedir):
      print "get_HOME",homedir
      return homedir

  # Arghh - no HOME
  return ""
  R#   R’   t   HOMERd   N(   R$   R%   R   R“   R”   R   R•   (   t   homedir(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyR–   ­  s    
c          C   se  t  j j t ƒ  d ƒ }  t  j j |  ƒ rU t j d k rU t  j j t ƒ  d ƒ }  n  t  j j |  ƒ s‹ y t  j |  ƒ Wq‹ d GHd SXn  xk d d d d	 d
 g D]T } t  j j |  | ƒ } t  j j | ƒ s¡ y t  j | ƒ Wqõ d G|  GHqõ Xq¡ q¡ Wxe d d g D]W } t  j j |  d | ƒ } t  j j | ƒ sy t  j | ƒ Wq]d G|  GHq]XqqW|  S(   Ns   .CCP4MG2R#   t   CCP4MG2s    ERROR creating .CCP4MG directoryRd   t   statust   logst   datat	   auto_savet   recovers!   ERROR creating subdirectories in t   monomer_libraryt   picture_wizard_files(   R   R   t   joinR–   R•   R$   R%   t   mkdir(   t   ccp4mgt   subdR   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt
   get_CCP4MGÚ  s0    "RÊ   c         C   s3  d d  l  } d d  l } t ƒ  } d Gt j j t j j | |  d ƒ ƒ GHd Gt j j t j j | |  d ƒ ƒ GHd G| j d | j ƒ  ƒ GHt	 j
 j ƒ  t j j | ƒ sÉ y t j | ƒ WqÉ qÉ Xn  t j j | ƒ rÞt t j j t j j | |  d ƒ ƒ d ƒ t	 _
 t t j j t j j | |  d ƒ ƒ d ƒ t	 _ t	 j d	 k r•| j t j j | |  d
 ƒ d ƒ | j t j j | |  d ƒ d ƒ qì| j t j j | |  d
 ƒ d ƒ | j t j j | |  d ƒ d ƒ nt j j d ƒ rèt j d } t t j j | |  d ƒ d ƒ t	 _
 t t j j | |  d ƒ d ƒ t	 _ t	 j d	 k rŸ| j t j j | |  d
 ƒ d ƒ | j t j j | |  d ƒ d ƒ qì| j t j j | |  d
 ƒ d ƒ | j t j j | |  d ƒ d ƒ n d  Sd Gt j j t j j | |  d ƒ ƒ GHd G| j d | j ƒ  ƒ GHd  S(   Niÿÿÿÿs   Redirecting output to s   _stdout.txts   Redirecting errors to s   _stderr.txts   Started at s   %cR1   R#   s   _c_stdout.txtt   wbs   _c_stderr.txtt   at	   HOMEDRIVEs   Redirected output to (   Rº   t   redirectRÌ   R   R   t   normpathRÈ   t   strftimet	   localtimeR$   t   stdoutt   flushR•   RÉ   R3   t   stderrR%   t   RedirectStdOutt   RedirectStdErrR“   R±   (   RL   Rº   RÐ   t	   directory(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   redirectOutputø  s@    	(( 11#&#&%%#&#&(c         C   s   |  GHt  j j ƒ  d  S(   N(   R$   RÔ   RÕ   (   R·   t   ifout(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   printOutput!  s    g        c         C   s0   |  d  k r( y t |  ƒ SWq, | SXn | Sd  S(   N(   R	   t   float(   t   inpt   default(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt	   safeFloat'  s    c         C   s0   |  d  k r( y t |  ƒ SWq, | SXn | Sd  S(   N(   R	   R7   (   RÞ   Rß   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   safeInt2  s    c         C   s�   y d d  l  } Wn d |  g SXd } d } xG |  D]? } y) | j j | ƒ r\ | | } n d } Wq4 d } q4 Xq4 W| | g S(   Niÿÿÿÿi    Rd   i   (   t   curses.asciit   asciit   isascii(   R·   t   cursest	   not_asciit
   ascii_textt   item(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt	   safeAscii=  s    
id   c         C   s   t  t |  | ƒ ƒ | S(   N(   RÝ   R7   (   RÞ   t   mult(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   roundP  s    g      ð?c         C   sá   d } |  t  d | ƒ k } | r. | d } n
 | d } x< |  t  d | ƒ k | k rv | ri | d } n
 | d } q; W| rŠ | d } n  |  d t  d | ƒ k r½ d t  d | d ƒ } n d t  d | d ƒ } | Sd „  } (   Ni   g      $@g      @g       @i   g      ð?c         S   s\   d d l  m } |  j ƒ  } | d ƒ } | j | ƒ } | d G| d G| d G| d GHd  S(   Niÿÿÿÿ(   t   doubleai   i    i   i   i   (   t
   pygl_coordRì   t   Getdvalt   frompointer(   t   qRì   t   p1t   a1(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt	   list_quatn  s
    (   t   pow(   R|   t   powert   gtt   resolRó   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   sliderResolutionU  s     
 c         C   s|  d d g j  |  ƒ r$ t ƒ  j }  n  |  d k rI t j j | ƒ rt | Sn+ |  d k rt t j j t j ƒ  | ƒ } | S|  rÕ t ƒ  j	 |  ƒ } | rÕ t j j | ƒ rÕ t j j | | ƒ } t j j | ƒ rÒ | SqÕ n  | rt j j | t j j
 | ƒ ƒ } t j j | ƒ r| Sn  d d  l } xT | j j D]F } | j |  k r.t j j | j ƒ r.t j j | j | ƒ } | Sq.W| S(   NRd   t   INLINEt   FULLPATHt	   dotCCP4MGiÿÿÿÿ(   R[   t   CCP4DATABASEt   mg_current_projectR   R   R•   RÈ   t   selfRÌ   t   get_project_dirRš   t   presentationt   instst
   identifiert
   master_dir(   t   DIR_filenameR   t   default_dirR   R   t   presobj(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   get_filenamex  s2     
! c         C   s“   |  d k r d |  |  g St  j j |  ƒ \ } } | d k sP t  j j | ƒ r] d |  |  g St ƒ  j | ƒ } | r‚ | | |  g Sd |  |  g Sd  S(   NRd   Rú   (   R   R   R8   R   Rü   t   aliasForDir(   R   t   dirt   filnt   alias(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   get_projectš  s    c          C   sA  d d  l  }  t j j t ƒ  d ƒ } |  j d d | ƒ } t j j | ƒ s€ y t j | ƒ d | d } WqŽ d | d SXn d	 | d } d d  l } d d  l	 } t j j t j
 d
 d d ƒ } | j t j j | d ƒ ƒ } xW| D]O} t j j | ƒ rOt j j | t j j | ƒ d ƒ }	 t j j |	 ƒ sO| j | |	 ƒ qOn  t j j | ƒ rê t j j | t j j | ƒ d ƒ }
 t j j |
 ƒ s¨t j |
 ƒ n  | j t j j | d ƒ ƒ } xp | D]e } t j j | ƒ rÍt j j |
 t j j | ƒ d ƒ }	 t j j |	 ƒ s2| j | |	 ƒ q2qÍqÍWqê qê W| S(   Niÿÿÿÿt   ccp4mg_tutorials   \\t   /s   Creating directory
sQ   
This directory will contain a selection of files to use for the CCP4mg tutorialss   Error creating directory
s   \nNo data copieds*   Updating data files in existing directory
t   CCP4MGt   tutorialRÃ   R    i   (   Rr   R   R   RÈ   R–   R…   R•   RÉ   t   globt   shutilR“   R*   R8   t   copyfileR   (   Rr   R  t   tut_textR·   R  R  t   sourcet   filesR›   t   dstt   dstdirt   files0t   file0(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   copyTutorialData«  s<    % % % !c         C   sh   |  j  ƒ  } | j  ƒ  } i  } xC | D]; } | j | ƒ sO |  | | | k r% | | | | <q% q% W| S(   N(   t   keysR[   (   t   d1t   d2t   k1t   k2t   diffRè   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   dict_diffs0Î  s    $c   	      C   sä   d } i  } i  } |  j  ƒ  } | j  ƒ  } x^ | D]V } | j | ƒ s[ |  | | | k r1 | | | | <t | | j | d ƒ ƒ } q1 q1 WxI | D]A } | j | ƒ s’ |  | | | <t | | j | d ƒ ƒ } q’ q’ W| | | f S(   Ni    i   (   R  R[   t   maxR”   (	   R  R  t   errcodesR´   R!  t
   minus_diffR  R   Rè   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt
   dict_diffsÛ  s    $""c         C   s�   |  j  ƒ  } | j  ƒ  } | j  ƒ  } xb | D]Z } | j | ƒ rw | j | ƒ rw |  | | | k r… | | |  | <q… q+ | | |  | <q+ W|  S(   N(   R  R[   (   R  R  t   minus_d2R  R   t   mk2Rè   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   dict_addð  s    c         C   sÍ   d d  l  } t j j |  ƒ r© t j j | t j j |  ƒ d ƒ } t j j | ƒ pX | r½ t |  | ƒ r½ y | j |  | ƒ Wq¦ d d |  d | g g  g SXq½ n d d |  g g  g Sd g  | g g S(   Niÿÿÿÿi   s   Error attempting to copy s    to s   File does not exists i    (   R  R   R   R•   RÈ   R8   R&   t   copy(   R   R	  Rž   R  t   newfile(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   copy_data_file  s    %)#c   	      C   s*  d d  l  } | sŸ yS | j | d ƒ } x: |  D]2 } t t d ƒ } | j | j ƒ  ƒ | j ƒ  q. WWn t ƒ  j d | ƒ d  SXy | j ƒ  Wq&q&Xn‡ y5 | j | ƒ } t t d ƒ } | j | j ƒ  ƒ Wn t ƒ  j d t ƒ d  SXy) | j ƒ  | j ƒ  t j	 t d ƒ Wn n Xd  S(   NiÿÿÿÿRÍ   t   rbs   Error saving data to file s   .gz(
   t   gzipR3   R
  R:   t   readR5   R®   R¯   R   R+   (	   t   filelistt   compress_filet
   uncompressR.  t   outR›   RÞ   R^   t   uncomp_output(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyR‡     s8    

c         C   sŸ   d d  l  } t j j |  ƒ } y9 | j |  d ƒ } | j ƒ  } | j | ƒ | j ƒ  Wn d d |  g SXt j j | t j j	 | d ƒ d ƒ } d | g S(   Niÿÿÿÿt   ri   s   Can not extract from zip file i    (
   t   zipfileR   R   t   dirnamet   ZipFilet   namelistt
   extractallR5   RÈ   R8   (   t   zip_fileR6  t   dir_patht   zR9  R   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   unzip5  s    )c         C   s„  d d  l  } | sè t j j | ƒ r4 t j | ƒ n  y | j | d ƒ } Wn d d | f SXx| |  D]t } t j j | ƒ } t j j | ƒ } t j j | ƒ r¾ t	 | | g d | g ƒ qf t j j
 | t	 | | g ƒ qf Wd d g S| st j j | ƒ st j j | ƒ r'd d | d g St j j } t j j }	 t j j }
 t j j } t j j | ƒ swd d	 | g Sy | j | ƒ } Wn d d | g SXxÊ | j ƒ  D]¼ } | j d
 ƒ s¬| | ƒ \ } } |
 |	 | | ƒ ƒ } | | ƒ sy t j | ƒ WqqXn  |	 | | ƒ } t j j | ƒ sht | d ƒ } | j | j | ƒ ƒ | j ƒ  qhq¬q¬W| j ƒ  d d g Sd  S(   NiÿÿÿÿR1   i   s   Error opening zip file: Rd   i    s   Output directory s    does not existss   Zip file does not exist: R  (   R6  R   R   R•   R+   R8  t   abspathR7  R*   t   AddToZipt   walkR   RÈ   RÑ   R8   R9  RI   t   makedirsR3   R:   R/  R5   (   R0  R;  R>  R   R6  R3  R›   t   top_pathR   RÈ   t   normR8   R^   t   eacht   rootRL   RÙ   R
  t   ff(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   ZipAFileH  sV      
-
c   	      C   s�   d d  l  } d d  l } |  \ } } xb | D]Z } t j j | | ƒ } t j j | ƒ d k r+ | j | | t | ƒ d | j ƒ q+ q+ Wd  S(   Niÿÿÿÿi   (	   R6  Rh   R   R   RÈ   R*   R:   R]   t   ZIP_DEFLATED(	   R3  R<  t   namesR6  Rh   t   outfileRC  RL   R
  (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyR@  ‚  s    'c   
      C   s  d d  l  } d d  l } y) | j |  ƒ } | j ƒ  } | j ƒ  Wn d SXt j j |  ƒ } t j j t j j	 |  ƒ ƒ d } y? t j j
 | | ƒ } t | d ƒ } | j | ƒ | j ƒ  WnN | j ƒ  }	 t j j
 |	 | ƒ } t | d ƒ } | j | ƒ | j ƒ  n Xd G| GH| S(   NiÿÿÿÿRd   i    s   w+bs   gunzipped input file to(   R.  RG   R3   R/  R5   R   R   R7  R™   Rš   RÈ   R:   t   mkdtemp(
   t   fnameR.  RG   R-   RÃ   R7  t   bnt   newFilet   tft   d(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   gunzipToNamedFile�  s.    "	c         C   sm   d d l  m } | j j ƒ  } y3 | rD | j | j d ƒ |  ƒ n  t j |  ƒ Wn d G|  G| GHn Xd  S(   Niÿÿÿÿ(   t   QtCoret   DeletingFiles   ERROR deleting(   t   PyQt4RS  t   QCoreApplicationt   instancet   emitt   SIGNALR   R+   (   R   t   err_messageRS  t   app(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   remove_file®  s    c         C   s   t  j j |  t d ƒ d  S(   NRd   (   R   R   RA  t   rem_dir(   R   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   remove_directoryº  s    c         C   sY   xR | D]J } t  j j | | ƒ } t  j j | ƒ rD t  j | ƒ q t  j | ƒ q Wd  S(   N(   R   R   RÈ   R*   R+   t   rmdir(   t   nowtt   topRJ  RL   R   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyR]  ¾  s
    i
   t   methodc      
   C   s1  d d  l  } d d l m } d } t | ƒ | j k rL t | j ƒ  ƒ } n  t | | j ƒ rJ| d k r† d | d | !|  d }	 n d | d | !d |  d	 }	 | s¸ | j	 ƒ  } n  x{| D]s}
 | j
 |
 ƒ r¿ t | |
 | j ƒ r$|	 d | d | d
 !d |
 d t | |
 ƒ d }	 q2t | |
 | j ƒ r²|	 d | d | d
 !d |
 d }	 x< | |
 D]0 } |	 d | d | d !| j | ƒ d }	 qjW|	 d d !d }	 q2t | |
 | j ƒ rõ|	 t |
 | |
 | d | d
 d d ƒ}	 q2|	 d | d | d
 !d |
 d | j | |
 ƒ d }	 q¿ q¿ W|	 d d !d }	 nã | d k r¿t | | j ƒ r’d | d | !|  d t | ƒ d }	 q-d | d | !|  d | j | ƒ d }	 nn t | | j ƒ rÿd | d | !d |  d t | ƒ d }	 n. d | d | !d |  d | j | ƒ d }	 |	 S(   Niÿÿÿÿ(   RS  s9                                                            Rb  s   
i    s    =  { t   's   ' :  { i   s   ' : t   ,s   ' : [ i   s    ],t   indentt   stylet   dicts     },s    = (   t   typesRU  RS  t   typet   QStringRi   t   toUtf8t
   isinstancet   DictTypeR  R±   t	   FloatTypet   ListTypet   pformatt   picture_defn_item(   t	   item_nameR}   t   pretty_printRe  Rf  t
   dict_orderRh  RS  t   spacest   streamt   keyRè   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyRq  È  s@     :%.-A*-..c         C   sõ  d d  l  } t |  | j ƒ rr t j j |  ƒ \ } } t ƒ  j | ƒ } | r` | | |  g }  qr d |  |  g }  n  t j j |  d ƒ rû t ƒ  j t j j |  d ƒ d ƒ } | rß | |  d <t j j |  d ƒ d |  d <n d |  d <|  d |  d <|  St ƒ  j	 |  d ƒ } | r�t j j
 | t j j |  d ƒ d ƒ } t j j | ƒ r�t j j |  d ƒ d |  d <| |  d <|  Sn  xm t j j
 t ƒ  |  d ƒ t j j
 t j ƒ  |  d ƒ g D]0 } t j j | ƒ r½d |  d <| |  d <|  Sq½W|  S(   NiÿÿÿÿRú   i   i    i   (   Rh  Rl  t
   StringTypeR   R   R8   Rü   R  R•   Rÿ   RÈ   R–   t   getcwd(   R   Rh  R   R
  R  t	   dir_aliasR<  t	   file_path(    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   check_filename  s<    &
!
)
&

c         C   sw   |  s
 g  Sy^ |  j  d ƒ } | j d d g ƒ g  } x+ | d d !D] } | j t | d ƒ ƒ qC W| SWn g  SXd  S(   NRg   t   0i    i   (   R8   Rs   R9   Rá   (   t   restore_from_versiont   vlt   ivlRè   (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   parseVersion6  s    (8   t   __doc__R$   R   t   global_definitionsR)   R   R   R"   R&   R0   RC   R   RJ   RZ   Rc   Rf   Ro   Rz   Rt   R€   R‰   Rƒ   R�   R˜   Rœ   Rµ   R¹   R½   R–   RÌ   RÚ   RÜ   R	   Rà   Rá   Ré   Rë   Rø   R  R  R  R"  R&  R)  R,  R‡   R>  RH  R@  RR  R\  R^  R]  Rq  R|  R�  (    (    (    s<   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/python/ui/utils.pyt   <module>   sj   
					
	&)						&						)		-	)	#"	#	":				
<2