Skip to content

Commit 18e6cfc

Browse files
authored
Merge pull request #2 from DMTF/csv-branch
Added CSV file compatibility
2 parents e0fbf14 + 59dd42d commit 18e6cfc

File tree

4 files changed

+67
-31
lines changed

4 files changed

+67
-31
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,13 @@ The addition of property-level annotations aid in completing the schema creation
8888

8989
A comma-separated variable (CSV) file can be specified as input to provide description text, using the following:
9090

91-
`<property name>, <description>, <long description>`
91+
`<property name>|<description>|<long description>`
9292

9393
The property name could also provide a JSON path `object/property` or similar style to allow inclusion of properties within embedded objects.
9494

95+
If a property is an EnumType, you may apply descriptions to each Enum by adding further values to a given row:
96+
97+
`<property name>|<description>|<long description>|<enum1desc>|<enum2desc>|...`
9598

9699
## XML header file
97100

Thingy.v1_0_0.xml

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
<EntityType Name="Thingy" BaseType="Thingy.Thingy">
6161
<Property Name="ThingType" Type="Thingy.v1_0_0.ThingType" Nullable="false">
6262
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
63-
<Annotation Term="OData.Description" String="The type of thingy that this thingy is - really..."/>
64-
<Annotation Term="OData.LongDescription" String="A long thingy description..."/>
63+
<Annotation Term="OData.Description" String="ShortDesc"/>
64+
<Annotation Term="OData.LongDescription" String="LongDesc"/>
6565
<Annotation Term="Redfish.Required"/>
6666
</Property>
6767
<Property Name="IndicatorLED" Type="Resource.IndicatorLED" Nullable="false">
@@ -81,23 +81,23 @@
8181
</Property>
8282
<Property Name="LotsOfStuff" Type="Collection(Edm.String)" Nullable="false">
8383
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
84-
<Annotation Term="OData.Description" String="TBD"/>
85-
<Annotation Term="OData.LongDescription" String="TBD"/>
84+
<Annotation Term="OData.Description" String="ShortDesc"/>
85+
<Annotation Term="OData.LongDescription" String="LongDesc"/>
8686
</Property>
8787
<Property Name="LotsOfStuff2" Type="Collection(Edm.Int64)" Nullable="false">
8888
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
89-
<Annotation Term="OData.Description" String="TBD"/>
90-
<Annotation Term="OData.LongDescription" String="TBD"/>
89+
<Annotation Term="OData.Description" String="ShortDesc"/>
90+
<Annotation Term="OData.LongDescription" String="LongDesc"/>
9191
</Property>
9292
<Property Name="LotsOfStuff3" Type="Collection(Thingy.v1_0_0.LotsOfStuff3)" Nullable="false">
9393
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
94-
<Annotation Term="OData.Description" String="TBD"/>
95-
<Annotation Term="OData.LongDescription" String="TBD"/>
94+
<Annotation Term="OData.Description" String="ShortDesc"/>
95+
<Annotation Term="OData.LongDescription" String="LongDesc"/>
9696
</Property>
9797
<Property Name="LotsOfStuff4" Type="Collection(Thingy.v1_0_0.LotsOfStuff4)" Nullable="false">
9898
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
99-
<Annotation Term="OData.Description" String="TBD"/>
100-
<Annotation Term="OData.LongDescription" String="TBD"/>
99+
<Annotation Term="OData.Description" String="ShortDesc"/>
100+
<Annotation Term="OData.LongDescription" String="LongDesc"/>
101101
</Property>
102102
<Property Name="ThingContainer" Type="Thingy.v1_0_0.ThingContainer" Nullable="false">
103103
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
@@ -133,10 +133,10 @@
133133
</EntityType>
134134
<EnumType Name="ThingType">
135135
<Member Name="RackMount">
136-
<Annotation Term="OData.Description" String="tmp"/>
136+
<Annotation Term="OData.Description" String="Enum1"/>
137137
</Member>
138138
<Member Name="Cheap">
139-
<Annotation Term="OData.Description" String="tmp1"/>
139+
<Annotation Term="OData.Description" String="Enum2"/>
140140
</Member>
141141
<Member Name="Expensive">
142142
<Annotation Term="OData.Description" String="tmp2"/>
@@ -150,18 +150,18 @@
150150
</EnumType>
151151
<EnumType Name="LotsOfStuff3">
152152
<Member Name="a">
153-
<Annotation Term="OData.Description" String="TBD"/>
153+
<Annotation Term="OData.Description" String="Enum1"/>
154154
</Member>
155155
<Member Name="b">
156-
<Annotation Term="OData.Description" String="TBD"/>
156+
<Annotation Term="OData.Description" String="Enum2"/>
157157
</Member>
158158
<Member Name="c">
159-
<Annotation Term="OData.Description" String="TBD"/>
159+
<Annotation Term="OData.Description" String="Enum3"/>
160160
</Member>
161161
</EnumType>
162162
<ComplexType Name="LotsOfStuff4" Nullable="false">
163-
<Annotation Term="OData.Description" String="TBD"/>
164-
<Annotation Term="OData.LongDescription" String="TBD"/>
163+
<Annotation Term="OData.Description" String="ShortDesc"/>
164+
<Annotation Term="OData.LongDescription" String="LongDesc"/>
165165
<Annotation Term="OData.AdditionalProperties" Bool="false"/>
166166
<Property Name="My_Name" Type="Edm.String" Nullable="false">
167167
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
@@ -170,13 +170,13 @@
170170
</Property>
171171
</ComplexType>
172172
<ComplexType Name="ThingButton" Nullable="false">
173-
<Annotation Term="OData.Description" String="TBD"/>
174-
<Annotation Term="OData.LongDescription" String="TBD"/>
173+
<Annotation Term="OData.Description" String="HahaButton"/>
174+
<Annotation Term="OData.LongDescription" String="HahaButton2"/>
175175
<Annotation Term="OData.AdditionalProperties" Bool="false"/>
176176
<Property Name="ButtonColor" Type="Edm.String" Nullable="false">
177177
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
178-
<Annotation Term="OData.Description" String="Color of the button."/>
179-
<Annotation Term="OData.LongDescription" String="TBD"/>
178+
<Annotation Term="OData.Description" String="ShortInnerDesc"/>
179+
<Annotation Term="OData.LongDescription" String="LongInnerDesc"/>
180180
</Property>
181181
<Property Name="ButtonActuation" Type="Edm.Decimal" Nullable="false">
182182
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
@@ -191,8 +191,8 @@
191191
<Annotation Term="OData.AdditionalProperties" Bool="false"/>
192192
<Property Name="ThingKnob" Type="Edm.String" Nullable="false">
193193
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
194-
<Annotation Term="OData.Description" String="TBD"/>
195-
<Annotation Term="OData.LongDescription" String="TBD"/>
194+
<Annotation Term="OData.Description" String="ShortInnerDesc"/>
195+
<Annotation Term="OData.LongDescription" String="LongInnerDesc"/>
196196
</Property>
197197
<Property Name="ThingKnobCount" Type="Edm.Int64" Nullable="false">
198198
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
@@ -201,8 +201,8 @@
201201
</Property>
202202
<Property Name="ThingButton" Type="Thingy.v1_0_0.ThingButton" Nullable="false">
203203
<Annotation Term="OData.Permissions" EnumMember="OData.Permission/Read"/>
204-
<Annotation Term="OData.Description" String="TBD"/>
205-
<Annotation Term="OData.LongDescription" String="TBD"/>
204+
<Annotation Term="OData.Description" String="HahaButton"/>
205+
<Annotation Term="OData.LongDescription" String="HahaButton2"/>
206206
</Property>
207207
</ComplexType>
208208
</Schema>

