Package org.python.modules._io
Class OpenMode
- java.lang.Object
-
- org.python.modules._io.OpenMode
-
public class OpenMode extends java.lang.ObjectAn 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
Fields Modifier and Type Field Description booleanappendingWhether this file is opened in appending mode ('a')booleanbinaryWhether this file is opened in binary mode ('b')booleaninvalidSet true when any invalid symbol or combination is discoveredjava.lang.StringmessageError message describing the way in which the mode is invalid, or null if no problem has been found.java.lang.StringoriginalModeStringOriginal string supplied as the modebooleanotherWhether the mode contained some other symbol from the allowed onesbooleanreadingWhether this file is opened for reading ('r')booleantextWhether this file is opened in text mode ('t')booleanuniversalWhether this file is opened in universal newlines mode ('U')booleanupdatingWhether this file is opened for updating ('+')booleanwritingWhether this file is opened for writing ('w')
-
Constructor Summary
Constructors Constructor Description OpenMode(java.lang.String mode)Decode the given string to an OpenMode object, checking for duplicate or unrecognised mode letters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcheckValid()Callvalidate()and raise an exception if the mode string is not valid, as signalled by eitherinvalidorotherbeingtrueafter that call.java.lang.StringforFileIO()The mode string we need when constructing aFileIOinitialised with the present mode.java.lang.Stringtext()The mode string that a text file should claim to have, when initialised with the present mode.java.lang.StringtoString()voidvalidate()Adjust and validate the flags decoded from the mode string.voidvalidate(java.lang.String encoding, java.lang.String errors, java.lang.String newline)Perform additional validation of the flags relevant to text files.
-
-
-
Field Detail
-
originalModeString
public final java.lang.String originalModeString
Original string supplied as the mode
-
reading
public boolean reading
Whether this file is opened for reading ('r')
-
writing
public boolean writing
Whether this file is opened for writing ('w')
-
appending
public boolean appending
Whether this file is opened in appending mode ('a')
-
updating
public boolean updating
Whether this file is opened for updating ('+')
-
binary
public boolean binary
Whether this file is opened in binary mode ('b')
-
text
public boolean text
Whether this file is opened in text mode ('t')
-
universal
public boolean universal
Whether this file is opened in universal newlines mode ('U')
-
other
public boolean other
Whether the mode contained some other symbol from the allowed ones
-
invalid
public boolean invalid
Set true when any invalid symbol or combination is discovered
-
message
public java.lang.String 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 Detail
-
OpenMode
public OpenMode(java.lang.String mode)
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 Detail
-
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 theinvalidflag 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 theinvalidflag, and setsmessageto a descriptive message. The point of the qualification "if theinvalidflag 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
public void validate(java.lang.String encoding, java.lang.String errors, java.lang.String newline)Perform additional validation of the flags relevant to text files. Ifinvalidis 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==trueandmessageis 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.openuses.)- Parameters:
encoding- argument toopen()errors- argument toopen()newline- argument toopen()
-
checkValid
public void checkValid() throws PyExceptionCallvalidate()and raise an exception if the mode string is not valid, as signalled by eitherinvalidorotherbeingtrueafter that call. If no more specific message has been assigned inmessage, report the original mode string.- Throws:
PyException-ValueErrorif the mode string was invalid.
-
forFileIO
public java.lang.String forFileIO()
The mode string we need when constructing aFileIOinitialised 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
public java.lang.String 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
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-