Commit a7de5f2c authored by Javier Quinteros's avatar Javier Quinteros
Browse files

Split functionality between __init__, __enter__ and __exit__.

parent 0623123c
......@@ -23,28 +23,31 @@ import datetime
class TDMS(object):
# def __exit__(self, exc_type, exc_val, exc_tb):
# if self.__fi is not None:
# self.__fi.close()
def __exit__(self, exc_type, exc_val, exc_tb):
if self.__fi is not None:
self.__fi.close()
def __init__(self, filename, iterate='D', loglevel='INFO'):
logs = logging.getLogger('OpenFile')
logs = logging.getLogger('Init TDMS')
logs.setLevel(loglevel)
# Log level
self.__loglevel = loglevel
# Name of file
self.__filename = filename
# What should we iterate? D: Data; M: Metadata
self.iterate = iterate
# Datatype of each channel
self.__datatypes = dict()
# Dictionary to save the metadata defined in the file
self.metadata = dict()
# Initialization of local variables
self.__HEADERLEN = 28
kTocMetaData = 1 << 1
kTocNewObjList = 1 << 2
kTocRawData = 1 << 3
kTocInterleavedData = 1 << 5
kTocBigEndian = 1 << 6
kTocDAQmxRawData = 1 << 7
self.__FF64b = 0xFFFFFFFFFFFFFFFF
self.__FF32b = 0xFFFFFFFF
......@@ -69,11 +72,22 @@ class TDMS(object):
# tdsTypeComplexDoubleFloat = 0x10000d,
# tdsTypeDAQmxRawData = 0xFFFFFFFF
self.__fi = open(filename, 'rb')
def __enter__(self):
logs = logging.getLogger('Open file')
logs.setLevel(self.__loglevel)
self.__fi = open(self.__filename, 'rb')
leadin = self.__fi.read(self.__HEADERLEN)
(tag, ToCmask) = struct.unpack('<4si', leadin[:8])
kTocMetaData = 1 << 1
kTocNewObjList = 1 << 2
kTocRawData = 1 << 3
kTocInterleavedData = 1 << 5
kTocBigEndian = 1 << 6
kTocDAQmxRawData = 1 << 7
self.hasmetadata = bool(ToCmask & kTocMetaData)
self.hasnewObjects = bool(ToCmask & kTocNewObjList)
self.hasrawData = bool(ToCmask & kTocRawData)
......@@ -114,9 +128,8 @@ class TDMS(object):
logs.info('BigEndian: ' + ('yes' if self.__endian == '<' else 'no'))
logs.info('DAQmx raw data: ' + ('yes' if self.hasDAQmxRawData else 'no'))
# Dictionary to save the metadata defined in the file
self.metadata = dict()
self.readMetadata()
return self
def readMetadata(self):
# Metadata
......@@ -194,14 +207,12 @@ class TDMS(object):
result = self.__readdata(self.__data2mask[datatype][0], self.__data2mask[datatype][1], samples,
numChannels, channel=ch)
def __iter_metadata__(self):
# Metadata
logs = logging.getLogger('Iterate Metadata')
for ch in self.metadata:
yield self.metadata[ch]
def __readstring(self):
# logs = logging.getLogger('readstring')
strlen = struct.unpack('%cI' % self.__endian, self.__fi.read(4))
......@@ -299,8 +310,9 @@ def main():
logs.setLevel(args.loglevel)
td = TDMS(args.filename, iterate='M')
for ch in td:
print(ch)
with td:
for ch in td:
print(ch)
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment