[Pymoul-svn] SF.net SVN: pymoul: [288] binaryfile/trunk/src/binaryfile/binary.py
Status: Alpha
Brought to you by:
tiran
From: <ti...@us...> - 2007-04-28 13:27:30
|
Revision: 288 http://pymoul.svn.sourceforge.net/pymoul/?rev=288&view=rev Author: tiran Date: 2007-04-28 06:27:31 -0700 (Sat, 28 Apr 2007) Log Message: ----------- Changed binary file class from file subclass to proxy class Modified Paths: -------------- binaryfile/trunk/src/binaryfile/binary.py Modified: binaryfile/trunk/src/binaryfile/binary.py =================================================================== --- binaryfile/trunk/src/binaryfile/binary.py 2007-04-28 13:25:46 UTC (rev 287) +++ binaryfile/trunk/src/binaryfile/binary.py 2007-04-28 13:27:31 UTC (rev 288) @@ -37,12 +37,14 @@ from binaryfile.binaryrecord import parseRecord from binaryfile.binaryrecord import registerRecord +_marker = object() -class BinaryFile(file): - """Binary file - - A file based class with additional methods to read and write binary data. - +class BinaryFile(object): + """Binary file proxy + + A file wrapper for binary files with additional methods to read and write + binary data. + The class supports reading and writing: - char: readChar / writeChar - byte: readByte / writeByte @@ -69,12 +71,28 @@ The class is using some optimization tricks like binding functions to the local namespace of a method. """ - def __new__(cls, fname, mode='rb'): + NULL = '\x00' + _methods = ('read', 'write', 'seek', 'tell') + + def __init__(self, fdname, mode='rb', buffer=4096): assert 'b' in mode - self = file.__new__(cls, fname, mode) - self.NULL = '\x00' - return self + if isinstance(fdname, basestring): + fd = open(fdname, mode, buffer) + else: + for name in self._methods: + if not hasattr(fdname, name): + raise TypeError("%s is missing %s" % (type(fdname), name)) + fd = fdname + self._fd = fd + for name in self._methods: + setattr(self, name, getattr(fd, name)) + def __getattr__(self, name, default=_marker): + result = getattr(self._fd, name, _marker) + if result is _marker: + raise AttributeError(name) + return result + def size(self): pos = self.tell() try: @@ -234,7 +252,7 @@ def set(self, s): """Replace current data with s """ - self._data = s + self._data = s def __repr__(self): """repr(self) @@ -300,4 +318,3 @@ else: fd.write16(size) fd.write(self._data) - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |