mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-24 09:20:53 +07:00
upgrade to Jython 2.5.0
This commit is contained in:
@@ -28,8 +28,10 @@ numbers are zero; nextfile() has no effect. After all lines have been
|
||||
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,14 +74,12 @@ buffer size.
|
||||
XXX Possible additions:
|
||||
|
||||
- optional getopt argument processing
|
||||
- specify open mode ('r' or 'rb')
|
||||
- fileno()
|
||||
- isatty()
|
||||
- read(), read(size), even readlines()
|
||||
|
||||
"""
|
||||
|
||||
import sys, os, stat
|
||||
import sys, os
|
||||
|
||||
__all__ = ["input","close","nextfile","filename","lineno","filelineno",
|
||||
"isfirstline","isstdin","FileInput"]
|
||||
@@ -88,8 +88,9 @@ _state = None
|
||||
|
||||
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 @@ def input(files=None, inplace=0, backup="", bufsize=0):
|
||||
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 @@ def filelineno():
|
||||
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,19 +182,21 @@ def isstdin():
|
||||
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):
|
||||
if type(files) == type(''):
|
||||
def __init__(self, files=None, inplace=0, backup="", bufsize=0,
|
||||
mode="r", openhook=None):
|
||||
if isinstance(files, basestring):
|
||||
files = (files,)
|
||||
else:
|
||||
if files is None:
|
||||
@@ -203,10 +215,20 @@ class FileInput:
|
||||
self._lineno = 0
|
||||
self._filelineno = 0
|
||||
self._file = None
|
||||
self._isstdin = 0
|
||||
self._isstdin = False
|
||||
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()
|
||||
@@ -215,7 +237,10 @@ class FileInput:
|
||||
self.nextfile()
|
||||
self._files = ()
|
||||
|
||||
def __getitem__(self, i):
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def next(self):
|
||||
try:
|
||||
line = self._buffer[self._bufindex]
|
||||
except IndexError:
|
||||
@@ -225,13 +250,19 @@ class FileInput:
|
||||
self._lineno += 1
|
||||
self._filelineno += 1
|
||||
return line
|
||||
if i != self._lineno:
|
||||
raise RuntimeError, "accessing lines out of order"
|
||||
line = self.readline()
|
||||
if not line:
|
||||
raise IndexError, "end of input reached"
|
||||
raise StopIteration
|
||||
return line
|
||||
|
||||
def __getitem__(self, i):
|
||||
if i != self._lineno:
|
||||
raise RuntimeError, "accessing lines out of order"
|
||||
try:
|
||||
return self.next()
|
||||
except StopIteration:
|
||||
raise IndexError, "end of input reached"
|
||||
|
||||
def nextfile(self):
|
||||
savestdout = self._savestdout
|
||||
self._savestdout = 0
|
||||
@@ -252,9 +283,9 @@ class FileInput:
|
||||
self._backupfilename = 0
|
||||
if backupfilename and not self._backup:
|
||||
try: os.unlink(backupfilename)
|
||||
except: pass
|
||||
except OSError: pass
|
||||
|
||||
self._isstdin = 0
|
||||
self._isstdin = False
|
||||
self._buffer = []
|
||||
self._bufindex = 0
|
||||
|
||||
@@ -275,12 +306,12 @@ class FileInput:
|
||||
self._files = self._files[1:]
|
||||
self._filelineno = 0
|
||||
self._file = None
|
||||
self._isstdin = 0
|
||||
self._isstdin = False
|
||||
self._backupfilename = 0
|
||||
if self._filename == '-':
|
||||
self._filename = '<stdin>'
|
||||
self._file = sys.stdin
|
||||
self._isstdin = 1
|
||||
self._isstdin = True
|
||||
else:
|
||||
if self._inplace:
|
||||
self._backupfilename = (
|
||||
@@ -289,10 +320,12 @@ class FileInput:
|
||||
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())[stat.ST_MODE]
|
||||
except:
|
||||
perm = os.fstat(self._file.fileno()).st_mode
|
||||
except (AttributeError, OSError):
|
||||
# AttributeError occurs in Jython, where there's no
|
||||
# os.fstat.
|
||||
self._output = open(self._filename, "w")
|
||||
else:
|
||||
fd = os.open(self._filename,
|
||||
@@ -300,14 +333,18 @@ class FileInput:
|
||||
perm)
|
||||
self._output = os.fdopen(fd, "w")
|
||||
try:
|
||||
os.chmod(self._filename, perm)
|
||||
except:
|
||||
if hasattr(os, 'chmod'):
|
||||
os.chmod(self._filename, perm)
|
||||
except OSError:
|
||||
pass
|
||||
self._savestdout = sys.stdout
|
||||
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:
|
||||
@@ -324,12 +361,41 @@ class FileInput:
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user