4 # SpecDict defines a means for converting a dictionary with plc specific keys
5 # to a dict with rspec specific keys.
7 # SpecDict.fields dict defines all the rspec specific attribute and their
10 # SpecDict.plc_fields defines a one to one mapping of plc attribute to rspec
13 ### $Id: specdict.py 15485 2009-10-26 18:31:09Z sapanb $
14 ### $URL: http://svn.planet-lab.org/svn/sfa/trunk/sfa/util/specdict.py $
16 from types import StringTypes, ListType
20 Base class of SpecDict objects.
26 def __init__(self, spec_dict):
27 # convert plc dict and initialize self
28 sdict = self.plcToSpec(spec_dict)
29 dict.__init__(self, sdict)
32 def plcToSpec(self, spec_dict):
34 Defines how to convert a plc dict to rspec dict
37 for field in self.fields:
39 expected = self.fields[field]
40 if isinstance(expected, StringTypes):
41 if self.plc_fields.has_key(field):
42 plc_field = self.plc_fields[field]
43 if spec_dict.has_key(plc_field):
44 value = spec_dict[plc_field]
45 elif isinstance(expected, ListType):
46 expected = expected[0]
47 if self.plc_fields.has_key(field):
48 plc_field = self.plc_fields[field]
49 if spec_dict.has_key(plc_field):
50 value = [expected(value) for value in spec_dict[plc_field]]
52 return {self.type: spec}
55 # fields = { geni_field: type. Could be class for nested classes, otherwise prob str}
56 # plc_fields = {geni_field : plc_field}
59 class IfSpecDict(SpecDict):
69 plc_fields = {'name': 'is_primary', # XXX needs munging to return name instead of True or False
73 class LinkSpecDict(SpecDict):
75 fields = {'min_alloc': '',
82 'endpoints': [IfSpecDict]}
83 plc_fields = {'min_alloc': 'min_alloc',
84 'max_alloc': 'max_alloc',
86 'start_time': 'start_time',
88 'duration': 'duration',
89 'init_params': 'init_params',
90 'endpoints': 'endpoints'}
93 class NodeSpecDict(SpecDict):
104 'net_if': [IfSpecDict]}
106 plc_fields = {'name': 'hostname',
107 'net_if': 'interfaces'}
109 class NetSpecDict(SpecDict):
111 fields = {'name': '',
114 'nodes': [NodeSpecDict],
115 'links': [LinkSpecDict],
117 plc_fields = {'name': 'name',
118 'start_time': 'start_time',
119 'duration': 'duration',
123 class RSpecDict(SpecDict):
125 fields = {'start_time': '',
127 'networks': [NetSpecDict]
129 plc_fields = {'networks': 'networks',
130 'start_time': 'start_tim',
131 'duration': 'duration'