Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finalized web structure and first working test page. Still a lot of s…
…tuff to do....
- Loading branch information
Showing
11 changed files
with
313 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
|
||
PLUGINS = [ | ||
"GwPluginsInfo", | ||
"GwWeb", | ||
"GwWebFlask" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from groundwork.util import gw_get | ||
|
||
|
||
class ContextManagerPlugin: | ||
|
||
def __init__(self, plugin): | ||
self.plugin = plugin | ||
self.log = plugin.log | ||
self.app = plugin.app | ||
|
||
def register(self, name, template_folder, static_folder, url_prefix, description): | ||
return self.app.web.contexts.register(name, template_folder, static_folder, | ||
url_prefix, description, self.plugin) | ||
|
||
|
||
class ContextManagerApplication: | ||
def __init__(self, app): | ||
self._contexts = {} | ||
self.app = app | ||
self.default_context = None | ||
|
||
def register(self, name, template_folder, static_folder, url_prefix, description, plugin): | ||
if name not in self._contexts.keys(): | ||
self._contexts[name] = Context(name, template_folder, static_folder, url_prefix, description, plugin) | ||
if name == self.app.config.get("DEFAULT_CONTEXT", None) or self.default_context is None: | ||
self.default_context = self._contexts[name] | ||
|
||
for name, provider in self.app.web.providers.get().items(): | ||
provider.register_context() | ||
|
||
def get(self, name=None, plugin=None): | ||
return gw_get(self._contexts, name, plugin) | ||
|
||
|
||
class Context: | ||
""" | ||
Contexts are used to collect common objects in a single place and make it easy for new web routes to reuse | ||
these objects. | ||
Common objects are: | ||
* A template folder | ||
* A static folder | ||
* A url_prefix | ||
* A static folder web route, which gets automatically calculated based on given context name | ||
They are similar to flask blueprint concept. | ||
""" | ||
def __init__(self, name, template_folder, static_folder, url_prefix, description, plugin): | ||
self.name = name | ||
self.template_folder = template_folder | ||
self.static_folder = static_folder | ||
self.url_prefix = url_prefix | ||
self.description = description | ||
self.plugin = plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from groundwork.util import gw_get | ||
|
||
|
||
class RouteManagerPlugin: | ||
|
||
def __init__(self, plugin): | ||
self.plugin = plugin | ||
self.log = plugin.log | ||
self.app = plugin.app | ||
|
||
def register(self, url, methods, endpoint, context=None, name=None, description=None): | ||
return self.app.web.routes.register(url, methods, endpoint, self.plugin, context, name, description) | ||
|
||
|
||
class RouteManagerApplication: | ||
def __init__(self, app): | ||
self._routes = {} | ||
self.app = app | ||
|
||
def register(self, url, methods, endpoint, plugin, context=None, name=None, description=None,): | ||
if context is None and self.app.web.contexts.default_context is None: | ||
raise RuntimeError("Context not give and no default context is available.") | ||
|
||
if context is None: | ||
context = self.app.web.contexts.default_context.name | ||
|
||
if name not in self._routes.keys(): | ||
self._routes[name] = Route(url, methods, endpoint, context, name, description, plugin) | ||
|
||
for provider in self.app.web.providers.get(): | ||
provider.instance.register_route() | ||
|
||
def get(self, name=None, plugin=None): | ||
return gw_get(self._routes, name, plugin) | ||
|
||
|
||
class Route: | ||
""" | ||
""" | ||
def __init__(self, url, methods, endpoint, context, name, description, plugin): | ||
self.url = url | ||
self.methods = methods | ||
self.endpoint = endpoint | ||
self.context = context | ||
self.name = name | ||
self.description = description | ||
self.plugin = plugin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,51 @@ | ||
class ServerManagerPlugin: | ||
from groundwork.util import gw_get | ||
|
||
|
||
class ServerManagerPlugin(): | ||
|
||
def __init__(self, plugin): | ||
self.plugin = plugin | ||
self.log = plugin.log | ||
self.app = plugin.app | ||
|
||
def register(self, name, description): | ||
return self.app.web.server.register(name, description, self.plugin) | ||
def register(self, name, function, description): | ||
return self.app.web.servers.register(name, function, description, self.plugin) | ||
|
||
def get(self, name=None): | ||
""" | ||
Returns servers, which can be filtered by name. | ||
:param name: name of the server | ||
:type name: str | ||
:return: None, single server or dict of servers | ||
""" | ||
return self.app.web.servers.get(name, self.plugin) | ||
|
||
|
||
class ServerManagerApplication: | ||
def __init__(self, app): | ||
self._servers = {} | ||
self.app = app | ||
|
||
def register(self, name, description, plugin): | ||
def register(self, name, function, description, plugin): | ||
if name not in self._servers.keys(): | ||
self._servers[name] = Server(name, description, plugin) | ||
self._servers[name] = Server(name, function, description, plugin) | ||
|
||
def get(self, name=None, plugin=None): | ||
""" | ||
Returns servers, which can be filtered by name or plugin. | ||
:param name: name of the server | ||
:type name: str | ||
:param plugin: plugin name, which registers the servers | ||
:return: None, single server or dict of servers | ||
""" | ||
return gw_get(self._servers, name, plugin) | ||
|
||
|
||
class Server: | ||
def __init__(self, name, start, description, plugin): | ||
class Server(): | ||
def __init__(self, name, function, description, plugin): | ||
self.name = name | ||
self.start = start | ||
self.function = function | ||
self.description = description | ||
self.plugin = plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__author__ = 'daniel' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
from click import Argument, echo | ||
|
||
from groundwork.patterns import GwCommandsPattern | ||
|
||
|
||
from groundwork_web.patterns import GwWebPattern | ||
|
||
|
||
class GwWeb(GwWebPattern, GwCommandsPattern): | ||
def __init__(self, *args, **kwargs): | ||
self.name = self.__class__.__name__ | ||
super().__init__(*args, **kwargs) | ||
|
||
def activate(self): | ||
self.commands.register("server_start", "starts a given server", | ||
self.__server_start, | ||
params=[Argument(("server",), required=True)]) | ||
|
||
self.commands.register("server_list", "prints a list of registered server", | ||
self.__server_list) | ||
|
||
template_folder = os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates") | ||
static_folder = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static") | ||
self.web.contexts.register("web", | ||
template_folder=template_folder, | ||
static_folder=static_folder, | ||
url_prefix=None, | ||
description="web context, which was created by GwWeb as initial context") | ||
|
||
if self.app.config.get("SHOW_WEB_TEST_PAGE", True): | ||
self.web.routes.register("/test", ["GET"], self.__test_view, | ||
name="Test", description="Test view of GwWeb") | ||
|
||
def deactivate(self): | ||
self.commands.unregister("server_start") | ||
self.commands.unregister("server_list") | ||
|
||
def __server_start(self, server): | ||
servers = self.app.web.servers.get() | ||
if server not in servers.keys(): | ||
echo("Server '%s' not found.") | ||
echo("Available servers: %s" % ",".join(servers.keys())) | ||
else: | ||
echo("Starting server %s" % server) | ||
servers[server].function() | ||
|
||
def __server_list(self): | ||
servers = self.app.web.servers.get() | ||
echo("List of registered servers\n") | ||
for name, server in servers.items(): | ||
echo(name) | ||
echo("*"*len(name)) | ||
echo(" Description: %s" % server.description) | ||
echo(" Plugin: %s" % server.plugin.name) | ||
|
||
def __test_view(self): | ||
return self.web.providers.render("test.html") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello there! |
Oops, something went wrong.