Spade
Mini Shell
3
\4?�@sLdZddlZddlmZmZmZmZmZmZm Z m
Z
mZmZm
Z
mZmZmZddlmZddlmZddlmZddd d
ddd
ddddddddddddddgZGdd�d�ZGdd�de�Zede�Gdd�de�Zede�Gd
d�de�Zed!e�Gd"d�d�ZGd#d�d�ZyeWnek
�r8eZYnXGd$d�d�ZdS)%z+
csv.py - read/write/investigate CSV files
�N)�Error�__version__�writer�reader�register_dialect�unregister_dialect�get_dialect�
list_dialects�field_size_limit�
QUOTE_MINIMAL� QUOTE_ALL�QUOTE_NONNUMERIC�
QUOTE_NONE�__doc__)�Dialect)�OrderedDict)�StringIOrrr
rrrr�excel� excel_tabr
rrrrr �Snifferrr�
DictReader�
DictWriter�unix_dialectc@sDeZdZdZdZdZdZdZdZdZ dZ
dZdZdd�Z
dd�ZdS) rz�Describe a CSV dialect.
This must be subclassed (see csv.excel). Valid attributes are:
delimiter, quotechar, escapechar, doublequote, skipinitialspace,
lineterminator, quoting.
�FNcCs|jtkrd|_|j�dS)NT)� __class__r�_valid� _validate)�self�r�/usr/lib64/python3.6/csv.py�__init__+s
zDialect.__init__cCs@yt|�Wn.tk
r:}ztt|���WYdd}~XnXdS)N)�_Dialect� TypeErrorr�str)r�errrr0szDialect._validate)�__name__�
__module__�__qualname__r�_namer� delimiter� quotecharZ
escapechar�doublequote�skipinitialspace�lineterminator�quotingr
rrrrrrsc@s(eZdZdZdZdZdZdZdZe Z
dS)rz;Describe the usual properties of Excel-generated CSV
files.�,�"TFz
N)r%r&r'rr)r*r+r,r-rr.rrrrr7sc@seZdZdZdZdS)rzEDescribe
the usual properties of Excel-generated TAB-delimited
files.� N)r%r&r'rr)rrrrrAsz excel-tabc@s(eZdZdZdZdZdZdZdZe Z
dS)rz:Describe the usual properties of Unix-generated CSV
files.r/r0TF�
N)r%r&r'rr)r*r+r,r-rr.rrrrrFsZunixc@s@eZdZddd�Zdd�Zedd��Zejd d��Zd
d�ZdS)
rNrcOs6||_||_||_t||f|�|�|_||_d|_dS)Nr)�_fieldnames�restkey�restvalr�dialect�line_num)r�f�
fieldnamesr4r5r6�args�kwdsrrrr
RszDictReader.__init__cCs|S)Nr)rrrr�__iter__[szDictReader.__iter__cCs@|jdkr0yt|j�|_Wntk
r.YnX|jj|_|jS)N)r3�nextr�
StopIterationr7)rrrrr9^s
zDictReader.fieldnamescCs
||_dS)N)r3)r�valuerrrr9hscCs�|jdkr|jt|j�}|jj|_x|gkr:t|j�}q&Wtt|j|��}t|j�}t|�}||krz||d�||j<n*||kr�x
|j|d�D]}|j||<q�W|S)Nr) r7r9r=rr�zip�lenr4r5)r�row�dZlfZlr�keyrrr�__next__ls
zDictReader.__next__)NNNr) r%r&r'r
r<�propertyr9�setterrErrrrrQs
c@s6eZdZddd�Zdd�Zdd �Zd
d�Zdd
�ZdS)rr�raisercOsB||_||_|j�dkr$td|��||_t||f|�|�|_dS)NrH�ignorez-extrasaction
(%s) must be 'raise' or
'ignore')rHrI)r9r5�lower�
ValueError�extrasactionr)rr8r9r5rLr6r:r;rrrr
�szDictWriter.__init__cCs
tt|j|j��}|j|�dS)N)�dictr@r9�writerow)r�headerrrr�writeheader�szDictWriter.writeheadercsN�jdkr8�j��j}|r8tddjdd�|D������fdd��jD�S)NrHz(dict
contains fields not in fieldnames: z,
cSsg|]}t|��qSr)�repr)�.0�xrrr�
<listcomp>�sz,DictWriter._dict_to_list.<locals>.<listcomp>c3s|]}�j|�j�VqdS)N)�getr5)rRrD)�rowdictrrr� <genexpr>�sz+DictWriter._dict_to_list.<locals>.<genexpr>)rL�keysr9rK�join)rrVZwrong_fieldsr)rVrr�
_dict_to_list�s
zDictWriter._dict_to_listcCs|jj|j|��S)N)rrNrZ)rrVrrrrN�szDictWriter.writerowcCs|jjt|j|��S)N)r� writerows�maprZ)rZrowdictsrrrr[�szDictWriter.writerowsN)rrHr)r%r&r'r
rPrZrNr[rrrrr�s
c@s:eZdZdZdd�Zd
dd�Zdd�Zd d
�Zdd�ZdS)rze
"Sniffs" the format of a CSV file (i.e. delimiter, quotechar)
Returns a Dialect object.
cCsdddddg|_dS)Nr/r1�;� �:)� preferred)rrrrr
�szSniffer.__init__NcCsd|j||�\}}}}|s(|j||�\}}|s4td��Gdd�dt�}||_||_|pVd|_||_|S)zI
Returns a dialect (or None) corresponding to the sample
zCould not determine
delimiterc@seZdZdZdZeZdS)zSniffer.sniff.<locals>.dialectZsniffedz
N)r%r&r'r(r-rr.rrrrr6�sr6r0)�_guess_quote_and_delimiter�_guess_delimiterrrr+r)r*r,)r�sample�
delimitersr*r+r)r,r6rrr�sniff�s
z
Sniffer.sniffcCs�g}x0dD](}tj|tjtjB�}|j|�}|r
Pq
W|s>dSi}i}d}|j} x�|D]�}
| d d
}|
|}|r�|j|d�d
||<y| dd
}|
|}Wntk
r�wVYnX|r�|dks�||kr�|j|d�d
||<y| dd
}Wntk
�rwVYnX|
|rV|d
7}qVWt||jd
�}
|�rZt||jd
�}|||k}|dk�rbd}nd}d}tjdtj |�|
d�tj�}|j
|��r�d}nd}|
|||fS)a�
Looks for text enclosed between two identical quotes
(the probable quotechar) which are preceded and followed
by the same character (the probable delimiter).
For example:
,'some text',
The quote with the most wins, same with the delimiter.
If there is no quotechar the delimiter can't be determined
this way.
�I(?P<delim>[^\w\n"\'])(?P<space>
?)(?P<quote>["\']).*?(?P=quote)(?P=delim)�G(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space>
?)�G(?P<delim>[^\w\n"\'])(?P<space>
?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)�-(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)rFNr�quote��delimZspace)rDr2z]((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$))rlrjT)rfrgrhri)rFNr)�re�compile�DOTALL� MULTILINE�findall�
groupindexrU�KeyError�max�escape�search)r�datardZmatchesZrestrZregexpZquotes�delimsZspacesrr�m�nrDr*rlr,Z dq_regexpr+rrrra�s`
z"Sniffer._guess_quote_and_delimitercCsttd|jd���}dd�td�D�}tdt|��}d}i}i}i}dt|t|��} }
�x| t|�k�rn|d7}xT|| |
�D]D}x>|D]6}|j|i�}
|j|�}|
j|d�d|
|<|
||<q�Wq�Wx�|j�D]�}t||j ��}t|�dk�r|dddk�rq�t|�dk�rht
|d d
�d�||<|j||�||d||dtdd
�|D��f||<q�|d||<q�W|j �}t
||�}d}d}x�t|�dk�r||k�rx\|D]T\}}|ddk�r�|ddk�r�|d||k�r�|dk�s�||k�r�|||<�q�W|d8}�q�Wt|�dk�r`t|j��d}|dj|�|djd|�k}||fS|
} |
|7}
q\W|�szdSt|�dk�r�xF|jD]<}||j�k�r�|dj|�|djd|�k}||fS�q�Wdd�|j �D�}|j�|dd}|dj|�|djd|�k}||fS)a�
The delimiter /should/ occur the same number of times on
each row. However, due to malformed data, it may not. We don't
want
an all or nothing approach, so we allow for small variations in
this
number.
1) build a table of the frequency of each character on every
line.
2) build a table of frequencies of this frequency
(meta-frequency?),
e.g. 'x occurred 5 times in 10 rows, 6 times in 1000
rows,
7 times in 2 rows'
3) use the mode of the meta-frequency to determine the /expected/
frequency for that character
4) find out how often the character actually meets that goal
5) the character that best meets its goal is the delimiter
For performance reasons, the data is evaluated in chunks, so it can
try and evaluate the smallest portion of the data possible,
evaluating
additional chunks as necessary.
Nr2cSsg|]}t|��qSr)�chr)rR�crrrrT.sz,Sniffer._guess_delimiter.<locals>.<listcomp>��
rrkcSs|dS)Nrkr)rSrrr�<lambda>Hsz*Sniffer._guess_delimiter.<locals>.<lambda>)rDcss|]}|dVqdS)rkNr)rR�itemrrrrWMsz+Sniffer._guess_delimiter.<locals>.<genexpr>g�?g�������?g{�G�z�?z%c
rcSsg|]\}}||f�qSrr)rR�k�vrrrrTws)rr���)�list�filter�split�range�minrArU�countrX�itemsrt�remove�sum�floatr`�sort)rrwrd�asciiZchunkLengthZ iterationZ
charFrequencyZmodesrx�start�end�line�charZ
metaFrequencyZfreqr�ZmodeListZtotalZconsistencyZ thresholdr�r�rlr,rCrrrrbst
zSniffer._guess_delimiterc
Cs�tt|�|j|��}t|�}t|�}i}xt|�D]}d||<q2Wd}x�|D]�}|dkrZP|d7}t|�|krpqLx�t|j��D]x} xJtt t
gD]0}
y|
|| �PWq�ttfk
r�Yq�Xq�Wt|| �}
|
|| kr~|| dkr�|
|| <q~|| =q~WqLWd}x�|j
�D]~\} }t|�td�k�rLt|| �|k�rB|d7}n|d8}n<y||| �Wn"ttfk
�r~|d7}Yn
X|d8}�qW|dkS)Nr�rk)rrrer=rAr�r�rX�intr��complexrK�
OverflowErrorr��typer")
rrcZrdrrO�columnsZcolumnTypes�iZcheckedrB�colZthisTypeZ hasHeaderZcolTyperrr�
has_header�sJ
zSniffer.has_header)N) r%r&r'rr
rerarbr�rrrrr�s
Lg) rrmZ_csvrrrrrrrr r
rrr
rrr!�collectionsr�ior�__all__rrrrrr�� NameErrorr�rrrrr�<module>s2@
2