diff --git a/peasant/client/__init__.py b/peasant/client/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/peasant/client/protocol.py b/peasant/client/protocol.py new file mode 100644 index 0000000..95194bb --- /dev/null +++ b/peasant/client/protocol.py @@ -0,0 +1,63 @@ +# Copyright 2020-2024 Flavio Garcia +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from peasant.client.transport import Transport +import logging + +logger = logging.getLogger(__name__) + + +class Peasant(object): + + _transport: Transport + + def __init__(self, transport): + self._directory_cache = None + self._transport = transport + self._transport.peasant = self + + @property + def directory_cache(self): + return self._directory_cache + + @directory_cache.setter + def directory_cache(self, directory_cache): + self._directory_cache = directory_cache + + @property + def transport(self): + return self._transport + + def directory(self): + if self.directory_cache is None: + self.transport.set_directory() + return self.directory_cache + + def new_nonce(self): + return self.transport.new_nonce() + + +class AsyncPeasant(Peasant): + + def __init__(self, transport): + super(AsyncPeasant, self).__init__(transport) + + async def directory(self): + if self._directory_cache is None: + future = self.transport.set_directory() + if future is not None: + logger.debug("Running transport set directory cache " + "asynchronously.") + await future + return self._directory_cache diff --git a/peasant/client.py b/peasant/client/tornado.py similarity index 72% rename from peasant/client.py rename to peasant/client/tornado.py index 47a94ab..8c7b5a2 100644 --- a/peasant/client.py +++ b/peasant/client/tornado.py @@ -15,90 +15,11 @@ import copy import logging from peasant import get_version +from peasant.client.transport import Transport from urllib.parse import urlencode logger = logging.getLogger(__name__) - -class PeasantTransport: - - _peasant: "Peasant" - - @property - def peasant(self): - return self._peasant - - @peasant.setter - def peasant(self, peasant: "Peasant"): - self._peasant = peasant - - def get(self, path, **kwargs): - raise NotImplementedError - - def head(self, path, **kwargs): - raise NotImplementedError - - def post(self, path, **kwargs): - raise NotImplementedError - - def post_as_get(self, path, **kwargs): - raise NotImplementedError - - def set_directory(self): - raise NotImplementedError - - def new_nonce(self): - raise NotImplementedError - - def is_registered(self): - raise NotImplementedError - - -class Peasant(object): - - _transport: PeasantTransport - - def __init__(self, transport): - self._directory_cache = None - self._transport = transport - self._transport.peasant = self - - @property - def directory_cache(self): - return self._directory_cache - - @directory_cache.setter - def directory_cache(self, directory_cache): - self._directory_cache = directory_cache - - @property - def transport(self): - return self._transport - - def directory(self): - if self.directory_cache is None: - self.transport.set_directory() - return self.directory_cache - - def new_nonce(self): - return self.transport.new_nonce() - - -class AsyncPeasant(Peasant): - - def __init__(self, transport): - super(AsyncPeasant, self).__init__(transport) - - async def directory(self): - if self._directory_cache is None: - future = self.transport.set_directory() - if future is not None: - logger.debug("Running transport set directory cache " - "asynchronously.") - await future - return self._directory_cache - - tornado_installed = False try: from tornado.httpclient import HTTPRequest @@ -139,7 +60,7 @@ def get_tornado_request(url, **kwargs): pass -class TornadoTransport(PeasantTransport): +class TornadoTransport(Transport): def __init__(self, bastion_address): super().__init__() diff --git a/peasant/client/transport.py b/peasant/client/transport.py new file mode 100644 index 0000000..9049b85 --- /dev/null +++ b/peasant/client/transport.py @@ -0,0 +1,57 @@ +# Copyright 2020-2024 Flavio Garcia +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import annotations + +import logging +import typing + +if typing.TYPE_CHECKING: + from peasant.client.protocol import Peasant + +logger = logging.getLogger(__name__) + + +class Transport: + + _peasant: Peasant + + @property + def peasant(self) -> Peasant: + return self._peasant + + @peasant.setter + def peasant(self, peasant: Peasant): + self._peasant = peasant + + def get(self, path, **kwargs): + raise NotImplementedError + + def head(self, path, **kwargs): + raise NotImplementedError + + def post(self, path, **kwargs): + raise NotImplementedError + + def post_as_get(self, path, **kwargs): + raise NotImplementedError + + def set_directory(self): + raise NotImplementedError + + def new_nonce(self): + raise NotImplementedError + + def is_registered(self): + raise NotImplementedError diff --git a/tests/runtests.py b/tests/runtests.py index f31bd96..1e089de 100644 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# print current directory import unittest from tests import tornado_test diff --git a/tests/tornado_test.py b/tests/tornado_test.py index 1e491f1..d6b8dfc 100644 --- a/tests/tornado_test.py +++ b/tests/tornado_test.py @@ -14,7 +14,7 @@ from firenado.testing import TornadoAsyncTestCase from firenado.launcher import ProcessLauncher -from peasant.client import TornadoTransport +from peasant.client.tornado import TornadoTransport from tests import chdir_fixture_app, PROJECT_ROOT from tornado.testing import gen_test