<commit_before>#
# Copyright (C) 2006 Collabora Limited
# Copyright (C) 2006 Nokia Corporation
#   @author <NAME> <<EMAIL>>
#
# This library 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.
#
# 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 Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import gobject
import telepathy.client
from telepathy import *


#
# Terrible hack to get around the following two issues:
#  1) Multiple asynchronous calls result in calls
#     getting "lost" (callbacks are never called).
#     The workaround is queuing calls and doing
#     them in a chained fashion.
#  2) No proper backtrace on exceptions raised from
#     within reply and signal callbacks.
#     The workaround is doing the replies transparently
#     through gobject.idle_add().
#

current_call = None
call_queue = []

def dbus_signal_cb(callback, *args):
    # do it directly for now
    #callback(*args)
    #return

    kwargs = { "priority" : gobject.PRIORITY_HIGH }
    gobject.idle_add(callback, *args, **kwargs)

def dbus_call_async(method, *args, **kwargs):
    # do it directly for now
    #method(*args, **kwargs)
    #return

    global current_call
    global call_queue

    if not kwargs.has_key("reply_handler"):
        raise KeyError("Missing reply_handler argument")

    if not kwargs.has_key("error_handler"):
        raise KeyError("Missing error_handler argument")

    call = (method, args, kwargs)
    call_queue.append(call)
    _try_next_call()

def _try_next_call():
    global current_call
    global call_queue

    # call in progress?
    if current_call is not None:
        return

    # empty queue?
    if not call_queue:
        return

    # get the next call
    current_call = call_queue.pop()

    # do it
    method, args, kwargs = current_call
    gia_kwargs = { "priority" : gobject.PRIORITY_HIGH }
    our_kwargs = { "reply_handler" : lambda *args: gobject.idle_add(_reply_cb, *args, **gia_kwargs),
                   "error_handler" : lambda *args: gobject.idle_add(_error_cb, *args, **gia_kwargs) }
    method(*args, **our_kwargs)

def _reply_cb(*args):
    global current_call

    method, func_args, func_kwargs = current_call
    if "extra_args" in func_kwargs:
        args += func_kwargs["extra_args"]
    func_kwargs["reply_handler"](*args)

    current_call = None
    _try_next_call()

def _error_cb(*args):
    global current_call

    method, func_args, func_kwargs = current_call
    func_kwargs["error_handler"](*args)
    current_call = None
    _try_next_call()


class Connection(telepathy.client.Connection):
    def __init__(self, bus_name, obj_path):
        telepathy.client.Connection.__init__(self, bus_name, obj_path)

        self._handle_cache = {}
        """Key: tuple (handle_type: int, handle_id: int)
        Value: name: str
        """

        self._handle_callbacks = {}
        """Key: tuple (handle_type: int, handle_id: int)
        Value: a list of tuples (callable, args)
        """

    def got_interfaces(self):
        print "Connection::got_interfaces"

    def lookup_handle(self, handle_type, handle_id, func, *args):
        if (handle_type, handle_id) in self._handle_cache:
            func(handle_id, handle_type,
                 self._handle_cache[(handle_type, handle_id)], *args)
        else:
            if not (handle_type, handle_id) in self._handle_callbacks:
                dbus_call_async(self[CONN_INTERFACE].InspectHandles,
                                handle_type, [handle_id],
                                reply_handler=lambda names: self._inspect_handle_reply_cb(handle_type, handle_id, names[0]),
                                error_handler=self.__error_cb)
                self._handle_callbacks[handle_id] = []

            self._handle_callbacks[(handle_type, handle_id)].append((func, args))

    def _inspect_handle_reply_cb(self, handle_type, handle_id, name):
        self._handle_cache[(handle_type, handle_id)] = name

        for func, args in self._handle_callbacks[(handle_type, handle_id)]:
            func(handle_type, handle_id, name, *args)

        del self._handle_callbacks[(handle_type, handle_id)]

    def __error_cb(self, exception):
        print "Connection.__error_cb: Exception received:", exception<commit_msg>tabby.util: initialize dictionary of handle introspection callbacks correctly<commit_after>#
