ó
ã™xTc           @   sV  d  d l  m Z d  d l m Z m Z d  d l m Z y d  d l Z Wn e k
 r_ d Z n Xy d  d l
 Z
 Wn e k
 r‰ d Z
 n Xy d  d l Z e j Z Wn& e k
 rË d  d l Z e j Z n Xd  d l m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z e j Z d j ƒ  Z d d d d „ Z d d d „ Z d d d	 d
 „ Z  d d d	 d „ Z! d „  Z" d „  Z# d „  Z$ e% d „ Z& d „  Z' d „  Z( d „  Z) g  d d „ Z* d „  Z+ e, d „ Z- d „  Z. d d „ Z/ d d e% d „ Z0 d e% d „ Z1 d d e, d „ Z2 d „  Z3 d d „ Z4 d „  Z5 d  „  Z6 d d! „ Z7 d" e8 f d# „  ƒ  YZ9 e9 ƒ  Z: d$ e; f d% „  ƒ  YZ< d& e j= k Z> e j? Z@ d' „  ZA eA e _? d( e< f d) „  ƒ  YZB d* e< f d+ „  ƒ  YZC d, e< f d- „  ƒ  YZD d. „  ZE d/ „  ZF d0 „  ZG d1 d2 „ ZH d d3 „ ZI d d4 d5 d6 „ ZJ d7 „  ZK d8 e8 f d9 „  ƒ  YZL d: eL f d; „  ƒ  YZM d< eL f d= „  ƒ  YZN d> e8 f d? „  ƒ  YZO d@ „  ZP dA „  ZQ e, dB dC „ ZR e, dB dD „ ZS eR ZT e% dE „ ZU d e% dF „ ZV d dG „ ZW dH d| dI „  ƒ  YZX d d dJ „ ZY dK d} dL „  ƒ  YZZ dM „  Z[ dN „  Z\ dO e8 f dP „  ƒ  YZ] dQ e] f dR „  ƒ  YZ^ dS e8 f dT „  ƒ  YZ_ dU e8 f dV „  ƒ  YZ` dW e8 f dX „  ƒ  YZa dY „  Zb dZ d~ d[ „  ƒ  YZc d\ e8 f d] „  ƒ  YZd d^ „  Ze d_ e8 f d` „  ƒ  YZf da d d db „ Zg dc e8 f dd „  ƒ  YZh de eh f df „  ƒ  YZi dg „  Zj dh „  Zk d di „ Zl d dj „ Zm dk „  Zn d dl „ Zo d dm „ Zp dn e8 f do „  ƒ  YZq e jr dp „ Zs d d dq „ Zt dr „  Zu ds „  Zv d aw dt d d du „ Zx dv „  Zy dw e8 f dx „  ƒ  YZz dy e8 f dz „  ƒ  YZ{ d{ „  Z| d S(   iÿÿÿÿ(   t   division(   t	   sge_utilst	   pbs_utils(   t   show_stringN(   t   mathsi   CON PRN AUX NUL COM1 COM2 COM3 COM4 COM5 COM6 COM7 COM8 COM9
