def write_types(self, fname): imports = [ "import ctypes", ] with open(fname, "w") as f: f.write(self.make_header(imports)) f.write(codegen_util.comment_line("ctypes struct declarations")) for struct in six.itervalues(self.structs_dict): f.write("\n" + struct.ctypes_struct_decl) f.write("\n" + codegen_util.comment_line("End of generated code"))
def write_consts(self, fname): imports = [ "# pylint: disable=invalid-name", ] with open(fname, "w") as f: f.write(self.make_header(imports)) f.write(codegen_util.comment_line("Constants") + "\n") for name, value in six.iteritems(self.consts_dict): f.write("{0} = {1}\n".format(name, value)) f.write("\n" + codegen_util.comment_line("End of generated code"))
def write_types(self, fname): """Write ctypes struct and function type declarations.""" imports = [ "import ctypes", ] with open(fname, "w") as f: f.write(self.make_header(imports)) f.write( codegen_util.comment_line( "ctypes struct, union, and function type declarations")) for type_decl in self.types_dict.values(): f.write("\n" + type_decl.ctypes_decl) f.write("\n" + codegen_util.comment_line("End of generated code"))
def write_wrappers(self, fname): with open(fname, "w") as f: imports = [ "import ctypes", "# Internal dependencies.", "# pylint: disable=undefined-variable", "# pylint: disable=wildcard-import", "from {} import util".format(_MODULE), "from {}.mjbindings.types import *".format(_MODULE), ] f.write(self.make_header(imports)) f.write(codegen_util.comment_line("Low-level wrapper classes")) for struct in six.itervalues(self.structs_dict): f.write("\n" + struct.wrapper_class) f.write("\n" + codegen_util.comment_line("End of generated code"))
def write_wrappers(self, fname): """Write wrapper classes for ctypes structs.""" with open(fname, "w") as f: imports = [ "import ctypes", "# pylint: disable=undefined-variable", "# pylint: disable=wildcard-import", "from {} import util".format(_MODULE), "from {}.mjbindings.types import *".format(_MODULE), ] f.write(self.make_header(imports)) f.write(codegen_util.comment_line("Low-level wrapper classes")) for type_decl in self.types_dict.values(): if isinstance(type_decl, c_declarations.Struct): f.write("\n" + type_decl.wrapper_class) f.write("\n" + codegen_util.comment_line("End of generated code"))
def write_funcs_and_globals(self, fname): """Write ctypes declarations for functions and global data.""" imports = [ "import collections", "import ctypes", "# Internal dependencies.", "# pylint: disable=undefined-variable", "# pylint: disable=wildcard-import", "from {} import util".format(_MODULE), "from {}.mjbindings.types import *".format(_MODULE), "import numpy as np", "# pylint: disable=line-too-long", "# pylint: disable=invalid-name", "# common_typos_disable", ] with open(fname, "w") as f: f.write(self.make_header(imports)) f.write("mjlib = util.get_mjlib()\n") f.write("\n" + codegen_util.comment_line("ctypes function declarations")) for function in six.itervalues(self.funcs_dict): f.write("\n" + function.ctypes_func_decl(cdll_name="mjlib")) # Only require strings for UI purposes. f.write("\n" + codegen_util.comment_line("String arrays") + "\n") for string_arr in six.itervalues(self.strings_dict): f.write(string_arr.ctypes_var_decl(cdll_name="mjlib")) f.write("\n" + codegen_util.comment_line("Function pointers")) fields = [repr(name) for name in self.func_ptrs_dict.keys()] values = [ func_ptr.ctypes_var_decl(cdll_name="mjlib") for func_ptr in self.func_ptrs_dict.values() ] f.write( textwrap.dedent(""" function_pointers = collections.namedtuple( 'FunctionPointers', [{0}] )({1}) """).format(",\n ".join(fields), ",\n ".join(values))) f.write("\n" + codegen_util.comment_line("End of generated code"))
def write_enums(self, fname): """Write enum definitions.""" with open(fname, "w") as f: imports = [ "import collections", "# pylint: disable=invalid-name", "# pylint: disable=line-too-long", ] f.write(self.make_header(imports)) f.write(codegen_util.comment_line("Enums")) for enum_name, members in self.enums_dict.items(): fields = ["\"{}\"".format(name) for name in members.keys()] values = [str(value) for value in members.values()] s = textwrap.dedent(""" {0} = collections.namedtuple( "{0}", [{1}] )({2}) """).format(enum_name, ",\n ".join(fields), ", ".join(values)) f.write(s) f.write("\n" + codegen_util.comment_line("End of generated code"))
def write_index_dict(self, fname): pp = pprint.PrettyPrinter() output_string = pp.pformat(dict(self.index_dict)) indent = codegen_util.Indenter() imports = [ "# pylint: disable=bad-continuation", "# pylint: disable=line-too-long", ] with open(fname, "w") as f: f.write(self.make_header(imports)) f.write("array_sizes = (\n") with indent: f.write(output_string) f.write("\n)") f.write("\n" + codegen_util.comment_line("End of generated code"))
def write_funcs_and_globals(self, fname): """Write ctypes declarations for functions and global data.""" imports = [ "import collections", "import ctypes", "# pylint: disable=undefined-variable", "# pylint: disable=wildcard-import", "from {} import util".format(_MODULE), "from {}.mjbindings.types import *".format(_MODULE), "import numpy as np", "# pylint: disable=line-too-long", "# pylint: disable=invalid-name", "# common_typos_disable", ] with open(fname, "w") as f: f.write(self.make_header(imports)) f.write("mjlib = util.get_mjlib()\n") f.write("\n" + codegen_util.comment_line("ctypes function declarations")) for function in self.funcs_dict.values(): f.write("\n" + function.ctypes_func_decl(cdll_name="mjlib")) # Only require strings for UI purposes. f.write("\n" + codegen_util.comment_line("String arrays") + "\n") for string_arr in self.strings_dict.values(): f.write(string_arr.ctypes_var_decl(cdll_name="mjlib")) f.write("\n" + codegen_util.comment_line("Callback function pointers")) fields = [ "'_{0}'".format(func_ptr.name) for func_ptr in self.func_ptrs_dict.values() ] values = [ func_ptr.ctypes_var_decl(cdll_name="mjlib") for func_ptr in self.func_ptrs_dict.values() ] f.write( textwrap.dedent(""" class _Callbacks: __slots__ = [ {0} ] def __init__(self): {1} """).format(",\n ".join(fields), "\n ".join(values))) indent = codegen_util.Indenter() with indent: for func_ptr in self.func_ptrs_dict.values(): f.write( indent( func_ptr.getters_setters_with_custom_prefix( "self._"))) f.write( "\n\ncallbacks = _Callbacks() # pylint: disable=invalid-name") f.write("\ndel _Callbacks\n") f.write("\n" + codegen_util.comment_line("End of generated code"))