U
    -e[                  	   @   s   d Z ddddddddd	g	Zd
dlZejddd	Zdd Zdd Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Z[dS )zb
Methods for serialized objects (or source code) stored in temporary files
and file-like objects.
dump_sourcedumpdumpIO_sourcedumpIOload_sourceloadloadIO_sourceloadIOcapture    Nstdoutc              
   c   sP   ddl }ddlm} t|| }t|| |  zt|| V  W 5 t|| | X dS )zbuilds a context that temporarily replaces the given stream name

    >>> with capture('stdout') as out:
    ...   print ("foo!")
    ... 
    >>> print (out.getvalue())
    foo!

    r
   N)StringIO)sysior   getattrsetattr)streamr   r   orig r   J/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/dill/temp.pyr	      s    
c                 C   s   dd l }|| d S )Nr
   )codecslatin_1_encode)xr   r   r   r   b+   s    r   c           
      K   s   | dd}| dd}t| d| }t|fd|i| }|s|  d  }|d dkrx| d }td	| |d }i }t|| t	d
| |}	|	S )aC  load an object that was stored with dill.temp.dump_source

    file: filehandle
    alias: string name of stored object
    mode: mode to open the file, one of: {'r', 'rb'}

    >>> f = lambda x: x**2
    >>> pyfile = dill.temp.dump_source(f, alias='_f')
    >>> _f = dill.temp.load_source(pyfile)
    >>> _f(4)
    16
    aliasNmodernamer
   #NAME:unknown name for code: %s%s)
popr   openreadstrip
splitlinessplitIOErrorexeceval)
filekwdsr   r   fnamesourcetagstublocal_r   r   r   r   /   s    
c              	   K   s   ddl m}m} ddl}|dd |dd |d	d}t|pJ|| }d
| }|jf ddi|}|t	d
|| |d|g |  |S )aM  write object source to a NamedTemporaryFile (instead of dill.dump)
Loads with "import" or "dill.temp.load_source".  Returns the filehandle.

    >>> f = lambda x: x**2
    >>> pyfile = dill.temp.dump_source(f, alias='_f')
    >>> _f = dill.temp.load_source(pyfile)
    >>> _f(4)
    16

    >>> f = lambda x: x**2
    >>> pyfile = dill.temp.dump_source(f, dir='.')
    >>> modulename = os.path.basename(pyfile.name).split('.py')[0]
    >>> exec('from %s import f as _f' % modulename)
    >>> _f(4)
    16

Optional kwds:
    If 'alias' is specified, the object will be renamed to the given string.

    If 'prefix' is specified, the file name will begin with that prefix,
    otherwise a default prefix is used.
    
    If 'dir' is specified, the file will be created in that directory,
    otherwise a default directory is used.
    
    If 'text' is specified and true, the file is opened in text
    mode.  Else (the default) the file is opened in binary mode.  On
    some operating systems, this makes no difference.

NOTE: Keep the return value for as long as you want your file to exist !
       
importablegetnamer
   NdeleteTsuffix r   
#NAME: %s
z.pyr   )r-   r4   r5   tempfile
setdefaultr!   strNamedTemporaryFilewriter   joinflush)objectr+   r4   r5   r;   r   r   r*   r   r   r   r   K   s      c                 K   s:   ddl }|dd}t| d| }|t|fd|i|S )zload an object that was stored with dill.temp.dump

    file: filehandle
    mode: mode to open the file, one of: {'r', 'rb'}

    >>> dumpfile = dill.temp.dump([1, 2, 3, 4, 5])
    >>> dill.temp.load(dumpfile)
    [1, 2, 3, 4, 5]
    r
   Nr   rbr   )dillr!   r   r   r"   )r*   r+   pickler   r   r   r   r   r   x   s    
c                 K   s@   ddl }ddl}|dd |jf |}|| | |  |S )aE  dill.dump of object to a NamedTemporaryFile.
Loads with "dill.temp.load".  Returns the filehandle.

    >>> dumpfile = dill.temp.dump([1, 2, 3, 4, 5])
    >>> dill.temp.load(dumpfile)
    [1, 2, 3, 4, 5]

Optional kwds:
    If 'suffix' is specified, the file name will end with that suffix,
    otherwise there will be no suffix.
    
    If 'prefix' is specified, the file name will begin with that prefix,
    otherwise a default prefix is used.
    
    If 'dir' is specified, the file will be created in that directory,
    otherwise a default directory is used.
    
    If 'text' is specified and true, the file is opened in text
    mode.  Else (the default) the file is opened in binary mode.  On
    some operating systems, this makes no difference.

NOTE: Keep the return value for as long as you want your file to exist !
    r
   Nr6   T)rD   r;   r<   r>   r   rA   )rB   r+   rE   r;   r*   r   r   r   r      s    c                 K   s<   ddl }ddlm} t| d| }|| kr.| }|||S )zload an object that was stored with dill.temp.dumpIO

    buffer: buffer object

    >>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
    >>> dill.temp.loadIO(dumpfile)
    [1, 2, 3, 4, 5]
    r
   NBytesIOgetvalue)rD   r   rG   r   r   )bufferr+   rE   r   valuer   r   r   r      s    	 c                 K   s2   ddl }ddlm} | }|| | |  |S )zdill.dump of object to a buffer.
Loads with "dill.temp.loadIO".  Returns the buffer object.

    >>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
    >>> dill.temp.loadIO(dumpfile)
    [1, 2, 3, 4, 5]
    r
   NrF   )rD   r   rG   r   rA   )rB   r+   rE   r   r*   r   r   r   r      s    c                 K   s   | dd}t| d| }|| kr&| }| }|sr|  d  }|d dkrj| d }td| |d }i }t|| td| |}|S )	a  load an object that was stored with dill.temp.dumpIO_source

    buffer: buffer object
    alias: string name of stored object

    >>> f = lambda x:x**2
    >>> pyfile = dill.temp.dumpIO_source(f, alias='_f')
    >>> _f = dill.temp.loadIO_source(pyfile)
    >>> _f(4)
    16
    r   NrH   r   r
   r   r   r    )	r!   r   decoder$   r%   r&   r'   r(   r)   )rI   r+   r   r-   r.   r/   r0   r1   r   r   r   r      s     
c              	   K   sr   ddl m}m} ddlm} |dd}t|p6|| }d| }| }|td	|| |d|g |
  |S )	ap  write object source to a buffer (instead of dill.dump)
Loads by with dill.temp.loadIO_source.  Returns the buffer object.

    >>> f = lambda x:x**2
    >>> pyfile = dill.temp.dumpIO_source(f, alias='_f')
    >>> _f = dill.temp.loadIO_source(pyfile)
    >>> _f(4)
    16

Optional kwds:
    If 'alias' is specified, the object will be renamed to the given string.
    r2   r3   r
   rF   r   r8   r9   r:   )r-   r4   r5   r   rG   r!   r=   r?   r   r@   rA   )rB   r+   r4   r5   r   r   r   r*   r   r   r   r      s     )r   )__doc____all__
contextlibcontextmanagerr	   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s(      - 