LPT1 LPT2 LPT3 LPT4 LPT5 LPT6 LPT7 LPT8 LPT9c         c   sÖ   | d k r |  d } d }  n
 |  d 7}  | d k r> d } n | d k sP t ‚ t t j | |  | ƒ ƒ } | d k	 r® t |  | | | ƒ t | | ƒ k  r® | d 7} n  x! t | ƒ D] } |  | | Vq» Wd S(   sÿ   
  A float range generator.

  Parameters
  ----------
  start : float
  stop : float, optional
      If empty, start at 0 and stop at the start parameter.
  step : float, optional
  tolerance : float, optional

  Returns
  -------
  generator of float
  g        g      ð?i   N(   t   Nonet   AssertionErrort   intR   t   ceilt   abst   xrange(   t   startt   stopt   stept	   tolerancet   countt   i(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   xfrange   s    
	
	(c         C   s   t  t |  d | d | ƒƒ S(   s  
  Non-generator version of xfrange.

  Parameters
  ----------
  start : float
  stop : float, optional
      If empty, start at 0 and stop at the start parameter.
  step : float, optional

  Returns
  -------
  list of float

  See Also
  --------
  libtbx.utils.xfrange
  R   R   (   t   listR   (   R   R   R   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   frange>   s    g�íµ ÷Æ°>c         C   s   t  |  | | | ƒ S(   s5  
  Wraps xfrange, acts identically.

  Parameters
  ----------
  start : float
  stop : float, optional
      If empty, start at 0 and stop at the start parameter.
  step : float, optional
  tolerance : float, optional

  Returns
  -------
  generator of float

  See Also
  --------
  libtbx.utils.xfrange
  (   R   (   R   R   R   R   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   xsamplesS   s    c         C   s   t  t |  | | | ƒ ƒ S(   sI  
  Non-generator version of xsamples.

  Parameters
  ----------
  start : float
  stop : float, optional
      If empty, start at 0 and stop at the start parameter.
  step : float, optional
  tolerance : float, optional

  Returns
  -------
  list of float

  See Also
  --------
  libtbx.utils.xfrange, libtbx.utils.xsamples
  (   R   R   (   R   R   R   R   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   samplesi   s    c         C   s,   |  d k r d S|  j d d ƒ j d d ƒ S(   sC   
  The result is supposed to be double-quoted when passed to sh.
  s   \s   \\t   "s   \"N(   R   t   replace(   t   s(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   escape_sh_double_quoted   s     c         C   s   |  d k r |  St |  ƒ S(   s†   
  Returns the length of a sequence or None.

  Parameters
  ----------
  seq : iterable or None

  Returns
  -------
  int or None
  N(   R   t   len(   t   seq(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   xlen†   s     c         C   s:   d } x- |  D]% } | d k r( | } q | | 9} q W| S(   s{   
  Calculates the result of multiplying all elements of a sequence together.

  Parameters
  ----------
  seq : iterable
  N(   R   (   R   t   resultt   val(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   product•   s    	c         C   sL   i  } x? t  |  ƒ D]1 \ } } | r: | | k s: t ‚ n  | | | <q W| S(   s6  
  Builds a dictionary for each element in seq mapped to its index in the sequence.

  Parameters
  ----------
  seq : iterable of object
  must_be_unique : bool, optional

  Returns
  -------
  dict of object, int

  Examples
  --------
  >>> libtbx.utils.sequence_index_dict(['a', 'b'])
  {'a': 0, 'b': 1}
  (   t	   enumerateR   (   R   t   must_be_uniqueR   R   t   elem(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   sequence_index_dict¥   s    c         C   s¢   |  j  ƒ  d k r% t d |  ƒ ‚ n  y t |  ƒ SWn t k
 rF n Xy t |  t j i  ƒ SWn= t k
 rt ‚  n* t k
 r� t d |  t ƒ  f ƒ ‚ n Xd S(   sð   
  Tries to covert a string into an integer, using builtin int() as well as eval().

  Parameters
  ----------
  string : str

  Returns
  -------
  int

  Raises
  ------
  ValueError
      If string cannot be converted into an integer.
  t   truet   falses0   Error interpreting "%s" as a numeric expression.s3   Error interpreting "%s" as a numeric expression: %sN(   R$   R%   (	   t   lowert
   ValueErrorR   t   evalR   t   __dict__t   KeyboardInterruptt	   Exceptiont   format_exception(   t   string(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   number_from_string¾   s        c         C   sl   | d k s t  ‚ t d k r\ d } | d d k r= d	 } n  t d
 | t |  ƒ f ƒ ‚ n  t j |  | ƒ S(   sÁ   
  Wraps gzip.open to open a .gz file.

  Parameters
  ----------
  file_name : str
  mode : str

  Returns
  -------
  file

  Raises
  ------
  RuntimeError
      If gzip is not available.
  t   rt   rbt   wt   wbt   at   abt    i    t   uns4   gzip module not available: cannot %scompress file %s(   R/   s   rbR1   s   wbR3   R4   N(   R   t   gzipR   t   RuntimeErrorR   t   open(   t	   file_namet   modeR6   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt	   gzip_openÝ   s     	c         C   sY   | d k s t  ‚ t d k rI t d i d d 6d d 6| |  f ƒ ‚ n  t j |  | ƒ S(   sÀ   
  Wraps bz2.open to open a .bz2 file.

  Parameters
  ----------
  file_name : str
  mode : str

  Returns
  -------
  file

  Raises
  ------
  RuntimeError
      If bz2 is not available.
  R/   R1   s2   bz2 module not available: cannot %compress file %sR6   R5   (   R/   R1   N(   R   t   bz2R   R8   t   BZ2File(   R:   R;   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   bz2_openø   s
    %c         C   s  t  ƒ  } | j d j t |  ƒ j ƒ  j ƒ  ƒ ƒ | j ƒ  } | j ƒ  | k rS t Sd } d t |  ƒ } d | j ƒ  } t	 g  | | | g D] }	 t
 |	 ƒ ^ qŒ ƒ }
 | d k r¿ t j } n  | d |
 IJ| | IJ| | IJ| | IJx | D] } | | IJqò W| d |
 IJt S(   s:  
  Checks the md5 hash of a file to see if it matches the expected hash.

  Parameters
  ----------
  path : str
  expected_md5_hexdigests : list of str
  hints : list of str, optional
  out : file, optional

  Returns
  -------
  bool
      False if md5 hash of file does not appear in expected_md5_hexdigests.
  s   
s"   Warning: unexpected md5 hexdigest:s
     File: %ss     New md5 hexdigest: %st   *N(   t   hashlib_md5t   updatet   joinR9   t   readt
   splitlinest	   hexdigestt   FalseR   t   maxR   R   t   syst   stdoutt   True(   t   patht   expected_md5_hexdigestst   hintst   outt   mt   current_md5_hexdigestt   warningR:   t   new_hexdigestR   t   widtht   hint(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt    warn_if_unexpected_md5_hexdigest  s&    	( . 


c         C   su  t  |  ƒ t  d ƒ t  d ƒ g k r( |  S|  j d d ƒ j ƒ  j ƒ  }  |  d k rV d Sd } xt d d d	 d
 d d d g ƒ D]¯ \ } } d } |  d | k r­ |  d  } n  |  j d | ƒ t |  ƒ d k rÝ |  d  } n  | d k	 r#y t | ƒ } Wn# t	 k
 r} t
 d |  ƒ ‚ n XPn  | d 9} q~ Wy t |  ƒ } Wn# t	 k
 rf} t
 d |  ƒ ‚ n Xd } | | S(   s\  
  Converts a string of a memory or file size (i.e. "10G") into a number.

  Parameters
  ----------
  mem_str : int or float

  Returns
  -------
  int or float

  Examples
  --------
  >>> libtbx.utils.get_memory_from_string("10G")
  10737418240.0
  >>> libtbx.utils.get_memory_from_string("10M")
  10485760.0

  Raises
  ------
  RuntimeError
  i   g      ð?t    R5   i    i   t   Kt   Mt   Gt   Tt   Et   Zt   Yiÿÿÿÿs   %sBi   iþÿÿÿs5   
   The numerical portion of %s is not a valid float
s1   
   There is no memory unit or valid float in %s
N(   t   typeR   t   stript   upperR    R   t   findR   t   floatR'   R8   R   (   t   mem_strt   factorR   t   greekt   num_strt   numt   e(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   get_memory_from_string6  s6    $  .#c         C   s`   t  j j |  d ƒ } | d k r% | S| j ƒ  } | d k rV t d |  | f ƒ ‚ n  | d k S(	   så   
  Checks the environment variables for variable, returning it as a boolean.

  Parameters
  ----------
  variable_name : str
  default : bool, optional
      Returned if variable_name is not found.

  Returns
  -------
  bool
  R%   R$   t   0t   1sS   Environment variable %s must be "True", "False", "0", or "1" (current value: "%s").N(   s   falses   trueRk   Rl   (   s   trueRl   (   t   ost   environt   getR   R&   t   Sorry(   t   variable_namet   defaultt   valuet   value_lower(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   getenv_boolj  s     c         C   s   t  j |  ƒ j S(   s�   
  Wraps os.stat to calculate a file's size.

  Parameters
  ----------
  file_name : str

  Returns
  -------
  int : size of file, in bytes
  (   Rm   t   statt   st_size(   R:   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt	   file_size�  s    c         C   s®   t  j |  ƒ s t ‚ t  j | ƒ rB t  j | t  j |  ƒ ƒ } n  | d k r` t | d ƒ } n+ | d k sr t ‚ t d | | d d ƒ } | j	 t |  d ƒ j
 ƒ  ƒ ~ d S(   s,  
  Copies a file from source to target, optionally compressing it before writing
  it out.

  Parameters
  ----------
  source : str
  target : str
  compress : str, optional
      The compression algorithm to use. Currently only ".gz" is supported. If
      set, target becomes target + compress.
  R2   s   .gzR:   R;   R0   N(   t   opt   isfileR   t   isdirRC   t   basenameR   R9   R<   t   writeRD   (   t   sourcet   targett   compresst   t(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt	   copy_file�  s    c         C   sÃ   |  | g j  d ƒ d k s! t ‚ | d k r? t j |  ƒ } n  x} | D]u } | rœ t j | ƒ r» t j | ƒ t j | ƒ r™ t d t	 | ƒ ƒ ‚ q™ q» qF t j
 | ƒ rF t j | ƒ qF qF Wd S(   sš   
  Removes a file from disk.

  Parameters
  ----------
  pattern : str, optional
  paths : iterable of str, optional
  ensure_success : bool, optional
  i   s   Cannot remove file: %sN(   R   R   R   t   globRy   t   existsRm   t   removeR8   R   Rz   (   t   patternt   pathst   ensure_successRL   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   remove_files§  s    
!R@   c         C   s¶   t  j j |  ƒ r | d k	 s$ t ‚ t j | ƒ } t  j |  ƒ } g  } xg | D]_ } t  j j |  | ƒ } | r‰ t  j j	 | ƒ r‰ qO n  | j
 | ƒ d k	 rO | j | ƒ qO qO W| S(   s¹   
  Find files matching a pattern in a directory.

  Parameters
  ----------
  dir_name : str
  pattern: str, optional
  files_only : bool, optional

  Returns
  -------
  list of str
  N(   Rm   RL   R{   R   R   t   ret   compilet   listdirRC   Rz   t   searcht   append(   t   dir_nameR†   t
   files_onlyt   regext   filest   matching_filesR:   t	   full_path(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt
   find_files¾  s    $c         C   sò   |  | g j  d ƒ d k s! t ‚ | d k	 ry t j j | ƒ sE t ‚ g  t j | ƒ D] } t j j | | ƒ ^ qU }  n  g  } x- |  D]% } | j | t j j	 | ƒ f ƒ q† W| j
 d d „  ƒ | rÕ | j ƒ  n  g  | D] \ } } | ^ qÜ S(   sé   
  Sorts a list of file names by when they were last modified, ascending.

  Parameters
  ----------
  file_names : iterable of str, optional
  dir_name : str, optional
  reverse : bool, optional

  Returns
  -------
  list of str
  i   t   keyc         S   s   |  d S(   Ni   (    (   t   x(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   <lambda>í  s    N(   R   R   R   Rm   RL   R{   RŒ   RC   RŽ   t   getmtimet   sortt   reverse(   t
   file_namesR�   R›   t   fnt   files_and_mtimesR:   t   mtime(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   sort_files_by_mtimeØ  s    !4#c         C   s*   y t  |  ƒ SWn t k
 r% |  f SXd S(   s©   
  Coverts x into a tuple, either as a direct cast or by making it the sole
  element of a tuple.

  Parameters
  ----------
  x : object

  Returns
  -------
  tuple
  N(   t   tuplet	   TypeError(   R—   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   tupleizeò  s    R   c         C   s    |  d k r |  d f S|  | f S(   s€   
  Returns a suffix if n != 1.

  Parameters
  ----------
  n : int
  suffix : str, optional

  Returns
  -------
  int
  str
  i   R5   (    (   t   nt   suffix(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   plural_s  s     
c         C   sp   t  | ƒ d k s t ‚ g  } xA t | ƒ D]3 } | d k sC t ‚ | j |  | ƒ |  | }  q+ W| j ƒ  | S(   Ni    (   R   R   t   reversedRŽ   R›   (   t   i1dt   sizesR   t   sz(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   n_dim_index_from_one_dim  s    
c         C   sM   g  } t  |  d ƒ r< x1 |  D] } | j t | ƒ ƒ q Wn | j |  ƒ | S(   Nt   __len__(   t   hasattrt   extendt	   flat_listRŽ   (   t   nested_listR   t   sub_list(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR¯     s    c         C   s=   x6 | D]. \ } } t  j | |  ƒ } | d k	 r | Sq W| S(   sõ   
  Selects a value from choices where its key pattern matches key.

  Parameters
  ----------
  key : str
  choices : iterable of str, object
  default : object, optional
      Returned if no pattern matches key.

  Returns
  -------
  object
  N(   RŠ   R�   R   (   R–   t   choicesRr   t   key_patternRs   RP   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   select_matching(  s
     t   KeepTypec           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   d S(   Nt   Keep(    (   t   self(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   __str__=  s    c         C   s   d S(   NR¶   (    (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   __repr__?  s    c         C   s   t  | ƒ |  j k S(   N(   R_   t	   __class__(   R·   t   other(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   __eq__A  s    (   t   __name__t
   __module__R¸   R¹   R¼   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRµ   <  s   		Rp   c           B   s&   e  Z d  Z e Z e j Z d „  Z RS(   sM   
  Basic exception type for user errors; the traceback will be suppressed.
  c         C   s   |  j  j |  j  _ d S(   s@   
    Reset the class module on an instance to libtbx.utils.
    N(   Rº   t   __orig_module__R¾   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   reset_moduleN  s    (   R½   R¾   t   __doc__R¿   R+   RÀ   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRp   F  s   	t   LIBTBX_DISABLE_TRACEBACKLIMITc         C   s�   t  o t | t ƒ } | rP d t f d „  ƒ  Y} t t d | ƒ } d t _ n  t |  | | ƒ } | r‰ | | k r} t ` q‰ | t _ n  | S(   sÀ   
  Intercepts exception tracebacks, removing tracebacks for Sorry exceptions.

  Parameters
  ----------
  type : type
  value : Exception
  traceback : traceback

  Returns
  -------
  str
  t	   __not_setc           B   s   e  Z RS(    (   R½   R¾   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRÃ   h  s    t   tracebacklimiti    (   t   disable_tracebacklimitt
   isinstanceRp   t   objectt   getattrRI   RÄ   t   __prev_excepthook(   R_   Rs   t	   tracebackt   tb_offRÃ   t   prev_tracebacklimitR   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   sorry_excepthookX  s    	t   Usagec           B   s   e  Z d  Z e j Z RS(   s{   
  Subclass of Sorry, for printing out usage instructions upon program
  invocation without arguments (or --help, etc.).
  (   R½   R¾   RÁ   R+   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRÎ   u  s   t   Abortc           B   s   e  Z d  Z e j Z RS(   sV   
  Subclass of Sorry, primarily used in the Phenix GUI in response to user
  input.
  (   R½   R¾   RÁ   R+   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRÏ   |  s   t   Failurec           B   s   e  Z d  Z e j Z RS(   s   
  Subclass of Sorry.
  (   R½   R¾   RÁ   R+   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRÐ   ƒ  s   c          C   s  t  j d  }  |  d k  r! d |  Sd d l } | j j d ƒ ra d d l } | j d k	 ra d Sn  d } d } z~ yD d d l } | j	 d	 d ƒ } | j
 d
 t d t d ƒ d d ƒ Wn3 t k
 ré } t | ƒ j | ƒ sá ‚  n  d | SXWd | d k	 r| j ƒ  | j ƒ  n  Xd S(   sÉ   
  Checks python and library versions and availability to diagnose why
  multiprocessing fails to work.

  Returns
  -------
  str or None
      String indicating why multiprocessing is not working.
  i   i   sh   multiprocessing module not available: Python 2.6 or higher is required (version currently in use: %d.%d)iÿÿÿÿNt   omptbxs-   multiprocessing is not compatible with OpenMPs9   This platform lacks a functioning sem_open implementationt	   processest   funct   iterablet	   chunksizei   s   multiprocessing import error: (   i   i   (   RI   t   version_infot   libtbx.load_envt   envt
   has_moduleRÑ   t   omp_versionR   t   multiprocessingt   Poolt   mapR	   t   ranget   ImportErrort   strt
   startswitht   closeRC   (   t	   vers_infot   libtbxRÑ   t   sem_open_msgt   poolRÛ   Ri   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   detect_multiprocessing_problem‰  s0    
&
c         C   s   |  d k r | S|  S(   s0   
  Returns value or default if value is None.
  N(   R   (   Rs   Rr   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   if_none®  s     c          C   s¾   t  j ƒ  }  |  d j } t |  d ƒ } | d k rJ | j d d ƒ } n` t j t  j ƒ  d d ƒ d d  \ } } | d
 k	 r� | d } n  | d
 k	 rª | d | 7} n  d	 | | f j ƒ  S(   s$   
  Formats an Exception object...
  i    i   R5   s    (<string>, line s    (line i   RW   s   line %ds   %s: %sN(	   RI   t   exc_infoR½   Rà   R   RÊ   t
   extract_tbR   t   rstrip(   t   eit   type_Rs   R:   t   line(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR,   µ  s    *s   EXCEPTION_INFO: c   	         s<  d d l  } | j j r$ t r$ d Sd d l m } d d l m } | ƒ  } | j d | ƒ t	 j
 d | ƒ d j g  | j ƒ  j ƒ  D] } |  | ^ q† ƒ d } ~ g  ‰  x‰ t j t j t j t j g D]i ‰ ‡  ‡ f d †  } | ƒ  rò qË n  ˆ j | ƒ t ˆ d	 d ƒ } | d k	 r'| ƒ  n  ˆ  j ˆ ƒ qË W| S(
   s‚   
  Shows information about an exception.

  Parameters
  ----------
  prefix : str, optional

  Returns
  -------
  str or None
  iÿÿÿÿN(   t   introspection(   t   StringIORO   t   files   
c             s%   x ˆ  D] }  |  ˆ k r t  Sq Wt S(   N(   RK   RG   (   t   o(   t   doneRO   (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   is_doneß  s     t   flush(   R×   RØ   t   full_testingRÅ   Rä   Rï   t	   cStringIORð   t
   show_stackRÊ   t	   print_excRC   t   getvalueRE   RI   RJ   t   stderrt
   __stdout__t
   __stderr__R}   RÈ   R   RŽ   (	   t   prefixRä   Rï   Rð   t   sioRî   t   msgRô   Rõ   (    (   Ró   RO   s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt#   show_exception_info_if_full_testingÆ  s,    	6%	  
c         C   s‘   d } g  } x6 |  d k rD t  |  d ƒ \ }  } | j | | ƒ q W| d k	 rz x& t | ƒ | k  rv | j d ƒ qT Wn  | j ƒ  d j | ƒ S(   sÆ   
  Encodes integer as a string in base 36, prepending 0's until string is of
  length equal to width.

  Parameters
  ----------
  integer : int
  width : int, optional

  Returns
  -------
  str
  t$   0123456789abcdefghijklmnopqrstuvwxyzi    i$   Rk   R5   N(   t   divmodRŽ   R   R   R›   RC   (   t   integerRT   t	   digit_sett   digitsR   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   base36_encodeê  s    
iè  i
   c         C   sB   |  } | d k r! t j ƒ  } n  t d t | | d ƒ d | ƒ S(   s¶   
  Encodes the number of seconds since the epoch in base 36.

  Parameters
  ----------
  seconds_since_epoch : time, optional
  multiplier : int, optional
  width : int, optional
  R  g      à?RT   N(   R   t   timeR  R   (   t   seconds_since_epocht
   multiplierRT   R   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   base36_timestamp  s    
c          C   s•   t  j  ƒ  }  t  j |  ƒ } t  j rN | d d k rN t  j d } t  j } n t  j d } t  j } t  j d | ƒ d | | d | d d |  f S(   sI   
  Converts the current time into a string.

  Returns
  -------
  str
  i   i    i   s   Date %Y-%m-%d Time %H:%M:%Ss    %s %+03d%02d (%.2f s)i  i<   (   R  t	   localtimet   daylightt   tznamet   altzonet   timezonet   strftime(   R	  R  R  t   offs(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   date_and_time  s    
t
   timer_basec           B   sG   e  Z d  Z d „  Z d „  Z d „  Z d d d „ Z d d d „ Z RS(   sP   
  Base timer class used to calculate the time elapsed by various operations.
  c         C   s   |  j  ƒ  |  _ d  S(   N(   Ro   R�   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   __init__+  s    c         C   s   |  j  ƒ  } | |  j } | S(   sb   
    Returns the time elapsed since object was initialized.

    Returns
    -------
    time
    (   Ro   R�   (   R·   R�   t   d(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   elapsed.  s    c         C   s&   |  j  ƒ  } | |  j } | |  _ | S(   sT   
    Returns time since last call of delta().

    Returns
    -------
    time
    (   Ro   R�   (   R·   R�   R  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   delta:  s    	R5   c         C   s4   | d k r t j } n  | | d |  j ƒ  IJd S(   sŠ   
    Prints the time since object was initialized.

    Parameters
    ----------
    prefix : str, optional
    out : file, optional
    s   %.2f sN(   R   RI   RJ   R  (   R·   Rþ   RO   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   show_elapsedG  s    	 c         C   s4   | d k r t j } n  | | d |  j ƒ  IJd S(   s˜   
    Prints the time since last call of delta() or show_delta().

    Parameters
    ----------
    prefix : str, optional
    out : file, optional
    s   %.2f sN(   R   RI   RJ   R  (   R·   Rþ   RO   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt
   show_deltaS  s    	 N(	   R½   R¾   RÁ   R  R  R  R   R  R  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  &  s   			t   user_plus_sys_timec           B   s   e  Z d  Z d „  Z RS(   sg   
  Timer class using os.times() to calculate time. Subclasses timer_base.

  Methods
  -------
  get
  c         C   s   t  j ƒ  } | d | d S(   s0   
    Uses os.times() to calculate the time.
    i    i   (   Rm   t   times(   R·   R�   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRo   i  s    (   R½   R¾   RÁ   Ro   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  `  s   t   wall_clock_timec           B   s   e  Z d  Z d „  Z RS(   s{  
  Timer class using time.time() to calculate time. Subclasses timer_base.

  Methods
  -------
  get

  Notes
  -----
  When running multithreaded code, user_plus_sys_time would report the cumulated
  times for all threads: not very useful to analyse the scaling with the number
  of threads! Wall clock time, although it is less reliable is the only solution
  in that case.
  c         C   s
   t  j  ƒ  S(   s1   
    Uses time.time() to calculate the time.
    (   R  (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRo   €  s    (   R½   R¾   RÁ   Ro   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  p  s   t   time_logc           B   sV   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d Z
 d „  Z RS(	   s1   
  Class used to log the time that tasks take.
  c         C   s:   | |  _  | |  _ d |  _ d |  _ d |  _ d  |  _ d  S(   Ni    (   t   labelt   use_wall_clockt   accumulationR¤   R  R   t   timer(   R·   R  R   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  ‹  s    					c         C   s(   |  j  r t ƒ  |  _ n t ƒ  |  _ |  S(   s   
    Starts the timer.
    (   R   R  R"  R  (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR   “  s    	c         C   s@   |  j  j ƒ  |  _ d |  _  |  j |  j 7_ |  j d 7_ d S(   s   
    Stops the timer.
    i   N(   R"  R  R   R!  R¤   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR   �  s    	c         C   s   |  j  t d |  j ƒ S(   sa   
    Calculates the average length for runs of the timer.

    Returns
    -------
    float
    i   (   R!  RH   R¤   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   average¦  s    c         C   s   |  j  ƒ  |  j ƒ  S(   s5   
    Stops the timer and runs its report method.
    (   R   t   report(   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   log°  s    
c         C   s   d |  j  |  j j ƒ  | f S(   sÚ   
    Returns a string displaying the time elapsed since the timer was started.

    Parameters
    ----------
    local_label : str
        String appended to the end of the output

    Returns
    -------
    str
    s   time_log: %s: %.2f elapsed %s(   R  R"  R  (   R·   t   local_label(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   log_elapsed·  s    s-   time_log: label: n accumulation delta averagec         C   s>   |  j  d k s t ‚ d |  j |  j |  j |  j |  j ƒ  f S(   s§   
    Returns a string including the label, number of stops, accumulated time,
    delta time, and average time for runs of timer.

    Returns
    -------
    str
    s   time_log: %s: %d %.2f %.3g %.3gN(   R"  R   R   R  R¤   R!  R  R#  (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR$  É  s    	(   R½   R¾   RÁ   RG   R  R   R   R#  R%  R'  t   legendR$  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  †  s   	
			
		c         C   ss   |  } d } | d k ri | d } d } | d k ri | d } d } | d k rf | d } d } qf qi n  | | f S(	   s"  
  Rounds a time in seconds to the nearest days / hours / minutes, depending on
  what unit is appropriate.

  Parameters
  ----------
  time_in_seconds : int
      Time, in seconds.

  Returns
  -------
  int : The rounded time in some unit.
  str : The accompanying units for the time.
  t   secondsix   i<   t   minutest   hoursi0   i   t   days(    (   t   time_in_secondst
   time_unitst	   time_unit(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   human_readable_time×  s    


c         C   sŠ   t  |  t ƒ r t |  ƒ }  n  | d k r. |  S| d k rB |  d S| d k rZ |  d d S| d k rv |  d d d St d | ƒ ‚ d S(	   sª   
  Converts time_units and time_unit back into a time with units of seconds.

  Parameters
  ----------
  time_units : int
  time_unit : str

  Returns
  -------
  int
  R)  R*  i<   R+  R,  i   s   Unknown time_unit: %sN(   RÆ   Rà   Rc   R8   (   R.  R/  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   human_readable_time_as_secondsó  s         t   unknownc         C   sX   |  d k r | S| r5 t j d t j t |  ƒ ƒ ƒ St j d t j t |  ƒ ƒ ƒ Sd S(   sÖ   
  Formats a unix_time in a 12-hour format.

  Parameters
  ----------
  unix_time : time
  short : bool, optional
  replace_with : str, optional
      Returned when unix_time is None.

  Returns
  -------
  str
  s   %d-%m-%y %I:%M %ps   %b %d %Y %I:%M %pN(   R   R  R  R  Rc   (   t	   unix_timet   shortt   replace_with(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   format_timestamp_12_hour  s
    c         C   sX   |  d k r d S| r5 t j d t j t |  ƒ ƒ ƒ St j d t j t |  ƒ ƒ ƒ Sd S(   sÖ   
  Formats a unix_time in a 24-hour format.

  Parameters
  ----------
  unix_time : time
  short : bool, optional
  replace_with : str, optional
      Returned when unix_time is None.

  Returns
  -------
  str
  R2  s   %d-%m-%y %H:%Ms   %b %d %Y %H:%MN(   R   R  R  R  Rc   (   R3  R4  R5  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   format_timestamp_24_hour  s
    c         C   s†   t  j ƒ  } d | d | d | d | d f } |  r‚ y t j ƒ  } Wn t k
 r\ q‚ X| d | d | d | d 7} n  | S(   Ns   u+s,u,s: %.2f %.2f %.2fi    i   s    micro-seconds/tick: %.3fg    €„.A(   Rm   R  RI   t   gettickeraccumulationt   AttributeError(   t   show_micro_seconds_per_tickR�   R   t   python_ticker(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   format_cpu_times5  s    '  %c         C   sƒ   |  d k r t j }  n  t ƒ  j ƒ  } y t j ƒ  } Wn t k
 rJ n! X|  d I|  d | | d IJ|  d t | ƒ IJd S(   sç   
  Prints the total CPU time and average time for each Python bytecode
  instruction since the process was started.

  Parameters
  ----------
  out : file, optional
  show_micro_seconds_per_bytecode_instruction : bool, optional
  s1   Time per interpreted Python bytecode instruction:s   %.3f micro secondsg    €„.As   Total CPU time: %.2f %sN(   R   RI   RJ   R  Ro   R8  R9  R0  (   RO   t+   show_micro_seconds_per_bytecode_instructiont
   total_timeR;  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   show_total_time?  s       
c         C   s¤   | d k r t j } n  | d I|  d k  r? | d |  IJn9 t |  d d ƒ } |  | d } | d | | |  f IJt | d d ƒ } | d k	 r  | ƒ  n  d S(	   s.  
  Prints seconds in a human-readable format.

  Parameters
  ----------
  seconds : float
  out : file, optional

  Examples
  --------
  >>> show_wall_clock_time(750)
  wall clock time: 12 minutes 30.00 seconds (750.00 seconds total)
  >>> show_wall_clock_time(20)
  wall clock time: 20.00 seconds
  s   wall clock time:ix   s   %.2f secondsi<   g�íµ ÷Æ°>s,   %d minutes %.2f seconds (%.2f seconds total)Rõ   N(   R   RI   RJ   R   RÈ   (   R)  RO   RP   R   t	   out_flush(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   show_wall_clock_timeT  s     
t
   show_timesc           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   s@   
  Class to track the time past an instance's initialization.
  c         C   ss   | d k r: t j ƒ  } t j ƒ  | d | d |  _ n, | d k rX t j ƒ  |  _ n d | |  _ | |  _ d S(   sc   
    Parameters
    ----------
    time_start : time or str, optional
    out : file, optional
    i    i   t   nowN(   R   Rm   R  R  t
   time_startRO   (   R·   RD  RO   R�   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  v  s    "c         C   sã   |  j  } | d  k r! t j } n  t j ƒ  } | d | d } y t j ƒ  } Wn t k
 rh d  } n Xd | } | d  k	 rµ | d | 7} | d k rµ | d | d | 7} qµ n  | | IJt d t	 j	 ƒ  |  j
 d | ƒ d  S(	   Ni    i   s   usr+sys time: %.2f secondss   , ticks: %ds   , micro-seconds/tick: %.3fg    €„.AR)  RO   (   RO   R   RI   RJ   Rm   R  R8  R9  RA  R  RD  (   R·   RO   R�   t   usr_plus_syst   ticksR   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   __call__†  s     	   


N(   R½   R¾   RÁ   R   R  RG  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRB  q  s   c         C   s    t  j t d |  d | ƒ ƒ d S(   s¯   
  Shows the time since time_start at exit.

  Parameters
  ----------
  time_start : time, optional
  out : file, optional

  See Also
  --------
  libtbx.utils.show_times
  RD  RO   N(   t   atexitt   registerRB  (   RD  RO   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   show_times_at_exit•  s    t   host_and_userc           B   s2   e  Z d  „  Z d „  Z d „  Z d d d „ Z RS(   c         C   s«  t  j j d ƒ |  _ t  j j d ƒ |  _ t  j j d ƒ |  _ t  j j d ƒ |  _ t  j j d ƒ |  _ t  j j d ƒ |  _ t  j j d ƒ |  _	 t  j j d ƒ |  _
 t  j j d	 ƒ |  _ t  j j d
 ƒ |  _ d  |  _ t  j d k r>t  j j d ƒ } t  j j d ƒ } d  | | g k rSt  j j | | ƒ |  _ qSn t  j j d ƒ |  _ t t  d d  ƒ } | d  k r}d  |  _ n | ƒ  |  _ t j ƒ  |  _ t j ƒ  |  _ d  S(   Nt   HOSTt   HOSTNAMEt   COMPUTERNAMEt   HOSTTYPEt   PROCESSOR_ARCHITECTUREt   MACHTYPEt   OSTYPEt   VENDORt   USERt   USERNAMEt   ntt	   HOMEDRIVEt   HOMEPATHt   HOMEt   getpid(   Rm   Rn   Ro   t   hostt   hostnamet   computernamet   hosttypet   processor_architecturet   machtypet   ostypet   vendort   usert   usernameR   t   homedirt   nameRL   RC   RÈ   t   pidR   t   infot   sge_infoR   t
   chunk_infot   pbs_info(   R·   t	   homedrivet   homepathRZ  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  ¦  s.    	c         C   s!   |  j  d  k	 r |  j  S|  j Sd  S(   N(   Rc  R   Rd  (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   get_user_nameÁ  s    c         C   sF   |  j  d  k	 r |  j  S|  j d  k	 r, |  j S|  j d  k	 rB |  j Sd  S(   N(   R[  R   R\  R]  (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   get_host_nameÇ  s    R5   c         C   sª  | d  k r t j } n  |  j d  k	 rA | | d I|  j IJn  |  j d  k	 r| |  j |  j k r| | | d I|  j IJn  |  j d  k	 r· |  j |  j k r· | | d I|  j IJn  |  j d  k	 rà | | d I|  j IJn  |  j d  k	 r	| | d I|  j IJn  |  j d  k sZ|  j d  k sZ|  j	 d  k sZd j
 |  j |  j	 g ƒ |  j k r¯|  j d  k	 rƒ| | d I|  j IJn  |  j	 d  k	 r¯| | d I|  j	 IJq¯n  |  j d  k	 rç|  j d	 k rç| | d
 I|  j IJn  |  j d  k	 r| | d I|  j IJn  |  j d  k	 rK|  j |  j k rK| | d I|  j IJn  |  j d  k	 rt| | d I|  j IJn  |  j j d | d | ƒ |  j j d | d | ƒ d  S(   Ns   HOST =s
   HOSTNAME =s   COMPUTERNAME =s
   HOSTTYPE =s   PROCESSOR_ARCHITECTURE =t   -s
   MACHTYPE =s   OSTYPE =R2  s   VENDOR =s   USER =s
   USERNAME =s   PID =RO   Rþ   (   R   RI   RJ   R[  R\  R]  R^  R_  R`  Ra  RC   Rb  Rc  Rd  Rg  Ri  t   showRk  (   R·   RO   Rþ   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRq  Ð  sJ     $N(   R½   R¾   R  Rn  Ro  R   Rq  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRK  ¤  s   				c      
   C   sÁ   t  ƒ  j } | t j j | d ƒ t j j | d ƒ t j j | d ƒ t j j | d ƒ t j j | d ƒ t j j | d ƒ d d d	 g
 } t j j |  ƒ }  x | D] } |  | k r£ t Sq£ Wt S(
   sÔ   
  Check for specified reserved directories which are standard on many systems;
  these should never be deleted as part of any program.

  Parameters
  ----------
  target_dir : str

  Returns
  -------
  bool
  t	   Documentst   Desktopt	   Downloadst   Libraryt   Moviest   datat   /s   /homes   /Users(   RK  Re  Rm   RL   RC   t   abspathRG   RK   (   t
   target_dirRe  t	   safe_dirst   safe_dir(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   allow_delete_directoryö  s     	c         C   sg   x` | D]X } t  | ƒ d k r( t } n- | rA |  | ƒ t } n |  | ƒ |  | ƒ |  d ƒ q Wd  S(   Ni    s   
(   R   RG   (   t   write_methodt   indentt   incomplete_linet   linesRî   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   _indentor_write_loop  s    	
	

t   indentorc           B   s8   e  Z d d  d d „ Z d „  Z d „  Z d d „ Z RS(   R5   c         C   sy   | d  k r0 | d  k r$ t j } q0 | j } n  | |  _ t |  j d ƒ rZ |  j |  _ n  | |  _ | |  _ t	 |  _
 d  S(   NRõ   (   R   RI   RJ   t   file_objectR­   t   _flushRõ   R  t   parentRG   R€  (   R·   R„  R  R†  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  $  s    			c      
   C   s  t  | ƒ d k r d  S| j d ƒ rb t d |  j j d |  j d |  j d | j ƒ  ƒ t |  _ n¯ | j ƒ  } t  | ƒ d k r¾ |  j r  |  j j | d ƒ q|  j j |  j | d ƒ nJ t d |  j j d |  j d |  j d | d  ƒ |  j j |  j | d ƒ t	 |  _ d  S(	   Ni    s   
R~  R  R€  R�  i   iÿÿÿÿ(
   R   t   endswithR‚  R„  R}   R  R€  RE   RG   RK   (   R·   t   blockR�  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR}   1  s*     					c         C   s   |  j  j ƒ  d  S(   N(   R„  Rõ   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR…  J  s    s     c         C   s   |  j  d |  j | d |  ƒ S(   NR  R†  (   Rº   R  (   R·   R  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   shift_rightM  s    N(   R½   R¾   R   R  R}   R…  R‰  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRƒ  "  s   		t   buffered_indentorc           B   s,   e  Z d d  d d „ Z d „  Z d „  Z RS(   R5   c         C   s#   t  j |  | | | ƒ g  |  _ d  S(   N(   Rƒ  R  t   buffer(   R·   R„  R  R†  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  R  s    c         C   s   |  j  j | ƒ d  S(   N(   R‹  RŽ   (   R·   Rˆ  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR}   V  s    c         C   sP   |  j  d  k	 r |  j  j ƒ  n  x! |  j D] } t j |  | ƒ q) Wg  |  _ d  S(   N(   R†  R   t   write_bufferR‹  Rƒ  R}   (   R·   Rˆ  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRŒ  Y  s
    N(   R½   R¾   R   R  R}   RŒ  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRŠ  P  s   	t   null_outc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s7   
  Pseudo-filehandle for suppressing printed output.
  c         C   s   t  S(   N(   RG   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   isattye  s    c         C   s   d  S(   N(    (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRâ   f  s    c         C   s   d  S(   N(    (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRõ   g  s    c         C   s   d  S(   N(    (   R·   Rà   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR}   h  s    c         C   s   d  S(   N(    (   R·   t   sequence(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt
   writelinesi  s    (   R½   R¾   RÁ   RŽ  Râ   Rõ   R}   R�  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR�  `  s   				t   raise_if_outputc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sŸ   
  Raises an exception when written to.

  Examples
  --------
  >>> import sys
  >>> sys.stdout = libtbx.utils.raise_if_output()
  >>> print
  RuntimeError
  c         C   s   t  S(   N(   RG   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRŽ  w  s    c         C   s   d  S(   N(    (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRâ   z  s    c         C   s   d  S(   N(    (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRõ   }  s    c         C   s
   t  ‚ d S(   s,   
    Raises
    ------
    RuntimeError
    N(   R8   (   R·   Rà   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR}   €  s    c         C   s
   t  ‚ d S(   s,   
    Raises
    ------
    RuntimeError
    N(   R8   (   R·   R�  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR�  ‡  s    (   R½   R¾   RÁ   RŽ  Râ   Rõ   R}   R�  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR‘  k  s   
				t	   multi_outc           B   se   e  Z d  Z d „  Z d „  Z d
 d „ Z d
 d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z RS(   s]   
  Multiplexing output stream, e.g. for simultaneously printing to stdout
  and a logfile.
  c         C   sA   g  |  _  g  |  _ g  |  _ t |  _ d |  _ t j |  j ƒ d  S(   Ni    (	   t   labelst   file_objectst   atexit_send_toRG   t   closedt	   softspaceRH  RI  t   _atexit(   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  •  s    					c         C   sX   |  j  sT xH t |  j |  j ƒ D]. \ } } | d  k	 r | j | j ƒ  ƒ q q Wn  d  S(   N(   R–  t   zipR”  R•  R   R}   Rú   (   R·   t   fR3   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR˜  �  s    	" c         C   sD   |  j  s t ‚ |  j j | ƒ |  j j | ƒ |  j j | ƒ |  S(   sÃ   
    Adds an output stream to the list.

    Parameters
    ----------
    label : str
        Label for replacing the stream later
    file_object : file
    atexit_send_to : file, optional
    (   R–  R   R“  RŽ   R”  R•  (   R·   R  R„  R•  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRI  ¢  s
    c         C   sg   |  j  j | ƒ } |  j | } | j | j ƒ  ƒ | j ƒ  | |  j  | <| |  j | <| |  j | <d S(   sÁ   
    Replaces a registered stream with a new file.

    Parameters
    ----------
    old_label : str
    new_label : str
    new_file_object : file
    new_atexit_send_to : file, optional
    N(   R“  t   indexR”  R}   Rú   Râ   R•  (   R·   t	   old_labelt	   new_labelt   new_file_objectt   new_atexit_send_toR   t   old_file_object(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   replace_stringio³  s    
c         C   s   t  S(   N(   RG   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRŽ  Ê  s    c         C   sU   xE |  j  D]: } | t j k r% q
 n  | t j k r: q
 n  | j ƒ  q
 Wt |  _ d  S(   N(   R”  RI   Rü   Rý   Râ   RK   R–  (   R·   R„  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRâ   Í  s      c         C   s@   x9 |  j  D]. } t | d d  ƒ } | d  k	 r
 | ƒ  q
 q
 Wd  S(   NRõ   (   R”  RÈ   R   (   R·   R„  Rõ   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRõ   Ô  s     c         C   s%   x |  j  D] } | j | ƒ q
 Wd  S(   N(   R”  R}   (   R·   Rà   R„  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR}   Ù  s    c         C   s%   x |  j  D] } | j | ƒ q
 Wd  S(   N(   R”  R�  (   R·   R�  R„  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR�  Ý  s    N(   R½   R¾   RÁ   R  R˜  R   RI  R¡  RŽ  Râ   Rõ   R}   R�  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR’  �  s   						c         C   s   |  d | IJd S(   s®   
  Writes a C header to a file indicating it was generated automatically.

  Parameters
  ----------
  f : file
  file_name_generator : str
      Name of source generator.
  sÉ   /* *****************************************************
   THIS IS AN AUTOMATICALLY GENERATED FILE. DO NOT EDIT.
   *****************************************************

   Generated by:
     %s
 */
N(    (   Rš  t   file_name_generator(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   write_this_is_auto_generatedá  s    
t   import_python_objectc           B   s   e  Z d  „  Z RS(   c   	      C   s$  | j  d ƒ } t | ƒ d k  r@ t d | | | | f ƒ ‚ n  d j | d  ƒ } y t | ƒ } Wn* t k
 r� t d | | | f ƒ ‚ n Xx$ | d d !D] } t | | ƒ } qž Wy t | | d ƒ |  _ Wn1 t k
 rt d | | d | | f ƒ ‚ n X| |  _	 | |  _
 | |  _ d  S(   Nt   .i   s#   %simport path "%s" is too short%s%siÿÿÿÿs   %sno module %s%si   s(   %sobject "%s" not found in module "%s"%s(   t   splitR   R'   RC   t
   __import__Rß   RÈ   RÇ   R9  t   path_elementst   module_patht   module(	   R·   t   import_patht   error_prefixt   target_must_bet	   where_strR¨  R©  Rª  t   attr(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  ÷  s,      		(   R½   R¾   R  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR¤  õ  s   t   input_with_promptc           B   s   e  Z d  d „ Z d „  Z RS(   i    c         C   s|   y d d  l  } Wn t k
 r# n Xy t j |  _ Wn t k
 rP d  |  _ n X| d  k	 ri | t _ n  t | ƒ |  _ d  S(   Niÿÿÿÿ(   t   readlineR+   RI   RÄ   t   previous_tracebacklimitR   t	   raw_inputt   input(   R·   t   promptRÄ   R±  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR    s        c         C   s(   |  j  d  k r t ` n |  j  t _ d  S(   N(   R²  R   RI   RÄ   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   __del__  s    	(   R½   R¾   R  R¶  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR°    s   	c         c   s8   d } x+ t  r3 d V| d 7} | |  k  s	 t ‚ q	 Wd S(   s3  
  Counts the number of times its generator is called, raising an exception if
  called too many times.

  Parameters
  ----------
  assert_less_than : int

  Returns
  -------
  generator of None

  Raises
  ------
  AssertionError
      If .next() is called more than assert_less_than on the generator.
  i    i   N(   RK   R   R   (   t   assert_less_thanR   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt	   count_max  s
    	
t   detect_binary_filec           B   s;   e  Z d d d  „ Z d „  Z d d d „ Z e e ƒ Z RS(   c         C   sa   | d  k r d |  _ n	 | |  _ | d  k r9 d |  _ n	 | |  _ d |  _ d |  _ d  |  _ d  S(   Niè  gš™™™™™©?i    (   R   t   monitor_initialt   max_fraction_non_asciit   n_ascii_characterst   n_non_ascii_characterst   status(   R·   Rº  R»  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  8  s    				c         C   s½   |  j  d k r¶ x¤ | D]™ } d t | ƒ k  o9 d k  n rP |  j d 7_ n |  j d 7_ |  j  d 8_  |  j  d k r |  j |  j |  j k r¢ t |  _ n	 t |  _ Pq q Wn  |  j S(   Ni    i   i€   (   Rº  t   ordR¼  R½  R»  RK   R¾  RG   (   R·   Rˆ  t   c(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   is_binary_fileE  s    "	c         C   sq   t  d | d | ƒ } t |  d ƒ j | j ƒ } t | ƒ d k rF t St t | ƒ | j ƒ | _ | j d | ƒ S(   NRº  R»  R0   i    Rˆ  (   R¹  R9   RD   Rº  R   RG   t   minRÁ  (   R:   Rº  R»  t   detectorRˆ  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   from_initial_blockV  s    	 N(   R½   R¾   R   R  RÁ  RÄ  t   staticmethod(    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR¹  6  s
   	i    c   	      C   s  | d k s t  ‚ | | g j d
 ƒ d k s3 t  ‚ | d
 k rZ t | ƒ j ƒ  j ƒ  } n  g  } | j } | d k r¢ x�| D] } | |  k r| | | ƒ q| q| Wne| d k rÞ xV| D]" } | j |  ƒ rµ | | ƒ qµ qµ Wn)| d k rx| D]" } | j |  ƒ rñ | | ƒ qñ qñ Wní | d k r\xÞ | D]( } | j	 |  ƒ d k r-| | ƒ q-q-Wn« | d k r¹d	 d
 l
 } x� | D]7 } | j d |  d | d | ƒ d
 k	 r{| | ƒ q{q{WnN d	 d
 l
 } x? | D]7 } | j d |  d | d | ƒ d
 k	 rÌ| | ƒ qÌqÌW| S(   s  
  Searches for a pattern in a file's contents.

  Parameters
  ----------
  pattern : str
  mode : str
      One of "==", "find", "startswith", "endswith", "re.search", "re.match"
  re_flags : int, optional
  lines : iterable of str, optional
  file_name : str, optional
  s   ==Rb   Rá   R‡  s	   re.searchs   re.matchi   i    iÿÿÿÿNR†   R-   t   flags(   s   ==s   finds
   startswiths   endswiths	   re.searchs   re.match(   R   R   R   R9   RD   RE   RŽ   Rá   R‡  Rb   RŠ   R�   t   match(	   R†   R;   t   re_flagsR�  R:   R   R3   t   lRŠ   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt
   search_forc  s@    !	    $$t   progress_displayed_as_fractionc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s>   | |  _  d |  _ |  j  d k r0 d „  |  _ n  |  j ƒ  d  S(   Ni    i   c           S   s   d  S(   N(   R   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR˜   ˜  s    (   R¤   R   t   advance(   R·   R¤   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  •  s
    		 c         C   sb   |  j  d k r" t j j d ƒ n  t j j d |  j  |  j f ƒ t j j ƒ  |  j  d 7_  d  S(   Ni    s   s   %i / %ii   (   R   RI   RJ   R}   R¤   Rõ   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRÌ  ›  s
      c         C   s4   |  j  d k r d  St j j d ƒ t j j ƒ  d  S(   Ni   s   
(   R¤   RI   RJ   R}   Rõ   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRó   ¡  s     (   R½   R¾   R  RÌ  Ró   (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRË  “  s   		t   progress_barc           B   s   e  Z d  „  Z RS(   c         C   s¤   d g } |  j  d k rC | j d g |  j  d ƒ | j d ƒ n  | j d |  j |  j  ƒ | j d ƒ t j j d j | ƒ ƒ t j j ƒ  |  j  d 7_  d  S(	   Nt   |i    t   =i   t   >RW   s   |R5   (	   R   R®   RŽ   R¤   RI   RJ   R}   RC   Rõ   (   R·   t
   characters(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRÌ  ©  s    	(   R½   R¾   RÌ  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRÍ  §  s   c         C   sÞ   | d k  r t  |  ƒ St t t j | ƒ ƒ ƒ } | d k rž | t j d | ƒ } t | d ƒ d k  r€ | d 9} | d 7} n  d | } | |  t | ƒ f S| d 7} t t | | ƒ ƒ } d } | t |  | ƒ | f Sd S(	   sg  
  Formats a float, including the uncertainty in its value.

  Parameters
  ----------
  value : float
  standard_uncertainty : float

  Returns
  -------
  str

  Examples
  --------
  >>> libtbx.utils.format_float_with_standard_uncertainty(5e-3, 1e-3)
  '0.0050(10)'
  >>> libtbx.utils.format_float_with_standard_uncertainty(5e-3, 1e-6)
  '0.0050000(10)'
  g¼‰Ø—²Òœ<iÿÿÿÿi
   i   i   s   %%.%if(%%i)s   %.0f(%i)N(   Rà   R   t   roundR   t   log10t   pow(   Rs   t   standard_uncertaintyt	   precisiont   sut   fmt_str(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt&   format_float_with_standard_uncertainty´  s     



c         C   sV   d d l  } g  } x4 t |  ƒ D]& } | j d ƒ } | j d | ƒ q Wd j | ƒ S(   s   
  Creates a random string of hex characters.

  Parameters
  ----------
  number_of_digits : int

  Returns
  -------
  str
  iÿÿÿÿNi   t   0123456789abcdefR5   (   t   randomR
   t	   randrangeRŽ   RC   (   t   number_of_digitsRÛ  R  t   i_digitR   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   random_hex_code×  s    c         C   sÀ   d  } |  d  k r? d d  l } t j | j j d d ƒ ƒ }  n  d |  } y t | d ƒ j ƒ  } Wn t k
 ru nG Xt	 j
 d | ƒ r¼ t	 j d | ƒ } | r¼ t | j ƒ  d ƒ } q¼ n  | S(	   Niÿÿÿÿt   module_nameRä   s   %s/.svn/entriesR/   s   (\d+)s   \d+\s+dir\s+(\d+)i    (   R   R×   Ry   t   dirnameRØ   t	   dist_pathR9   RD   t   IOErrorRŠ   RÇ  R�   R   t   groups(   RL   t   revRä   t   entries_patht   entriest	   rev_match(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   get_svn_revisionê  s    !
c         C   sw   d } |  d k r? d d l } t j | j j d d ƒ ƒ }  n  d |  } t j | ƒ rs t | ƒ j ƒ  j	 ƒ  } n  | S(   st   
  Returns the build tag for libtbx.

  Parameters
  ----------
  path : str, optional

  Returns
  -------
  str
  iÿÿÿÿNRà  Rä   s   %s/TAG(
   R   R×   Ry   Rá  RØ   Râ  R„   R9   R±  R`   (   RL   t   tagRä   t   tag_file_path(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   get_build_tagÿ  s    !
c          C   sR   y t  j ƒ  }  Wn; t k
 rM } | j d k rD t d d ƒ ‚ qN | ‚ n X|  S(   sÝ   
  Returns the current working directory, raising Sorry if it has been deleted or
  unmounted.

  Returns
  -------
  str

  Raises
  ------
  Sorry
      If the current working directory has been deleted or unmounted.
  i   s:   Could not determine the current working directory because s!   it has been deleted or unmounted.(   Rm   t   getcwdt   OSErrort   errnoRp   (   t   cwdRi   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   getcwd_safe  s    
c         C   s²   |  d	 k rj t j d k rR t j j d d ƒ } t j j d d ƒ } | | }  qj t j j d d ƒ }  n  y t j ƒ  } Wn1 t k
 r­ } | j d k r¤ |  } q® | ‚ n X| S(
   s�   
  Returns the current working directory or default if it cannot be found.

  Parameters
  ----------
  default : str, optional

  Returns
  -------
  str
  RV  RW  s   C:RX  s   \RY  Rx  i   N(   R   Rm   Rf  Rn   Ro   Rí  Rî  Rï  (   Rr   t
   home_drivet   home_dirRð  Ri   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   getcwd_or_default,  s    	
c         C   s*  | } | d k rÈ g  } xv t j t j ƒ  ƒ D]_ } t j j | ƒ r. | j |  ƒ r. | j d ƒ d } | j ƒ  r� | j	 t
 | ƒ ƒ q� q. q. Wt | ƒ d k r¿ t t | ƒ d d ƒ } qÈ d } n  |  d t | ƒ } t j j | ƒ rt d t j j | ƒ ƒ ‚ n t j | ƒ t j j | ƒ S(   s  
  Create a program output directory using sequential numbering, picking the
  highest run ID.  In other words, if the prefix is 'Refine' and the current
  directory contains subdirectories named Refine_2 and Refine_9, the new
  directory will be Refine_10.
  t   _iÿÿÿÿi    i   s    The directory %s already exists.N(   R   Rm   RŒ   Rí  RL   R{   Rá   R¦  t   isdigitRŽ   R   R   RH   Rà   Rî  Ry  t   makedirs(   Rþ   t   default_directory_numbert
   dir_numbert   dir_idsR:   t   dir_idR�   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   create_run_directoryH  s     !	t   tmp_dir_wrapperc           B   s/   e  Z d  Z d e j d „ Z e j d „ Z RS(   sL  
  Convenience methods for running in a (presumably empty) temporary directory
  and copying all files to another directory.  Can be used whether or not the
  temporary directory is actually defined; if None, no action will be taken.
  Otherwise, both tmp_dir and dest_dir (default is current directory) must be
  existing paths.
  c         C   s³   | d  k r t j ƒ  } n  | |  _ | |  _ | d  k r< ns t j j | ƒ sa t d | ƒ ‚ nN t j j | ƒ s† t d | ƒ ‚ n  | d | IJ| d | IJt j | ƒ d  S(   Ns*   The temporary directory %s does not exist.s,   The destination directory %s does not exist.s    Changing working directory to %ss   Ultimate destination is %s(	   R   Rm   Rí  t   tmp_dirt   dest_dirRL   R{   Rp   t   chdir(   R·   Rþ  Rÿ  RO   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  j  s    		c         C   s£   |  j  d  k r t St j j |  j ƒ s. t ‚ t j |  j  ƒ } | d |  j IJxA | D]9 } | d | IJt	 j
 t j j |  j  | ƒ |  j ƒ qX W| d IJt S(   Ns   Copying all output files to %ss     ... %sR5   (   Rþ  R   RG   Rm   RL   R{   Rÿ  R   RŒ   t   shutilt   copyRC   RK   (   R·   RO   R’   R:   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   transfer_filesz  s     )
N(   R½   R¾   RÁ   R   RI   RJ   R  R  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRý  b  s   c         C   s   |  d IJd S(   sm   
  Shows a warning when running an experimental program.

  Parameters
  ----------
  out : file, optional
  s*  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !!                  WARNING - EXPERIMENTAL PROGRAM                        !!
  !!                                                                        !!
  !! This program is still in development - some functionality may be       !!
  !! missing and/or untested.  Use at your own risk!  For bug reports, etc. !!
  !! email bugs@phenix-online.org.                                          !!
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
N(    (   RO   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   show_development_warning…  s    c         C   sò   |  d  k	 r3 | d  k s t ‚ t j j |  ƒ } n  | d k rC d  S| d  k r^ t d ƒ ‚ n  t j | ƒ s„ t d d | ƒ ‚ nj t j j | ƒ \ } } xO | d k rí | d k rÒ t	 j
 d d d t ƒ n  t j j | ƒ \ } } qŸ Wd  S(	   NR5   s   No output directory specified.s6   The specified output directory (%s) does not exist or s   is not a directory.t   Dropboxs2   You are directing output to a Dropbox directory.  s>   Please note that this is not guaranteed to work in all cases; s   use at your own risk.(   R   R   Rm   RL   Rá  Rp   Ry   R{   R¦  t   warningst   warnt   UserWarning(   R:   R�   t   headt   tail(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt    check_if_output_directory_exists—  s"     c         C   s‡   t  | d ƒ j ƒ  } |  d IJ|  d t j j | ƒ IJx3 | j ƒ  D]% } | j d ƒ ra qF qF |  | IJqF W|  d IJ|  d IJd S(   sÚ   
  Insert a Python script into an existing file, removing any __future__
  import to prevent syntax errors.  (This could be dangerous in most contexts
  but is required for some of our Coot-related scripts to work.)
  R/   R5   s   #--- script copied from %ss   from __future__s   #--- endN(   R9   RD   Rm   RL   R|   RE   Rá   (   RO   R:   Rw  Rî   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   concatenate_python_script«  s    

c         C   sA   x4 d d d d g D]  } |  d k r) Pn  |  d 9}  q W|  | f S(   s  
  Converts seconds into its closest form in greek units.

  Parameters
  ----------
  secs : float

  Returns
  -------
  float
  str

  Examples
  --------
  >>> libtbx.utils.greek_time(1e-3)
  (1, "milli")
  >>> libtbx.utils.greek_time(1e-6)
  (1, "micro")
  R5   t   millit   microt   nanoi   iè  (    (   t   secsRf   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt
   greek_time¼  s
    iP   c         C   sº   d d  l  } | d  k rJ | j i d |  | f d 6ƒ } | j | ƒ } nJ | j i d | | |  | f d 6ƒ } | j ƒ  } | j | | | j ƒ } | a | j | ƒ d |  | f GH| S(   Niÿÿÿÿs   %s:%dt   https   http://%s:%s@%s:%ss    Installed urllib2 proxy at %s:%d(   t   urllib2R   t   ProxyHandlert   build_openert   HTTPBasicAuthHandlert   HTTPHandlert   libtbx_urllib_proxyt   install_opener(   t   servert   portRc  t   passwordR  t   proxyt   openert   auth(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   install_urllib_http_proxyÜ  s     	c       	   O   sÔ   d t  j k r¸ t d k r¸ t  j d } t  j j d d ƒ } t  j j d d ƒ } t  j j d d ƒ } | d k	 r– t d k r– t d d d	 ƒ ‚ n  t d
 | d | d | d t ƒ n  d d l } | j	 |  | Ž  S(   s~   
  Substitute for urllib2.urlopen, with automatic HTTP proxy configuration
  if specific environment variables are defined.
  t   CCTBX_HTTP_PROXYt   CCTBX_HTTP_PROXY_SERVERt   CCTBX_HTTP_PROXY_PORTiP   t   CCTBX_HTTP_PROXY_USERt   CCTBX_HTTP_PROXY_PASSWORDs5   You have defined a user name for the HTTP proxy, but s@   no password was specified.  Please set the environment variable s   CCTBX_HTTP_PROXY_PASSWORD.R  R  Rc  R  iÿÿÿÿN(
   Rm   Rn   R  R   Ro   R  Rp   R   R  t   urlopen(   t   argst   kwdsR  R  Rc  t   passwdR  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR&  í  s     
t   download_progressc           B   sS   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s™   
  Simple proxy for displaying download status - here with methods for
  writing to the console, but can be subclassed and used for graphical display.
  c         C   s7   | d  k r t ƒ  } n  | |  _ | |  _ d |  _ d  S(   Ni    (   R   R�  R%  t
   n_kb_totalt   n_kb_elapsed(   R·   R%  R+  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR    s
    		c         C   s   | |  _  d |  _ d S(   sÈ   
    Updates the total number of bytes to download and resets the number of bytes
    downloaded.

    Parameters
    ----------
    n_kb_total : int
        Total size of download, in kilobytes.
    i    N(   R+  R,  (   R·   R+  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   set_total_size  s    
	c         C   s.   |  j  d k	 s t ‚ |  j | 7_ |  j ƒ  S(   s„   
    Increments the number of bytes downloaded.

    Parameters
    ----------
    n_kb : int

    Returns
    -------
    bool
    N(   R+  R   R   R,  t   show_progress(   R·   t   n_kb(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt	   increment  s    c         C   s1   |  j  j d |  j |  j f ƒ |  j  j ƒ  t S(   sc   
    Prints the number of bytes downloaded out of the total.

    Returns
    -------
    bool
    s   %d/%d KB downloaded(   R%  R}   R,  R+  Rõ   RK   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR.  ,  s    c         C   s0   |  j  d k	 s t ‚ d t d |  j |  j  ƒ S(   s[   
    Calculates the percent completion of download.

    Returns
    -------
    float
    id   g      ð?N(   R+  R   R   RÂ  R,  (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   percent_finished9  s    c         C   s   |  j  j d ƒ d S(   s@   
    Prints a final message indicating download completion.
    s   Download completeN(   R%  R}   (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   completeD  s    c         C   s   d S(   sS   
    Placeholder for cases where the download is not being run asynchronously.
    N(    (   R·   (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   run_continuouslyJ  s    N(   R½   R¾   RÁ   R   R  R-  R0  R.  R1  R2  R3  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR*    s   					t   download_targetc           B   s2   e  Z d  Z d d d d d „ Z d d d „ Z RS(   so  
  Flexible callable object for retrieving a file from a URL, with optional
  HTTPS authentication.  Designed to be runnable in a separate thread with
  graphical progress update.

  Note that in some circumstances SSL support may be missing from the socket
  module, in which case we use 'curl' to download securely.  (This will not
  work on Windows, obviously.)
  c         C   s¶   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  |  j |  j g k r² |  j d  k	 sc t ‚ d d  l } |  j r¦ t	 | d ƒ r¦ t	 | j
 d ƒ r¦ t |  _ q² t |  _ n  d  S(   Niÿÿÿÿt   sslRG  (   t   urlR:   t   use_curlRc  R  t   base_urlR   R   t   socketR­   R5  RG   RK   (   R·   R6  R:   R7  Rc  R  R8  R9  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR  Z  s    						c         C   sK  | d  k r t ƒ  } n  | d  k r6 t d | ƒ } n  d d l m } d d  l } |  j } |  j s¥d  |  j |  j	 g k rÕ | j
 ƒ  } | j d  |  j |  j |  j	 ƒ | j | ƒ } | j | ƒ } | j | ƒ n  | j |  j ƒ }	 |	 j ƒ  }
 t |
 d ƒ d } | j | ƒ t |  d d ƒ } | d } t |  j d ƒ } xI t r�|	 j | ƒ } | sdPn  | j | ƒ s}d  } Pn  | j | ƒ qEW| j ƒ  | j ƒ  n† | j ƒ  d  |  j |  j	 g k ràd	 |  j |  j	 f } n  | j d
 | |  j |  j f ƒ } | j ƒ  | d k r+t d | ƒ ‚ n  | d  k r;d  St  j! |  j ƒ S(   NR%  iÿÿÿÿ(   t   easy_runs   Content-lengthi   t
   n_kb_chunki   R2   s   --user %s:%ss   curl %s "%s" -o %si    s   curl exited with code %d("   R   R�  R*  Rä   R:  R  R:   R7  Rc  R  t   HTTPPasswordMgrWithDefaultRealmt   add_passwordR8  R  R  R  R&  R6  Rh  R   R-  RÈ   R9   RK   RD   R0  R}   Râ   R2  R3  t   callR8   Ry   Ry  (   R·   R%  t   progress_meterR:  R  R:   t   passmant   authhandlerR  t   reqRh  R+  R;  RÕ   t   fpt   chunkt	   curl_argst   rc(    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyRG  p  sT    		
	 


N(   R½   R¾   RÁ   R   R  RG  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyR4  P  s   	c         C   s;   d d  l  } | j d |  d t d | j d | j ƒd k S(   Niÿÿÿÿs   type t   shellRJ   Rû   i    (   t
   subprocessR>  RK   t   PIPE(   t   cmdRH  (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt
   cmd_exists�  s
    	(    (    (    (}   t
   __future__R    t   libtbx.queuing_system_utilsR   R   t   libtbx.str_utilsR   R7   Rß   R   R=   t   hashlibt   md5RA   t   newt   stdlibR   R  R  Rƒ   R  RH  RÊ   RŠ   RI   Rm   RL   Ry   R¦  t   windows_device_namesR   R   R   R   R   R   R   RK   R#   R.   R<   R?   RV   Rj   RG   Ru   Rx   R‚   R‰   R•   R    R£   R¦   R«   R¯   R´   RÇ   Rµ   R¶   R+   Rp   Rn   RÅ   t
   excepthookRÉ   RÍ   RÎ   RÏ   RÐ   Rç   Rè   R,   R  R  R  R  R  R  R  R  R0  R1  R6  R7  t   format_timestampR<  R?  RA  RB  RJ  RK  R}  R‚  Rƒ  RŠ  R�  R‘  R’  R£  R¤  R°  R¸  R¹  RÊ  RË  RÍ  RÙ  Rß  Ré  Rì  Rñ  Rô  Rü  Rý  RJ   R  R  R  R  R  R   R&  R*  R4  RK  (    (    (    s:   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/utils.pyt   <module>   sð     
  
	!						"	4			
							%		$	:Q		$R	 	.$R		0+	#		#			MM