<commit_before>from twisted.protocols.jabber import jid
from twisted.python import components
from twisted.application import service
from twisted.xish import utility
from twisted.internet import defer

class Error(Exception):
    msg = ''

    def __str__(self):
        return self.msg

class NodeNotFound(Error):
    msg = 'Node not found'

class NotAuthorized(Error):
    pass

class PayloadExpected(Error):
    msg = 'Payload expected'

class NoPayloadAllowed(Error):
    msg = 'No payload allowed'

class NoInstantNodes(Error):
    pass

class NodeExists(Error):
    pass

class NotImplemented(Error):
    pass

class NotSubscribed(Error):
    pass

class IBackendService(components.Interface):
    """ Interface to a backend service of a pubsub service. """

    def get_supported_affiliations(self):
        """ Reports the list of supported affiliation types.

        @return: a list of supported affiliation types.
        """

class INodeCreationService(components.Interface):
    """ A service for creating nodes """

    def create_node(self, node_id, requestor):
        """ Create a node.

        @return: a deferred that fires when the node has been created.
        """

class IPublishService(components.Interface):
    """ A service for publishing items to a node. """

    def publish(self, node_id, items, requestor):
        """ Publish items to a pubsub node.

        @return: a deferred that fires when the items have been published.
        """
class INotificationService(components.Interface):
    """ A service for notification of published items. """

    def register_notifier(self, observerfn, *args, **kwargs):
        """ Register callback which is called for notification. """

    def get_notification_list(self, node_id, items):
        pass

class ISubscriptionService(components.Interface):
    """ A service for managing subscriptions. """

    def subscribe(self, node_id, subscriber, requestor):
        """ Request the subscription of an entity to a pubsub node.

        Depending on the node's configuration and possible business rules, the
        C{subscriber} is added to the list of subscriptions of the node with id
        C{node_id}. The C{subscriber} might be different from the C{requestor},
        and if the C{requestor} is not allowed to subscribe this entity an
        exception should be raised.

        @return: a deferred that returns the subscription state
        """

    def unsubscribe(self, node_id, subscriber, requestor):
        """ Cancel the subscription of an entity to a pubsub node.

        The subscription of C{subscriber} is removed from the list of
        subscriptions of the node with id C{node_id}. If the C{requestor}
        is not allowed to unsubscribe C{subscriber}, an an exception should
        be raised.

        @return: a deferred that fires when unsubscription is complete.
        """

class IAffiliationsService(components.Interface):
    """ A service for retrieving the affiliations with this pubsub service. """

    def get_affiliations(self, entity):
        """ Report the list of current affiliations with this pubsub service.

        Report the list of the current affiliations with all nodes within this
        pubsub service, along with subscriptions to such nodes, for the
        C{entity}.

        @return: a deferred that returns the list of all current affiliations
        and subscriptions.
        """

class IRetractionService(components.Interface):
    """ A service for retracting published items """

    def retract_item(self, node_id, item_id, requestor):
        """ Removes item in node from persistent storage """

    def purge_node(self, node_id, requestor):
        """ Removes all items in node from persistent storage """

class IItemRetrievalService(components.Interface):
    """ A service for retrieving previously published items. """

    def get_items(self, node_id, max_items=None, item_ids=[],
                  requestor=None):
        """ Retrieve items from persistent storage

        If C{max_items} is given, return the C{max_items} last published
        items, else if C{item_ids} is not empty, return the items requested.
        If neither is given, return all items.

        @return: a deferred that returns the requested items
        """

class BackendService(service.MultiService, utility.EventDispatcher):

    __implements__ = IBackendService,

    def __init__(self, storage):
        service.MultiService.__init__(self)
        utility.EventDispatcher.__init__(self)
        self.storage = storage

    def supports_publisher_affiliation(self):
        return True

    def supports_outcast_affiliation(self):
        return True

    def supports_persistent_items(self):
        return True

class PublishService(service.Service):

    __implements__ = IPublishService,

    def publish(self, node_id, items, requestor):
        d1 = self.parent.storage.get_node_configuration(node_id)
        d2 = self.parent.storage.get_affiliation(node_id, requestor)
        d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
        d.addErrback(lambda x: x.value[0])
        d.addCallback(self._do_publish, node_id, items, requestor)
        return d

    def _do_publish(self, result, node_id, items, requestor):
        configuration = result[0][1]
        persist_items = configuration["persist_items"]
        deliver_payloads = configuration["deliver_payloads"]
        affiliation = result[1][1]

        if affiliation not in ['owner', 'publisher']:
            raise NotAuthorized

        if items and not persist_items and not deliver_payloads:
            raise NoPayloadAllowed
        elif not items and (persist_items or deliver_payloads):
            raise PayloadExpected

        print "publish by %s to %s" % (requestor.full(), node_id)

        if persist_items or deliver_payloads:
            for item in items:
                if item["id"] is None:
                    item["id"] = 'random'   # FIXME

        if persist_items:
            d = self.parent.storage.store_items(node_id, items,
                                                requestor)
        else:
            d = defer.succeed(None)

        d.addCallback(self._do_notify, node_id, items, deliver_payloads)

    def _do_notify(self, result, node_id, items, deliver_payloads):
        if items and not deliver_payloads:
            for item in items:
                item.children = []

        self.parent.dispatch({ 'items': items, 'node_id': node_id },
                             '//event/pubsub/notify')

class NotificationService(service.Service):

    __implements__ = INotificationService,

    def get_notification_list(self, node_id, items):
        d = self.parent.storage.get_subscribers(node_id)
        d.addCallback(self._magic_filter, node_id, items)
        return d

    def _magic_filter(self, subscribers, node_id, items):
        list = {}
        for subscriber in subscribers:
            list[subscriber] = items
        return list

    def register_notifier(self, observerfn, *args, **kwargs):
        self.parent.addObserver('//event/pubsub/notify', observerfn,
                                *args, **kwargs)

class SubscriptionService(service.Service):

    __implements__ = ISubscriptionService,

    def subscribe(self, node_id, subscriber, requestor):
        if subscriber.userhostJID() != requestor:
            raise NotAuthorized

        d1 = self.parent.storage.get_node_configuration(node_id)
        d2 = self.parent.storage.get_affiliation(node_id, subscriber)
        d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
        d.addErrback(lambda x: x.value[0])
        d.addCallback(self._do_subscribe, node_id, subscriber)
        return d

    def _do_subscribe(self, result, node_id, subscriber):
        configuration = result[0][1]
        affiliation = result[1][1]

        if affiliation == 'outcast':
            raise NotAuthorized

        d = self.parent.storage.add_subscription(node_id, subscriber,
                                                 'subscribed')
        d.addCallback(self._return_subscription, affiliation)
        return d

    def _return_subscription(self, result, affiliation):
        result['affiliation'] = affiliation
        return result

    def unsubscribe(self, node_id, subscriber, requestor):
        if subscriber.userhostJID() != requestor:
            raise NotAuthorized

        d = self.parent.storage.get_node_configuration(node_id)
        d.addCallback(self._do_unsubscribe, node_id, subscriber)
        return d

    def _do_unsubscribe(self, result, node_id, subscriber):
        return self.parent.storage.remove_subscription(node_id,
                                                       subscriber)

class NodeCreationService(service.Service):

    __implements__ = INodeCreationService,

    def supports_instant_nodes(self):
        return False

    def create_node(self, node_id, requestor):
        if not node_id:
            raise NoInstantNodes

        d = self.parent.storage.create_node(node_id, requestor)
        d.addCallback(lambda _: node_id)
        return d

class AffiliationsService(service.Service):

    __implements__ = IAffiliationsService,

    def get_affiliations(self, entity):
        d1 = self.parent.storage.get_affiliations(entity)
        d2 = self.parent.storage.get_subscriptions(entity)
        d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
        d.addErrback(lambda x: x.value[0])
        d.addCallback(self._affiliations_result, entity)
        return d

    def _affiliations_result(self, result, entity):
        affiliations = result[0][1]
        subscriptions = result[1][1]

        new_affiliations = {}

        for node, affiliation in affiliations:
            new_affiliations[(node, entity.full())] = {'node': node,
                                                'jid': entity,
                                                'affiliation': affiliation,
                                                'subscription': None
                                               }

        for node, subscriber, subscription in subscriptions:
            key = node, subscriber.full()
            if new_affiliations.has_key(key):
                new_affiliations[key]['subscription'] = subscription
            else:
                new_affiliations[key] = {'node': node,
                                         'jid': subscriber,
                                         'affiliation': None,
                                         'subscription': subscription}

        return new_affiliations.values()
<commit_msg>Add get_node_type() to BackendService.<commit_after>from twisted.protocols.jabber import jid
from twisted.python import components
from twisted.application import service
from twisted.xish import utility
from twisted.internet import defer

class Error(Exception):
    msg = ''

    def __str__(self):
        return self.msg

class NodeNotFound(Error):
    msg = 'Node not found'

class NotAuthorized(Error):
    pass

class PayloadExpected(Error):
    msg = 'Payload expected'

class NoPayloadAllowed(Error):
    msg = 'No payload allowed'

class NoInstantNodes(Error):
    pass

class NodeExists(Error):
    pass

class NotImplemented(Error):
    pass

class NotSubscribed(Error):
    pass

class IBackendService(components.Interface):
    """ Interface to a backend service of a pubsub service. """

    def get_supported_affiliations(self):
        """ Reports the list of supported affiliation types.

        @return: a list of supported affiliation types.
        """

class INodeCreationService(components.Interface):
    """ A service for creating nodes """

    def create_node(self, node_id, requestor):
        """ Create a node.

        @return: a deferred that fires when the node has been created.
        """

