from __future__ import generators

import fiatio


class utterances:
	DB = 'DB.fiat'
	D = '.'

	def __init__(self, db=None):
		if db is None:
			db = '%s/%s' % (self.D, self.DB)
		self.header, self.db, self.comments = fiatio.read(open(db, "r"))

	def __iter__(self):
		for t in self.db:
			yield t



class SillyLabelSequence(RuntimeError):
	def __init__(self, x):
		RuntimeError.__init__(self, x)


class entity:
	def __init__(self, utt_info, t0=None, tf=None):
		self.t0 = t0
		self.tf = tf
		if t0 is not None and tf is not None:
			assert t0 <= tf, "Times out of order: %.3f %.3f" % (t0, tf)
		self._info = utt_info


	def info(self):
		d = self._info.copy()
		d['t0'] = self.t0
		d['tf'] = self.tf
		d['phid'] = '%s:%.3f' % (d['f'], 0.5*(self.t0+self.tf))
		return d



class entity_list:
	def __init__(self):
		self.contents = []


	def __iter__(self):
		for p in self.contents:
			yield p


	def __len__(self):
		return len(self.contents)


	def append(self, x):
		self.contents.append(x)

	def extend(self, x):
		self.contents.extend(x)
