forked from mehdiBezahaf/intent-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
59 lines (46 loc) · 1.96 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import importlib.util
from pathlib import Path
import logging
import sys
from intent_deployer.compile import nile_handlers, dialogflow_handlers, intent_executors, alert_listeners
log: logging.Logger = logging.getLogger(__name__)
intent_dir = Path(__file__).parent.parent / "intents"
alert_dir = Path(__file__).parent.parent / "alerts"
def load_intents():
for intent_defn in intent_dir.glob("*.py"):
log.info("Loading definition of intent '%s'", intent_defn.stem)
load_intent(intent_defn)
log.info("Loaded NILE intent defns: %s", list(nile_handlers.keys()))
log.info("Loaded Dialogflow intent defns: %s", list(dialogflow_handlers.keys()))
log.info(
"Loaded intent executors: %s", [k.__name__ for k in intent_executors.keys()]
)
def load_intent(path: Path):
try:
name = f"intents.{path.stem}"
spec = importlib.util.spec_from_file_location(name, path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert module is not None
sys.modules[name] = module
assert spec.loader is not None
spec.loader.exec_module(module)
except Exception as e:
raise Exception(f"Failed to load definition of intent '{path.stem}'") from e
def load_alerts():
for alert_defn in alert_dir.glob("*.py"):
log.info("Loading definition of alert '%s'", alert_defn.stem)
load_alert(alert_defn)
log.info("Loaded alert listeners for events: %s", list(alert_listeners.keys()))
def load_alert(path: Path):
try:
name = f"intents.{path.stem}"
spec = importlib.util.spec_from_file_location(name, path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert module is not None
sys.modules[name] = module
assert spec.loader is not None
spec.loader.exec_module(module)
except Exception as e:
raise Exception(f"Failed to load definition of alert '{path.stem}'") from e