<commit_before># -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------------
# event.py - Event handling for the notifier
# -----------------------------------------------------------------------------
# $Id$
#
# -----------------------------------------------------------------------------
# kaa-notifier - Notifier Wrapper
# Copyright (C) 2005 <NAME>, et al.
#
# First Version: <NAME> <<EMAIL>>
# Maintainer:    <NAME> <<EMAIL>>
#
# Please see the file doc/AUTHORS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# -----------------------------------------------------------------------------

__all__ = [ 'Event', 'EventHandler', 'WeakEventHandler' ]

# python imports
import copy
import logging

# kaa.notifier imports
from callback import NotifierCallback, WeakNotifierCallback, OneShotTimer

# get logging object
log = logging.getLogger('notifier')

# manager object for eveny handling
manager = None

class Event(object):
    """
    A simple event that can be passed to the registered event handler.
    """
    def __init__(self, name, *args):
        """
        Init the event.
        """
        if isinstance(name, Event):
            self.name = name.name
            self.arg  = name.arg
        else:
            self.name = name
            self.arg  = None
        if args:
            self._set_args(args)

            
    def _set_args(self, args):
        """
        Set arguments of the event.
        """
        if not args:
            self.arg = None
        elif len(args) == 1:
            self.arg = args[0]
        else:
            self.arg = args


    def post(self, *args):
        """
        Post event into the queue.
        """
        if args:
            event = copy.copy(self)
            event._set_args(args)
            return manager.post(event)
        return manager.post(self)

        
    def __str__(self):
        """
        Return the event as string
        """
        return self.name


    def __cmp__(self, other):
        """
        Compare function, return 0 if the objects are identical, 1 otherwise
        """
        if not other:
            return 1
        if isinstance(other, Event):
            return self.name != other.name
        return self.name != other


class EventHandler(NotifierCallback):
    """
    Event handling callback.
    """
    def register(self, *events):
        """
        Register to a list of events. If no event is given, all events
        will be used.
        """
        self.events = events
        if not self in manager.handler:
            manager.handler.append(self)


    def active(self):
        """
        Return if the object is bound to the event manager.<commit_msg>make it thread safe and change interface<commit_after># -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------------
# event.py - Event handling for the notifier
# -----------------------------------------------------------------------------
# $Id$
#
# -----------------------------------------------------------------------------
# kaa-notifier - Notifier Wrapper
# Copyright (C) 2005 <NAME>, et al.
#
# First Version: <NAME> <<EMAIL>>
# Maintainer:    <NAME> <<EMAIL>>
#
# Please see the file doc/AUTHORS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# -----------------------------------------------------------------------------

__all__ = [ 'Event', 'EventHandler', 'WeakEventHandler' ]

# python imports
import copy
import logging

# kaa.notifier imports
from callback import NotifierCallback, WeakNotifierCallback, OneShotTimer
from thread import MainThreadCallback, is_mainthread

# get logging object
log = logging.getLogger('notifier')

# manager object for eveny handling
manager = None

class Event(object):
    """
    A simple event that can be passed to the registered event handler.
    """
    def __init__(self, name, *args):
        """
        Init the event.
        """
        if isinstance(name, Event):
            self.name = name.name
            self.arg  = name.arg
        else:
            self.name = name
            self.arg  = None
        if args:
            self._set_args(args)

            
    def _set_args(self, args):
        """
        Set arguments of the event.
        """
        if not args:
            self.arg = None
        elif len(args) == 1:
            self.arg = args[0]
        else:
            self.arg = args


    def post(self, *args):
        """
        Post event into the queue.
        """
        event = self
        if args:
            event = copy.copy(self)
            event._set_args(args)
        if not is_mainthread():
            return MainThreadCallback(manager.post, event)()
        else:
            return manager.post(event)

        
    def __str__(self):
        """
        Return the event as string
        """
        return self.name


    def __cmp__(self, other):
        """
        Compare function, return 0 if the objects are identical, 1 otherwise
        """
        if not other:
            return 1
        if isinstance(other, Event):
            return self.name != other.name
        return self.name != other


class EventHandler(NotifierCallback):
    """
    Event handling callback.
    """
    def register(self, events=[]):
        """
        Register to a list of events. If no event is given, all events
        will be used.
        """
        self.events = events
        if not self in manager.handler:
            manager.handler.append(self)


    def active(self):
        """
        Return if the object is bound to the event manager.
{'fraction_non_alphanumeric': 0.1698946169182569, 'fraction_numerical': 0.008259755055539733, 'mean_word_length': 1.6264023934181002, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '772475', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1721, 'n_words': 689}