예제 #1
0
class Bridge(object):
    def __init__(self, library_path, assembly_path, enable_debug=False):
        self.is_windows = "win" in sys.platform

        print(library_path)
        self.module = Module(library_path)

        state = State(self)
        self.rpc = RPC(state)

        # Prepare delegates
        self.on_message = on_message_delegate(RPC.on_message)
        self.on_exit = on_exit_delegate(Bridge.on_exit)

        self._imp_common()
        self._imp_proxy()

        pluginDir = os.path.dirname(
            os.path.abspath(
                sys.modules['__main__'].__file__
            )
        )

        # Initialize AppDomain and call initialize from the plugin
        self.plugin_handle = self.module.clrInit(
            assembly_path, pluginDir,
            self.on_message,
            self.on_exit,
            enable_debug
        )

    def post_event(self, jsonString):
        self.module.PostEvent(self.plugin_handle, jsonString)

    def run(self):
        g_closed.clear()

        print("GOING TO CALL C# ENTRY")
        self.module.PluginMain(self.plugin_handle)

        print("RETURNED FROM C#")
        # wait for C# to unblock us
        g_closed.wait()

    '''
    C# Calls this method when it's done and handles control back to Python
    '''
    @staticmethod
    def on_exit():
        print("=> OnExit called")
        g_closed.set()
        pass

    def _imp_common(self):
        return self.module.import_methods(common_imports)

    def _imp_proxy(self):
        return self.module.import_methods(proxy_imports)
예제 #2
0
class Bridge(object):
    def __init__(self, library_path, assembly, enable_debug=False):
        self.is_windows = "win" in sys.platform

        self.module = Module(library_path)

        #print("Init State: %s" % extra_vars)
        #state = State(**extra_vars)
        state = State(self)

        self.rpc = RPC(state)

        # Prepare delegates
        self.on_message = on_message_delegate(RPC.on_message)
        self.on_exit = on_exit_delegate(Bridge._on_exit)

        self._imp_common()
        # Initialize MonoHost on Unix
        if not self.is_windows:
            self._imp_monohost()

            self.module.setMainMethodName(str(assembly.entry))

            self.module.clrInit(assembly.path, self.on_message, self.on_exit)

        # Call initialize from the plugin
        self.module.Initialize(self.on_message, self.on_exit, enable_debug)

    def run(self):
        g_closed.clear()

        self.module.PluginMain()

        # wait synchonously for C# to unblock us
        g_closed.wait()

    def _initialize(self):
        if not self.is_windows:
            self.module.clrInit()

    @staticmethod
    def _reaper():
        g_closed.set()

    @staticmethod
    def _on_exit():
        print("=> OnExit called")
        # Start a thread so C# can return from the exit call
        # while we unblock the RPC state
        rt = threading.Thread(target=Bridge._reaper)
        rt.start()
        pass

    def _imp_common(self):
        return self.module.import_methods(common_imports)

    def _imp_monohost(self):
        return self.module.import_methods(monohost_imports)