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 from types import StringTypes, ListType
18 Base class of SpecDict objects.
24 def __init__(self, spec_dict):
25 # convert plc dict and initialize self
26 sdict = self.plcToSpec(spec_dict)
27 dict.__init__(self, sdict)
30 def plcToSpec(self, spec_dict):
32 Defines how to convert a plc dict to rspec dict
35 for field in self.fields:
37 expected = self.fields[field]
38 if isinstance(expected, StringTypes):
39 if self.plc_fields.has_key(field):
40 plc_field = self.plc_fields[field]
41 if spec_dict.has_key(plc_field):
42 value = spec_dict[plc_field]
43 elif isinstance(expected, ListType):
44 expected = expected[0]
45 if self.plc_fields.has_key(field):
46 plc_field = self.plc_fields[field]
47 if spec_dict.has_key(plc_field):
48 value = [expected(value) for value in spec_dict[plc_field]]
50 return {self.type: spec}
53 # fields = { geni_field: type. Could be class for nested classes, otherwise prob str}
54 # plc_fields = {geni_field : plc_field}
57 class IfSpecDict(SpecDict):
67 plc_fields = {'name': 'is_primary', # XXX needs munging to return name instead of True or False
71 class LinkSpecDict(SpecDict):
83 class NodeSpecDict(SpecDict):
94 'net_if': [IfSpecDict]}
96 plc_fields = {'name': 'hostname',
97 'net_if': 'interfaces'}
99 class NetSpecDict(SpecDict):
101 fields = {'name': '',
104 'nodes': [NodeSpecDict],
106 plc_fields = {'name': 'name',
107 'start_time': 'start_time',
108 'duration': 'duration',
111 class RspecDict(SpecDict):
113 fields = {'start_time': '',
115 'networks': [NetSpecDict]
117 plc_fields = {'networks': 'networks',
118 'start_time': 'start_tim',
119 'duration': 'duration'