csdl_creator.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<!---->
3434
"""
3535

36-
def database_builder(annotated_json):
36+
def database_builder(annotated_json, csv={}):
3737
"""Transforms annotated json into a dictionary database
3838
3939
:param annotated_json: Annotated json to turn into a dictionary.
@@ -60,14 +60,28 @@ def database_builder(annotated_json):
6060
data_base[prop]["type"] = "array"
6161
value = value[0]
6262
if isinstance(value, dict):
63-
data_base[prop]["properties"] = database_builder(value)
63+
sub_csv = {k.split('/', 1)[1]: s for k, s in csv.items() if prop in k and '/' in k}
64+
data_base[prop]["properties"] = database_builder(value, sub_csv)
6465
elif isinstance(value, str) and '|' in value:
6566
#For enum values
6667
value = [val.strip() for val in value.split('|') if val]
6768
data_base[prop]["enum"] = True
6869
data_base[prop]["value"] = value
6970
else:
7071
data_base[prop][annotation] = value
72+
for prop in data_base:
73+
if prop in csv:
74+
print(prop)
75+
data_base[prop]['description'] = csv[prop][0]
76+
data_base[prop]['longDescription'] = csv[prop][1]
77+
if data_base[prop].get("enum"):
78+
csv_enum = csv[prop][2:]
79+
print(csv_enum, prop)
80+
if data_base[prop].get("enumDescriptions") is None:
81+
data_base[prop]["enumDescriptions"] = {}
82+
data_base[prop]["enumDescriptions"].update({e: d for e, d in zip(data_base[prop]["value"], csv_enum)})
83+
84+
7185
return data_base
7286