class IPublishService(components.Interface):
    """ A service for publishing items to a node. """

    def publish(self, node_id, items, requestor):
        """ Publish items to a pubsub node.

        @return: a deferred that fires when the items have been published.
        """
class INotificationService(components.Interface):
    """ A service for notification of published items. """

    def register_notifier(self, observerfn, *args, **kwargs):
        """ Register callback which is called for notification. """

    def get_notification_list(self, node_id, items):
        pass

class ISubscriptionService(components.Interface):
    """ A service for managing subscriptions. """

    def subscribe(self, node_id, subscriber, requestor):
        """ Request the subscription of an entity to a pubsub node.

        Depending on the node's configuration and possible business rules, the
        C{subscriber} is added to the list of subscriptions of the node with id
        C{node_id}. The C{subscriber} might be different from the C{requestor},
        and if the C{requestor} is not allowed to subscribe this entity an
        exception should be raised.

        @return: a deferred that returns the subscription state
        """

    def unsubscribe(self, node_id, subscriber, requestor):
        """ Cancel the subscription of an entity to a pubsub node.

        The subscription of C{subscriber} is removed from the list of
        subscriptions of the node with id C{node_id}. If the C{requestor}
        is not allowed to unsubscribe C{subscriber}, an an exception should
        be raised.

        @return: a deferred that fires when unsubscription is complete.
        """

class IAffiliationsService(components.Interface):
    """ A service for retrieving the affiliations with this pubsub service. """

    def get_affiliations(self, entity):
        """ Report the list of current affiliations with this pubsub service.

        Report the list of the current affiliations with all nodes within this
        pubsub service, along with subscriptions to such nodes, for the
        C{entity}.

        @return: a deferred that returns the list of all current affiliations
        and subscriptions.
        """

class IRetractionService(components.Interface):
    """ A service for retracting published items """

    def retract_item(self, node_id, item_id, requestor):
        """ Removes item in node from persistent storage """

    def purge_node(self, node_id, requestor):
        """ Removes all items in node from persistent storage """

class IItemRetrievalService(components.Interface):
    """ A service for retrieving previously published items. """

    def get_items(self, node_id, max_items=None, item_ids=[],
                  requestor=None):
        """ Retrieve items from persistent storage

        If C{max_items} is given, return the C{max_items} last published
        items, else if C{item_ids} is not empty, return the items requested.
        If neither is given, return all items.

        @return: a deferred that returns the requested items
        """

class BackendService(service.MultiService, utility.EventDispatcher):

    __implements__ = IBackendService,

    def __init__(self, storage):
        service.MultiService.__init__(self)
        utility.EventDispatcher.__init__(self)
        self.storage = storage

    def supports_publisher_affiliation(self):
        return True

    def supports_outcast_affiliation(self):
        return True

    def supports_persistent_items(self):
        return True

    def get_node_type(self, node_id):
        d = self.storage.get_node_type(node_id)
        return d

class PublishService(service.Service):

    __implements__ = IPublishService,

    def publish(self, node_id, items, requestor):
        d1 = self.parent.storage.get_node_configuration(node_id)
        d2 = self.parent.storage.get_affiliation(node_id, requestor)
        d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
        d.addErrback(lambda x: x.value[0])
        d.addCallback(self._do_publish, node_id, items, requestor)
        return d

    def _do_publish(self, result, node_id, items, requestor):
        configuration = result[0][1]
        persist_items = configuration["persist_items"]
        deliver_payloads = configuration["deliver_payloads"]
        affiliation = result[1][1]

        if affiliation not in ['owner', 'publisher']:
            raise NotAuthorized

        if items and not persist_items and not deliver_payloads:
            raise NoPayloadAllowed
        elif not items and (persist_items or deliver_payloads):
            raise PayloadExpected

        print "publish by %s to %s" % (requestor.full(), node_id)

        if persist_items or deliver_payloads:
            for item in items:
                if item["id"] is None:
                    item["id"] = 'random'   # FIXME

        if persist_items:
            d = self.parent.storage.store_items(node_id, items,
                                                requestor)
        else:
            d = defer.succeed(None)

        d.addCallback(self._do_notify, node_id, items, deliver_payloads)

    def _do_notify(self, result, node_id, items, deliver_payloads):
        if items and not deliver_payloads:
            for item in items:
                item.children = []

        self.parent.dispatch({ 'items': items, 'node_id': node_id },
                             '//event/pubsub/notify')

class NotificationService(service.Service):

    __implements__ = INotificationService,

    def get_notification_list(self, node_id, items):
        d = self.parent.storage.get_subscribers(node_id)
        d.addCallback(self._magic_filter, node_id, items)
        return d

    def _magic_filter(self, subscribers, node_id, items):
        list = {}
        for subscriber in subscribers:
            list[subscriber] = items
        return list

    def register_notifier(self, observerfn, *args, **kwargs):
        self.parent.addObserver('//event/pubsub/notify', observerfn,
                                *args, **kwargs)

class SubscriptionService(service.Service):

    __implements__ = ISubscriptionService,

    def subscribe(self, node_id, subscriber, requestor):
        if subscriber.userhostJID() != requestor:
            raise NotAuthorized

        d1 = self.parent.storage.get_node_configuration(node_id)
        d2 = self.parent.storage.get_affiliation(node_id, subscriber)
        d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
        d.addErrback(lambda x: x.value[0])
        d.addCallback(self._do_subscribe, node_id, subscriber)
        return d

    def _do_subscribe(self, result, node_id, subscriber):
        configuration = result[0][1]
        affiliation = result[1][1]

        if affiliation == 'outcast':
            raise NotAuthorized

        d = self.parent.storage.add_subscription(node_id, subscriber,
                                                 'subscribed')
        d.addCallback(self._return_subscription, affiliation)
        return d

    def _return_subscription(self, result, affiliation):
        result['affiliation'] = affiliation
        return result

    def unsubscribe(self, node_id, subscriber, requestor):
        if subscriber.userhostJID() != requestor:
            raise NotAuthorized

        d = self.parent.storage.get_node_configuration(node_id)
        d.addCallback(self._do_unsubscribe, node_id, subscriber)
        return d

    def _do_unsubscribe(self, result, node_id, subscriber):
        return self.parent.storage.remove_subscription(node_id,
                                                       subscriber)

class NodeCreationService(service.Service):

    __implements__ = INodeCreationService,

    def supports_instant_nodes(self):
        return False

    def create_node(self, node_id, requestor):
        if not node_id:
            raise NoInstantNodes

        d = self.parent.storage.create_node(node_id, requestor)
        d.addCallback(lambda _: node_id)
        return d

class AffiliationsService(service.Service):

    __implements__ = IAffiliationsService,

    def get_affiliations(self, entity):
        d1 = self.parent.storage.get_affiliations(entity)
        d2 = self.parent.storage.get_subscriptions(entity)
        d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
        d.addErrback(lambda x: x.value[0])
        d.addCallback(self._affiliations_result, entity)
        return d

    def _affiliations_result(self, result, entity):
        affiliations = result[0][1]
        subscriptions = result[1][1]

        new_affiliations = {}

        for node, affiliation in affiliations:
            new_affiliations[(node, entity.full())] = {'node': node,
                                                'jid': entity,
                                                'affiliation': affiliation,
                                                'subscription': None
                                               }

        for node, subscriber, subscription in subscriptions:
            key = node, subscriber.full()
            if new_affiliations.has_key(key):
                new_affiliations[key]['subscription'] = subscription
            else:
                new_affiliations[key] = {'node': node,
                                         'jid': subscriber,
                                         'affiliation': None,
                                         'subscription': subscription}

        return new_affiliations.values()
<|endoftext|>


<commit_before>"""Contains implementations for each ported operation in NumPy.

Attributes:
    decoder_ (TYPE): Description
    encoder_ (TYPE): Description
    norm_factor_id_ (int): Unique IDs given to norm factor literals
"""
from encoders import *
from weld.weldobject import *

encoder_ = NumPyEncoder()
decoder_ = NumPyDecoder()

def div(array, other, ty):
    """
    Normalizes the passed-in array by the passed in quantity.

    Args:
        array (WeldObject / Numpy.ndarray): Input array to normalize
        other (WeldObject / float): Normalization factor
        ty (WeldType): Type of each element in the input array

    Returns:
        A WeldObject representing this computation
    """
    weld_obj = WeldObject(encoder_, decoder_)

    array_var = weld_obj.update(array)
    if isinstance(array, WeldObject):
        array_var = array.obj_id
        weld_obj.dependencies[array_var] = array

    other_var = None
    if isinstance(other, WeldObject):
        other_var = other.obj_id
        weld_obj.dependencies[other_var] = other
    else:
        other_var = ("%.2f" % other)

    weld_template = """
       map(
         %(array)s,
         |value| value / %(ty)s(%(other)s)
       )
    """
    weld_obj.weld_code = weld_template % {"array": array_var,
                                          "other": other_var,
                                          "ty": ty}
    return weld_obj

def aggr(array, op, initial_value, ty):
    """
    Computes the aggregate of elements in the array.

    Args:
        array (WeldObject / Numpy.ndarray): Input array to aggregate
        op (str): Op string used to aggregate the array (+ / *)
        initial_value (int): Initial value for aggregation
        ty (WeldType): Type of each element in the input array

    Returns:
        A WeldObject representing this computation
    """
    weld_obj = WeldObject(encoder_, decoder_)

    array_var = weld_obj.update(array)
    if isinstance(array, WeldObject):
        array_var = array.obj_id
        weld_obj.dependencies[array_var] = array

    weld_template = """
      result(
        for(
          %(array)s,
          merger[%(ty)s,%(op)s],
          |b, i, e| merge(b, e)
        )
      )
    """
    weld_obj.weld_code = weld_template % {
        "array": array_var, "ty": ty, "op": op}
    return weld_obj

