improve the entity auto-generator with error checks Signed-off-by: Prasanna Santhanam <t...@apache.org>
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/de716db7 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/de716db7 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/de716db7 Branch: refs/heads/marvin_refactor Commit: de716db7ef96db7fab620289841ed7ec38fe8b5f Parents: 4d54820 Author: Prasanna Santhanam <t...@apache.org> Authored: Thu Apr 25 20:12:34 2013 +0530 Committer: Prasanna Santhanam <t...@apache.org> Committed: Thu Apr 25 20:12:34 2013 +0530 ---------------------------------------------------------------------- tools/marvin/marvin/codegenerator.py | 2 +- tools/marvin/marvin/cs_entity_generator.py | 28 +++++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/de716db7/tools/marvin/marvin/codegenerator.py ---------------------------------------------------------------------- diff --git a/tools/marvin/marvin/codegenerator.py b/tools/marvin/marvin/codegenerator.py index 36ba180..0783af9 100644 --- a/tools/marvin/marvin/codegenerator.py +++ b/tools/marvin/marvin/codegenerator.py @@ -338,7 +338,7 @@ class codeGenerator: assert paramProperty.name if param.has_key('required'): - paramProperty.required = param['required'] + paramProperty.required = str(param['required']).lower() if param.has_key('description'): paramProperty.desc = param['description'] http://git-wip-us.apache.org/repos/asf/cloudstack/blob/de716db7/tools/marvin/marvin/cs_entity_generator.py ---------------------------------------------------------------------- diff --git a/tools/marvin/marvin/cs_entity_generator.py b/tools/marvin/marvin/cs_entity_generator.py index c4592ad..f0c5fb8 100644 --- a/tools/marvin/marvin/cs_entity_generator.py +++ b/tools/marvin/marvin/cs_entity_generator.py @@ -80,7 +80,7 @@ def singularize(word, num=0): @return: singular of `word` """ inflector = inflect.engine() - return inflector.singular_noun(word, num) + return inflector.singular_noun(word) def transform_entity(entity): @@ -102,16 +102,17 @@ def skip_list(): return ['ldapConfigCmd', 'ldapRemoveCmd'] -def get_verb_and_entity(api): +def get_verb_and_entity(cmd): """Break down the API cmd instance in to `verb` and `Entity` @return: verb, Entity tuple """ + api = cmd.__class__.__name__ matching_verbs = filter(lambda v: api.startswith(v), grammar) if len(matching_verbs) > 0: verb = matching_verbs[0] entity = api.replace(verb, '').replace('Cmd', '') entity = transform_entity(entity) - return verb, singularize(entity) + return verb, singularize(entity) if singularize(entity) else entity else: print "No matching verb, entity breakdown for api %s" % api @@ -122,7 +123,8 @@ def get_actionable_entities(): along with the required arguments to satisfy the action @return: Dictionary of Entity { "verb" : [required] } """ - cmdlets = sorted(get_api_cmds(), key=lambda k: get_verb_and_entity(k.__class__.__name__)) + cmdlets = sorted(filter(lambda api: api.__class__.__name__ not in skip_list(), get_api_cmds()), + key=lambda k: get_verb_and_entity(k)[1]) entities = {} for cmd in cmdlets: requireds = getattr(cmd, 'required') @@ -130,7 +132,7 @@ def get_actionable_entities(): api = cmd.__class__.__name__ if api in skip_list(): continue - verb, entity = get_verb_and_entity(api) + verb, entity = get_verb_and_entity(cmd) if entity not in entities: entities[entity] = {} entities[entity][verb] = {} @@ -184,14 +186,17 @@ def write_entity_classes(entities, module=None): else: body.append(tabspace * 2 + 'return %s if %s else None' % (entity.lower(), entity.lower())) else: - body.append(tabspace + 'def %s(cls, apiclient, %s, factory=None, **kwargs):' % ( - action, ', '.join(map(lambda arg: arg + '=None', list(set(details['args'])))), entity)) + if len(details['args']) > 0: + body.append(tabspace + 'def %s(cls, apiclient, %s, factory=None, **kwargs):' % ( + action, ', '.join(map(lambda arg: arg + '=None', list(set(details['args'])))))) + else: + body.append(tabspace + 'def %s(cls, apiclient, factory=None, **kwargs):' % action) #TODO: Add docs for actions body.append(tabspace * 2 + 'cmd = %(module)s.%(command)s()' % {"module": details["apimodule"], "command": details["apicmd"]}) body.append(tabspace * 2 + 'if factory:') body.append( - tabspace * 3 + '[setattr(cmd, factoryKey, factoryValue) for factoryKey, factoryValue in %sFactory.__dict__.iteritems()]' % entity) + tabspace * 3 + '[setattr(cmd, factoryKey, factoryValue) for factoryKey, factoryValue in factory.__dict__.iteritems()]') body.append(tabspace * 2 + 'else:') for arg in details["args"]: body.append(tabspace * 3 + "cmd.%s = %s" % (arg, arg)) @@ -206,7 +211,12 @@ def write_entity_classes(entities, module=None): entitydict[entity] = code #write_entity_factory(entity, actions, path) - module_path = '/'.join(module.split('.'))[1:] + if module.find('.') > 0: + module_path = '/'.join(module.split('.'))[1:] + else: + module_path = '/'+module + if not os.path.exists(".%s" % module_path): + os.mkdir(".%s" % module_path) with open(".%s/%s.py" % (module_path, entity), "w") as writer: writer.write(LICENSE) writer.write(code)