Execute javascript in such order: * setup: :obj:`.setup`, :meth:`.add_setup` * run: :obj:`.run`, :meth:`.add_run` * post: :obj:`.post`, :meth:`.add_post` If it is required to change executable name, use classvar :obj:`.node`. Note that this must be a executable name, so somethi
| 53 | |
| 54 | |
| 55 | class ExecJS: |
| 56 | """Execute javascript in such order: |
| 57 | |
| 58 | * setup: :obj:`.setup`, :meth:`.add_setup` |
| 59 | * run: :obj:`.run`, :meth:`.add_run` |
| 60 | * post: :obj:`.post`, :meth:`.add_post` |
| 61 | |
| 62 | If it is required to change executable name, use classvar :obj:`.node`. Note that this must be a |
| 63 | executable name, so something like ``bash -c`` is illegal. |
| 64 | """ |
| 65 | |
| 66 | __slots__ = "setup", "run", "post", "__dict__" |
| 67 | |
| 68 | node: str = "node" |
| 69 | """node executable name. Default as :program:`node`.""" |
| 70 | |
| 71 | setup: List[JsExpr] |
| 72 | """Expressions executed before :obj:`.run`. |
| 73 | Will not be cleared after executing.""" |
| 74 | |
| 75 | run: List[JsExpr] |
| 76 | """Expressions to be executed, after :obj:`.setup` but before :obj:`.post`. |
| 77 | Will be cleared after executing.""" |
| 78 | |
| 79 | post: List[JsExpr] |
| 80 | """Expressions executed after :obj:`.run`. |
| 81 | Will not be cleared after executing.""" |
| 82 | |
| 83 | @classmethod |
| 84 | def check_node(cls): |
| 85 | return which(cls.node) is not None |
| 86 | |
| 87 | @classmethod |
| 88 | def loop_policy_check(cls): |
| 89 | """On Windows, the default event loop :external+python:class:`asyncio.ProactorEventLoop` supports subprocesses, |
| 90 | whereas :external+python:class:`asyncio.SelectorEventLoop` does not. |
| 91 | """ |
| 92 | # if sys.platform == "win32" and isinstance( |
| 93 | # asyncio.get_event_loop_policy(), asyncio.WindowsSelectorEventLoopPolicy |
| 94 | # ): |
| 95 | # return False |
| 96 | # return True |
| 97 | |
| 98 | def check_all(self): |
| 99 | """Run all checks to see if the environment is ready to run.""" |
| 100 | if not self.check_node(): |
| 101 | raise NodeNotFoundError(self.node) |
| 102 | # assert self.loop_policy_check(), "loop policy cannot be `WindowsSelectorEventLoopPolicy`" |
| 103 | |
| 104 | def __init__(self): |
| 105 | super().__init__() |
| 106 | self.check_all() |
| 107 | self.setup = [] |
| 108 | self.run = [] |
| 109 | self.post = [] |
| 110 | |
| 111 | def add_setup(self, func: str, *args, asis: bool = False): |
| 112 | """Add a setup function to :obj:`.setup`.""" |
no outgoing calls
no test coverage detected