ó
æQc           @   sN   d  Z  d d l Z d d l m Z d d d „  ƒ  YZ d d „ Z d „  Z d S(	   s*   
This module implements the SmartIO class
iÿÿÿÿN(   t   StringIOt   SmartIOc           B   sA   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s´  
    The SmartIO class allows one to put a cap on the memory consumption.
    StringIO objects are very fast, because they are stored in memory, but
    if they are too big the memory footprint becomes noticeable.
    The write method of a SmartIO determines if the data that is to be added
    to the (initially) StrintIO object does not exceed a certain threshold; if
    it does, it switches the storage to a temporary disk file
    i @  i    c         C   s4   | |  _  t ƒ  |  _ | r' d |  _ n	 d |  _ d  S(   Ni   i    (   t   _max_mem_sizeR    t   _iot   _fixed(   t   selft   max_mem_sizet	   force_mem(    (    s/   /usr/lib/python2.7/site-packages/rhn/SmartIO.pyt   __init__   s
    	c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s/   /usr/lib/python2.7/site-packages/rhn/SmartIO.pyt   set_max_mem_size%   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s/   /usr/lib/python2.7/site-packages/rhn/SmartIO.pyt   get_max_mem_size(   s    c         C   sv   |  j  sb t | ƒ |  j j ƒ  |  j k rb t ƒ  } | j |  j j ƒ  ƒ d |  _  | |  _ qb n  |  j j | ƒ d  S(   Ni   (   R   t   lenR   t   tellR   t	   _tempfilet   writet   getvalue(   R   t   datat   tmpfile(    (    s/   /usr/lib/python2.7/site-packages/rhn/SmartIO.pyR   +   s    	"		c         C   s   t  |  j | ƒ S(   N(   t   getattrR   (   R   t   name(    (    s/   /usr/lib/python2.7/site-packages/rhn/SmartIO.pyt   __getattr__7   s    (   t   __name__t
   __module__t   __doc__R   R	   R
   R   R   (    (    (    s/   /usr/lib/python2.7/site-packages/rhn/SmartIO.pyR      s   			c         C   sc   d d  l  } |  s t ƒ  }  n  | j d d t j ƒ  d |  ƒ \ } } t j | ƒ t j | d ƒ S(   Niÿÿÿÿt   prefixs   _rhn_transports-%d-t   dirs   wb+(   t   tempfilet   getDefaultStoraget   mkstempt   ost   getpidt   unlinkt   fdopen(   t   tmpdirR   t   fdt   fname(    (    s/   /usr/lib/python2.7/site-packages/rhn/SmartIO.pyR   ;   s    c          C   s¢   d }  d } y t  |  ƒ } Wn t k
 r0 | SXx` | j ƒ  D]R } | j d ƒ r> | j d ƒ } t | ƒ d k r | d j ƒ  p‡ | } q q> q> W| j ƒ  | S(   sY    Reads the default temp dir from up2date config.
    if defined uses that else use /tmp. s   /etc/sysconfig/rhn/up2dates   /tmps   tmpDir=t   =i   iÿÿÿÿ(   t   opent   IOErrort	   readlinest
   startswitht   splitR   t   stript   close(   t
   up2dateCfgR!   t   ft   linet   cfgdir(    (    s/   /usr/lib/python2.7/site-packages/rhn/SmartIO.pyR   E   s     
(    (   R   R   t	   cStringIOR    R   t   NoneR   R   (    (    (    s/   /usr/lib/python2.7/site-packages/rhn/SmartIO.pyt   <module>   s
   +
