/ lib / distutils / text_file.pyc
text_file.pyc
  1  o

  2  4�Hc�1�@s&dZddlZddlZGdd�d�ZdS)z�text_file
  3  
  4  provides the TextFile class, which gives an interface to text files
  5  that (optionally) takes care of stripping comments, ignoring blank
  6  lines, and joining lines with backslashes.�Nc@steZdZdZdddddddd�Zddd�Zd	d
  7  �Zdd�Zdd
d�Zddd�Z	ddd�Z
  8  dd�Zdd�Zdd�Z
dS)�TextFilea�Provides a file-like object that takes care of all the things you
  9         commonly want to do when processing a text file that has some
 10         line-by-line syntax: strip comments (as long as "#" is your
 11         comment character), skip blank lines, join adjacent lines by
 12         escaping the newline (ie. backslash at end of line), strip
 13         leading and/or trailing whitespace.  All of these are optional
 14         and independently controllable.
 15  
 16         Provides a 'warn()' method so you can generate warning messages that
 17         report physical line number, even if the logical line in question
 18         spans multiple physical lines.  Also provides 'unreadline()' for
 19         implementing line-at-a-time lookahead.
 20  
 21         Constructor is called as:
 22  
 23             TextFile (filename=None, file=None, **options)
 24  
 25         It bombs (RuntimeError) if both 'filename' and 'file' are None;
 26         'filename' should be a string, and 'file' a file object (or
 27         something that provides 'readline()' and 'close()' methods).  It is
 28         recommended that you supply at least 'filename', so that TextFile
 29         can include it in warning messages.  If 'file' is not supplied,
 30         TextFile creates its own using 'io.open()'.
 31  
 32         The options are all boolean, and affect the value returned by
 33         'readline()':
 34           strip_comments [default: true]
 35             strip from "#" to end-of-line, as well as any whitespace
 36             leading up to the "#" -- unless it is escaped by a backslash
 37           lstrip_ws [default: false]
 38             strip leading whitespace from each line before returning it
 39           rstrip_ws [default: true]
 40             strip trailing whitespace (including line terminator!) from
 41             each line before returning it
 42           skip_blanks [default: true}
 43             skip lines that are empty *after* stripping comments and
 44             whitespace.  (If both lstrip_ws and rstrip_ws are false,
 45             then some lines may consist of solely whitespace: these will
 46             *not* be skipped, even if 'skip_blanks' is true.)
 47           join_lines [default: false]
 48             if a backslash is the last non-newline character on a line
 49             after stripping comments and whitespace, join the following line
 50             to it to form one "logical line"; if N consecutive lines end
 51             with a backslash, then N+1 physical lines will be joined to
 52             form one logical line.
 53           collapse_join [default: false]
 54             strip leading whitespace from lines that are joined to their
 55             predecessor; only matters if (join_lines and not lstrip_ws)
 56           errors [default: 'strict']
 57             error handler used to decode the file content
 58  
 59         Note that since 'rstrip_ws' can strip the trailing newline, the
 60         semantics of 'readline()' must differ from those of the builtin file
 61         object's 'readline()' method!  In particular, 'readline()' returns
 62         None for end-of-file: an empty string might just be a blank line (or
 63         an all-whitespace line), if 'rstrip_ws' is true but 'skip_blanks' is
 64         not.�r�strict)�strip_comments�skip_blanks�	lstrip_ws�	rstrip_ws�
 65  join_lines�
