def export_class(cls): members = [] if cls.vtable: members.append(SourceBlock("_polymorphism_info_ = wrappyr.runtime.PolymorphismInfo()")) for block in (export_constructor(cls), export_destructor(cls)): if block: members.append(block) for method in sorted(cls.methods.values(), key=cmp_to_key(sort_methods(lambda method: method.name))): members.append(export_method(cls, method)) for member in sorted(cls.members.values(), key=lambda member: member.name): members.append(export_member(cls, member)) from_c = SourceBlock() from_c.add_line("@staticmethod") from_c.add_line("def _from_c(inst, ownership = False):") from_c.add_line("cls = %s.__new__(%s)" % (cls.name, cls.name), 1) from_c.add_line("cls._inst = inst", 1) from_c.add_line("cls._ownership = ownership", 1) from_c.add_line("cls._valid = True", 1) from_c.add_line("return cls", 1) members.append(from_c) parent_module = cls.get_closest_parent_module() base_imports = [] bases = [] for base, num in zip(cls.bases, count()): base_var = "_Base%d" % num base_import = get_node_import(parent_module, base.type, base_var) base_imports.append(base_import) bases.append(base_var) bases = ", ".join(bases) if bases else "wrappyr.runtime.CPPClass" block = SourceBlock() for base_import in base_imports: block.add_line(base_import) block.add_line("class %s(%s):" % (cls.name, bases)) block.add_block(SourceBlock("").join(members, 1)) return block
def export_module(mod, base_dir): is_pkg = isinstance(mod, Package) name = mod.name if not is_pkg else "__init__" blocks = [] block = SourceBlock() block.add_line("from __future__ import absolute_import") block.add_line("import ctypes") block.add_line("import functools") block.add_line("import wrappyr.runtime") blocks.append(block) if mod.libraries: block = SourceBlock() block.add_line("_libraries = {") for lib in mod.libraries.values(): block.add_line("'%s': ctypes.CDLL('%s')," % (lib.name, lib.path), 1) block.add_line("}") blocks.append(block) classes = sorted(mod.every_class(), key=cmp_to_key(sort_classes)) for cls in classes: blocks.append(export_class(cls)) functions = mod.every_function() for f in functions: blocks.append(export_function(f)) for cls in classes: blocks.append(export_class_setup(cls)) for f in functions: blocks.append(export_function_setup(f)) block = SourceBlock("").join(blocks) with open(os.path.join(base_dir, "%s.py" % name), "w") as f: f.write(block.as_text())