Debian Codegraph @

s_tui/sources/script_hook_loader.py

raw symbols SCIP
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2017-2025 Alex Manuskin, Gil Tsuker
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
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"