# -*- coding: latin-1 -*-
""" 
   Copyright (C) 1999-2005 PimenTech SARL (http://www.pimentech.net)

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  
"""

__version__ = '0.1'

from Globals import HTMLFile

from OFS.SimpleItem import *

# File inheritence : can we find better ?
class ZPipe(SimpleItem):
	" ZPipe (persistante fifo) Object "

	meta_type = "ZPipe"

	manage_options = ({'label' : 'View FIFO', 'action' : 'html_view_zpipe'}, ) + SimpleItem.manage_options

	html_view_zpipe = HTMLFile('dtml/view_zpipe', globals())

	def __init__(self):
		self.fifo = []
		self.title = 'Pipe'

	def add_line(self, line):
		" Add a line in fifo "
		if type(line) == type(''):
			spl = line.split('\n')
			# the line is only one line
			if len(spl) > 2 or (len(spl) == 2 and spl[1] != ''):
				return 0
			self.fifo.append(spl[0])
			self._p_changed = 1
			return 1
		return 0

	def get_line(self):
		" Return the first line of fifo and remove it "
		if len(self.fifo)==0:
			return ''
		self._p_changed = 1
		return self.fifo.pop(0)
			
	def get_length(self):
		" Return the length of fifo "
		return len(self.fifo)

	def reset(self):
		" Empty fifo "
		self.fifo = []
		