def dot(matrix, vector, matrix_ty, vector_ty):
    """
    Computes the dot product between a matrix and a vector.

    Args:
        matrix (WeldObject / Numpy.ndarray): 2-d input matrix
        vector (WeldObject / Numpy.ndarray): 1-d input vector
        ty (WeldType): Type of each element in the input matrix and vector

    Returns:
        A WeldObject representing this computation
    """
    weld_obj = WeldObject(encoder_, decoder_)

    matrix_var = weld_obj.update(matrix)
    if isinstance(matrix, WeldObject):
        matrix_var = matrix.obj_id
        weld_obj.dependencies[matrix_var] = matrix

    vector_var = weld_obj.update(vector)
    if isinstance(vector, WeldObject):
        vector_var = vector.obj_id
        weld_obj.dependencies[vector_var] = vector

    weld_template = """
       map(
         %(matrix)s,
         |row: vec[%(matrix_ty)s]|
           result(
             for(
               map(
                 zip(row, %(vector)s),
                 |ele: {%(matrix_ty)s, %(vector_ty)s}|
                   f64(ele.$0 * %(matrix_ty)s(ele.$1))
               ),
               merger[f64,+],
               |b, i, e| merge(b, e)
             )
           )
       )
    """
    weld_obj.weld_code = weld_template % {"matrix": matrix_var,
                                          "vector": vector_var,
                                          "matrix_ty": matrix_ty,
                                          "vector_ty": vector_ty}
    return weld_obj

def exp(array, ty):
    """
    Computes the per-element exponenet of the passed-in array.

    Args:
        array (WeldObject / Numpy.ndarray): Input array
        ty (WeldType): Type of each element in the input array

    Returns:
        A WeldObject representing this computation
    """
    weld_obj = WeldObject(encoder_, decoder_)

    array_var = weld_obj.update(array)
    if isinstance(array, WeldObject):
        array_var = array.obj_id
        weld_obj.dependencies[array_var] = array

    weld_template = """
       map(
         %(array)s,
         |ele: %(ty)s| exp(ele)
       )
    """
    weld_obj.weld_code = weld_template % {"array": array_var, "ty": ty}
    return weld_obj
