From dcc82d4bd8461e9cc4c390f6c001def056bc7b73 Mon Sep 17 00:00:00 2001 From: manuel83 Date: Mon, 7 Jan 2019 22:05:52 +0100 Subject: [PATCH] requirements fix --- cbpi/controller/config_controller.py | 4 +++ cbpi/controller/dashboard_controller.py | 3 +++ cbpi/controller/plugin_controller.py | 7 ++--- cbpi/controller/system_controller.py | 10 ++++--- cbpi/controller/translation_controller.py | 29 ++++++++++++++++++++ cbpi/craftbeerpi.py | 7 +++-- cbpi/database/model.py | 32 +++++++++++++++++++++++ cbpi/http_endpoints/http_translation.py | 20 ++++++++++++++ 8 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 cbpi/controller/translation_controller.py create mode 100644 cbpi/http_endpoints/http_translation.py diff --git a/cbpi/controller/config_controller.py b/cbpi/controller/config_controller.py index 224bc90..6fc0d3d 100644 --- a/cbpi/controller/config_controller.py +++ b/cbpi/controller/config_controller.py @@ -18,6 +18,10 @@ class ConfigController(): self.cbpi = cbpi self.cbpi.register(self) + + def get_state(self): + return self.cache + async def init(self): this_directory = os.path.dirname(__file__) diff --git a/cbpi/controller/dashboard_controller.py b/cbpi/controller/dashboard_controller.py index b4f9082..e7952d9 100644 --- a/cbpi/controller/dashboard_controller.py +++ b/cbpi/controller/dashboard_controller.py @@ -17,6 +17,9 @@ class DashboardController(CRUDController): self.logger = logging.getLogger(__name__) self.cbpi.register(self) + def get_state(self): + return dict(items=self.cache) + async def get_content(self, dashboard_id): return await DashboardContentModel.get_by_dashboard_id(dashboard_id) diff --git a/cbpi/controller/plugin_controller.py b/cbpi/controller/plugin_controller.py index 4919d49..02cd616 100644 --- a/cbpi/controller/plugin_controller.py +++ b/cbpi/controller/plugin_controller.py @@ -108,13 +108,14 @@ class PluginController(): ''' logger.info("Register %s Class %s" % (name, clazz.__name__)) if issubclass(clazz, CBPiActor): - self.cbpi.actor.types[name] = {"class": clazz, "config": self._parse_props(clazz)} + #self.cbpi.actor.types[name] = {"class": clazz, "config": self._parse_props(clazz)} + self.cbpi.actor.types[name] = self._parse_props(clazz) if issubclass(clazz, CBPiSensor): - self.cbpi.sensor.types[name] = {"class": clazz, "config": self._parse_props(clazz)} + self.cbpi.sensor.types[name] = self._parse_props(clazz) if issubclass(clazz, CBPiKettleLogic): - self.cbpi.kettle.types[name] = {"class": clazz, "config": self._parse_props(clazz)} + self.cbpi.kettle.types[name] = self._parse_props(clazz) if issubclass(clazz, CBPiSimpleStep): self.cbpi.step.types[name] = self._parse_props(clazz) diff --git a/cbpi/controller/system_controller.py b/cbpi/controller/system_controller.py index 47a0814..d7d5e12 100644 --- a/cbpi/controller/system_controller.py +++ b/cbpi/controller/system_controller.py @@ -14,14 +14,17 @@ class SystemController(): self.service = cbpi.actor self.cbpi.register(self, "/system") - @request_mapping("/state", method="GET", auth_required=False) + @request_mapping("/", method="GET", auth_required=False) def state(self, request): # TODO implement restart return web.json_response(data=dict( actor=self.cbpi.actor.get_state(), sensor=self.cbpi.sensor.get_state(), kettle=self.cbpi.kettle.get_state(), - step=self.cbpi.step.get_state()) + step=self.cbpi.step.get_state(), + dashboard=self.cbpi.dashboard.get_state(), + translations=self.cbpi.translation.get_all(), + config=self.cbpi.config.get_state()) , dumps=json_dumps) @request_mapping("/restart", method="POST", name="RestartServer", auth_required=False) @@ -47,4 +50,5 @@ class SystemController(): @request_mapping("/events", method="GET", name="get_all_events", auth_required=False) def get_all_events(self, request): - return web.json_response(data=self.cbpi.bus.dump()) \ No newline at end of file + return web.json_response(data=self.cbpi.bus.dump()) + diff --git a/cbpi/controller/translation_controller.py b/cbpi/controller/translation_controller.py new file mode 100644 index 0000000..88ccaaa --- /dev/null +++ b/cbpi/controller/translation_controller.py @@ -0,0 +1,29 @@ +import logging + +from database.model import TranslationModel + + +class TranslationController(object): + + + def __init__(self, cbpi): + self.cbpi = cbpi + self._cache = {} + self.logger = logging.getLogger(__name__) + + async def init(self): + self._cache = await TranslationModel.get_all() + print(self._cache) + + + def get_all(self): + return self._cache + + async def add_key(self, locale, key): + + try: + if locale not in self._cache or key not in self._cache[locale]: + await TranslationModel.add_key(locale, key) + self._cache = await TranslationModel.get_all() + except Exception as e: + self.logger.error("Error during adding translation key %s - %s - %s" % (key, locale, str(e))) diff --git a/cbpi/craftbeerpi.py b/cbpi/craftbeerpi.py index 4af48b3..2d5a9df 100644 --- a/cbpi/craftbeerpi.py +++ b/cbpi/craftbeerpi.py @@ -30,7 +30,8 @@ from cbpi.http_endpoints.http_dashboard import DashBoardHttpEndpoints from cbpi.http_endpoints.http_kettle import KettleHttpEndpoints from cbpi.http_endpoints.http_sensor import SensorHttpEndpoints from cbpi.http_endpoints.http_step import StepHttpEndpoints - +from controller.translation_controller import TranslationController +from http_endpoints.http_translation import TranslationHttpEndpoint logger = logging.getLogger(__name__) @@ -73,6 +74,7 @@ class CraftBeerPi(): self.bus = CBPiEventBus(self.app.loop, self) self.ws = CBPiWebSocket(self) self.job = JobController(self) + self.translation = TranslationController(self) self.actor = ActorController(self) self.sensor = SensorController(self) self.plugin = PluginController(self) @@ -88,6 +90,7 @@ class CraftBeerPi(): self.http_actor = ActorHttpEndpoints(self) self.http_kettle = KettleHttpEndpoints(self) self.http_dashboard = DashBoardHttpEndpoints(self) + self.http_translation = TranslationHttpEndpoint(self) self.notification = NotificationController(self) self.login = Login(self) @@ -221,7 +224,7 @@ class CraftBeerPi(): await self.job.init() await DBModel.setup() await self.config.init() - + await self.translation.init() self._setup_http_index() self.plugin.load_plugins() self.plugin.load_plugins_from_evn() diff --git a/cbpi/database/model.py b/cbpi/database/model.py index 5c03512..c8fb5f2 100644 --- a/cbpi/database/model.py +++ b/cbpi/database/model.py @@ -130,3 +130,35 @@ class DashboardContentModel(DBModel): async with aiosqlite.connect(DATABASE_FILE) as db: await db.execute("DELETE FROM %s WHERE dbid = ?" % (cls.__table_name__), (id,)) await db.commit() + + +class TranslationModel(DBModel): + __fields__ = ["key", "text", "language_code"] + __table_name__ = "translation" + __json_fields__ = [] + __priamry_key__ = "key" + + @classmethod + async def get_all(cls): + + result = {} + async with aiosqlite.connect(DATABASE_FILE) as db: + sql = "SELECT * FROM %s" % cls.__table_name__ + db.row_factory = DBModel.dict_factory + async with db.execute(sql) as cursor: + async for row in cursor: + code = row.get("language_code") + key = row.get("key") + text = row.get("text") + if code not in result: + result[code] = {} + result[code][key] = text + await cursor.close() + + return result + + @classmethod + async def add_key(cls, locale, key): + async with aiosqlite.connect(DATABASE_FILE) as db: + await db.execute("INSERT INTO %s (language_code, key, text) VALUES (?,?, ' ')" % (cls.__table_name__), (locale, key)) + await db.commit() diff --git a/cbpi/http_endpoints/http_translation.py b/cbpi/http_endpoints/http_translation.py new file mode 100644 index 0000000..413c6a3 --- /dev/null +++ b/cbpi/http_endpoints/http_translation.py @@ -0,0 +1,20 @@ +from aiohttp import web +from aiohttp_auth import auth + +from cbpi.api import * + + +class TranslationHttpEndpoint(): + + def __init__(self,cbpi): + self.cbpi = cbpi + self.cbpi.register(self, url_prefix="/translation") + + + @request_mapping(path="/missing_key", method="POST", auth_required=False) + async def missing_key(self, request): + data = await request.json() + await self.cbpi.translation.add_key(**data) + return web.Response(status=204) + +