Top Description Fields Constructors Methods
org.python.modules._io

public Class OpenMode

extends Object
Class Inheritance
Imports
org.python.core.Py, .PyException

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 TypeField and Description
public boolean
appending

Whether this file is opened in appending mode ('a')

public boolean
binary

Whether this file is opened in binary mode ('b')

public boolean
invalid

Set true when any invalid symbol or combination is discovered

public String
message

Error message describing the way in which the mode is invalid, or null if no problem has been found.

public final String
originalModeString

Original string supplied as the mode

public boolean
other

Whether the mode contained some other symbol from the allowed ones

public boolean
reading

Whether this file is opened for reading ('r')

public boolean
text

Whether this file is opened in text mode ('t')

public boolean
universal

Whether this file is opened in universal newlines mode ('U')

public boolean
updating

Whether this file is opened for updating ('+')

public boolean
writing

Whether this file is opened for writing ('w')

Constructor Summary

AccessConstructor and Description
public
OpenMode(String mode)

Decode the given string to an OpenMode object, checking for duplicate or unrecognised mode letters.

Method Summary

Modifier and TypeMethod and Description
public void
checkValid()

Call validate() and raise an exception if the mode string is not valid, as signalled by either invalid or other being true after that call.

public String

Returns:

"r", "w", or "a" with optional "+".
forFileIO
()

The mode string we need when constructing a FileIO initialised with the present mode.

public String

Returns:

"", or "U".
text
()

The mode string that a text file should claim to have, when initialised with the present mode.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
validate()

Adjust and validate the flags decoded from the mode string.

public void
validate(String
argument to open()
encoding
,
String
argument to open()
errors
,
String
argument to open()
newline
)

Perform additional validation of the flags relevant to text files.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

appendingback to summary
public boolean appending

Whether this file is opened in appending mode ('a')

binaryback to summary
public boolean binary

Whether this file is opened in binary mode ('b')

invalidback to summary
public boolean invalid

Set true when any invalid symbol or combination is discovered

messageback to summary
public 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 the validate() method, or by client code.

originalModeStringback to summary
public final String originalModeString

Original string supplied as the mode

otherback to summary
public boolean other

Whether the mode contained some other symbol from the allowed ones

readingback to summary
public boolean reading

Whether this file is opened for reading ('r')

textback to summary
public boolean text

Whether this file is opened in text mode ('t')

universalback to summary
public boolean universal

Whether this file is opened in universal newlines mode ('U')

updatingback to summary
public boolean updating

Whether this file is opened for updating ('+')

writingback to summary
public boolean writing

Whether this file is opened for writing ('w')

Constructor Detail

OpenModeback to summary
public OpenMode(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 in message. After construction, a client should always call validate() to complete validity checks.

Method Detail

checkValidback to summary
public void checkValid() throws PyException

Call validate() and raise an exception if the mode string is not valid, as signalled by either invalid or other being true after that call. If no more specific message has been assigned in message, report the original mode string.

Exceptions
PyException:
ValueError if the mode string was invalid.
forFileIOback to summary
public String forFileIO()

The mode string we need when constructing a FileIO 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:String

"r", "w", or "a" with optional "+".

textback to summary
public 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:String

"", or "U".

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override

validateback to summary
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 the invalid flag is not already true, it checks the validity of the flags against combinations allowed by the Python io.open() function. In the case of a violation, it sets the invalid flag, and sets message to a descriptive message. The point of the qualification "if the invalid flag is not already true" 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 when checkValid() 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)
See also validate(String, String, String) for additional checks relevant to text files.
validateback to summary
public void validate(String encoding, String errors, String newline)

Perform additional validation of the flags relevant to text files. If invalid is not already true, and the mode includes binary, then all the arguments to this call must be null. If the criterion is not met, then on return from the method, invalid==true and message 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 CPython io.open uses.)

Parameters
encoding:String

argument to open()

errors:String

argument to open()

newline:String

argument to open()