text_log.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 # -*- coding: utf-8 -*-
00004 
00005 '''rtshell
00006 
00007 Copyright (C) 2009-2014
00008     Geoffrey Biggs
00009     RT-Synthesis Research Group
00010     Intelligent Systems Research Institute,
00011     National Institute of Advanced Industrial Science and Technology (AIST),
00012     Japan
00013     All rights reserved.
00014 Licensed under the Eclipse Public License -v 1.0 (EPL)
00015 http://www.opensource.org/licenses/eclipse-1.0.txt
00016 
00017 Text-based log.
00018 
00019 '''
00020 
00021 
00022 import copy
00023 import os
00024 
00025 import ilog
00026 
00027 
00028 ###############################################################################
00029 ## Text-based log object. It only supports writing log files. It relies on
00030 ## the data types having suitable __repr__ methods.
00031 
00032 class TextLog(ilog.Log):
00033     def __init__(self, filename='', *args, **kwargs):
00034         self._is_open = False
00035         self._fn = filename
00036         super(TextLog, self).__init__(*args, **kwargs)
00037 
00038     def __str__(self):
00039         if self._is_open:
00040             return 'TextLog({0}, {1}) at position {2}.'.format(self._fn,
00041                     self._mode, self._file.tell())
00042         else:
00043             return 'TextLog({0}, {1}).'.format(self._fn, self._mode)
00044 
00045     def write(self, timestamp, data):
00046         pos = self._file.tell()
00047         self._file.write('{0}\t{1}\n'.format(timestamp, data))
00048         self._vb_print('Wrote entry at {0}.'.format(pos))
00049 
00050     def _close(self):
00051         if not self._is_open:
00052             return
00053         self._file.close()
00054         self._is_open = False
00055         self._vb_print('Closed file.')
00056 
00057     def _get_cur_pos(self):
00058         if self._is_open:
00059             self._vb_print('Current position: {0}'.format(self._file.tell()))
00060             return self._file.tell()
00061         else:
00062             self._vb_print('Current position: file closed.')
00063             return 0
00064 
00065     def _open(self):
00066         if self._is_open:
00067             return
00068         if self._mode == 'w':
00069             flags = 'w'
00070         else:
00071             raise NotImplementedError
00072         self._file = open(self._fn, flags)
00073         self._is_open = True
00074         self._vb_print('Opened file {0} in mode {1}.'.format(self._fn,
00075             self._mode))
00076 


rtshell
Author(s): Geoffrey Biggs
autogenerated on Fri Aug 28 2015 12:55:12