<commit_before># receiver.py
#
# Copyright (c) 2004 <NAME>
#
# This file is part of Candygram.
#
# Candygram is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Candygram 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Candygram; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from candygram import _checkSignal, self_
from pattern import genFilter
import types
import time
import weakref



Message = object()

class Receiver:
	def __init__(self):
		_checkSignal()
		self.__handlers = []
		self.__lastMessage = 0
		self.__owner = self_()
		self.__mailbox = self.__owner._mailbox
		self.__mailboxCondition = self.__owner._mailboxCondition
		self.__timeout = None
		self.__timeoutHandler = None
		self.__timeoutArgs = None
		self.__timeoutKWArgs = None
		self.__receiverRefs = self.__owner._receiverRefs
		self.__receiverRefsLock = self.__owner._receiverRefsLock
		self.__receiverRefsLock.acquire()
		self.__receiverRefs.append(weakref.ref(self, self.__finalizeRef))
		self.__receiverRefsLock.release()

	def __finalizeRef(self, ref):
		assert ref is self
		self.__receiverRefsLock.acquire()
		for i in range(len(self.__receiverRefs)):
			if self.__receiverRefs[i]() is ref:
				del self.__receiverRefs[i]
				break
			pass
		self.__receiverRefsLock.release()

	def addHandler(self, pattern, handler=None, *args, **kwargs):
		_checkSignal()
		self.__checkOwner()
		if handler is not None:
			assert callable(handler),'The handler value must be callable'
		filter_ = genFilter(pattern)
		self.__handlers.append((filter_, handler, args, kwargs))
		# Clear all skipped messages, since the new handler might be able to handle
		# them.
		self.__lastMessage = 0
		return filter_

	def after(self, timeout, handler=None, *args, **kwargs):
		_checkSignal()
		self.__checkOwner()
		self.__setAfter(timeout, handler, args, kwargs)

	def __setAfter(self, timeout, handler, args, kwargs):
		assert self.__timeout is None, 'A timeout has already been set for this '\
				'Receiver'
		assert type(timeout) is types.IntType,'The timeout value must be an integer'
		if handler is not None:
			assert callable(handler),'The handler value must be callable'
		# Timeout is specified in milliseconds
		self.__timeout = float(timeout) / 1000
		self.__timeoutHandler = handler
		self.__timeoutArgs = args
		self.__timeoutKWArgs = kwargs

	def __setitem__(self, key, value):
		if type(value) is types.TupleType:
			self.addHandler(key, value[0], *value[1:])
		else:
			self.addHandler(key, value)
		pass

	def __getitem__(self, key):
		self.addHandler(key)
		return None

	def addHandlers(self, receiver):
		_checkSignal()
		self.__checkOwner()
		self.__handlers.extend(receiver.__handlers)
		# Clear all skipped messages, since the new handler might be able to handle
		# them.
		self.__lastMessage = 0

	def receive(self, timeout=None, handler=None, *args, **kwargs):
		_checkSignal()
		self.__checkOwner()
		if timeout is not None:
			self.__setAfter(timeout, handler, args, kwargs)
		expire = None
		if self.__timeout is not None:
			expire = time.time() + self.__timeout
		handlerInfo = None
		self.__mailboxCondition.acquire()
		while handlerInfo is None:
			handlerInfo = self.__scanMailbox()
			# If none of the filters picked up a message from the mailbox, wait
			# until a new message is sent and then try again.
			if handlerInfo is None:
				handlerInfo = self.__wait(expire)
			pass
		self.__mailboxCondition.release()
		message, handler, args, kwargs = handlerInfo
		if handler is None:
			return None
		args = _replaceMessageArgs(args, message)
		kwargs = _replaceMessageKWArgs(kwargs, message)
		return handler(*args, **kwargs)

	__call__ = receive
	next = receive

	def __iter__(self):
		return self

	def __scanMailbox(self):
		assert self.__mailboxCondition.locked()
		for i in xrange(self.__lastMessage, len(self.__mailbox)):
			message = self.__mailbox[i]
			for filter_, handler, args, kwargs in self.__handlers:
				if filter_(message):
					self.__deleteMessage(i)
					return message, handler, args, kwargs<commit_msg>[candygram @ 2004-08-18 18:51:12 by hobb0001] Remove timeout handler from Receiver at end of receive() call, so that receive() can be called multiple times with a timeout.<commit_after># receiver.py
#
# Copyright (c) 2004 <NAME>
#
# This file is part of Candygram.
#
# Candygram is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Candygram 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Candygram; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from candygram import _checkSignal, self_
from pattern import genFilter
import types
import time
import weakref



