From: <pj...@us...> - 2008-06-27 04:27:34
|
Revision: 4800 http://jython.svn.sourceforge.net/jython/?rev=4800&view=rev Author: pjenvey Date: 2008-06-26 21:27:32 -0700 (Thu, 26 Jun 2008) Log Message: ----------- from: http://svn.python.org/projects/python/branches/release25-maint/Lib/fileinput.py@42507 Modified Paths: -------------- branches/asm/Lib/fileinput.py Modified: branches/asm/Lib/fileinput.py =================================================================== --- branches/asm/Lib/fileinput.py 2008-06-27 04:17:33 UTC (rev 4799) +++ branches/asm/Lib/fileinput.py 2008-06-27 04:27:32 UTC (rev 4800) @@ -28,8 +28,10 @@ read, filename() and the line number functions return the values pertaining to the last line read; nextfile() has no effect. -All files are opened in text mode. If an I/O error occurs during -opening or reading a file, the IOError exception is raised. +All files are opened in text mode by default, you can override this by +setting the mode parameter to input() or FileInput.__init__(). +If an I/O error occurs during opening or reading a file, the IOError +exception is raised. If sys.stdin is used more than once, the second and further use will return no lines, except perhaps for interactive use, or if it has been @@ -72,8 +74,6 @@ XXX Possible additions: - optional getopt argument processing -- specify open mode ('r' or 'rb') -- fileno() - isatty() - read(), read(size), even readlines() @@ -88,8 +88,9 @@ DEFAULT_BUFSIZE = 8*1024 -def input(files=None, inplace=0, backup="", bufsize=0): - """input([files[, inplace[, backup]]]) +def input(files=None, inplace=0, backup="", bufsize=0, + mode="r", openhook=None): + """input([files[, inplace[, backup[, mode[, openhook]]]]]) Create an instance of the FileInput class. The instance will be used as global state for the functions of this module, and is also returned @@ -99,7 +100,7 @@ global _state if _state and _state._file: raise RuntimeError, "input() already active" - _state = FileInput(files, inplace, backup, bufsize) + _state = FileInput(files, inplace, backup, bufsize, mode, openhook) return _state def close(): @@ -153,6 +154,15 @@ raise RuntimeError, "no active input()" return _state.filelineno() +def fileno(): + """ + Return the file number of the current file. When no file is currently + opened, returns -1. + """ + if not _state: + raise RuntimeError, "no active input()" + return _state.fileno() + def isfirstline(): """ Returns true the line just read is the first line of its file, @@ -172,18 +182,20 @@ return _state.isstdin() class FileInput: - """class FileInput([files[, inplace[, backup]]]) + """class FileInput([files[, inplace[, backup[, mode[, openhook]]]]]) Class FileInput is the implementation of the module; its methods - filename(), lineno(), fileline(), isfirstline(), isstdin(), nextfile() - and close() correspond to the functions of the same name in the module. + filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(), + nextfile() and close() correspond to the functions of the same name + in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the sequence behavior. The sequence must be accessed in strictly sequential order; random access and readline() cannot be mixed. """ - def __init__(self, files=None, inplace=0, backup="", bufsize=0): + def __init__(self, files=None, inplace=0, backup="", bufsize=0, + mode="r", openhook=None): if isinstance(files, basestring): files = (files,) else: @@ -207,6 +219,16 @@ self._backupfilename = None self._buffer = [] self._bufindex = 0 + # restrict mode argument to reading modes + if mode not in ('r', 'rU', 'U', 'rb'): + raise ValueError("FileInput opening mode must be one of " + "'r', 'rU', 'U' and 'rb'") + self._mode = mode + if inplace and openhook: + raise ValueError("FileInput cannot use an opening hook in inplace mode") + elif openhook and not callable(openhook): + raise ValueError("FileInput openhook must be callable") + self._openhook = openhook def __del__(self): self.close() @@ -298,12 +320,10 @@ except os.error: pass # The next few lines may raise IOError os.rename(self._filename, self._backupfilename) - self._file = open(self._backupfilename, "r") + self._file = open(self._backupfilename, self._mode) try: perm = os.fstat(self._file.fileno()).st_mode - except (AttributeError, OSError): - # AttributeError occurs in Jython, where there's no - # os.fstat. + except OSError: self._output = open(self._filename, "w") else: fd = os.open(self._filename, @@ -319,7 +339,10 @@ sys.stdout = self._output else: # This may raise IOError - self._file = open(self._filename, "r") + if self._openhook: + self._file = self._openhook(self._filename, self._mode) + else: + self._file = open(self._filename, self._mode) self._buffer = self._file.readlines(self._bufsize) self._bufindex = 0 if not self._buffer: @@ -336,12 +359,41 @@ def filelineno(self): return self._filelineno + def fileno(self): + if self._file: + try: + return self._file.fileno() + except ValueError: + return -1 + else: + return -1 + def isfirstline(self): return self._filelineno == 1 def isstdin(self): return self._isstdin + +def hook_compressed(filename, mode): + ext = os.path.splitext(filename)[1] + if ext == '.gz': + import gzip + return gzip.open(filename, mode) + elif ext == '.bz2': + import bz2 + return bz2.BZ2File(filename, mode) + else: + return open(filename, mode) + + +def hook_encoded(encoding): + import codecs + def openhook(filename, mode): + return codecs.open(filename, mode, encoding) + return openhook + + def _test(): import getopt inplace = 0 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |