Handy Utilities and Options

RSFile Utilities

rsfile.read_from_file(filename, binary=False, buffering=None, encoding=None, errors=None, newline=None, locking=True, timeout=None)

Returns the whole content of the file filename, as a binary or unicode string depending on the boolean binary.

Other arguments are similar to those of rsfile.rsopen().

Don’t use on huge files, of course.

This function may raise EnvironmentError exceptions.

rsfile.write_to_file(filename, data, sync=False, must_create=False, must_not_create=False, buffering=None, encoding=None, errors=None, newline=None, locking=True, timeout=None)

Write the binary or unicode string data to the file filename.

Other arguments are similar to those of rsfile.rsopen().

This function may raise EnvironmentError exceptions.

rsfile.append_to_file(filename, data, sync=False, must_not_create=False, buffering=None, encoding=None, errors=None, newline=None, locking=True, timeout=None)

Append the binary or unicode string data to the file filename.

Other arguments are similar to those of rsfile.rsopen().

This function may raise EnvironmentError exceptions.

RSFile Options

rsfile.set_rsfile_options(**options)

Sets process-global options for rsfile, according to keyword arguments provided.

Warning

These options shall not be set by libraries, only from main scripts, since they affect the behaviour of rsfile in the whole program.

The following keyword arguments are currently available:

  • enforced_locking_timeout_value (None or positive float, defaults to None): if set, this value is enforced as a timeout value (in seconds) for all blocking rsfile.lock_file() calls (i.e those having a timeout argument set to None). If that timeout is reached, a RuntimeError is raised. This exception is not meant to be caught, as it’s normally the sign that a deadlock is occurring around the locking of that file. This feature is for debugging purpose only.

  • default_spinlock_delay (positive float, defaults to 0.1): this value represents the sleeping time between two attempts at locking a file, when using rsfile.lock_file() with a non-zero timeout argument. Modify this value with care, as some libraries might expect a sufficient reactivity for file locking operations.

rsfile.get_rsfile_options()

Returns a dictionary with all rsfile options as key/value entries.

RSFile Stdlib Patching

These monkey-patchers are used massively to test rsfile against the stdlib test suite.

rsfile.monkey_patch_io_module(module=None)

Replaces standard file streams of module module (i.e classes FileIO, BufferedReader, BufferedWriter, BufferedRandom, and TextIOWrapper), as well as its open() factory, by RSFile versions with compatible signatures.

By default module is the standard io module, but you may provide _pyio (the stdlib pure python version), _io (the C extension module behind io) instead.

rsfile.monkey_patch_open_builtin()

Replaces the default open() builtin with a version compatible in signature and semantic (no file locking or thread safety on stream opening), which returns rsfile streams on invocation.