Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/tailer/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/tailer/__pycache__/__init__.cpython-35.pyc



T�hV�!�@s�ddlZddlZddlZejdkr9eZGdd�de�Zddd�Zddd	�Z	d
dd�Z
d
d�Zdd�Zdd�Z
edkr�e
�dS)�N�c@s�eZdZdZd#Zdddd�Zd	d
�Zdd�Zd
dd�Zddd�Z	dd�Z
dd�Zddd�Zddd�Z
ddd�Zdd �Zd!d"�ZdS)$�TailerzZ    Implements tailing and heading functionality like GNU tail and head
    commands.
    �
�
�
iFcCs8||_||_|jj�|_|r4|j�dS)N)�	read_size�file�tell�	start_pos�seek_end)�selfrr�end�r�@/opt/alt/python35/lib/python3.5/site-packages/tailer/__init__.py�__init__s
		zTailer.__init__cCstjdj|j�|�S)N�|)�re�split�join�line_terminators)r�datarrr�
splitlinesszTailer.splitlinescCs|jdd�dS)Nr�)�seek)rrrrrszTailer.seek_endrcCs|jj||�dS)N)rr)r�pos�whencerrrrszTailer.seekNcCs:|r|jj|�}n|jj�}t|�|fS)N)r�read�len)rr�read_strrrrrszTailer.readcCs�|jj�}}|j|j�\}}d}|rT|d|jkrT|d7}x�|dkr�|}xO||kr�|||jkr�|j||d�|jj�S|d7}qlW||j7}|j|�|j|j�\}}qWWdS)z�        Searches forward from the current file position for a line terminator
        and seeks to the charachter after it.
        r�N)rr	rrrr)rrr
�
bytes_readr�start�irrr�seek_line_forward's 



zTailer.seek_line_forwardcCs�|jj�}}|j}||kr5||8}nd}|}|j|�|j|�\}}|r�|d|jkr�|d8}|dd�dkr�d|jkr�|d8}x�|dkr�|d}xO|dkr|||jkr|j||d�|jj�S|d8}q�W|dks?||jdkrP|jd�dS||j8}|j|�|j|j�\}}q�WdS)z�        Searches backwards from the current file position for a line terminator
        and seeks to the charachter after it.
        rrrNz
������)rr	rrrr)rr�end_posrr rr"rrr�	seek_lineEs4	


%





zTailer.seek_line�
cCs{|j�|jj�}x!t|�D]}|j�s&Pq&W|jj||jj�d�}|rs|j|�SgSdS)z3        Return the last lines of the file.
        rN)rrr	�ranger'rr)r�linesr&r"rrrr�tailts
#
zTailer.tailcCs~|jd�x!t|�D]}|j�sPqW|jj�}|jd�|jj|d�}|rv|j|�SgSdS)z2        Return the top lines of the file.
        rrN)rr)r#rr	rr)rr*r"r&rrrr�head�s


zTailer.headg�?ccs�d}x�|jj�}|jj�}|r�|rK||jkrKd}q	|d|jkr�|dd�}|dd�dkr�d|jkr�|dd	�}d}|Vq	d}|j|�tj|�q	WdS)
z�        Iterator generator that returns lines as data is added to the file.

        Based on: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157035
        TFrNz
r$r$r$r$)rr	�readlinerr�time�sleep)r�delayZtrailingZwhere�linerrr�follow�s"%
z
Tailer.followcCs
|j�S)N)r2)rrrr�__iter__�szTailer.__iter__cCs|jj�dS)N)r�close)rrrrr4�szTailer.close)rrr)�__name__�
__module__�__qualname__�__doc__rrrrrrr#r'r+r,r2r3r4rrrrrs/rr(cCst|�j|�S)a;    Return the last lines of the file.

    >>> try:
    ...    from StringIO import StringIO
    ... except ImportError:
    ...    from io import StringIO
    >>> f = StringIO()
    >>> for i in range(11):
    ...     _ = f.write('Line %d\n' % (i + 1))
    >>> tail(f, 3)
    ['Line 9', 'Line 10', 'Line 11']
    )rr+)rr*rrrr+�sr+cCst|�j|�S)a8    Return the top lines of the file.

    >>> try:
    ...    from StringIO import StringIO
    ... except ImportError:
    ...    from io import StringIO
    >>> f = StringIO()
    >>> for i in range(11):
    ...     _ = f.write('Line %d\n' % (i + 1))
    >>> head(f, 3)
    ['Line 1', 'Line 2', 'Line 3']
    )rr,)rr*rrrr,�sr,g�?cCst|dd�j|�S)a�    Iterator generator that returns lines as data is added to the file.

    >>> import os
    >>> f = open('test_follow.txt', 'w')
    >>> fo = open('test_follow.txt', 'r')
    >>> generator = follow(fo)
    >>> _ = f.write('Line 1\n')
    >>> f.flush()
    >>> next(generator)
    'Line 1'
    >>> _ = f.write('Line 2\n')
    >>> f.flush()
    >>> next(generator)
    'Line 2'
    >>> f.close()
    >>> fo.close()
    >>> os.remove('test_follow.txt')
    r
T)rr2)rr0rrrr2�sr2cCsddl}|j�dS)Nr)�doctestZtestmod)r9rrr�_test�sr:cCs
tt|d��}z�y�|jdkr�|jrn|jrYtjjd�tjd�|j|j�}n|j	|j�}x.|D]}t
|�q�Wn|jr�|j�|jr�x'|jd|j�D]}t
|�q�WWnt
k
r�YnXWd|j�XdS)N�rbrz Cannot follow from top of file.
rr0)r�openr*r,r2�sys�stderr�write�exitr+�printrr/�KeyboardInterruptr4)�filepath�options�tailerr*r1rrr�_main�s(		

	
	
	rFc
Cs_ddlm}ddl}|dd�}|jddddd	d
ddd
d�|jddddd	dddd
d�|jddddd	d
ddd
d�|jddddd	ddddd d
d!�|jd"d#dd$d	d
ddd
d%�|j�\}}|jrt�n=t|�d&ksJ|j�|j	d&�nt
|d|�dS)'Nr)�OptionParser�usagezusage: %prog [options] filenamez-fz--follow�destr2�defaultF�action�
store_true�helpz)output appended data as  the  file  growsz-nz--linesr*r(�type�intz/output the last N lines, instead of the last 10z-tz--topr,zJoutput lines from the top instead of the bottom. Does not work with followz-sz--sleep-intervalr/g�?�metavar�S�floatzCwith  -f,  sleep  for  approximately  S  seconds between iterations�z--test�testzRun some basic testsr)ZoptparserGr=�
add_option�
parse_argsrTr:r�
print_helpr@rF)rGr=�parserrD�argsrrr�mains(!!!'!	

rZ�__main__)r)rr=r.�version_infoZxranger)�objectrr+r,r2r:rFrZr5rrrr�<module>s