import yaml

managed_objects = yaml.safe_load(open("managed_object_graph.yaml"))
header_text = "from psphere import ManagedObject, cached_property\n\n"

footer_text = "classmap = dict((x.__name__, x) for x in (\n"

# Basic text to import these base classes from psphere

body_text = ""
for i, mo in enumerate(managed_objects):
    if mo["extends"] is None:
        mo["extends"] = "ManagedObject"

    body_text += "class %s(%s):\n" % (mo["name"], mo["extends"])
    props = []
    for prop in mo["properties"]:
        props.append("%s" % prop["name"])
    body_text += "    props = set(%s)\n" % props
    body_text += "    def __init__(self, mo_ref, client):\n"
    body_text += "        %s.__init__(self, mo_ref, client)\n" % (mo["extends"])
    body_text += ("        self.valid_attrs = set.union(self.props, %s.props)\n" % mo["extends"])
    for prop in mo["properties"]:
        body_text += "    @cached_property\n"
        body_text += "    def %s(self):\n" % prop["name"]
        body_text += ("       return self._get_property(\"%s\", %s, %s)\n" %
                 (prop["name"], prop["mor"], prop["multivalue"]))

    body_text += "\n\n"

    footer_text += "    %s" % mo["name"]
    # Put a comma at the end of every line but the last
    if i < (len(managed_objects) - 1):
        footer_text += ",\n"
    else:
        footer_text += "\n"

footer_text += "))\n"
footer_text += "def classmapper(name):\n"
footer_text += "    return classmap[name]"

f = open("output.py", "w")
f.write(header_text)
f.write(body_text)
f.write(footer_text)
f.close()

@@ -970,24 +970,60 @@
 rop%5B%22name%22%5D%0A
+        if prop%5B%22mor%22%5D is True:%0A    
         body
@@ -1061,24 +1061,19 @@
 get_
-property
+mor
 (%5C%22%25s%5C%22,
  %25s,
@@ -1072,12 +1072,8 @@
 s%5C%22,
- %25s,
  %25s)
@@ -1095,16 +1095,20 @@
         
+    
 (prop%5B%22n
@@ -1121,18 +1121,155 @@
  prop%5B%22m
-or
+ultivalue%22%5D))%0A        else:%0A            body_text += (%22       return self._get_dataobject(%5C%22%25s%5C%22, %25s)%5Cn%22 %25%0A                     (prop%5B%22name
 %22%5D, prop

Call different method depending on whether it's a MOR or not