creates smart function objects for every method in the commands.xml file
()
| 118 | |
| 119 | |
| 120 | def load_dynamic_methods(): |
| 121 | '''creates smart function objects for every method in the commands.xml file''' |
| 122 | |
| 123 | def getText(nodelist): |
| 124 | rc = [] |
| 125 | for node in nodelist: |
| 126 | if node.nodeType == node.TEXT_NODE: rc.append(node.data) |
| 127 | return ''.join(rc) |
| 128 | |
| 129 | # FIXME figure out installation and packaging |
| 130 | xmlfile = os.path.join("/etc/cloud/cli/","commands.xml") |
| 131 | dom = xml.dom.minidom.parse(xmlfile) |
| 132 | |
| 133 | for cmd in dom.getElementsByTagName("command"): |
| 134 | name = getText(cmd.getElementsByTagName('name')[0].childNodes).strip() |
| 135 | assert name |
| 136 | |
| 137 | description = getText(cmd.getElementsByTagName('description')[0].childNodes).strip() |
| 138 | if description: |
| 139 | description = '"""%s"""' % description |
| 140 | else: description = '' |
| 141 | arguments = [] |
| 142 | options = [] |
| 143 | descriptions = [] |
| 144 | |
| 145 | for param in cmd.getElementsByTagName("request")[0].getElementsByTagName("arg"): |
| 146 | argname = getText(param.getElementsByTagName('name')[0].childNodes).strip() |
| 147 | assert argname |
| 148 | |
| 149 | required = getText(param.getElementsByTagName('required')[0].childNodes).strip() |
| 150 | if required == 'true': required = True |
| 151 | elif required == 'false': required = False |
| 152 | else: raise AssertionError("Not reached") |
| 153 | if required: arguments.append(argname) |
| 154 | options.append(argname) |
| 155 | |
| 156 | #import ipdb; ipdb.set_trace() |
| 157 | requestDescription = param.getElementsByTagName('description') |
| 158 | if requestDescription: |
| 159 | descriptionParam = getText(requestDescription[0].childNodes) |
| 160 | else: |
| 161 | descriptionParam = '' |
| 162 | if descriptionParam: descriptions.append( (argname,descriptionParam) ) |
| 163 | |
| 164 | funcparams = ["self"] + [ "%s=None"%o for o in options ] |
| 165 | funcparams = ", ".join(funcparams) |
| 166 | |
| 167 | code = """ |
| 168 | def %s(%s): |
| 169 | %s |
| 170 | parms = dict(locals()) |
| 171 | del parms["self"] |
| 172 | for arg in %r: |
| 173 | if locals()[arg] is None: |
| 174 | raise TypeError, "%%s is a required option"%%arg |
| 175 | for k,v in parms.items(): |
| 176 | if v is None: del parms[k] |
| 177 | output = self._make_request("%s",parms) |