7387

@@ -165,7 +179,7 @@ def create_property_w_type(property_name, value=None, my_type=None, **kwargs):
165179

166180
class CsdlFile:
167181
"""CSDL file that is created from the passed JSON"""
168-
def __init__(self, annotated_json, inherited_prop_list=[]):
182+
def __init__(self, annotated_json, inherited_prop_list=[], csv=None):
169183
self.csdl = None
170184
self.main_csdl = None
171185
self.annotated_json = annotated_json
@@ -178,7 +192,7 @@ def __init__(self, annotated_json, inherited_prop_list=[]):
178192
if "@" in key:
179193
del self._annotation_database[key]
180194
else:
181-
self._annotation_database = database_builder(self.annotated_json)
195+
self._annotation_database = database_builder(self.annotated_json, csv)
182196
self._name = annotated_json['@odata.type']
183197

184198
for item in inherited_prop_list:
@@ -295,11 +309,22 @@ def main():
295309
except Exception:
296310
sys.stderr.write("Problem getting file provided")
297311
return 1
312+
313+
csv_dict = {}
298314

299-
csdl = CsdlFile(json_data, RESOURCE_PROPERTIES)
315+
if args.csv:
316+
with open(args.csv) as f:
317+
csv_reader = csv.reader(f, delimiter='|')
318+
for line in csv_reader:
319+
csv_dict[line[0]] = line[1:]
320+
print(csv_dict)
321+
322+
323+
csdl = CsdlFile(json_data, RESOURCE_PROPERTIES, csv=csv_dict)
300324
csdl.init_csdl()
301325
csdl.build_csdl()
302326
output_xml = csdl.name+'.xml'
327+
303328
with open(output_xml, 'w') as output:
304329
xml_string = etree.tostring(csdl.main_csdl, encoding="unicode", method="xml")
305330
xml_obj = xml.dom.minidom.parseString(xml_string)

csvtest.csv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ThingType|ShortDesc|LongDesc|Enum1|Enum2
2+
LotsOfStuff|ShortDesc|LongDesc
3+
LotsOfStuff2|ShortDesc|LongDesc
4+
LotsOfStuff3|ShortDesc|LongDesc|Enum1|Enum2|Enum3
5+
LotsOfStuff4|ShortDesc|LongDesc
6+
ThingContainer/ThingKnob|ShortInnerDesc|LongInnerDesc
7+
ThingContainer/ThingButton|HahaButton|HahaButton2
8+
ThingContainer/ThingButton/ButtonColor|ShortInnerDesc|LongInnerDesc

0 commit comments

Comments
 (0)