<commit_msg>Loop size annotation (#289)<commit_after>"""Contains implementations for each ported operation in NumPy.

Attributes:
    decoder_ (TYPE): Description
    encoder_ (TYPE): Description
    norm_factor_id_ (int): Unique IDs given to norm factor literals
"""
from encoders import *
from weld.weldobject import *

encoder_ = NumPyEncoder()
decoder_ = NumPyDecoder()

def div(array, other, ty):
    """
    Normalizes the passed-in array by the passed in quantity.

    Args:
        array (WeldObject / Numpy.ndarray): Input array to normalize
        other (WeldObject / float): Normalization factor
        ty (WeldType): Type of each element in the input array

    Returns:
        A WeldObject representing this computation
    """
    weld_obj = WeldObject(encoder_, decoder_)

    array_var = weld_obj.update(array)
    if isinstance(array, WeldObject):
        array_var = array.obj_id
        weld_obj.dependencies[array_var] = array

    other_var = None
    if isinstance(other, WeldObject):
        other_var = other.obj_id
        weld_obj.dependencies[other_var] = other
    else:
        other_var = ("%.2f" % other)

    weld_template = """
       map(
         %(array)s,
         |value| value / %(ty)s(%(other)s)
       )
    """
    weld_obj.weld_code = weld_template % {"array": array_var,
                                          "other": other_var,
                                          "ty": ty}
    return weld_obj

def aggr(array, op, initial_value, ty):
    """
    Computes the aggregate of elements in the array.

    Args:
        array (WeldObject / Numpy.ndarray): Input array to aggregate
        op (str): Op string used to aggregate the array (+ / *)
        initial_value (int): Initial value for aggregation
        ty (WeldType): Type of each element in the input array

    Returns:
        A WeldObject representing this computation
    """
    weld_obj = WeldObject(encoder_, decoder_)

    array_var = weld_obj.update(array)
    if isinstance(array, WeldObject):
        array_var = array.obj_id
        weld_obj.dependencies[array_var] = array

    weld_template = """
      result(
        for(
          %(array)s,
          merger[%(ty)s,%(op)s],
          |b, i, e| merge(b, e)
        )
      )
    """
    weld_obj.weld_code = weld_template % {
        "array": array_var, "ty": ty, "op": op}
    return weld_obj

def dot(matrix, vector, matrix_ty, vector_ty):
    """
    Computes the dot product between a matrix and a vector.

    Args:
        matrix (WeldObject / Numpy.ndarray): 2-d input matrix
        vector (WeldObject / Numpy.ndarray): 1-d input vector
        ty (WeldType): Type of each element in the input matrix and vector

    Returns:
        A WeldObject representing this computation
    """
    weld_obj = WeldObject(encoder_, decoder_)

    matrix_var = weld_obj.update(matrix)
    if isinstance(matrix, WeldObject):
        matrix_var = matrix.obj_id
        weld_obj.dependencies[matrix_var] = matrix

    vector_var = weld_obj.update(vector)
    loopsize_annotation = ""
    if isinstance(vector, WeldObject):
        vector_var = vector.obj_id
        weld_obj.dependencies[vector_var] = vector
    if isinstance(vector, np.ndarray):
        loopsize_annotation = "@(loopsize: %dL)" % len(vector)

    weld_template = """
       map(
         %(matrix)s,
         |row: vec[%(matrix_ty)s]|
           result(
             for(
               result(
                 %(loopsize_annotation)s
                 for(
                   zip(row, %(vector)s),
                   appender,
                   |b2, i2, e2: {%(matrix_ty)s, %(vector_ty)s}|
                     merge(b2, f64(e2.$0 * %(matrix_ty)s(e2.$1)))
                 )
               ),
               merger[f64,+],
               |b, i, e| merge(b, e)
             )
           )
       )
    """
    weld_obj.weld_code = weld_template % {"matrix": matrix_var,
                                          "vector": vector_var,
                                          "matrix_ty": matrix_ty,
                                          "vector_ty": vector_ty,
                                          "loopsize_annotation": loopsize_annotation}
    return weld_obj

def exp(array, ty):
    """
    Computes the per-element exponenet of the passed-in array.

    Args:
        array (WeldObject / Numpy.ndarray): Input array
        ty (WeldType): Type of each element in the input array

    Returns:
        A WeldObject representing this computation
    """
    weld_obj = WeldObject(encoder_, decoder_)

    array_var = weld_obj.update(array)
    if isinstance(array, WeldObject):
        array_var = array.obj_id
        weld_obj.dependencies[array_var] = array

    weld_template = """
       map(
         %(array)s,
         |ele: %(ty)s| exp(ele)
       )
    """
    weld_obj.weld_code = weld_template % {"array": array_var, "ty": ty}
    return weld_obj
<|endoftext|>


<commit_before>import discord
from discord.ext import commands

class CustomContext(commands.Context):
    async def error(self, message: str):
        em = discord.Embed(title=f':no_entry_sign: {message}',
                           color=discord.Color.dark_red())

        await self.send(embed=em)

    async def success(self, message: str):
        em = discord.Embed(title=f':white_check_mark: {message}',
                           color=discord.Color.dark_green())

        await self.send(embed=em)
<commit_msg>alter custom context to allow for descriptions<commit_after>import discord
from discord.ext import commands

class CustomContext(commands.Context):
    async def error(self, title: str, description: str = None):
        em = discord.Embed(title=f':no_entry_sign: {title}',
                           color=discord.Color.dark_red(),
                           description=description)

        await self.send(embed=em)

    async def success(self, title: str, description: str = None):
        em = discord.Embed(title=f':white_check_mark: {title}',
                           color=discord.Color.dark_green(),
                           description=None)

        await self.send(embed=em)
<|endoftext|>


<commit_before>#! /usr/bin/env python3

import time
import random
import abc
import pickle
import argparse
import sys
from pyroaring import BitMap

def mean(l):
    s = float(sum(l))
    return s/len(l)

class AbstractBenchMark(metaclass=abc.ABCMeta):
    max_int = 2**32
    sizes = [2**n for n in range(0, 22)]
    classes = [BitMap, set]

    @classmethod
    def get_data(cls, size, bound):
        assert size < cls.max_int
        assert bound < cls.max_int
        assert size < bound
        return random.sample(range(0, bound), size)

    @classmethod
    def sparse_data(cls, size):
        bound = size*16
        return cls.get_data(size, bound)

    @classmethod
    def dense_data(cls, size):
        bound = size*2
        return cls.get_data(size, bound)

    def __init__(self):
        self.results = {data_func : dict() for data_func in [self.sparse_data, self.dense_data]}
        for data_func, d in self.results.items():
            for size in self.sizes:
                d[size] = {cls:list() for cls in self.classes}

    def compute_ratios(self):
        self.ratios = {data_func : dict() for data_func in self.results.keys()}
        for data_func, d in self.ratios.items():
            for size in self.sizes:
                d[size] = {cls:list() for cls in self.classes[1:]}
                for cls in self.classes[1:]:
                    r = list()
                    result = self.results[data_func][size]
                    for i in range(len(result[cls])):
                        r.append(result[cls][i]/result[self.classes[0]][i])
                    d[size][cls] = mean(r)

    @abc.abstractmethod
    def run(self, data_func, size):
        pass

    def run_all(self):
        for data_func in self.results.keys():
            for size in self.sizes:
                times = self.run(data_func, size)
                for i, cls in enumerate(self.classes):
                    self.results[data_func][size][cls].append(times[i])

    def print_results(self):
        print('\n# %s' % self.__class__.__name__)
        for data_func, d in sorted(self.ratios.items(), key = lambda x: x[0].__name__):
            print('\t%s' % data_func.__name__)
            for size, d_size in sorted(d.items()):
                print('\t\tSize=%d' % size)
                for test_class, ratio in sorted(d_size.items(), key = lambda x: x[0].__name__):
                    print('\t\t\tRatio %s/%s: %f' % (test_class.__name__, self.classes[0].__name__, ratio))

    @staticmethod
    def _results_to_tex(f, results): # results has the form { size1 : result1, size2 : result2, ...}
        f.write('\t\t\t\\addplot table {\n')
        for size, value in sorted(results.items()):
            f.write('\t\t\t\t%d %f\n' % (size, value))
        f.write('\t\t\t};\n')

    @staticmethod
    def add_legend(f, legend):
        f.write('\t\t\t\\addlegendentry{%s}\n' % legend.replace('_', '\\_'))

    def _plot_func(self, f):
        f.write('\t\\begin{tikzpicture}\n')
        f.write('\t\t\\begin{axis}[\n')
        f.write('\t\t\txlabel=size,\n')
        f.write('\t\t\tylabel=speedup,\n')
        f.write('\t\t\txmode=log,\n')
        f.write('\t\t\tymode=log,\n')
        f.write('\t\t\txmajorgrids,\n')
        f.write('\t\t\tymajorgrids,\n')
        f.write('\t\t\tlegend style={at={(0, 1)},anchor=north west,fill=none}\n')
        f.write('\t\t\t]\n')
        for data_func, d_func in sorted(self.ratios.items(), key=lambda x: x[0].__name__):
            results = dict()
            # First, we "inverse" the dict...
            for size, d_size in d_func.items():
                for test_class, value in d_size.items():
                    try:
                        results[test_class][size] = value
                    except KeyError:
                        results[test_class] = {size : value}
            for test_class in self.classes[1:]:
                self._results_to_tex(f, results[test_class])
        # Finally, the legend
        for data_func in sorted(self.ratios, key=lambda x: x.__name__):
            for test_class in self.classes[1:]:
                self.add_legend(f, 'ratio %s/%s: %s' % (test_class.__name__, self.classes[0].__name__, data_func.__name__))
        f.write('\t\t\\end{axis}\n')
        f.write('\t\\end{tikzpicture}\n')

    def plot_results(self, f):
        f.write('% Do not forget \\usepackage{pgfplots}\n')
        f.write('\\begin{figure}\n')
        self._plot_func(f)
        f.write('\t\\caption{%s}\n' % self.__class__.__name__)
        f.write('\\end{figure}\n')

class AbstractConstructorBenchMark(AbstractBenchMark):

    @abc.abstractmethod
    def get_values(self, data_func, size):
        pass

    def run_cls(self, cls, values):
        begin = time.time()
        result = cls(values)
        return time.time()-begin

    def run(self, data_func, size):
        values = self.get_values(data_func, size)
        assert len(values) == size
        return [self.run_cls(cls, values) for cls in self.classes]

class ListConstructor(AbstractConstructorBenchMark):

    def get_values(self, data_func, size):
        return data_func(size)

class RangeConstructor(AbstractConstructorBenchMark):

    def get_values(self, data_func, size):
        start = random.randint(0, self.max_int-size)
        max_step = (self.max_int-start)//size
        if data_func == self.sparse_data:
            step = random.randint(max_step//2, max_step)
        elif data_func == self.dense_data:
            step = random.randint(1, min(max_step, 4))
        else:
            assert False
        end = start + step*size
        return range(start, end, step)

class CopyConstructor(AbstractConstructorBenchMark):

    def get_values(self, data_func, size):
        return data_func(size)

    def run_cls(self, cls, values):
        values = cls(values)
        return super().run_cls(cls, values)

class AbstractBinaryOpBenchMark(AbstractBenchMark):

    @abc.abstractmethod
    def do_binary_op(self, values1, values2):
        pass

    def run_cls(self, cls, values1, values2):
        values1 = cls(values1)
        values2 = cls(values2)
        begin = time.time()
        self.do_binary_op(values1, values2)
        return time.time()-begin

    def run(self, data_func, size):
        values1 = data_func(size)
        values2 = data_func(size)
        return [self.run_cls(cls, values1, values2) for cls in self.classes]

class BinaryUnion(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 | values2

class BinaryIntersection(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 & values2

class BinarySymmetricDifference(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 ^ values2

class BinaryDifference(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 - values2

class BinaryUnionInPlace(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 |= values2

class BinaryIntersectionInPlace(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 &= values2

class BinaryDifferenceInPlace(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 ^= values2

class BinarySymmetricDifferenceInPlace(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 ^= values2

class EqualityTest(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 == values2

class InclusionTest(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 < values2

class AbstractManyOpBenchMark(AbstractBenchMark):

    @abc.abstractmethod
    def do_many_op(self, values):
        pass

    def run_cls(self, cls, values_list):
        values_list = [cls(v) for v in values_list]
        begin = time.time()
        self.do_many_op(cls, *values_list)
        return time.time()-begin

    def run(self, data_func, size):
        nb = 20#random.randint(2, 100)
        values_list = [data_func(size) for _ in range(nb)]
        return [self.run_cls(cls, values_list) for cls in self.classes]

class ManyUnion(AbstractManyOpBenchMark):

    def do_many_op(self, cls, *values):
        cls.union(*values)

classes = [ListConstructor, RangeConstructor, CopyConstructor,
    BinaryUnion, BinaryIntersection, BinaryDifference, BinarySymmetricDifference,
    BinaryUnionInPlace, BinaryIntersectionInPlace, BinaryDifferenceInPlace, BinarySymmetricDifferenceInPlace,
    EqualityTest, InclusionTest,
    ManyUnion]

class Runer:
    def __init__(self):
        self.benchmarks = [cls() for cls in classes]
        self.nb_measures = 0

    def run(self):
        begin = time.time()
        while True:
            print('Number of measures: %d' % self.nb_measures)
            try:
                for bench in self.benchmarks:
                    print('\t%s' % bench.__class__.__name__)
                    bench.run_all()
            except KeyboardInterrupt:
                end = time.time()
                break
            self.nb_measures += 1
        for bench in self.benchmarks:
            bench.compute_ratios()
        print('\nFinished after a time of %fs.' % (end-begin))

    def print_results(self):
        for bench in self.benchmarks:
            bench.print_results()

    def plot_results(self, file_name):
        with open(file_name, 'w') as f:
            f.write('%% Number of measures: %d\n' % self.nb_measures)
            f.write('\\documentclass{article}\n')
            f.write('\\usepackage{tikz, pgfplots}\n')
            f.write('\\begin{document}\n')
            f.write('\n')
            for bench in self.benchmarks:
                bench.plot_results(f)
                f.write('\n')
            f.write('\\end{document}')

if __name__ == '__main__':
    # Parse arguments
    parser = argparse.ArgumentParser(
            description='Benchmark for pyroaring')
    parser.add_argument('-p', '--plot', type=str,
            default=None, help='Latex file to generate.')
    parser.add_argument('-d', '--dump', type=str,
            default=None, help='Pickle file to dump.')
    parser.add_argument('-l', '--load', type=str,
            default=None, help='Pickle file to load. If specified, the computation will not be done, the results will be loaded from the file instead.')
    args = parser.parse_args()

    if args.load is None:
        runer = Runer()
    else:
        with open(args.load, 'rb') as f:
            runer = pickle.load(f)
        print('Loaded results from file %s\n' % args.load)
    print('Now launching the benchmark. You can stop it properly at any time by pressing Ctrl-C (results will then be written and plotted).\n')
    runer.run()
    runer.print_results()
    print('')
    if args.plot is not None:
        runer.plot_results(args.plot)
        print('\nPlots written in file %s' % args.plot)
    if args.dump is not None:
        with open(args.dump, 'wb') as f:
            pickle.dump(runer, f)
        print('Results dumped in file %s' % args.dump)
<commit_msg>Python 2 compatibility for the benchmark.<commit_after>#! /usr/bin/env python3

import time
import random
import abc
import pickle
import argparse
import sys
from pyroaring import BitMap

def mean(l):
    if len(l) > 0:
        s = float(sum(l))
        return s/len(l)
    else:
        return float('NaN')

class AbstractBenchMark:
    max_int = 2**32
    sizes = [2**n for n in range(0, 22)]
    classes = [BitMap, set]

    @classmethod
    def get_data(cls, size, bound):
        assert size < cls.max_int
        assert bound < cls.max_int
        assert size < bound
        return random.sample(range(0, bound), size)

    @classmethod
    def sparse_data(cls, size):
        bound = size*16
        return cls.get_data(size, bound)

    @classmethod
    def dense_data(cls, size):
        bound = size*2
        return cls.get_data(size, bound)

    def __init__(self):
        self.results = {data_func.__name__ : dict() for data_func in [self.sparse_data, self.dense_data]}
        for data_func, d in self.results.items():
            for size in self.sizes:
                d[size] = {cls:list() for cls in self.classes}

    def compute_ratios(self):
        self.ratios = {data_func : dict() for data_func in self.results.keys()}
        for data_func, d in self.ratios.items():
            for size in self.sizes:
                d[size] = {cls:list() for cls in self.classes[1:]}
                for cls in self.classes[1:]:
                    r = list()
                    result = self.results[data_func][size]
                    for i in range(len(result[cls])):
                        r.append(result[cls][i]/result[self.classes[0]][i])
                    d[size][cls] = mean(r)

        def run(self, data_func, size):
            raise NotImplementedError

    def run_all(self):
        for data_func in [self.dense_data, self.sparse_data]:
            for size in self.sizes:
                times = self.run(data_func, size)
                for i, cls in enumerate(self.classes):
                    self.results[data_func.__name__][size][cls].append(times[i])

    def print_results(self):
        print('\n# %s' % self.__class__.__name__)
        for data_func, d in sorted(self.ratios.items(), key = lambda x: x[0]):
            print('\t%s' % data_func)
            for size, d_size in sorted(d.items()):
                print('\t\tSize=%d' % size)
                for test_class, ratio in sorted(d_size.items(), key = lambda x: x[0].__name__):
                    print('\t\t\tRatio %s/%s: %f' % (test_class.__name__, self.classes[0].__name__, ratio))

    @staticmethod
    def _results_to_tex(f, results): # results has the form { size1 : result1, size2 : result2, ...}
        f.write('\t\t\t\\addplot table {\n')
        for size, value in sorted(results.items()):
            f.write('\t\t\t\t%d %f\n' % (size, value))
        f.write('\t\t\t};\n')

    @staticmethod
    def add_legend(f, legend):
        f.write('\t\t\t\\addlegendentry{%s}\n' % legend.replace('_', '\\_'))

    def _plot_func(self, f):
        f.write('\t\\begin{tikzpicture}\n')
        f.write('\t\t\\begin{axis}[\n')
        f.write('\t\t\txlabel=size,\n')
        f.write('\t\t\tylabel=speedup,\n')
        f.write('\t\t\txmode=log,\n')
        f.write('\t\t\tymode=log,\n')
        f.write('\t\t\txmajorgrids,\n')
        f.write('\t\t\tymajorgrids,\n')
        f.write('\t\t\tlegend style={at={(0, 1)},anchor=north west,fill=none}\n')
        f.write('\t\t\t]\n')
        for data_func, d_func in sorted(self.ratios.items(), key=lambda x: x[0]):
            results = dict()
            # First, we "inverse" the dict...
            for size, d_size in d_func.items():
                for test_class, value in d_size.items():
                    try:
                        results[test_class][size] = value
                    except KeyError:
                        results[test_class] = {size : value}
            for test_class in self.classes[1:]:
                self._results_to_tex(f, results[test_class])
        # Finally, the legend
        for data_func in sorted(self.ratios, key=lambda x: x):
            for test_class in self.classes[1:]:
                self.add_legend(f, 'ratio %s/%s: %s' % (test_class.__name__, self.classes[0].__name__, data_func))
        f.write('\t\t\\end{axis}\n')
        f.write('\t\\end{tikzpicture}\n')

    def plot_results(self, f):
        f.write('% Do not forget \\usepackage{pgfplots}\n')
        f.write('\\begin{figure}\n')
        self._plot_func(f)
        f.write('\t\\caption{%s}\n' % self.__class__.__name__)
        f.write('\\end{figure}\n')

class AbstractConstructorBenchMark(AbstractBenchMark):

    def get_values(self, data_func, size):
        raise NotImplementedError

    def run_cls(self, cls, values):
        begin = time.time()
        result = cls(values)
        return time.time()-begin

    def run(self, data_func, size):
        values = self.get_values(data_func, size)
        assert len(values) == size
        return [self.run_cls(cls, values) for cls in self.classes]

class ListConstructor(AbstractConstructorBenchMark):

    def get_values(self, data_func, size):
        return data_func(size)

class RangeConstructor(AbstractConstructorBenchMark):

    def get_values(self, data_func, size):
        start = random.randint(0, self.max_int-size)
        max_step = (self.max_int-start)//size
        if data_func == self.sparse_data:
            step = random.randint(max_step//2, max_step)
        elif data_func == self.dense_data:
            step = random.randint(1, min(max_step, 4))
        else:
            assert False
        end = start + step*size
        return range(start, end, step)

class CopyConstructor(AbstractConstructorBenchMark):

    def get_values(self, data_func, size):
        return data_func(size)

    def run_cls(self, cls, values):
        values = cls(values)
        return AbstractConstructorBenchMark.run_cls(self, cls, values)

class AbstractBinaryOpBenchMark(AbstractBenchMark):

    def do_binary_op(self, values1, values2):
        raise NotImplementedError

    def run_cls(self, cls, values1, values2):
        values1 = cls(values1)
        values2 = cls(values2)
        begin = time.time()
        self.do_binary_op(values1, values2)
        return time.time()-begin

    def run(self, data_func, size):
        values1 = data_func(size)
        values2 = data_func(size)
        return [self.run_cls(cls, values1, values2) for cls in self.classes]

class BinaryUnion(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 | values2

class BinaryIntersection(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 & values2

class BinarySymmetricDifference(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 ^ values2

class BinaryDifference(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 - values2

class BinaryUnionInPlace(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 |= values2

class BinaryIntersectionInPlace(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 &= values2

class BinaryDifferenceInPlace(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 ^= values2

class BinarySymmetricDifferenceInPlace(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 ^= values2

class EqualityTest(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 == values2

class InclusionTest(AbstractBinaryOpBenchMark):

    def do_binary_op(self, values1, values2):
        values1 < values2

class AbstractManyOpBenchMark(AbstractBenchMark):

    def do_many_op(self, values):
        raise NotImplementedError

    def run_cls(self, cls, values_list):
        values_list = [cls(v) for v in values_list]
        begin = time.time()
        self.do_many_op(cls, *values_list)
        return time.time()-begin

    def run(self, data_func, size):
        nb = 20#random.randint(2, 100)
        values_list = [data_func(size) for _ in range(nb)]
        return [self.run_cls(cls, values_list) for cls in self.classes]

class ManyUnion(AbstractManyOpBenchMark):

    def do_many_op(self, cls, *values):
        cls.union(*values)

classes = [ListConstructor, RangeConstructor, CopyConstructor,
    BinaryUnion, BinaryIntersection, BinaryDifference, BinarySymmetricDifference,
    BinaryUnionInPlace, BinaryIntersectionInPlace, BinaryDifferenceInPlace, BinarySymmetricDifferenceInPlace,
    EqualityTest, InclusionTest,
    ManyUnion]

class Runer:
    def __init__(self):
        self.benchmarks = [cls() for cls in classes]
        self.nb_measures = 0

    def run(self):
        begin = time.time()
        while True:
            print('Number of measures: %d' % self.nb_measures)
            try:
                for bench in self.benchmarks:
                    print('\t%s' % bench.__class__.__name__)
                    bench.run_all()
            except KeyboardInterrupt:
                end = time.time()
                break
            self.nb_measures += 1
        for bench in self.benchmarks:
            bench.compute_ratios()
        print('\nFinished after a time of %fs.' % (end-begin))

    def print_results(self):
        for bench in self.benchmarks:
            bench.print_results()

    def plot_results(self, file_name):
        with open(file_name, 'w') as f:
            f.write('%% Number of measures: %d\n' % self.nb_measures)
            f.write('\\documentclass{article}\n')
            f.write('\\usepackage{tikz, pgfplots}\n')
            f.write('\\begin{document}\n')
            f.write('\n')
            for bench in self.benchmarks:
                bench.plot_results(f)
                f.write('\n')
            f.write('\\end{document}')

if __name__ == '__main__':
    # Parse arguments
    parser = argparse.ArgumentParser(
            description='Benchmark for pyroaring')
    parser.add_argument('-p', '--plot', type=str,
            default=None, help='Latex file to generate.')
    parser.add_argument('-d', '--dump', type=str,
            default=None, help='Pickle file to dump.')
    parser.add_argument('-l', '--load', type=str,
            default=None, help='Pickle file to load. If specified, the computation will not be done, the results will be loaded from the file instead.')
    args = parser.parse_args()

    if args.load is None:
        runer = Runer()
    else:
        with open(args.load, 'rb') as f:
            runer = pickle.load(f)
        print('Loaded results from file %s\n' % args.load)
    print('Now launching the benchmark. You can stop it properly at any time by pressing Ctrl-C (results will then be written and plotted).\n')
    runer.run()
    runer.print_results()
    print('')
    if args.plot is not None:
        runer.plot_results(args.plot)
        print('\nPlots written in file %s' % args.plot)
    if args.dump is not None:
        with open(args.dump, 'wb') as f:
            pickle.dump(runer, f)
        print('Results dumped in file %s' % args.dump)
<|endoftext|>


<commit_before>#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2005-2006 Async Open Source
#
# 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
# Lesser 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
#
# Author(s): Lorenzo Gil Sanchez <lgs@sicem.biz>
#            Johan Dahlin <jdahlin@async.com.br>
#

"""Gazpacho integration: loader and extensions"""

import datetime
import gettext
import os
import warnings

import gobject
import gtk
from gazpacho.editor import PropertyCustomEditor
from gazpacho.loader.loader import ObjectBuilder
from gazpacho.loader.custom import Adapter, ComboBoxAdapter, \
     PythonWidgetAdapter, adapter_registry
from gazpacho.properties import prop_registry, CustomProperty, StringType
from gazpacho.widgets.base.base import ContainerAdaptor
from gazpacho.widgets.base.box import BoxAdaptor

from kiwi.datatypes import currency, Decimal
from kiwi.environ import environ
from kiwi.log import Logger
from kiwi.python import disabledeprecationcall
from kiwi.ui.hyperlink import HyperLink
from kiwi.ui.objectlist import Column, ObjectList
from kiwi.ui.widgets.checkbutton import ProxyCheckButton
from kiwi.ui.widgets.combo import ProxyComboEntry, ProxyComboBox, \
     ProxyComboBoxEntry
from kiwi.ui.widgets.entry import ProxyDateEntry, ProxyEntry
from kiwi.ui.widgets.label import ProxyLabel
from kiwi.ui.widgets.radiobutton import ProxyRadioButton
from kiwi.ui.widgets.spinbutton import ProxySpinButton
from kiwi.ui.widgets.textview import ProxyTextView

# Backwards compatibility + pyflakes
from kiwi.ui.widgets.combobox import ComboBox, ComboBoxEntry
from kiwi.ui.widgets.list import List
HyperLink
_ = lambda m: gettext.dgettext('kiwi', m)

log = Logger('gazpacholoader')

class Builder(ObjectBuilder):
    def find_resource(self, filename):
        return environ.find_resource("pixmaps",  filename)

class GazpachoWidgetTree:
    """Example class of GladeAdaptor that uses Gazpacho loader to load the
    glade files
    """
    def __init__(self, view, gladefile, widgets, gladename=None, domain=None):

        if not gladefile:
            raise ValueError("A gladefile wasn't provided.")
        elif not isinstance(gladefile, basestring):
            raise TypeError(
                  "gladefile should be a string, found %s" % type(gladefile))
        filename = os.path.splitext(os.path.basename(gladefile))[0]
        self._filename = filename + '.glade'
        self._view = view
        self._gladefile = environ.find_resource("glade", self._filename)
        self._widgets =  (widgets or view.widgets or [])[:]
        self.gladename = gladename or filename
        self._showwarning = warnings.showwarning
        warnings.showwarning = self._on_load_warning
        self._tree = Builder(self._gladefile, domain=domain)
        warnings.showwarning = self._showwarning
        if not self._widgets:
            self._widgets = [w.get_data("gazpacho::object-id")
                             for w in self._tree.get_widgets()]
        self._attach_widgets()

    def _on_load_warning(self, warning, category, file, line):
        self._showwarning('while loading glade file: %s' % warning,
                          category, self._filename, '???')

    def _attach_widgets(self):
        # Attach widgets in the widgetlist to the view specified, so
        # widgets = [label1, button1] -> view.label1, view.button1
        for w in self._widgets:
            widget = self._tree.get_widget(w)
            if widget is not None:
                setattr(self._view, w, widget)
            else:
                log.warn("Widget %s was not found in glade widget tree." % w)

    def get_widget(self, name):
        """Retrieves the named widget from the View (or glade tree)"""
        name = name.replace('.', '_')
        name = name.replace('-', '_')
        widget = self._tree.get_widget(name)
        if widget is None:
            raise AttributeError(
                  "Widget %s not found in view %s" % (name, self._view))
        return widget

    def get_widgets(self):
        return self._tree.get_widgets()

    def signal_autoconnect(self, dic):
        self._tree.signal_autoconnect(dic)

    def get_sizegroups(self):
        return self._tree.sizegroups

# Normal widgets
for prop in ('normal-color', 'normal-underline', 'normal-bold',
             'hover-color', 'hover-underline', 'hover-bold',
             'active-color', 'active-underline', 'active-bold'):
    prop_registry.override_simple('HyperLink::%s' % prop, editable=False)

class HyperLinkAdaptor(ContainerAdaptor):
    def fill_empty(self, context, widget):
        pass

    def post_create(self, context, widget, interactive):
        widget.set_text(widget.get_name())

class ComboEntryAdaptor(BoxAdaptor):
    def get_children(self, context, comboentry):
        return []

class DateEntryAdaptor(BoxAdaptor):
    def get_children(self, context, comboentry):
        return []

class KiwiColumnAdapter(Adapter):
    object_type = Column
    def construct(self, name, gtype, properties):
        return Column(name)
adapter_registry.register_adapter(KiwiColumnAdapter)

class ObjectListAdapter(PythonWidgetAdapter):
    object_type = ObjectList
    def construct(self, name, gtype, properties):
        if gtype == List:
            gtype == ObjectList
        return super(ObjectListAdapter, self).construct(name, gtype,
                                                        properties)
adapter_registry.register_adapter(ObjectListAdapter)

# Framework widgets

class DataTypeAdaptor(PropertyCustomEditor):
    def __init__(self):
        super(DataTypeAdaptor, self).__init__()
        self._input = self.create_editor()

    def get_editor_widget(self):
        return self._input

    def get_data_types(self):
        """
        Subclasses should override this.
        Expected to return a list of 2 sized tuples with
        name of type and type, to be used in a combo box.
        """
        raise NotImplementedError

    def create_editor(self):
        model = gtk.ListStore(str, object)
        for datatype in self.get_data_types():
            model.append(datatype)
        combo = gtk.ComboBox(model)
        renderer = gtk.CellRendererText()
        combo.pack_start(renderer)
        combo.add_attribute(renderer, 'text', 0)
        combo.set_active(0)
        combo.set_data('connection-id', -1)
        return combo

    def update(self, context, kiwiwidget, proxy):
        combo = self._input
        connection_id = combo.get_data('connection-id')
        if (connection_id != -1):
            combo.disconnect(connection_id)
        model = combo.get_model()
        connection_id = combo.connect('changed', self._editor_edit,
                                      proxy, model)
        combo.set_data('connection-id', connection_id)
        value = kiwiwidget.get_property('data-type')
        for row in model:
            if row[1] == value:
                combo.set_active_iter(row.iter)
                break

    def _editor_edit(self, combo, proxy, model):
        active_iter = combo.get_active_iter()
        proxy.set_value(model[active_iter][1])

class SpinBtnDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Currency'), currency)
            ]

class EntryDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('String'), str),
            (_('Unicode'), unicode),
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Currency'), currency),
            (_('Date'), datetime.date),
            (_('Date and Time'), datetime.datetime),
            (_('Time'), datetime.time),
            (_('Object'), object)
            ]

class TextViewDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('String'), str),
            (_('Unicode'), unicode),
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Date'), datetime.date),
            ]

class ComboBoxDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('String'), str),
            (_('Unicode'), unicode),
            (_('Boolean'), bool),
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Object'), object)
            ]

class LabelDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('String'), str),
            (_('Unicode'), unicode),
            (_('Boolean'), bool),
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Date'), datetime.date),
            (_('Date and Time'), datetime.datetime),
            (_('Time'), datetime.time),
            (_('Currency'), currency)
            ]

class DataType(CustomProperty, StringType):
    translatable = False
    def save(self):
        value = self.get()
        if value is not None:
            return value.__name__

class BoolOnlyDataType(CustomProperty, StringType):
    translatable = False
    editable = False
    def save(self):
        return 'bool'

class DateOnlyDataType(CustomProperty, StringType):
    translatable = False
    editable = False
    def save(self):
        return 'date'

class ModelProperty(CustomProperty, StringType):
    translatable = False

class DataValueProperty(CustomProperty, StringType):
    translatable = False

# Register widgets which have data-type and model-attributes
# ComboBox is a special case, it needs to inherit from another
# adapter and need to support two types.
class KiwiComboBoxAdapter(ComboBoxAdapter):
    object_type = ProxyComboBox, ProxyComboBoxEntry
    def construct(self, name, gtype, properties):
        if gtype in (ProxyComboBox.__gtype__,
                     ComboBox.__gtype__):
            object_type = ProxyComboBox
        elif gtype in (ProxyComboBoxEntry.__gtype__,
                       ComboBoxEntry.__gtype__):
            object_type = ProxyComboBoxEntry
        else:
            raise AssertionError("Unknown ComboBox GType: %r" % gtype)

        obj = disabledeprecationcall(object_type)
        obj.set_name(name)
        return obj
adapter_registry.register_adapter(KiwiComboBoxAdapter)

def register_widgets():
    for gobj, editor, data_type in [
        (ProxyEntry, EntryDataType, DataType),
        (ProxyDateEntry, None, DateOnlyDataType),
        (ProxyCheckButton, None, BoolOnlyDataType),
        (ProxyLabel, LabelDataType, DataType),
        (ProxyComboBox, ComboBoxDataType, DataType),
        (ProxyComboBoxEntry, ComboBoxDataType, DataType),
        (ProxyComboEntry, ComboBoxDataType, DataType),
        (ProxySpinButton, SpinBtnDataType, DataType),
        (ProxyRadioButton, None, BoolOnlyDataType),
        (ProxyTextView, TextViewDataType, DataType)
        ]:
        # Property overrides, used in the editor
        type_name = gobject.type_name(gobj)

        data_name = type_name + '::data-type'
        if editor:
            prop_registry.override_simple(data_name, data_type, custom_editor=editor)
        else:
            prop_registry.override_simple(data_name, data_type)

        prop_registry.override_simple(type_name + '::model-attribute',
                                      ModelProperty)

        if issubclass(gobj, ProxyRadioButton):
            prop_registry.override_simple(type_name + '::data-value',
                                          DataValueProperty)
        # Register custom adapters, since gobject.new is broken in 2.6
        # Used by loader, eg in gazpacho and in applications
        # ComboBox is registered above
        if gobj == ProxyComboBox:
            continue

        klass = type('Kiwi%sAdapter', (PythonWidgetAdapter,),
                     dict(object_type=gobj))
        adapter_registry.register_adapter(klass)

if not environ.epydoc:
    register_widgets()
<commit_msg>make it work with gazpacho trunk<commit_after>#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2005-2006 Async Open Source
#
# 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
# Lesser 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
#
# Author(s): Lorenzo Gil Sanchez <lgs@sicem.biz>
#            Johan Dahlin <jdahlin@async.com.br>
#

"""Gazpacho integration: loader and extensions"""

import datetime
import gettext
import os
import warnings

import gobject
import gtk

try:
    from gazpacho.propertyeditor import PropertyCustomEditor
except ImportError:
    from gazpacho.editor import PropertyCustomEditor

from gazpacho.loader.loader import ObjectBuilder
from gazpacho.loader.custom import Adapter, ComboBoxAdapter, \
     PythonWidgetAdapter, adapter_registry
from gazpacho.properties import prop_registry, CustomProperty, StringType
from gazpacho.widgets.base.base import ContainerAdaptor
from gazpacho.widgets.base.box import BoxAdaptor

from kiwi.datatypes import currency, Decimal
from kiwi.environ import environ
from kiwi.log import Logger
from kiwi.python import disabledeprecationcall
from kiwi.ui.hyperlink import HyperLink
from kiwi.ui.objectlist import Column, ObjectList
from kiwi.ui.widgets.checkbutton import ProxyCheckButton
from kiwi.ui.widgets.combo import ProxyComboEntry, ProxyComboBox, \
     ProxyComboBoxEntry
from kiwi.ui.widgets.entry import ProxyDateEntry, ProxyEntry
from kiwi.ui.widgets.label import ProxyLabel
from kiwi.ui.widgets.radiobutton import ProxyRadioButton
from kiwi.ui.widgets.spinbutton import ProxySpinButton
from kiwi.ui.widgets.textview import ProxyTextView

# Backwards compatibility + pyflakes
from kiwi.ui.widgets.combobox import ComboBox, ComboBoxEntry
from kiwi.ui.widgets.list import List
HyperLink
_ = lambda m: gettext.dgettext('kiwi', m)

log = Logger('gazpacholoader')

class Builder(ObjectBuilder):
    def find_resource(self, filename):
        return environ.find_resource("pixmaps",  filename)

class GazpachoWidgetTree:
    """Example class of GladeAdaptor that uses Gazpacho loader to load the
    glade files
    """
    def __init__(self, view, gladefile, widgets, gladename=None, domain=None):

        if not gladefile:
            raise ValueError("A gladefile wasn't provided.")
        elif not isinstance(gladefile, basestring):
            raise TypeError(
                  "gladefile should be a string, found %s" % type(gladefile))
        filename = os.path.splitext(os.path.basename(gladefile))[0]
        self._filename = filename + '.glade'
        self._view = view
        self._gladefile = environ.find_resource("glade", self._filename)
        self._widgets =  (widgets or view.widgets or [])[:]
        self.gladename = gladename or filename
        self._showwarning = warnings.showwarning
        warnings.showwarning = self._on_load_warning
        self._tree = Builder(self._gladefile, domain=domain)
        warnings.showwarning = self._showwarning
        if not self._widgets:
            self._widgets = [w.get_data("gazpacho::object-id")
                             for w in self._tree.get_widgets()]
        self._attach_widgets()

    def _on_load_warning(self, warning, category, file, line):
        self._showwarning('while loading glade file: %s' % warning,
                          category, self._filename, '???')

    def _attach_widgets(self):
        # Attach widgets in the widgetlist to the view specified, so
        # widgets = [label1, button1] -> view.label1, view.button1
        for w in self._widgets:
            widget = self._tree.get_widget(w)
            if widget is not None:
                setattr(self._view, w, widget)
            else:
                log.warn("Widget %s was not found in glade widget tree." % w)

    def get_widget(self, name):
        """Retrieves the named widget from the View (or glade tree)"""
        name = name.replace('.', '_')
        name = name.replace('-', '_')
        widget = self._tree.get_widget(name)
        if widget is None:
            raise AttributeError(
                  "Widget %s not found in view %s" % (name, self._view))
        return widget

    def get_widgets(self):
        return self._tree.get_widgets()

    def signal_autoconnect(self, dic):
        self._tree.signal_autoconnect(dic)

    def get_sizegroups(self):
        return self._tree.sizegroups

# Normal widgets
for prop in ('normal-color', 'normal-underline', 'normal-bold',
             'hover-color', 'hover-underline', 'hover-bold',
             'active-color', 'active-underline', 'active-bold'):
    prop_registry.override_simple('HyperLink::%s' % prop, editable=False)

class HyperLinkAdaptor(ContainerAdaptor):
    def fill_empty(self, context, widget):
        pass

    def post_create(self, context, widget, interactive):
        widget.set_text(widget.get_name())

class ComboEntryAdaptor(BoxAdaptor):
    def get_children(self, context, comboentry):
        return []

class DateEntryAdaptor(BoxAdaptor):
    def get_children(self, context, comboentry):
        return []

class KiwiColumnAdapter(Adapter):
    object_type = Column
    def construct(self, name, gtype, properties):
        return Column(name)
adapter_registry.register_adapter(KiwiColumnAdapter)

class ObjectListAdapter(PythonWidgetAdapter):
    object_type = ObjectList
    def construct(self, name, gtype, properties):
        if gtype == List:
            gtype == ObjectList
        return super(ObjectListAdapter, self).construct(name, gtype,
                                                        properties)
adapter_registry.register_adapter(ObjectListAdapter)

# Framework widgets

class DataTypeAdaptor(PropertyCustomEditor):
    def __init__(self):
        super(DataTypeAdaptor, self).__init__()
        self._input = self.create_editor()

    def get_editor_widget(self):
        return self._input

    def get_data_types(self):
        """
        Subclasses should override this.
        Expected to return a list of 2 sized tuples with
        name of type and type, to be used in a combo box.
        """
        raise NotImplementedError

    def create_editor(self):
        model = gtk.ListStore(str, object)
        for datatype in self.get_data_types():
            model.append(datatype)
        combo = gtk.ComboBox(model)
        renderer = gtk.CellRendererText()
        combo.pack_start(renderer)
        combo.add_attribute(renderer, 'text', 0)
        combo.set_active(0)
        combo.set_data('connection-id', -1)
        return combo

    def update(self, context, kiwiwidget, proxy):
        combo = self._input
        connection_id = combo.get_data('connection-id')
        if (connection_id != -1):
            combo.disconnect(connection_id)
        model = combo.get_model()
        connection_id = combo.connect('changed', self._editor_edit,
                                      proxy, model)
        combo.set_data('connection-id', connection_id)
        value = kiwiwidget.get_property('data-type')
        for row in model:
            if row[1] == value:
                combo.set_active_iter(row.iter)
                break

    def _editor_edit(self, combo, proxy, model):
        active_iter = combo.get_active_iter()
        proxy.set_value(model[active_iter][1])

class SpinBtnDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Currency'), currency)
            ]

class EntryDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('String'), str),
            (_('Unicode'), unicode),
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Currency'), currency),
            (_('Date'), datetime.date),
            (_('Date and Time'), datetime.datetime),
            (_('Time'), datetime.time),
            (_('Object'), object)
            ]

class TextViewDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('String'), str),
            (_('Unicode'), unicode),
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Date'), datetime.date),
            ]

class ComboBoxDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('String'), str),
            (_('Unicode'), unicode),
            (_('Boolean'), bool),
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Object'), object)
            ]

class LabelDataType(DataTypeAdaptor):
    def get_data_types(self):
        return [
            (_('String'), str),
            (_('Unicode'), unicode),
            (_('Boolean'), bool),
            (_('Integer'), int),
            (_('Float'), float),
            (_('Decimal'), Decimal),
            (_('Date'), datetime.date),
            (_('Date and Time'), datetime.datetime),
            (_('Time'), datetime.time),
            (_('Currency'), currency)
            ]

class DataType(CustomProperty, StringType):
    translatable = False
    def save(self):
        value = self.get()
        if value is not None:
            return value.__name__

class BoolOnlyDataType(CustomProperty, StringType):
    translatable = False
    editable = False
    def save(self):
        return 'bool'

class DateOnlyDataType(CustomProperty, StringType):
    translatable = False
    editable = False
    def save(self):
        return 'date'

class ModelProperty(CustomProperty, StringType):
    translatable = False

class DataValueProperty(CustomProperty, StringType):
    translatable = False

# Register widgets which have data-type and model-attributes
# ComboBox is a special case, it needs to inherit from another
# adapter and need to support two types.
class KiwiComboBoxAdapter(ComboBoxAdapter):
    object_type = ProxyComboBox, ProxyComboBoxEntry
    def construct(self, name, gtype, properties):
        if gtype in (ProxyComboBox.__gtype__,
                     ComboBox.__gtype__):
            object_type = ProxyComboBox
        elif gtype in (ProxyComboBoxEntry.__gtype__,
                       ComboBoxEntry.__gtype__):
            object_type = ProxyComboBoxEntry
        else:
            raise AssertionError("Unknown ComboBox GType: %r" % gtype)

        obj = disabledeprecationcall(object_type)
        obj.set_name(name)
        return obj
adapter_registry.register_adapter(KiwiComboBoxAdapter)

def register_widgets():
    for gobj, editor, data_type in [
        (ProxyEntry, EntryDataType, DataType),
        (ProxyDateEntry, None, DateOnlyDataType),
        (ProxyCheckButton, None, BoolOnlyDataType),
        (ProxyLabel, LabelDataType, DataType),
        (ProxyComboBox, ComboBoxDataType, DataType),
        (ProxyComboBoxEntry, ComboBoxDataType, DataType),
        (ProxyComboEntry, ComboBoxDataType, DataType),
        (ProxySpinButton, SpinBtnDataType, DataType),
        (ProxyRadioButton, None, BoolOnlyDataType),
        (ProxyTextView, TextViewDataType, DataType)
        ]:
        # Property overrides, used in the editor
        type_name = gobject.type_name(gobj)

        data_name = type_name + '::data-type'
        if editor:
            prop_registry.override_simple(data_name, data_type, custom_editor=editor)
        else:
            prop_registry.override_simple(data_name, data_type)

        prop_registry.override_simple(type_name + '::model-attribute',
                                      ModelProperty)

        if issubclass(gobj, ProxyRadioButton):
            prop_registry.override_simple(type_name + '::data-value',
                                          DataValueProperty)
        # Register custom adapters, since gobject.new is broken in 2.6
        # Used by loader, eg in gazpacho and in applications
        # ComboBox is registered above
        if gobj == ProxyComboBox:
            continue

        klass = type('Kiwi%sAdapter', (PythonWidgetAdapter,),
                     dict(object_type=gobj))
        adapter_registry.register_adapter(klass)

if not environ.epydoc:
    register_widgets()
<|endoftext|>


<commit_before>#!/usr/bin/env python

"""A tool for looking for indirect jumps and calls in x86 binaries.

   Helpful to verify whether or not retpoline mitigations are catching
   all of the indirect branches in a binary and telling you which
   functions the remaining ones are in (assembly, etc).

   Depends on llvm-objdump being in your path and is tied to the
   dump format.
"""

import os
import sys
import re
import subprocess
import optparse

# Look for indirect calls/jmps in a binary. re: (call|jmp).*\*
def look_for_indirect(file):
    args = ['llvm-objdump']
    args.extend(["-d"])
    args.extend([file])

    p = subprocess.Popen(args=args, stdin=None, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    (stdout,stderr) = p.communicate()

    function = ""
    for line in stdout.splitlines():
        if line.startswith(' ') == False:
            function = line
        result = re.search('(call|jmp).*\*', line)
        if result != None:
            # TODO: Perhaps use cxxfilt to demangle functions?
            print function
            print line
    return

# Compare the debug info between two files.
def main(args):
    # No options currently other than the binary.
    parser = optparse.OptionParser("%prog [options] <binary>")
    (opts, args) = parser.parse_args(args)
    if len(args) != 2:
        parser.error("invalid number of arguments: %s" % len(args))
    look_for_indirect(args[1])

if __name__ == '__main__':
    main(sys.argv)
<commit_msg>Remove a stale comment cut and pasted from another file.<commit_after>#!/usr/bin/env python

"""A tool for looking for indirect jumps and calls in x86 binaries.

   Helpful to verify whether or not retpoline mitigations are catching
   all of the indirect branches in a binary and telling you which
   functions the remaining ones are in (assembly, etc).

   Depends on llvm-objdump being in your path and is tied to the
   dump format.
"""

import os
import sys
import re
import subprocess
import optparse

# Look for indirect calls/jmps in a binary. re: (call|jmp).*\*
def look_for_indirect(file):
    args = ['llvm-objdump']
    args.extend(["-d"])
    args.extend([file])

    p = subprocess.Popen(args=args, stdin=None, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    (stdout,stderr) = p.communicate()

    function = ""
    for line in stdout.splitlines():
        if line.startswith(' ') == False:
            function = line
        result = re.search('(call|jmp).*\*', line)
        if result != None:
            # TODO: Perhaps use cxxfilt to demangle functions?
            print function
            print line
    return

def main(args):
    # No options currently other than the binary.
    parser = optparse.OptionParser("%prog [options] <binary>")
    (opts, args) = parser.parse_args(args)
    if len(args) != 2:
        parser.error("invalid number of arguments: %s" % len(args))
    look_for_indirect(args[1])

if __name__ == '__main__':
    main(sys.argv)
<|endoftext|>


<commit_before>import multiprocessing
from multiprocessing import Process
import logging

from cloudscheduler.lib.db_config import Config
from cloudscheduler.lib.poller_functions import set_orange_count

class ProcessMonitor:
    config = None
    processes = {}
    process_ids = {}
    orange_count_row = None
    previous_orange_count = 0
    current_orange_count = 0
    logging = None

    def __init__(self, file_name, pool_size, orange_count_row, process_ids=None):
        self.config = Config('/etc/cloudscheduler/cloudscheduler.yaml', file_name, pool_size=pool_size)
        self.logging = logging.getLogger()
        logging.basicConfig(
            filename=self.config.log_file,
            level=self.config.log_level,
            format='%(asctime)s - %(processName)-12s - %(levelname)s - %(message)s')
        self.orange_count_row = orange_count_row
        self.previous_count, self.current_count = set_orange_count(self.logging, self.config, orange_count_row, 1, 0)
        self.process_ids = process_ids

    def get_process_ids(self):
        return self.process_ids

    def add_process_id(self, process_id, function):
        self.process_ids[process_id] = function
        return

    def del_process(self, process_id):
        proc = self.processes[process_id]
        if self.is_alive(proc):
            proc.join()
        del self.processes[process_id]
        self.process_ids.pop(process_id)
        return

    def get_logging(self):
        return self.logging

    def get_config(self):
        return self.config

    def start_all(self):
        for process in self.process_ids:
            if process not in self.processes or not processes[process].is_alive():
                if process in self.processes:
                    logging.error("Restarting %s...", process)
                else:
                    logging.info("Starting %s...", process)
                self.processes[process] = Process(target=self.process_ids[process])
                self.processes[process].start()

    def restart_process(self, process):
        processes[process] = Process(target=process_ids[process])
        processes[process].start()

    def is_alive(self, process):
        return self.processes[process].is_alive()

    def join_all(self):
        for proc in self.processes:
            try:
                proc.join()
            except:
                logging.error("failed to join process %s", proc.name)

    def check_processes(self):
        orange = False
        for process in self.process_ids:
            if process not in self.processes or not self.is_alive(process):
                if process in self.processes:
                    orange = True
                    logging.error("%s process died, restarting...", process)
                    del self.processes[process]
                else:
                    self.logging.info("Restarting %s process", process)
                restart_process(process)
                time.sleep(self.config.sleep_interval_main_short)
        if orange:
                self.previous_orange_count, self.current_orange_count = set_orange_count(self.logging, self.config, self.orange_count_row, self.previous_orange_count, self.current_orange_count+1)
        else:
            self.previous_orange_count, self.current_orange_count = set_orange_count(self.logging, self.config, self.orange_count_row, self.previous_orange_count, self.current_orange_count-1)
<commit_msg>fix class attribute access in ProcessMonitor<commit_after>import multiprocessing
from multiprocessing import Process
import logging
import time

from cloudscheduler.lib.db_config import Config
from cloudscheduler.lib.poller_functions import set_orange_count

class ProcessMonitor:
    config = None
    processes = {}
    process_ids = {}
    orange_count_row = None
    previous_orange_count = 0
    current_orange_count = 0
    logging = None

    def __init__(self, file_name, pool_size, orange_count_row, process_ids=None):
        self.config = Config('/etc/cloudscheduler/cloudscheduler.yaml', file_name, pool_size=pool_size)
        self.logging = logging.getLogger()
        logging.basicConfig(
            filename=self.config.log_file,
            level=self.config.log_level,
            format='%(asctime)s - %(processName)-12s - %(levelname)s - %(message)s')
        self.orange_count_row = orange_count_row
        self.previous_count, self.current_count = set_orange_count(self.logging, self.config, orange_count_row, 1, 0)
        self.process_ids = process_ids

    def get_process_ids(self):
        return self.process_ids

    def add_process_id(self, process_id, function):
        self.process_ids[process_id] = function
        return

    def del_process(self, process_id):
        proc = self.processes[process_id]
        if self.is_alive(proc):
            proc.join()
        del self.processes[process_id]
        self.process_ids.pop(process_id)
        return

    def get_logging(self):
        return self.logging

    def get_config(self):
        return self.config

    def start_all(self):
        for process in self.process_ids:
            if process not in self.processes or not processes[process].is_alive():
                if process in self.processes:
                    logging.error("Restarting %s...", process)
                else:
                    logging.info("Starting %s...", process)
                self.processes[process] = Process(target=self.process_ids[process])
                self.processes[process].start()

    def restart_process(self, process):
        self.processes[process] = Process(target=self.process_ids[process])
        self.processes[process].start()

    def is_alive(self, process):
        return self.processes[process].is_alive()

    def join_all(self):
        for proc in self.processes:
            try:
                proc.join()
            except:
                logging.error("failed to join process %s", proc.name)

    def check_processes(self):
        orange = False
        for process in self.process_ids:
            if process not in self.processes or not self.is_alive(process):
                if process in self.processes:
                    orange = True
                    logging.error("%s process died, restarting...", process)
                    del self.processes[process]
                else:
                    self.logging.info("Restarting %s process", process)
                self.restart_process(process)
                time.sleep(self.config.sleep_interval_main_short)
        if orange:
                self.previous_orange_count, self.current_orange_count = set_orange_count(self.logging, self.config, self.orange_count_row, self.previous_orange_count, self.current_orange_count+1)
        else:
            self.previous_orange_count, self.current_orange_count = set_orange_count(self.logging, self.config, self.orange_count_row, self.previous_orange_count, self.current_orange_count-1)
<|endoftext|>


<commit_before># coding=utf-8
# Filename: __init__.py
"""
A collection of pumps for different kinds of data formats.

"""
from __future__ import division, absolute_import, print_function

from km3pipe.pumps.evt import EvtPump  # noqa
from km3pipe.pumps.daq import DAQPump  # noqa
from km3pipe.pumps.clb import CLBPump  # noqa
from km3pipe.pumps.aanet import AanetPump  # noqa
from km3pipe.pumps.jpp import JPPPump  # noqa
from km3pipe.pumps.ch import CHPump  # noqa
from km3pipe.pumps.hdf5 import HDF5Pump  # noqa
from km3pipe.pumps.pickle import PicklePump  # noqa
<commit_msg>Import HDF5Sink<commit_after># coding=utf-8
# Filename: __init__.py
"""
A collection of pumps for different kinds of data formats.

"""
from __future__ import division, absolute_import, print_function

from km3pipe.pumps.evt import EvtPump  # noqa
from km3pipe.pumps.daq import DAQPump  # noqa
from km3pipe.pumps.clb import CLBPump  # noqa
from km3pipe.pumps.aanet import AanetPump  # noqa
from km3pipe.pumps.jpp import JPPPump  # noqa
from km3pipe.pumps.ch import CHPump  # noqa
from km3pipe.pumps.hdf5 import HDF5Pump, HDF5Sink  # noqa
from km3pipe.pumps.pickle import PicklePump  # noqa
<|endoftext|>