U
    a±dŠ  ã                   @   sT   d dl mZ d dlZd dlZd dlZd dlmZ dd„ Zd
dd„ZG dd	„ d	ƒZ	dS )é    )ÚBytesION)Úsettingsc              	   C   sT   ddl m} ddlm} z|ƒ |  }t |¡j}|W S  |tfk
rN   Y dS X dS )zp
    Load a templatetag library on multiple Django versions.

    Returns None if the library isn't loaded.
    r   )Úget_installed_libraries)ÚInvalidTemplateLibraryN)Zdjango.template.backends.djangor   Zdjango.template.libraryr   Ú	importlibÚimport_moduleÚregisterÚKeyError)Zlibnamer   r   Úlib© r   ú</tmp/pip-unpacked-wheel-portabvm/django_extensions/compat.pyÚload_tag_library   s    
r   c                 C   s2   t tddƒ}|r.|D ]}| |kr||    S q|S )z Read template settings Z	TEMPLATESN)Úgetattrr   )Ztemplate_keyÚdefaultZtemplates_varZtdictr   r   r   Úget_template_setting   s    r   c                   @   s0   e Zd ZdZejdfdd„Zdd„ Zdd„ Zd	S )
ÚUnicodeWriterzª
    CSV writer which will write rows to CSV file "f",
    which is encoded in the given encoding.
    We are using this custom UnicodeWriter for python versions 2.x
    úutf-8c                 K   s:   t ƒ | _tj| jfd|i|—Ž| _|| _t |¡ƒ | _d S )NÚdialect)r   ÚqueueÚcsvÚwriterÚstreamÚcodecsÚgetincrementalencoderÚencoder)ÚselfÚfr   ÚencodingÚkwdsr   r   r   Ú__init__/   s    zUnicodeWriter.__init__c                 C   sR   | j  dd„ |D ƒ¡ | j ¡ }| d¡}| j |¡}| j |¡ | j 	d¡ d S )Nc                 S   s   g | ]}|  d ¡‘qS )r   )Úencode)Ú.0Úsr   r   r   Ú
<listcomp>6   s     z*UnicodeWriter.writerow.<locals>.<listcomp>r   r   )
r   Úwriterowr   ÚgetvalueÚdecoder   r    r   ÚwriteÚtruncate)r   ÚrowÚdatar   r   r   r$   5   s    

zUnicodeWriter.writerowc                 C   s   |D ]}|   |¡ qd S )N)r$   )r   Zrowsr)   r   r   r   Ú	writerowsA   s    zUnicodeWriter.writerowsN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úexcelr   r$   r+   r   r   r   r   r   (   s   r   )N)
Úior   r   r   r   Zdjango.confr   r   r   r   r   r   r   r   Ú<module>   s   

