This repository has been archived on 2023-02-07. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-package-manager-addon/subprocess_adapter.py

78 lines
2.7 KiB
Python
Raw Normal View History

2017-07-07 17:56:49 -07:00
import logging
from multiprocessing import Process, Pipe
from bpy.types import Operator
2017-07-07 17:56:49 -07:00
def subprocess_operator(cls: Operator, polling_interval=.01) -> Operator:
"""
Class decorator which wraps Operator methods with setup code for running a subprocess.
2017-07-07 17:56:49 -07:00
Expects args for Process() to defined in cls.proc_args and cls.proc_kwargs. For example,
setting cls.proc_kwargs = {'target:' some_func} can be used to run 'some_func' in
a subprocess.
"""
2017-07-07 17:56:49 -07:00
def decoratify(cls, methodname, decorator):
"""
Decorate `cls.methodname` with `decorator` if method `methodname` exists in cls
else just call the decorator with an empty function
"""
orig_method = getattr(cls, methodname, None)
if orig_method:
setattr(cls, methodname, decorator(orig_method))
else:
# HACK: need a no-op which accepts any arguments
setattr(cls, methodname, decorator(lambda *args, **kwargs: None))
2017-07-07 17:56:49 -07:00
def decorate_execute(orig_execute):
def execute(self, context):
orig_execute(self, context)
call_copy_of_method_if_exist(cls, 'execute', self, context)
return self.invoke(context, None)
return execute
2017-07-07 17:56:49 -07:00
def decorate_invoke(orig_invoke):
def invoke(self, context, event):
orig_invoke(self, context, event)
2017-07-07 17:56:49 -07:00
self.pipe = Pipe()
self.proc = Process(
*getattr(self, 'proc_args', []),
**getattr(self, 'proc_kwargs', [])
)
self.proc.start()
# we have to explicitly close the end of the pipe we are NOT using,
# otherwise no exception will be generated when the other process closes its end.
self.pipe[1].close()
2017-07-07 17:56:49 -07:00
wm = context.window_manager
wm.modal_handler_add(self)
self.timer = wm.event_timer_add(polling_interval, context.window)
2017-07-07 17:56:49 -07:00
return {'RUNNING_MODAL'}
return invoke
def poll_subprocess(self):
2017-07-07 17:56:49 -07:00
self.log.debug("polling")
try:
if self.pipe[0].poll():
newdata = self.pipe[0].recv()
else:
newdata = None
except EOFError:
self.log.debug("done polling")
return {'FINISHED'}
if newdata is not None:
self.handle_response(newdata) #TODO: make this a customizable callback
# this should allow chaining of multiple subprocess in a single operator
2017-07-07 17:56:49 -07:00
return {'PASS_THROUGH'}
decoratify(cls, 'execute', decorate_execute)
decoratify(cls, 'invoke', decorate_invoke)
setattr(cls, 'poll_subprocess', poll_subprocess)
return cls