Ñò
 ©Bc           @   s  d  Z  d d k l Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k 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 d d „ Z d d „ Z d  „  Z d! d" d# „ Z d$ „  Z d% „  Z d& „  Z d' „  Z d S()   så   A library of commonly useful functions and classes.

This module contains a varied collection of functions and classes that are
potentially useful in a wide range of programming tasks.

It is safe to "import *" from this module.
iÿÿÿÿ(   t
   generatorsNt   grept   antigrept   staggered_merget   parse_ranget   wrapt   xmlrpc_server_doct   center_textt   uniqt	   sh_escapet   flattent   canLoopOvert   isStringLiket   isScalart   make_string_filterc         C   sB   h  } g  } |  D]* } | | j o | | i  | | ƒ q q ~ S(   s-  Returns a list of all the elements of the given sequence in their
    original order, but without duplicates.
    
    Unlike the Unix tool of the same name, duplicates are removed even if they
    aren't adjacent.  This function only works if the elements of the input
    sequence are hashable.
    (   t
   setdefault(   t   seqt   dt   _[1]t   i(    (    s   moosic/utilities.pyR   %   s    c         C   s!   y t  |  ƒ Wn t SXt Sd S(   s+   Tests whether an object can be looped over.N(   t   itert   Falset   True(   t   maybeIterable(    (    s   moosic/utilities.pyR   3   s
      c         C   s+   y |  d Wn t  j
 o t SXt Sd S(   s.   Tests whether an object behaves like a string.t    N(   t	   TypeErrorR   R   (   t   obj(    (    s   moosic/utilities.pyR   :   s
      c         C   s   t  |  ƒ p t |  ƒ S(   sJ   Tests whether an object is a scalar value.  Strings are considered scalar.(   R   R   (   R   (    (    s   moosic/utilities.pyR   A   s    c         c   sJ   xC |  D]; } | | ƒ o	 | Vq x t  | | ƒ D] } | Vq3 Wq Wd S(   sd   Flattens a nested sequence.
    
    For example, [[1, 2], [3, [4, 5]] becomes [1, 2, 3, 4, 5].
    N(   R
   (   R   t   scalarpt   itemt   subitem(    (    s   moosic/utilities.pyR
   F   s     	 c         C   sP   g  } t  |  ƒ }  x7 |  o/ | i |  i t i t t |  ƒ ƒ ƒ ƒ ƒ q W| S(   s‘  Returns a shuffled version of the given sequence. (Deprecated)
 
    The returned list contains exactly the same elements as the given sequence,
    but in a random order.  This function is much slower than the shuffle
    function in the random module in the standard library, and it returns a new
    list instead of shuffling the sequence in place.  Using this function is not
    recommended.
    (   t   listt   appendt   popt   randomt   choicet   ranget   len(   R   t   shuffled(    (    s   moosic/utilities.pyt   shuffleS   s    	 0c          G   s6   |  p d }  t  d „  t t i t d |  Œ  d ƒ ƒ S(   sw  Merges multiple sequences, interleaving their elements. (Old version.)
    
    This returns a sequence whose elements alternate between the elements of
    the input sequences.  For example, if the input sequences are (1, 2, 3) and
    (11, 12, 13), then the output will be (1, 11, 2, 12, 3, 13).  And if the
    input sequences are (a, b, c), (v, w, x, y, z), and (q, r, s, t), then the
    output will be (a, v, q, b, w, r, c, x, s, y, t, z).
    
    Beware that this function throws away input elements that are equal to
    None.  This is the price that must be paid in order to merge sequences of
    different lengths.
    c         S   s
   |  d  j	 S(   N(   t   None(   t   x(    (    s   moosic/utilities.pyt   <lambda>v   s    (    (   (    N(   N(    (   t   filtert   reducet   operatort   concatt   mapR'   (   t	   sequences(    (    s   moosic/utilities.pyt   staggered_merge_oldc   s    c          G   sŒ   t  |  ƒ p g  Sg  } |  D]  } | d  o | | d q q ~ } g  } |  D]  } | d o | | d qQ qQ ~ } | t | Œ  Sd S(   s¶  Merges multiple sequences, interleaving their elements.
    
    This returns a sequence whose elements alternate between the elements of
    the input sequences.  For example, if the input sequences are (1, 2, 3) and
    (11, 12, 13), then the output will be (1, 11, 2, 12, 3, 13).  And if the
    input sequences are (a, b, c), (v, w, x, y, z), and (q, r, s, t), then the
    output will be (a, v, q, b, w, r, c, x, s, y, t, z).
    
    Backward compatibility notes: This function does not throw away None values
    found in the input sequences, although previous versions did. Also, this
    function always returns a list, while previous versions tended to always
    return a tuple.
    i   i    N(   R$   R   (   R/   R   R   t   carst   _[2]t   cdrs(    (    s   moosic/utilities.pyR   ‰   s
    44c         C   sY   t  |  ƒ o t i |  ƒ }  n |  i } g  } | D] } | | ƒ o | | q4 q4 ~ S(   s¤   Returns a list of the elements of "seq" that match the regular expression
    represented by "regex", which may be a string or a regular expression
    object.
    (   R   t   ret   compilet   search(   t   regexR   R6   R   R   (    (    s   moosic/utilities.pyR   Ÿ   s    	c         C   sY   t  |  ƒ o t i |  ƒ }  n |  i } g  } | D] } | | ƒ p | | q4 q4 ~ S(   s«   Returns a list of the elements of "seq" that do not match the regular
    expression represented by "regex", which may be a string or a regular
    expression object.
    (   R   R4   R5   R6   (   R7   R   R6   R   R   (    (    s   moosic/utilities.pyR   ª   s    	i    c         C   s~  |  p t  d ‚ n |  d i ƒ  p |  d d	 j p |  d }  n |  d i ƒ  p |  d d
 j p |  d  }  n t i |  d ƒ } | d j oq t i |  d ƒ \ } } y2 | o t | ƒ } n | o t | ƒ } n Wqtt  j
 o t  d |  ‚ qtXnq | d j oV y t |  ƒ } Wn  t  j
 o t  d |  ‚ n X| d d j o | d } qtn t  d |  ‚ | | f S(   s§  Changes a string representing a range into the indices of the range.
    
    This function converts a string with a form similar to that of the Python
    array slice notation into into a pair of ints which represent the starting
    and ending indices of of the slice.
    
    If the string contains a pair of colon-separated integers, a 2-tuple of
    ints with the respective values of those integers will be returned.  If the
    first number in this pair is omitted, then the value of the "start"
    argument is used.  If the second number in this pair is omitted, then the
    value of the "end" argument is used.
    
    If the string contains a single integer, then a pair of ints with the value
    of that integer and its successor, respectively, will be returned.  The
    exception to this rule is when the successor of the single integer is zero,
    in which case the value of the second element of the returned pair will be
    the value of the "end" argument.
    
    The string can optionally be bracketed by single non-digit, non-dash
    characters at the beginning and/or the end.  The character at the beginning
    doesn't have to be the same as the one at the end.  If either of these
    characters exist, they will be stripped away (and ignored) before any of
    the above-mentioned processing is done.
    
    If the string contains anything else, it is considered invalid and a
    ValueError will be thrown.
    s   Invalid range: empty string.i    t   -t   :i   iÿÿÿÿs(   Invalid range (non-integer value): "%s".s-   Invalid range (wrong number of colons): "%s".(   R8   R9   (   R8   R9   (   t
   ValueErrort   isdigitt   stringt   countt   splitt   int(   R#   t   startt   endt   colon_countt   at   b(    (    s   moosic/utilities.pyR   µ   s2    ""iO   c         C   s   t  | d „ |  i d ƒ ƒ S(   s™   A word-wrap function that preserves existing line breaks and most spaces
    in the text. Expects that existing line breaks are posix newlines (\n).
    c      	   S   sL   d  |  d t  |  |  i d ƒ d ƒ t  | i d d ƒ d ƒ | j | f S(   s   %s%s%ss    
s   
i   i    (   R$   t   rfindR>   (   t   linet   wordt   width(    (    s   moosic/utilities.pyR)   ô   s   !t    (   R+   R>   (   t   textRH   (    (    s   moosic/utilities.pyR   ð   s    c         C   s7   t  i d d ƒ } | i | |  ƒ } | | d „ } | S(   s…   Return a function that takes a string and returns a partial copy of that
    string consisting only of the characters in 'keep'.
    R   c         S   s   |  i  | | ƒ S(   N(   t	   translate(   t   sRC   R   (    (    s   moosic/utilities.pyt   string_filter  s    (   R<   t	   maketransRK   (   t   keept   allcharst   delcharsRM   (    (    s   moosic/utilities.pyR   ÿ   s    iP   R8   c         C   sv   t  |  ƒ | j o |  | d d  d }  n | t  |  ƒ d d } | t  |  ƒ d } d | | |  | | | f S(   s<   Returns the given text centered within a line, with padding.i   i   s   ...i   s   %s %s %s(   R$   (   RJ   t
   line_widtht   pad_chart   pad_lent   extra(    (    s   moosic/utilities.pyR   	  s
    c         C   s   t  i d d |  ƒ S(   s•   Returns a version of the given text that uses backslashes to make it
    safe to pass to a Bourne-type shell if you enclose it in double quotes.
    s   ([$`\"])s   \\\1(   R4   t   sub(   RJ   (    (    s   moosic/utilities.pyR	     s    c         C   s°   h  } x£ |  i  i ƒ  D]’ } d | | <|  i  i | ƒ } x< | D]4 } | | c d | d | d i | d ƒ f 7<q? W|  i  i | ƒ } | | c t i d d | ƒ 7<q W| S(   sÙ  Produces documentation for the methods of an XML-RPC server.
    
    This function queries an XML-RPC server via the introspection API, and
    returns a collection of descriptions that document each of the server's
    methods. The returned collection is a dictionary whose keys are the names of
    the methods and whose values are blurbs of text that describe the methods.
    
    This was inspired by the xml-rpc-api2txt Perl script that comes with
    xmlrpc-c.
    R   s   =item %s B<%s> (%s)

i    s   , i   s   (?m)^s      (   t   systemt   listMethodst   methodSignaturet   joint
   methodHelpR4   RV   (   t   server_proxyt   method_docst   methodt	   signaturet   sigt   help(    (    s   moosic/utilities.pyR     s     
 %#c         C   sf   g  } xA |  d j o3 t  i i |  ƒ t  i i |  ƒ g }  | d d +q	 W|  o |  | d d +n | S(   s˜   Splits a Unix pathname into a list of its components.
 
    If the pathname is absolute, the resulting list will have '/' as its first
    element.
    t   /R   i    (   Rb   R   (   t   ost   patht   dirnamet   basename(   Rd   t   plist(    (    s   moosic/utilities.pyt	   splitpath0  s     4c         C   s  |  d d j  o | |  d |  d <n |  d d j  o | |  d |  d <n | d d j  o | | d | d <n | d d j  o | | d | d <n |  d | d j o t  S|  d | d j o t  S|  d | d j  o |  d j  n o t  S|  d | d j  o |  d j  n o t  S| d |  d j  o | d j  n o t  S| d |  d j  o | d j  n o t  St S(   s‚  Tests whether the slices described by two ranges overlap each other.
    
    Arguments "A" and "B": pairs (2-tuples) of integers, each of which
    represents a range within a sequence.  The first of each pair is the index
    of the first item included in the range, and the second of each pair is the
    index of the item that terminates the range (but is not included in the
    range).
    
    Argument "n": an integer that represents the length of the sequence in which
    these ranges will be applied.  This is needed to handle negative range
    indices sensibly.
    
    Returns: True if the ranges overlap, otherwise False.
    i    i   (   R   R   (   t   At   Bt   n(    (    s   moosic/utilities.pyt   is_overlapping>  s*          * * * * (   s   greps   antigreps   staggered_merges   parse_ranges   wraps   xmlrpc_server_docs   center_texts   uniqs	   sh_escapes   flattens   canLoopOvers   isStringLikes   isScalars   make_string_filter(   t   __doc__t
   __future__R    R4   R!   R<   R,   Rc   t   os.patht   __all__R   R   R   R   R
   R&   R0   R   R   R   R   R   R   R   R	   R   Rh   Rl   (    (    (    s   moosic/utilities.pyt   <module>   s.   H  						&			;	
				
