From 9877dda9b9db39c276e8dd89925d2521adcce707 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 17 Oct 2015 23:43:14 +0200 Subject: [PATCH] Create a basic, modular frontend web service Signed-off-by: Michael Tremer --- Makefile.am | 17 ++++++- po/POTFILES.in | 1 + src/scripts/westferry.in | 25 ++++++++++ src/westferry/__init__.py | 22 +++++++++ src/westferry/application.py | 48 ++++++++++++++++++++ src/westferry/handlers/__init__.py | 29 ++++++++++++ src/westferry/handlers/base.py | 46 +++++++++++++++++++ src/westferry/handlers/index.py | 28 ++++++++++++ src/westferry/services.py | 53 ++++++++++++++++++++++ src/westferry/ui/__init__.py | 34 ++++++++++++++ src/westferry/ui/base.py | 73 ++++++++++++++++++++++++++++++ 11 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 src/westferry/application.py create mode 100644 src/westferry/handlers/__init__.py create mode 100644 src/westferry/handlers/base.py create mode 100644 src/westferry/handlers/index.py create mode 100644 src/westferry/services.py create mode 100644 src/westferry/ui/__init__.py create mode 100644 src/westferry/ui/base.py diff --git a/Makefile.am b/Makefile.am index cd6aebd..edeaac9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -75,7 +75,9 @@ dist_configs_DATA = \ westferry.conf.sample westferry_PYTHON = \ - src/westferry/__init__.py + src/westferry/__init__.py \ + src/westferry/application.py \ + src/westferry/services.py westferrydir = $(pythondir)/westferry @@ -84,6 +86,19 @@ westferry_backend_PYTHON = \ westferry_backenddir = $(pythondir)/westferry/backend +westferry_handlers_PYTHON = \ + src/westferry/handlers/__init__.py \ + src/westferry/handlers/base.py \ + src/westferry/handlers/index.py + +westferry_handlersdir = $(pythondir)/westferry/handlers + +westferry_ui_PYTHON = \ + src/westferry/ui/__init__.py \ + src/westferry/ui/base.py + +westferry_uidir = $(pythondir)/westferry/ui + # ------------------------------------------------------------------------------ if ENABLE_MANPAGES diff --git a/po/POTFILES.in b/po/POTFILES.in index ee12baa..77f3c22 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,2 +1,3 @@ src/westferry/backend/__version__.py +src/westferry/handlers/base.py src/westferry/__init__.py diff --git a/src/scripts/westferry.in b/src/scripts/westferry.in index e69de29..aa23655 100644 --- a/src/scripts/westferry.in +++ b/src/scripts/westferry.in @@ -0,0 +1,25 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import westferry + +s = westferry.services.WebService() +s.run() diff --git a/src/westferry/__init__.py b/src/westferry/__init__.py index e69de29..9bd1ba1 100644 --- a/src/westferry/__init__.py +++ b/src/westferry/__init__.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from . import services diff --git a/src/westferry/application.py b/src/westferry/application.py new file mode 100644 index 0000000..0caabdf --- /dev/null +++ b/src/westferry/application.py @@ -0,0 +1,48 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import tornado.web + +from . import handlers +from . import ui + +class WebApplication(tornado.web.Application): + def __init__(self, **kwargs): + settings = { + # Enable compressed output + "compress_response" : True, + + # Use Cross-Site-Request-Forgery protection + "xsrf_cookies" : True, + } + settings.update(kwargs) + + # Get handlers + h = [(h.url, h) for h in handlers.get_handlers()] + + # Get UI modules and methods + settings.update({ + "ui_methods" : ui.get_ui_methods(), + "ui_modules" : ui.get_ui_modules(), + }) + + # Initialise the web application + tornado.web.Application.__init__(self, handlers=h, **settings) diff --git a/src/westferry/handlers/__init__.py b/src/westferry/handlers/__init__.py new file mode 100644 index 0000000..a934bbd --- /dev/null +++ b/src/westferry/handlers/__init__.py @@ -0,0 +1,29 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from . import base +from . import index + +def get_handlers(): + """ + Returns a list of all registered handlers + """ + return base.HandlerRegistration.get_handlers() diff --git a/src/westferry/handlers/base.py b/src/westferry/handlers/base.py new file mode 100644 index 0000000..e3d2461 --- /dev/null +++ b/src/westferry/handlers/base.py @@ -0,0 +1,46 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import tornado.web + +_handlers = [] + +class HandlerRegistration(type): + def __init__(handler, name, bases, dict): + type.__init__(handler, name, bases, dict) + + # The main class from which is inherited is not registered + # as a plugin + if name.endswith("BaseHandler"): + return + + if handler.url is None: + raise RuntimeError(_("Handler %s is improperly configured") % handler) + + _handlers.append(handler) + + @staticmethod + def get_handlers(): + return _handlers + + +class BaseHandler(tornado.web.RequestHandler, metaclass=HandlerRegistration): + url = None diff --git a/src/westferry/handlers/index.py b/src/westferry/handlers/index.py new file mode 100644 index 0000000..3edf7bc --- /dev/null +++ b/src/westferry/handlers/index.py @@ -0,0 +1,28 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from . import base + +class IndexHandler(base.BaseHandler): + url = r"/" + + def get(self): + self.finish("Hello World") diff --git a/src/westferry/services.py b/src/westferry/services.py new file mode 100644 index 0000000..f459318 --- /dev/null +++ b/src/westferry/services.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import tornado.httpserver +import tornado.ioloop + +from . import application + +class Service(object): + pass + + +class WebService(Service): + def __init__(self, port=80): + self.port = port + + def make_application(self, **kwargs): + return application.WebApplication(**kwargs) + + @property + def ioloop(self): + return tornado.ioloop.IOLoop.instance() + + def run(self, **kwargs): + app = self.make_application(**kwargs) + + # Create a HTTP server instance + server = tornado.httpserver.HTTPServer(app) + server.bind(self.port) + + # Launch the server + server.start() + + # Launch the IOLoop + self.ioloop.start() diff --git a/src/westferry/ui/__init__.py b/src/westferry/ui/__init__.py new file mode 100644 index 0000000..a7641dd --- /dev/null +++ b/src/westferry/ui/__init__.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from . import base + +def get_ui_methods(): + """ + Returns all registered UI methods. + """ + return base.UIMethodsRegistration.get_ui_methods() + +def get_ui_modules(): + """ + Returns all registered UI modules. + """ + return base.UIModulesRegistration.get_ui_modules() diff --git a/src/westferry/ui/base.py b/src/westferry/ui/base.py new file mode 100644 index 0000000..699d07d --- /dev/null +++ b/src/westferry/ui/base.py @@ -0,0 +1,73 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import tornado.web + +_ui_methods = {} +_ui_modules = {} + +class UIMethodsRegistration(type): + def __init__(ui_method, name, bases, dict): + type.__init__(ui_method, name, bases, dict) + + # The main class from which is inherited is not registered + # as a plugin + if name == "BaseUIMethod": + return + + if ui_method.handle is None: + raise RuntimeError + + _ui_methods[name] = ui_method() + + @staticmethod + def get_ui_methods(): + return _ui_methods + + +class UIModulesRegistration(type): + def __init__(ui_module, name, bases, dict): + type.__init__(ui_module, name, bases, dict) + + # The main class from which is inherited is not registered + # as a plugin + if name == "BaseUIModule": + return + + if name.endswith("Module"): + name = name[:-6] + + _ui_modules[name] = ui_module + + @staticmethod + def get_ui_modules(): + return _ui_modules + + +class BaseUIMethod(object, metaclass=UIMethodsRegistration): + handle = None + + def __call__(self, *args, **kwargs): + raise NotImplementedError + + +class BaseUIModule(tornado.web.UIModule, metaclass=UIModulesRegistration): + pass -- 2.39.2