From 90a209e69f455b49c4a642fb2b3288f915fed1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Tue, 3 Oct 2017 22:02:16 +0200 Subject: Basic server implementation --- archivist/cli.py | 11 +++++++++++ archivist/server.py | 31 +++++++++++++++++++++++++++++++ archivist/utils.py | 25 +++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 archivist/server.py create mode 100644 archivist/utils.py diff --git a/archivist/cli.py b/archivist/cli.py index 5f1d8ba..9299c94 100644 --- a/archivist/cli.py +++ b/archivist/cli.py @@ -5,6 +5,7 @@ from functools import reduce import operator as op from .virtual_prefixes import query as query_virtual, is_virtual, register_prefixes +from .utils import ProxyCommand CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) @@ -19,6 +20,16 @@ def enable_debug(): def cli(debug): if debug or 'DEBUG' in os.environ: enable_debug() + +class ServerCommand(ProxyCommand): + def _get_proxy(self): + from .server import server_group + return server_group + +@cli.group(cls=ServerCommand) +def server(): + """Flask server handling""" + @cli.group() def db(): """Database Management""" diff --git a/archivist/server.py b/archivist/server.py new file mode 100644 index 0000000..8500af0 --- /dev/null +++ b/archivist/server.py @@ -0,0 +1,31 @@ +import click +from flask.cli import FlaskGroup, ScriptInfo +from flask import Flask +from flask_restful import Api, Resource, marshal_with, marshal_with_field, fields + +from . import model as m + +class Tag(Resource): + tag_fields = { + 'name' : fields.String, + 'prefix' : fields.String + } + +class TagList(Resource): + tag_list = { fields.List(fields.Nested(Tag.tag_fields)) } + + @marshal_with_field(fields.List(fields.Nested(Tag.tag_fields))) + def get(self): + res = list(m.Tag.select().where(~m.Tag.default).dicts().iterator()) + print(res) + return res + +def create_app(info): + app = Flask('archivist') + api = Api(app) + api.add_resource(TagList, '/') + return app + +server_group = FlaskGroup( + name='server', + context_settings = {'obj' : ScriptInfo(create_app=create_app)}) diff --git a/archivist/utils.py b/archivist/utils.py new file mode 100644 index 0000000..391a8fb --- /dev/null +++ b/archivist/utils.py @@ -0,0 +1,25 @@ +from click import MultiCommand + +class ProxyCommand(MultiCommand): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.__proxy = None + + def _get_proxy(self): + raise NotImplementedError("_get_proxy") + + def __load(self): + if self.__proxy is None: + self.__proxy = self._get_proxy() + + def list_commands(self,ctx): + self.__load() + return self.__proxy.list_commands(ctx) + + def get_command(self,ctx,name): + self.__load() + return self.__proxy.get_command(ctx,name) + + def make_context(self, *args, **kwargs): + self.__load() + return self.__proxy.make_context(*args, **kwargs) -- cgit v1.2.3