# Copyright (C) 2006 Collabora Limited
# Copyright (C) 2006 Nokia Corporation
#   @author <NAME> <<EMAIL>>
#
# This library 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.
#
# 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 Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import gobject
import telepathy.client
from telepathy import *


#
# Terrible hack to get around the following two issues:
#  1) Multiple asynchronous calls result in calls
#     getting "lost" (callbacks are never called).
#     The workaround is queuing calls and doing
#     them in a chained fashion.
#  2) No proper backtrace on exceptions raised from
#     within reply and signal callbacks.
#     The workaround is doing the replies transparently
#     through gobject.idle_add().
#

current_call = None
call_queue = []

def dbus_signal_cb(callback, *args):
    # do it directly for now
    #callback(*args)
    #return

    kwargs = { "priority" : gobject.PRIORITY_HIGH }
    gobject.idle_add(callback, *args, **kwargs)

def dbus_call_async(method, *args, **kwargs):
    # do it directly for now
    #method(*args, **kwargs)
    #return

    global current_call
    global call_queue

    if not kwargs.has_key("reply_handler"):
        raise KeyError("Missing reply_handler argument")

    if not kwargs.has_key("error_handler"):
        raise KeyError("Missing error_handler argument")

    call = (method, args, kwargs)
    call_queue.append(call)
    _try_next_call()

def _try_next_call():
    global current_call
    global call_queue

    # call in progress?
    if current_call is not None:
        return

    # empty queue?
    if not call_queue:
        return

    # get the next call
    current_call = call_queue.pop()

    # do it
    method, args, kwargs = current_call
    gia_kwargs = { "priority" : gobject.PRIORITY_HIGH }
    our_kwargs = { "reply_handler" : lambda *args: gobject.idle_add(_reply_cb, *args, **gia_kwargs),
                   "error_handler" : lambda *args: gobject.idle_add(_error_cb, *args, **gia_kwargs) }
    method(*args, **our_kwargs)

def _reply_cb(*args):
    global current_call

    method, func_args, func_kwargs = current_call
    if "extra_args" in func_kwargs:
        args += func_kwargs["extra_args"]
    func_kwargs["reply_handler"](*args)

    current_call = None
    _try_next_call()

def _error_cb(*args):
    global current_call

    method, func_args, func_kwargs = current_call
    func_kwargs["error_handler"](*args)
    current_call = None
    _try_next_call()


class Connection(telepathy.client.Connection):
    def __init__(self, bus_name, obj_path):
        telepathy.client.Connection.__init__(self, bus_name, obj_path)

        self._handle_cache = {}
        """Key: tuple (handle_type: int, handle_id: int)
        Value: name: str
        """

        self._handle_callbacks = {}
        """Key: tuple (handle_type: int, handle_id: int)
        Value: a list of tuples (callable, args)
        """

    def got_interfaces(self):
        print "Connection::got_interfaces"

    def lookup_handle(self, handle_type, handle_id, func, *args):
        if (handle_type, handle_id) in self._handle_cache:
            func(handle_id, handle_type,
                 self._handle_cache[(handle_type, handle_id)], *args)
        else:
            if not (handle_type, handle_id) in self._handle_callbacks:
                dbus_call_async(self[CONN_INTERFACE].InspectHandles,
                                handle_type, [handle_id],
                                reply_handler=lambda names: self._inspect_handle_reply_cb(handle_type, handle_id, names[0]),
                                error_handler=self.__error_cb)
                self._handle_callbacks[(handle_type, handle_id)] = []

            self._handle_callbacks[(handle_type, handle_id)].append((func, args))

    def _inspect_handle_reply_cb(self, handle_type, handle_id, name):
        self._handle_cache[(handle_type, handle_id)] = name

        for func, args in self._handle_callbacks[(handle_type, handle_id)]:
            func(handle_type, handle_id, name, *args)

        del self._handle_callbacks[(handle_type, handle_id)]

    def __error_cb(self, exception):
        print "Connection.__error_cb: Exception received:", exception
{'fraction_non_alphanumeric': 0.0906285361588314, 'fraction_numerical': 0.005554984055138361, 'mean_word_length': 3.1725321888412017, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '53575', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2828, 'n_words': 869}