def generate_schema_classes(self, parent, name): """ 1. Do a level order BFS on visiting on all the classes that has members. 2. generate the schema and validation classes. 3. return the classes in reverse order to be generated in the ReportSchema.py """ classes = [] visited = {} queue = [(parent, name)] while queue: node, name = queue.pop(0) if node in visited: continue visited[node] = True # add the members to queue args = {} members = '' for m in node.members: mname = '' # if this is class type if m.cls: # if there are no members in this member then use the correct schema if len(m.cls.members): mname = camelcase(m.cls.name) + ( 'SchemaDict' if m.cls.singleton else 'SchemaList') # need to visit the node queue.append((m.cls, camelcase(m.cls.name))) else: mname = 'LeafSchemaDict' elif m.type: mname = self.get_python_type(m.type) members += ' "%s":%s,\n' % (m.name, mname) if not node.singleton: methods = [ PyLines(lines=tokenize(discovery_py_list_schema_body % name)) ] classes.append( PyClass(name + "SchemaList", parent="SchemaList", methods=methods)) args['members'] = members methods = [ PyLines(lines=tokenize(discovery_py_dict_schema_body % args)) ] classes.append( PyClass(name + "SchemaDict", desc=[ PyLines(lines=[ ' Refer ' + node._yfile + ' ' + node.name ]) ], parent="SchemaDict", methods=methods)) return classes[::-1]
def __init__(self, test, fname): self._test = test self._fname = fname self._header = [PyLines(lines=tokenize(py_ts_header%self._test._yfile))] self._imports = [] self._classes = [] self._methods = []
def __init__(self, cls, fname): self._cls = cls self._fname = fname self._header = [PyLines(lines=tokenize(py_header % self._cls._yfile))] self._imports = [] self._classes = [] self._methods = []
def __init__(self, pkg, fname): self._pkg = pkg self._fname = fname self._header = [ PyLines(lines=tokenize(discovery_py_header % pkg.name)) ] self._imports = [] self._classes = [] self._methods = []
def __init__(self, cls, parent, fname): self._cls = cls self._parent = parent self._fname = fname self._header = [ PyLines(lines=tokenize(discovery_py_header % self._cls._yfile)) ] self._imports = [] self._classes = [] self._methods = []
def generate_code(self): args = self._cls.to_dict() args["cname_cc"] = camelcase(self._cls.name) self._imports.append( PyImport( "%(cname_cc)s " % args, _from="dent_os_testbed.lib.%s.%s." % (self._cls._mod.name, self._platform) + self._cls.name + " ", )) methods = [] # add format_cmd method entries = "" for cmd in self._cls.commands: # add the methods to handle the generation cargs = cmd.to_dict() format_cmd_body = tokenize(py_impl_class_common_format_cmd % cargs) methods.append( PyMethod( "format_%s" % cmd.name, "self, command, *argv, **kwarg", format_cmd_body, indent=4, )) if self._cls.implements.local: run_cmd_body = tokenize(py_impl_class_common_run_cmd % cargs) methods.append( PyMethod( "run_%s" % cmd.name, "self, device, command, *argv, **kwarg", run_cmd_body, indent=4, )) parse_cmd_body = tokenize(py_impl_class_common_parse_cmd % cargs) methods.append( PyMethod( "parse_%s" % cmd.name, "self, command, output, *argv, **kwarg", format_cmd_body, indent=4, )) self._classes.append( PyClass( camelcase(self._cls.name + "Impl"), desc=[PyLines(lines=tokenize(self._cls.desc, indent=8))], parent=camelcase(self._cls.name), methods=methods, ))
def generate_code(self): self._imports.append(PyImport("io")) self._imports.append(PyImport("json")) self._imports.append(PyImport("copy")) self._imports.append( PyImport("(LeafSchemaDict, SchemaList, SchemaDict, Report)", _from="dent_os_testbed.discovery.Report ")) # recurse the level from base. # discard other accessors methods = [PyLines(lines=tokenize(discovery_py_report_body))] self._classes.extend( self.generate_schema_classes( self._pkg.modules['base'].classes_dct['base'], "Report")) self._classes.append( PyClass("ReportSchema", parent="Report", methods=methods))
def generate_code(self): args = self._test.to_dict() self._imports.append(PyImport("pytest")) self._imports.append(PyImport("time")) self._imports.append(PyImport("re")) self._imports.append(PyImport("TestCaseSetup", _from="dent_os_testbed.utils.decorators ")) imported = {} classes = '' for tc in self._test.test_cases: targs = {**args, **tc.to_dict()} if tc.cls: targs = {**targs, **tc.cls.to_dict()} # if there is an class then do an import if not done. if tc.cls.implemented_by and tc.cls.name not in imported: self._imports.append( PyImport( "%(cls_cc_name)s " % targs, _from="dent_os_testbed.lib.%s." % (tc.cls._mod.name) + tc.cls.name + " ", ) ) imported[tc.cls.name] = True if tc.template not in imported: self._imports.append( PyImport( "%sBase, %sMeta "%(tc.template, tc.template), _from="dent_os_testbed.test.lib.%s " %(tc.template) ) ) imported[tc.template] = True if tc.args: tc_args = json.loads(tc.args) methods = [] for k in tc_args.keys(): methods.append( PyMethod(k, "obj=None", ["return "+tc_args[k]], indent=4) ) cls = camelcase(tc.cls.name)+tc.template+"Meta" self._classes.append( PyClass(cls, desc=[PyLines(lines=[])], parent=("%sMeta"%tc.template), methods=methods) ) self._classes.append( PyClass(camelcase(tc.cls.name)+tc.template, desc=[PyLines(lines=[])], parent=("%sBase"%tc.template), methods=[PyLines( lines=[" meta="+ camelcase(tc.cls.name)+ tc.template+ "Meta"])] ) ) classes += camelcase(tc.cls.name)+tc.template + ',' #self._methods.append(PyLines(lines=tokenize(self._templates[tc.template]._data%targs))) self._methods.append(PyLines(lines=["@pytest.fixture(params=[%s])"%classes])) self._methods.append(PyMethod(self._test.name+'_class', "request", ["return request.param"])) self._methods.append(PyLines(lines=["@pytest.mark.asyncio"])) self._methods.append(PyMethod('test_'+self._test.name, "testbed, "+self._test.name+'_class', ['await ' + self._test.name+'_class'+'().run_test(testbed)'], coroutine=True)) self._imports.append(PyLines(lines=['pytestmark = pytest.mark.suite_basic_trigger']))
def generate_code(self): args = self._cls.to_dict() args["cname_cc"] = camelcase(self._cls.name) self._imports.append(PyImport("pytest")) self._imports.append( PyImport("TestLibObject", _from="dent_os_testbed.lib.test_lib_object ")) for impl in self._cls.implemented_by: pd_impl = "" if "dentos" in impl.platforms: args['pd_impl_cc'] = '%sImpl' % camelcase(impl.name) args['pd_impl'] = 'linux' elif "ixnetwork" in impl.platforms: args['pd_impl_cc'] = '%sImpl' % camelcase(impl.name) args['pd_impl'] = 'ixnetwork' else: continue self._imports.append( PyImport( "%(pd_impl_cc)s " % args, _from="dent_os_testbed.lib.%(cls_mod_name)s.%(pd_impl)s." % (args) + impl.name + "_impl ", )) # generate the init routine methods = [] impl_form_cmd = '' for obj in self._cls.implemented_by: iargs = { 'cname_cc': camelcase(obj.name), 'platforms': obj.platforms, } impl_form_cmd += py_class_common_impl_form_command % iargs args['impl_form_cmd'] = impl_form_cmd if self._cls.local: args[ 'invoke_command'] = 'rc, output = impl_obj.run_command(device_obj, command=api, params=device[device_name])' else: args[ 'invoke_command'] = 'rc, output = await device_obj.run_cmd(("sudo " if device_obj.ssh_conn_params.pssh else "") + commands)' args['local'] = 'impl' if self._cls.local else 'device' run_body = tokenize(py_class_common_run % args), methods.append( PyMethod("_run_command", "api, *argv, **kwarg", run_body[0], indent=4, coroutine=True)) for api in self._cls.apis: args['cls_api'] = api params = '' platforms = '' cmd_desc = '' for impl in self._cls.implemented_by: platforms = impl.platforms for cmd in impl.commands: if api in cmd.apis: cmd_desc = cmd.desc for p in cmd.params: if not isinstance(p, str): params += " '%s':'%s',\n" % ( p.name, p.type) else: params += " '%s':'undefined',\n" % ( p) break args['params'] = params[:-1] args['platforms'] = platforms args['cmd_desc'] = cmd_desc run_body = tokenize(py_class_common_run_api % args), methods.append( PyMethod(api.lower(), "*argv, **kwarg", run_body[0], indent=4, coroutine=True)) # discard other accessors self._classes = [ PyClass(camelcase(self._cls.name), desc=[PyLines(lines=tokenize(self._cls.desc, indent=8))], parent="TestLibObject", methods=methods) ]
def generate_code(self): self._imports.append( PyImport("TestLibObject", _from="dent_os_testbed.lib.test_lib_object ")) args = self._cls.to_dict() args["cname_cc"] = camelcase(self._cls.name) methods = [] # add format_cmd method format_entries = "" run_entries = "" parse_entries = "" need_run = False for cmd in self._cls.commands: # add the methods to handle the generation cargs = cmd.to_dict() format_cmd_body = tokenize(py_class_common_format_cmd % cargs) methods.append( PyMethod( "format_%s" % cmd.name, "self, command, *argv, **kwarg", format_cmd_body, indent=4, )) if self._cls.implements.local: need_run = True run_cmd_body = tokenize(py_class_common_run_cmd % cargs) methods.append( PyMethod( "run_%s" % cmd.name, "self, device, command, *argv, **kwarg", run_cmd_body, indent=4, )) parse_cmd_body = tokenize(py_class_common_parse_cmd % cargs) methods.append( PyMethod( "parse_%s" % cmd.name, "self, command, output, *argv, **kwarg", parse_cmd_body, indent=4, )) args['cmds'] = cmd.apis args['cmd'] = cmd.name format_entries += py_class_common_format_cmd_case % (args) run_entries += py_class_common_run_cmd_case % (args) parse_entries += py_class_common_parse_cmd_case % (args) args["format_entries"] = format_entries args["run_entries"] = run_entries if need_run else "" args["parse_entries"] = parse_entries format_cmd_body = tokenize(py_class_common_format_cmds % args) methods.append( PyMethod("format_command", "self, command, *argv, **kwarg", format_cmd_body, indent=4)) if need_run: run_cmd_body = tokenize(py_class_common_run_cmds % args) methods.append( PyMethod("run_command", "self, device_obj, command, *argv, **kwarg", run_cmd_body, indent=4)) parse_cmd_body = tokenize(py_class_common_parse_cmds % args) methods.append( PyMethod("parse_output", "self, command, output, *argv, **kwarg", parse_cmd_body, indent=4)) self._classes.append( PyClass(camelcase(self._cls.name), desc=[PyLines(lines=tokenize(self._cls.desc, indent=8))], parent="TestLibObject", methods=methods))