| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import os |
| 22 |
|
| 23 |
from s_tui.sources.hook_script import ScriptHook |
| 24 |
|
| 25 |
|
| 26 |
class ScriptHookLoader: |
| 27 |
""" |
| 28 |
Loads shell scripts from a directory into ScriptHooks for a given Source |
| 29 |
""" |
| 30 |
|
| 31 |
def __init__(self, dir_path: str) -> None: |
| 32 |
self.scripts_dir_path = os.path.join(dir_path, "hooks.d") |
| 33 |
|
| 34 |
def load_script( |
| 35 |
self, source_name: str, timeoutMilliseconds: int = 0 |
| 36 |
) -> ScriptHook | None: |
| 37 |
""" |
| 38 |
Return ScriptHook for source_name Source and with a ready timeout |
| 39 |
of timeoutMilliseconds |
| 40 |
""" |
| 41 |
|
| 42 |
script_path = os.path.join( |
| 43 |
self.scripts_dir_path, self._source_to_script_name(source_name) |
| 44 |
) |
| 45 |
|
| 46 |
if os.path.isfile(script_path): |
| 47 |
return ScriptHook(script_path, timeoutMilliseconds) |
| 48 |
return None |
| 49 |
|
| 50 |
def _source_to_script_name(self, source_name: str) -> str: |
| 51 |
return source_name.lower() + ".sh" |