Source code for nxpy.core.file_object

# nxpy.core package ----------------------------------------------------------

# Copyright Nicola Musatti 2008 - 2014
# Use, modification, and distribution are subject to the Boost Software
# License, Version 1.0. (See accompanying file LICENSE.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

# See http://nxpy.sourceforge.net for library home page. ---------------------

r"""
Helper classes for the implementation of read-only and writable file objects that forward calls
to an actual file object variable.

These smell a lot of statically typed languages and are likely to be removed or changed a lot in a
future release.

"""

from __future__ import absolute_import

import os


[docs]class ReadOnlyFileObject(object): r""" Implement the non modifying portion of the file object protocol by delegating to another file object. Subclass and override as needed. """
[docs] def __init__(self, file_=None): r"""Set the delegate file object.""" self.setFile(file_)
@property def closed(self): return self._file.closed @property def encoding(self): return self._file.encoding @property def mode(self): return self._file.mode @property def name(self): return self._file.name @property def newlines(self): return self._file.newlines @property def softspace(self): return self._file.softspace
[docs] def setFile(self, file_): r"""Set the delegate file object.""" self._file = file_
[docs] def close(self): self._file.close()
[docs] def flush(self): self._file.flush()
[docs] def __iter__(self): return self
[docs] def next(self): return next(self._file)
[docs] def read(self, size=-1): return self._file.read(size)
[docs] def readline(self, size=-1): return self._file.readline(size)
[docs] def readlines(self, sizehint=None): if sizehint is None: return self._file.readlines() else: return self._file.readlines(sizehint)
[docs] def xreadlines(self): return self._file
[docs] def seek(self, offset, whence=os.SEEK_SET): return self._file.seek(offset, whence)
[docs] def tell(self): return self._file.tell()
[docs]class WritableFileObject(ReadOnlyFileObject): r""" Implement the file object protocol by delegating to another file object. Subclass and override as needed. """
[docs] def __init__(self, file_=None): super(WritableFileObject, self).__init__(file_)
[docs] def truncate(self, size=None): if size is None: self._file.truncate() else: self._file.truncate(size)
[docs] def write(self, str_): self._file.write(str_)
[docs] def writelines(self, sequence): self._file.writelines(sequence)