3
K^D                 @   s   d Z ddlZyddlmZ W n  ek
r<   ddlmZ Y nX ddlZddlZG dd dej	Z
f f dfddZf f df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__lineterminator r	   r	   :/tmp/pip-unpacked-wheel-1_ii6739/stem/descriptor/export.pyr      s   r   Tc             C   s   t  }t|| ||| |j 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   sP  t |tjjr|f}|sdS t|d }|j}t|}tt|d j	 }|r~x<|D ]$}||krTt
d||dj|f qTW ndd |D }x0|D ](}y|j| W q t
k
r   Y qX qW tj| |t dd}	|rtjj  r|	j  x`|D ]X}
t |
tjjst
d	t|
j n"|t|
kr:t
d
|t|
f |	jt|
 qW d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 | ]}|j d s|qS )_)
startswith).0attrr	   r	   r
   
<listcomp>`   s    z#export_csv_file.<locals>.<listcomp>ignore)dialect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csv
DictWriterr   ZprereqZ_is_python_26writeheaderwriterow)Zoutput_filer   r   r   r   Zdescriptor_typeZdescriptor_type_labelZ	desc_attrfieldwriterdescr	   r	   r
   r   :   s6    



r   )__doc__r$   	cStringIOr   ImportErrorioZstem.descriptorr   Zstem.prereqexcelr   r   r   r	   r	   r	   r
   <module>   s   