Package org.python.modules._io
Class OpenMode
java.lang.Object
org.python.modules._io.OpenMode
An object able to check a file access mode provided as a String and represent it as boolean
attributes and in a normalised form. Such a string is the the mode argument of the several open()
functions available in Python and certain constructors for streams-like objects.
-
Field Summary
Modifier and TypeFieldDescriptionboolean
Whether this file is opened in appending mode ('a')boolean
Whether this file is opened in binary mode ('b')boolean
Set true when any invalid symbol or combination is discoveredError message describing the way in which the mode is invalid, or null if no problem has been found.final String
Original string supplied as the modeboolean
Whether the mode contained some other symbol from the allowed onesboolean
Whether this file is opened for reading ('r')boolean
Whether this file is opened in text mode ('t')boolean
Whether this file is opened in universal newlines mode ('U')boolean
Whether this file is opened for updating ('+')boolean
Whether this file is opened for writing ('w') -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Callvalidate()
and raise an exception if the mode string is not valid, as signalled by eitherinvalid
orother
beingtrue
after that call.The mode string we need when constructing aFileIO
initialised with the present mode.text()
The mode string that a text file should claim to have, when initialised with the present mode.toString()
void
validate()
Adjust and validate the flags decoded from the mode string.void
Perform additional validation of the flags relevant to text files.
-
Field Details
-
originalModeString
Original string supplied as the mode -
reading
public boolean readingWhether this file is opened for reading ('r') -
writing
public boolean writingWhether this file is opened for writing ('w') -
appending
public boolean appendingWhether this file is opened in appending mode ('a') -
updating
public boolean updatingWhether this file is opened for updating ('+') -
binary
public boolean binaryWhether this file is opened in binary mode ('b') -
text
public boolean textWhether this file is opened in text mode ('t') -
universal
public boolean universalWhether this file is opened in universal newlines mode ('U') -
other
public boolean otherWhether the mode contained some other symbol from the allowed ones -
invalid
public boolean invalidSet true when any invalid symbol or combination is discovered -
message
Error message describing the way in which the mode is invalid, or null if no problem has been found. This field may be set by the constructor (in the case of duplicate or unrecognised mode letters), by thevalidate()
method, or by client code.
-
-
Constructor Details
-
OpenMode
Decode the given string to an OpenMode object, checking for duplicate or unrecognised mode letters. Valid letters are those in "rwa+btU". Errors in the mode string do not raise an exception, they simply generate an appropriate error message inmessage
. After construction, a client should always callvalidate()
to complete validity checks.- Parameters:
mode
-
-
-
Method Details
-
validate
public void validate()Adjust and validate the flags decoded from the mode string. The method affects the flags where the presence of one flag implies another, then if theinvalid
flag is not alreadytrue
, it checks the validity of the flags against combinations allowed by the Pythonio.open()
function. In the case of a violation, it sets theinvalid
flag, and setsmessage
to a descriptive message. The point of the qualification "if theinvalid
flag is not alreadytrue
" is that the message should always describe the first problem discovered. If left blank, as in fact the constructor does, it will be filled by the generic message whencheckValid()
is finally called. Clients may override this method (by sub-classing) to express the validation correct in their context.The invalid combinations enforced here are those for the "raw" (ie non-text) file types:
universal & (writing | appending)
,text & binary
,reading & writing
,appending & (reading | writing)
validate(String, String, String)
for additional checks relevant to text files. -
validate
Perform additional validation of the flags relevant to text files. Ifinvalid
is not alreadytrue
, and the mode includesbinary
, then all the arguments to this call must benull
. If the criterion is not met, then on return from the method,invalid==true
andmessage
is set to a standard error message. This is the standard additional validation applicable to text files. (By "standard" we mean the test and messages that CPythonio.open
uses.)- Parameters:
encoding
- argument toopen()
errors
- argument toopen()
newline
- argument toopen()
-
checkValid
Callvalidate()
and raise an exception if the mode string is not valid, as signalled by eitherinvalid
orother
beingtrue
after that call. If no more specific message has been assigned inmessage
, report the original mode string.- Throws:
PyException
-ValueError
if the mode string was invalid.
-
forFileIO
The mode string we need when constructing aFileIO
initialised with the present mode. Note that this is not the same as the full open mode because it omits the text-based attributes.- Returns:
- "r", "w", or "a" with optional "+".
-
text
The mode string that a text file should claim to have, when initialised with the present mode. Note that this only contains text-based attributes. Since mode 't' has no effect, except to produce an error if specified with 'b', we don't reproduce it.- Returns:
- "", or "U".
-
toString
-