collapse_join�errorsNcKs�|dur|durtd��|j��D]}||vr t||||�qt|||j|�q|��D]
}||jvr;td|��q.|durF|�|�n	||_||_d|_g|_	dS)z�Construct a new TextFile object.  At least one of 'filename'
 66             (a string) and 'file' (a file-like object) must be supplied.
 67             They keyword argument options are described above and affect
 68             the values returned by 'readline()'.Nz7you must supply either or both of 'filename' and 'file'zinvalid TextFile option '%s'r)
 69  �RuntimeError�default_options�keys�setattr�KeyError�open�filename�file�current_line�linebuf)�selfrr�options�opt�r�{C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\distutils\text_file.py�__init__Ns 
 70  �
 71  zTextFile.__init__cCs&||_tj|jd|jd�|_d|_dS)zyOpen a new file named 'filename'.  This overrides both the
 72             'filename' and 'file' arguments to the constructor.�r)rrN)r�iorrrr)rrrrrros
 73  z
TextFile.opencCs$|j}d|_d|_d|_|��dS)ziClose the current file and forget everything we know about it
 74             (filename, current line number).N)rrr�close)rrrrrrvs
 75  zTextFile.closecCsjg}|dur	|j}|�|jd�t|ttf�r"|�dt|��n|�d|�|�t|��d�|�S)Nz, z
lines %d-%d: z	line %d: �)r�appendr�
 76  isinstance�list�tuple�str�join)r�msg�line�outmsgrrr�	gen_errors
 77  zTextFile.gen_errorcCstd|�||���)Nzerror: )�
 78  ValueErrorr)�rr&r'rrr�error�szTextFile.errorcCs tj�d|�||�d�dS)a�Print (to stderr) a warning message tied to the current logical
 79             line in the current file.  If the current logical line in the
 80             file spans multiple physical lines, the warning refers to the
 81             whole range, eg. "lines 3-5".  If 'line' supplied, it overrides
 82             the current line number; it may be a list or tuple to indicate a
 83             range of physical lines, or an integer for a single physical
 84             line.z	warning: �
 85  N)�sys�stderr�writer)r+rrr�warn�s z
TextFile.warncCs�|jr|jd}|jd=|Sd}	|j��}|dkrd}|jrW|rW|�d�}|dkr+n,|dks7||ddkrQ|dd	kr?d	p@d}|d|�|}|��dkrPqn|�d
 86  d�}|jr�|r�|durg|�d�|S|j	rn|�
 87  �}||}t|jt
�r�|jdd|jd<n%|j|jdg|_n|dur�dSt|jt
�r�|jdd|_n|jd|_|jr�|jr�|��}n|jr�|�
 88  �}n|jr�|��}|dks�|d	kr�|jr�q|jr�|ddkr�|dd�}q|dd�d
kr�|dd�d	}q|S)aURead and return a single logical line from the current file (or
 89             from an internal buffer if lines have previously been "unread"
 90             with 'unreadline()').  If the 'join_lines' option is true, this
 91             may involve reading multiple physical lines concatenated into a
 92             single string.  Updates the current line number, so calling
 93             'warn()' after 'readline()' emits a warning about the physical
 94             line(s) just read.  Returns None on end-of-file, since the empty
 95             string can occur if 'rstrip_ws' is true but 'strip_blanks' is
 96             not.�����rTN�#rr�\r-z\#z2continuation line immediately precedes end-of-file�����z\
 97  )rr�readliner�find�strip�replacer	r1r
 98  �lstripr!rr"rr�rstripr)rr'�buildup_line�pos�eolrrrr6�sj
 99  
100  
101  
102  
103  	�
104  
105  �
106  
107  zTextFile.readlinecCs&g}	|��}|dur
|S|�|�q)zWRead and return the list of all logical lines remaining in the
108             current file.TN)r6r )r�linesr'rrr�	readliness
109  �zTextFile.readlinescCs|j�|�dS)z�Push 'line' (a string) onto an internal buffer that will be
110             checked by future 'readline()' calls.  Handy for implementing
111             a parser with line-at-a-time lookahead.N)rr )rr'rrr�
112  unreadlineszTextFile.unreadline)NN)N)�__name__�
113  __module__�__qualname__�__doc__r
rrrr)r,r1r6r@rArrrrr
114  s&:�
115  	!
116  	
117  
118  
119  x
120  r)rEr.rrrrrr�<module>s