Changeset 5f6c3af for fedd/compose.py


Ignore:
Timestamp:
May 17, 2010 3:10:41 AM (14 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-3.01, version-3.02
Children:
eab6ae1
Parents:
c7a982b
Message:

refactor for processing xml

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/compose.py

    rc7a982b r5f6c3af  
    55import os
    66import random
     7import copy
    78
    89from optparse import OptionParser, OptionValueError
    910from federation.remote_service import service_caller
     11from federation.service_error import service_error
    1012from federation import topdl
    1113
     
    2224        return "%s:%s:%s:%s" % (self.name, self.required,
    2325                ",".join(self.provides), ",".join(self.accepts))
    24 
    25 
    26 def add_to_map(exp, mark, provides, accepts):
    27     """
    28     Create a constraint from a string of the form
    29         name:required:provides:accepts
    30     where name is the name of the node in the current topology, if the second
    31     field is "required" this is a required constraint, a list of attributes
    32     that this connection point provides and a list that it accepts.  The
    33     provides and accepts lists are comma-separated.  Note that the string
    34     passed in as exp should have whitespace removed. The constraint is added to
    35     the mark dict under the name key and entries in the provides and accepts
    36     are made to teh constraing for each attribute that it provides or accepts.
    37     """
    38     nn, r, p, a = exp.split(":")
    39     p = p.split(",")
    40     a = a.split(",")
    41     c = constraint(name=nn, required=(r == 'required'), provides=p, accepts=a)
    42     mark[nn] = c
    43 
    44     for attr, dict in ((p, provides), (a, accepts)):
    45         for a in attr:
    46             if a not in dict: dict[a] = [c]
    47             else: dict[a].append(c)
    4826
    4927def make_new_name(names, prefix="name"):
     
    169147    req = { 'description' : { 'ns2description': desc }, }
    170148
    171     try:
    172         r = service_caller('Ns2Topdl')(uri, req, cert)
    173     except:
    174         return None
     149    r = service_caller('Ns2Topdl')(uri, req, cert)
    175150
    176151    if r.has_key('Ns2TopdlResponseBody'):
     
    231206    top.incorporate_elements()
    232207
    233 def import_tcl_constraints(marks, provides, accepts, contents):
     208def import_ns2_constraints(contents):
    234209    """
    235210    Contents is a list containing the lines of an annotated ns2 file.  This
     
    237212    constraints in the namespace of the tcl experiment, as well as inserting
    238213    them in the accepts and provides indices.
    239     """
     214
     215    Constraints are given in lines of the form
     216        name:required:provides:accepts
     217    where name is the name of the node in the current topology, if the second
     218    field is "required" this is a required constraint, a list of attributes
     219    that this connection point provides and a list that it accepts.  The
     220    provides and accepts lists are comma-separated.  The constraint is added to
     221    the marks dict under the name key and that dict is returned.
     222    """
     223
     224    marks = { }
    240225    for l in contents:
    241226        m = const_re.search(l)
    242227        if m:
    243             add_to_map(re.sub('\s', '', m.group(1)), marks, provides,
    244                     accepts)
    245 
     228            exp = re.sub('\s', '', m.group(1))
     229            nn, r, p, a = exp.split(":")
     230            p = p.split(",")
     231            a = a.split(",")
     232            c = constraint(name=nn, required=(r == 'required'),
     233                    provides=p, accepts=a)
     234            marks[nn] = c
     235    return marks
     236
     237def import_ns2_component(fn):
     238    """
     239    Pull a composition component in from an ns2 description.  The Constraints
     240    are parsed from the comments using import_ns2_constraints and the topology
     241    is created using a call to a fedd exporting the Ns2Topdl function.  A
     242    topdl.Topology object rempresenting the component's topology and a dict
     243    mapping constraints from the components names to the conttraints is
     244    returned.  If either the file read or the conversion fails, appropriate
     245    Exceptions are thrown.
     246    """
     247    f = open(fn, "r")
     248    contents = [ l for l in f ]
     249
     250    marks = import_ns2_constraints(contents)
     251    top = remote_ns2topdl(opts.url, "".join(contents), cert)
     252    if not top:
     253        raise RuntimeError("Cannot create topology from: %s" % fn)
     254
     255    return (top, marks)
     256
     257def index_constraints(constraints, provides, accepts):
     258    """
     259    Add constraints to the provides and accepts indices based on what the
     260    attributes of the contstraints.
     261    """
     262    for c in constraints:
     263        for attr, dict in ((c.provides, provides), (c.accepts, accepts)):
     264            for a in attr:
     265                if a not in dict: dict[a] = [c]
     266                else: dict[a].append(c)
    246267
    247268def multi_callback(option, opt_str, value, parser):
     
    299320accepts = { }
    300321for fn, cnt in files:
    301     marks = { }
    302322    try:
    303         f = open(fn, "r")
    304         contents = [ l for l in f ]
     323        top, marks = import_ns2_component(fn)
     324    except service_error, e:
     325        print >>sys.stderr, "Remote error on %s: %s" % (fn, e)
     326        continue
    305327    except EnvironmentError, e:
    306328        print >>sys.stderr, "Error on %s: %s" % (fn, e)
    307329        continue
    308330
    309     top = remote_ns2topdl(opts.url, "".join(contents), cert)
    310     if not top:
    311         sys.exit("Cannot create topology from: %s" % fn)
    312 
    313331    for i in range(0, cnt):
    314332        t = top.clone()
    315         import_tcl_constraints(marks, provides, accepts, contents)
    316         add_interfaces(t, marks)
    317         localize_names(t, names, marks)
     333        m = copy.deepcopy(marks)
     334        index_constraints(m.values(), provides, accepts)
     335        add_interfaces(t, m)
     336        localize_names(t, names, m)
    318337        t.incorporate_elements()
    319         constraints.extend(marks.values())
     338        constraints.extend(m.values())
    320339
    321340# Let the user know if they messed up on specifying constraints.
Note: See TracChangeset for help on using the changeset viewer.