Coverage for django_napse/core/models/bots/plugin.py: 68%
37 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
1from django.db import models
3from django_napse.core.models.bots.managers.plugin import PluginManager
4from django_napse.utils.findable_class import FindableClass
7class Plugin(models.Model, FindableClass):
8 strategy = models.ForeignKey("Strategy", on_delete=models.CASCADE, related_name="plugins")
9 category = models.CharField(max_length=255)
11 objects = PluginManager()
13 def __str__(self):
14 return f"PLUGIN {self.pk=}"
16 def info(self, verbose=True, beacon=""):
17 string = ""
18 string += f"{beacon}Plugin {self.pk}:\n"
19 string += f"{beacon}Args:\n"
20 string += f"{beacon}\t{self.category=}\n"
21 return string
23 @classmethod
24 def plugin_category(cls):
25 if cls == Plugin:
26 error_msg = "plugin_category not implemented for the Plugin base class, please implement it in a subclass."
27 else:
28 error_msg = f"plugin_category not implemented for the Plugin base class, please implement it in the {cls} class."
29 raise NotImplementedError(error_msg)
31 def _connect(self, connection):
32 if self.__class__ == Plugin:
33 error_msg = "connect not implemented for the Plugin base class, please implement it in a subclass."
34 else:
35 error_msg = f"connect not implemented for the Plugin base class, please implement it in the {self.__class__} class."
36 raise NotImplementedError(error_msg)
38 def connect(self, connection):
39 self = self.find()
40 return self._connect(connection)
42 def _apply(self, data: dict) -> dict:
43 if self.__class__ == Plugin:
44 error_msg = "_apply not implemented for the Plugin base class, please implement it in a subclass."
45 else:
46 error_msg = f"_apply not implemented for the Plugin base class, please implement it in the {self.__class__} class."
47 raise NotImplementedError(error_msg)
49 def apply(self, data: dict) -> dict:
50 self = self.find()
51 return self._apply(data)