Message = object()

class Receiver:
	def __init__(self):
		_checkSignal()
		self.__handlers = []
		self.__lastMessage = 0
		self.__owner = self_()
		self.__mailbox = self.__owner._mailbox
		self.__mailboxCondition = self.__owner._mailboxCondition
		self.__timeout = None
		self.__timeoutHandler = None
		self.__timeoutArgs = None
		self.__timeoutKWArgs = None
		self.__receiverRefs = self.__owner._receiverRefs
		self.__receiverRefsLock = self.__owner._receiverRefsLock
		self.__receiverRefsLock.acquire()
		self.__receiverRefs.append(weakref.ref(self, self.__finalizeRef))
		self.__receiverRefsLock.release()

	def __finalizeRef(self, ref):
		assert ref is self
		self.__receiverRefsLock.acquire()
		for i in range(len(self.__receiverRefs)):
			if self.__receiverRefs[i]() is ref:
				del self.__receiverRefs[i]
				break
			pass
		self.__receiverRefsLock.release()

	def addHandler(self, pattern, handler=None, *args, **kwargs):
		_checkSignal()
		self.__checkOwner()
		if handler is not None:
			assert callable(handler),'The handler value must be callable'
		filter_ = genFilter(pattern)
		self.__handlers.append((filter_, handler, args, kwargs))
		# Clear all skipped messages, since the new handler might be able to handle
		# them.
		self.__lastMessage = 0
		return filter_

	def after(self, timeout, handler=None, *args, **kwargs):
		_checkSignal()
		self.__checkOwner()
		self.__setAfter(timeout, handler, args, kwargs)

	def __setitem__(self, key, value):
		if type(value) is types.TupleType:
			self.addHandler(key, value[0], *value[1:])
		else:
			self.addHandler(key, value)
		pass

	def __getitem__(self, key):
		self.addHandler(key)
		return None

	def addHandlers(self, receiver):
		_checkSignal()
		self.__checkOwner()
		self.__handlers.extend(receiver.__handlers)
		# Clear all skipped messages, since the new handler might be able to handle
		# them.
		self.__lastMessage = 0

	def receive(self, timeout=None, handler=None, *args, **kwargs):
		_checkSignal()
		self.__checkOwner()
		if timeout is not None:
			self.__setAfter(timeout, handler, args, kwargs)
		expire = None
		if self.__timeout is not None:
			expire = time.time() + self.__timeout
		handlerInfo = None
		self.__mailboxCondition.acquire()
		while handlerInfo is None:
			handlerInfo = self.__scanMailbox()
			# If none of the filters picked up a message from the mailbox, wait
			# until a new message is sent and then try again.
			if handlerInfo is None:
				handlerInfo = self.__wait(expire)
			pass
		self.__mailboxCondition.release()
		if timeout is not None:
			self.__removeAfter()
		message, handler, args, kwargs = handlerInfo
		if handler is None:
			return None
		args = _replaceMessageArgs(args, message)
		kwargs = _replaceMessageKWArgs(kwargs, message)
		return handler(*args, **kwargs)

	__call__ = receive
	next = receive

	def __iter__(self):
		return self

	def __setAfter(self, timeout, handler, args, kwargs):
		assert self.__timeout is None, 'A timeout has already been set for this '\
				'Receiver'
		assert type(timeout) is types.IntType,'The timeout value must be an integer'
		if handler is not None:
			assert callable(handler),'The handler value must be callable'
		# Timeout is specified in milliseconds
		self.__timeout = float(timeout) / 1000
		self.__timeoutHandler = handler
		self.__timeoutArgs = args
		self.__timeoutKWArgs = kwargs

	def __removeAfter(self):
		assert self.__timeout is not None
		self.__timeout = None
		self.__timeoutHandler = None
		self.__timeoutArgs = None
		self.__timeoutKWArgs = None

	def __scanMailbox(self):
		assert self.__mailboxCondition.locked()
		for i in xrange(self.__lastMessage, len(self.__mailbox)):
			message = self.__mailbox[i]
			for filter_, handler, args, kwargs in self.__handlers:
				if filter_(message):
					self.__deleteMessage(i)
					return message, handler, args, kwargs
{'fraction_non_alphanumeric': 0.08772114574557709, 'fraction_numerical': 0.008003369839932602, 'mean_word_length': 4.299665178571429, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2173444', 'n_tokens_mistral': 3159, 'n_tokens_neox': 2831, 'n_words': 981}