Example #1
0
  def add_message_handlers(self, message_handlers):
    """Add message handlers.

    :param message_handlers: A list/tuple of :class:`MessageHandler`.
    """
    if not typecheck.is_list(message_handlers):
      raise TypeError('message_handlers has to be a list')
    for handler in message_handlers:
      if isinstance(handler, MessageHandler):
        self.add_message_handler(handler)
      else:
        if not typecheck.is_tuple(handler):
          raise TypeError("handler must be a tuple: %s" % type(handler))
        if len(handler) < 2:
          raise Exception("Handler should have more than two parameters")
        handler = list(handler)
        if typecheck.is_tuple(handler[1]):
          handler[1] = Message(*handler[1])
        else:
          raise TypeError()
        if len(handler) > 2:
          if typecheck.is_tuple(handler[2]):
            handler[2] = Message(*handler[2])
          else:
            raise TypeError()
        handler = MessageHandler(*handler)
      self.add_message_handler(handler)
    return self
Example #2
0
  def set_fields(self, fields):
    """Sets message fields.

    :param fields: A list of :class:`Field` instances.

    :raises: TypeError
    """
    self._fields = []
    if not typecheck.is_list(fields) and not typecheck.is_tuple(fields):
      raise TypeError("`fields` has to be list or tuple: %s" % fields)
    for field in fields:
      if typecheck.is_list(field) or typecheck.is_tuple(field):
        field = self.field_type(field[0], field[1])
      elif not isinstance(field, self.field_type):
        field = self.field_type(field)
      self._fields.append(field)
Example #3
0
 def add_dependency(self, dep):
   rule = None
   if typecheck.is_tuple(dep) or typecheck.is_list(dep):
     rule = Fork(name="r%d" % hash(dep), deps=dep)
   else:
     rule = isinstance(dep, Rule) and dep or parse_address(dep)
   if rule:
     dep = rule
   else:
     if callable(dep):
       dep = new.instancemethod(dep, self, self.__class__)
       extra_deps = dep(self.target)
       if extra_deps:
         self.add_dependencies(extra_deps)
       return
   dependency_graph.add_edge(self, dep)
   self._dependencies.append(dep)
Example #4
0
 def _gen_preprocess_macro_options(self, macros):
   """Macros is the usual thing, a list of 1- or 2-tuples, where (name,) means
   undefine (-U) macro 'name', and (name, value) means define (-D) macro 'name'
   to 'value'.
   """
   options = []
   for macro in macros:
     if not (typecheck.is_tuple(macro) and 1 <= len(macro) <= 2):
       raise TypeError("bad macro definition " + repr(macro) + ": " +
                       "each element of 'macros' list must be a 1- or 2-tuple")
     if len (macro) == 1: # undefine this macro
       options.append("-U%s" % macro[0])
     elif len (macro) == 2:
       if macro[1] is None: # define with no explicit value
         options.append("-D%s" % macro[0])
       else:
         # XXX *don't* need to be clever about quoting the
         # macro value here, because we're going to avoid the
         # shell at all costs when we spawn the command!
         options.append("-D%s=%s" % macro)
   return options
Example #5
0
  def add_edge(self, sender, receiver, key=None, attr_dict=None, **attrs):
    """Create an edge between sender and receiver. Return an edge
    represented by :class:`NetworkXEdge` instance which is mainly replacing
    a tuple (sender, receiver, key, attrs).

    `sender` and `receiver` can be represented by a tuple of mapping and
    target communication device.

    `key` is an optional hashable identifier. By default it has
    :const:`NetworkXEdge.KEY_FORMAT` format. Note, this key has to be unique
    in order to distinguish multiedges between a pair of nodes. At the same
    time edge's label equals to the key.

    By default label equals to the key value but can be changes by using
    associated data as follows::

      edge = network.add_edge(Mapping("M1"), Mapping("M2"),
                              label="My serial connection")

    or::

      edge = network.add_edge(Mapping("M1"), Mapping("M2"))
      edge.set_label("My serial connection")

    Note, the nodes for sender and receiver mappings will be automatically
    added if they are not already in the graph.
    """

    # Analyse incomming arguments
    if typecheck.is_tuple(sender):
      (sender, sending_device) = sender
    if typecheck.is_tuple(receiver):
      (receiver, receiving_device) = receiver
    # Setup dictionary of attributes
    if attr_dict is None:
      attr_dict = attrs
    if not self.has_node(sender):
      self.add_node(sender)
    if not self.has_node(receiver):
      self.add_node(receiver)
        # Define hashable identifier
    if not key:
      key_format = attr_dict.get("key_format", None) or \
          NetworkXEdge.KEY_FORMAT
      if not typecheck.is_string(key_format):
        raise TypeError("Has to be string")
      key = key_format % self.number_of_edges(sender, receiver)
    else:
      if not typecheck.is_string(key):
        raise TypeError("Has to be string")
      if self.has_edge(sender, receiver, key):
        raise Exception("Edge %s already exists between %s and %s"
                        % (key, sender, receiver))
    # Define edge label
    if not attr_dict.get("label", None):
      attr_dict["label"] = key
    else:
      if not typecheck.is_string(attr_dict.get("label")):
        raise TypeError("Has to be string")
    # Use super method
    networkx.MultiDiGraph.add_edge(self, sender, receiver,
                                   key=key, attr_dict=attr_dict)
    return NetworkXEdge(sender, receiver, key,
                        self.get_edge_data(sender, receiver, key=key))
Example #6
0
 def add_dependencies(self, deps):
   if not typecheck.is_list(deps) and not typecheck.is_tuple(deps):
     raise TypeError("%s: deps has to be a list or tuple, got %s" %
                     (self, type(deps)))
   for dep in deps:
     self.add_dependency(dep)