o
    u]D                     @   sv   d Z ddlZzddlmZ W n ey   ddlmZ Y nw ddlZddlZG dd dej	Z
ddd	Zdd
dZdS )ag  
Toolkit for exporting descriptors to other formats.

**Module Overview:**

::

  export_csv - Exports descriptors to a CSV
  export_csv_file - Writes exported CSV output to a file

.. deprecated:: 1.7.0

   This module will likely be removed in Stem 2.0 due to lack of usage. If you
   use this modle please `let me know <https://www.atagar.com/contact/>`_.
    N)StringIOc                   @   s   e Zd ZdZdS )_ExportDialect
N)__name__
__module____qualname__Zlineterminator r   r   8/usr/lib/python3/dist-packages/stem/descriptor/export.pyr      s    r   r   Tc                 C   s   t  }t|| ||| | S )a\  
  Provides a newline separated CSV for one or more descriptors. If simply
  provided with descriptors then the CSV contains all of its attributes,
  labeled with a header row. Either 'included_fields' or 'excluded_fields' can
  be used for more granular control over its attributes and the order.

  :param Descriptor,list descriptors: either a
    :class:`~stem.descriptor.Descriptor` or list of descriptors to be exported
  :param list included_fields: attributes to include in the csv
  :param list excluded_fields: attributes to exclude from the csv
  :param bool header: if **True** then the first line will be a comma separated
    list of the attribute names (**only supported in python 2.7 and higher**)

  :returns: **str** of the CSV for the descriptors, one per line
  :raises: **ValueError** if descriptors contain more than one descriptor type
  )r   export_csv_filegetvalue)descriptorsincluded_fieldsexcluded_fieldsheaderZoutput_bufferr   r   r	   
export_csv#   s   r   c              	   C   s8  t |tjjr
|f}|sdS t|d }|j}t|}tt|d 	 }|r=|D ]}||vr;t
d||d|f q)ndd |D }|D ]}z|| W qF t
yX   Y qFw tj| |t dd}	|rntj sn|	  |D ])}
t |
tjjst
d	t|
j |t|
krt
d
|t|
f |	t|
 qpdS )a  
  Similar to :func:`stem.descriptor.export.export_csv`, except that the CSV is
  written directly to a file.

  :param file output_file: file to be written to
  :param Descriptor,list descriptors: either a
    :class:`~stem.descriptor.Descriptor` or list of descriptors to be exported
  :param list included_fields: attributes to include in the csv
  :param list excluded_fields: attributes to exclude from the csv
  :param bool header: if **True** then the first line will be a comma separated
    list of the attribute names (**only supported in python 2.7 and higher**)

  :returns: **str** of the CSV for the descriptors, one per line
  :raises: **ValueError** if descriptors contain more than one descriptor type
  Nr   z7%s does not have a '%s' attribute, valid fields are: %sz, c                 S   s   g | ]	}| d s|qS )_)
startswith).0attrr   r   r	   
<listcomp>`   s    z#export_csv_file.<locals>.<listcomp>ignore)ZdialectZextrasactionz?Unable to export a descriptor CSV since %s is not a descriptor.z|To export a descriptor CSV all of the descriptors must be of the same type. First descriptor was a %s but we later got a %s.)
isinstancestemZ
descriptorZ
Descriptortyper   listsortedvarskeys
ValueErrorjoinremovecsvZ
DictWriterr   ZprereqZ_is_python_26ZwriteheaderZwriterow)Zoutput_filer   r   r   r   Zdescriptor_typeZdescriptor_type_labelZ	desc_attrZfieldwriterZdescr   r   r	   r
   :   s>   r
   )r   r   T)__doc__r!   	cStringIOr   ImportErrorioZstem.descriptorr   Zstem.prereqZexcelr   r   r
   r   r   r   r	   <module>   s   
