Skip to content

Commit

Permalink
chore: use waylay namespaced package (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-schoonjans committed Jan 5, 2024
1 parent ed768f8 commit b32a6ec
Show file tree
Hide file tree
Showing 22 changed files with 69 additions and 59 deletions.
29 changes: 15 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,24 @@ install: ### install waylay with frozen dependencies
dev-reinstall: full-clean dev-install ### Remove all dependences and reinstall with frozen dependencies

lint:
@pylint -E waylay test/*/*.py
@pylint -E src test/*/*.py
@${printMsg} lint OK

lint-minimal:
@pylint -E waylay test/*/*.py --confidence=INFERENCE
@pylint -E src test/*/*.py --confidence=INFERENCE
@${printMsg} lint-minimal OK

codestyle:
@pycodestyle waylay test/*/*.py
@pycodestyle src test/*/*.py
@${printMsg} codestyle OK

typecheck:
@mypy waylay test/*/*.py
@mypy -p waylay
@mypy test/*/*.py
@${printMsg} typecheck OK

docstyle:
@pydocstyle waylay test/*/*.py
@pydocstyle src test/*/*.py
@${printMsg} docstyle OK

code-qa: codestyle docstyle lint typecheck ### perform code quality checks
Expand All @@ -100,19 +101,19 @@ test-unit:
@pytest test/unit -m "not (sklearn or pytorch or tensorflow or xgboost or cleanup)"

test-unit-coverage:
@pytest --cov-report term-missing:skip-covered --cov=waylay --cov-fail-under=90 test/unit
@pytest --cov-report term-missing:skip-covered --cov=src --cov-fail-under=90 test/unit

test-unit-coverage-report: ### generate html coverage report for the unit tests
@pytest --cov-report html:cov_report --cov=waylay --cov-fail-under=90 test/unit
@pytest --cov-report html:cov_report --cov=src --cov-fail-under=90 test/unit

test-coverage-report: ### generate html coverage report for the unit and integration tests
@pytest --cov-report html:cov_report_all --cov=waylay --cov-fail-under=90 test/unit test/integration -m "not byoml_integration"
@pytest --cov-report html:cov_report_all --cov=src --cov-fail-under=90 test/unit test/integration -m "not byoml_integration"

test-integration:
@pytest test/integration -m "not byoml_integration"

test-integration-coverage-report: ### generate html coverage report for the integration tests
@pytest --cov-report html:cov_report/integration --cov=waylay test/integration
@pytest --cov-report html:cov_report/integration --cov=src test/integration

test: code-qa test-unit ### perform all quality checks and tests, except for integration tests

Expand Down Expand Up @@ -181,8 +182,8 @@ freeze-dependencies: ## perform a full reinstall procedure and regenerate the 'r
pdoc: # TODO
rm -fr ./doc/api/
pdoc -o ./doc/api \
waylay/client.py \
waylay/config.py \
waylay/exceptions.py \
waylay/auth.py \
waylay/service/base.py
src/waylay/client.py \
src/waylay/config.py \
src/waylay/exceptions.py \
src/waylay/auth.py \
src/waylay/service/base.py
20 changes: 11 additions & 9 deletions docs/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import os
import asyncio

from waylay import WaylayClient
from waylay.client import WaylayClient
from waylay.exceptions import RestResponseError
from waylay.api import ApiResponse

# from waylay.service import registry
from registry.queries.plug_functions_api_queries import ListVersionsQuery
from registry.models import ListPlugsWithQueryResponseV2
try:
from waylay.services.registry.queries.plug_functions_api import ListVersionsQuery
from waylay.services.registry.models import ListPlugsWithQueryResponseV2
registry_types_available = True
except ImportError:
registry_types_available = False


[api_key, api_secret] = os.environ["WU"].split(':')
Expand All @@ -18,14 +20,14 @@ waylay = WaylayClient.from_client_credentials(
async def main():
try:
# example of a request
query: ListVersionsQuery = {'archive_format': ['node'], 'include_deprecated': True}
result = await waylay.registry.plug_functions.list_versions('test', query=query)
print(isinstance(result, ListPlugsWithQueryResponseV2))
query_: 'ListVersionsQuery' = {'archive_format': ['node'], 'include_deprecated': True}
result = await waylay.registry.plug_functions.list_versions('test', query=query_)
print(type(result)) # `ListPlugsWithQueryResponseV2` if registry_types_available else `SimpleNamespace`
print(result.entities[0].plug.name, result.entities[0].plug.version, result.entities[0].status)

# example of a request where the result should include http info such as `status_code`, `raw_data`, ...
result = await waylay.registry.plug_functions.list_versions('test', query=query_, with_http_info=True)
print(result.status_code, isinstance(result, ApiResponse[ListPlugsWithQueryResponseV2]))
print(result.status_code, result.data.entities[0].plug.name)

# example of a request that will result in an ApiError
bad_query: ListVersionsQuery = {'archive_format': ['node'], 'include_deprecated': True, 'limit': 500000}
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ dependencies = [


[tool.setuptools.package-data]
"waylay" = ["py.typed"]

[tool.setuptools.packages.find]
where = ["src/"]
exclude = []

[project.optional-dependencies]
Expand All @@ -39,10 +39,10 @@ dev = [
'waylay[services,services-types]',
]
services = [
"registry_api @ git+https://github.com/waylayio/waylay-py-services@feat/setup#subdirectory=services/registry/registry_api"
"waylay_registry_api @ git+https://github.com/waylayio/waylay-py-services@feat/waylay_namespace#subdirectory=services/registry/waylay_registry_api"
]
services-types = [
"registry_types @ git+https://github.com/waylayio/waylay-py-services@feat/setup#subdirectory=services/registry/registry_types"
"waylay_registry_types @ git+https://github.com/waylayio/waylay-py-services@feat/waylay_namespace#subdirectory=services/registry/waylay_registry_types"
]
[tool.pytest.ini_options]
env = [
Expand Down
File renamed without changes.
28 changes: 24 additions & 4 deletions waylay/api/api_client.py → src/waylay/api/api_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""API client."""

from importlib import import_module
from types import SimpleNamespace
from typing import Any, Dict, Optional, List, cast

import datetime
Expand All @@ -13,7 +14,7 @@

from urllib.parse import quote

from waylay import __version__
from waylay.client import __version__
from waylay.api.api_config import ApiConfig
from waylay.api.api_response import ApiResponse

Expand Down Expand Up @@ -312,11 +313,13 @@ def __deserialize(self, data, klass):
klass = self.NATIVE_TYPES_MAPPING[klass]
else:
try:
types_pkg_name, class_name = klass.split('.')
types_module = import_module(types_pkg_name)
[types_module_name, class_name] = klass.rsplit('.', 1)
types_module = import_module(types_module_name)
klass = getattr(types_module, class_name)
return self.__deserialize_model(data, klass)
except BaseException:
klass = object
return self.__deserialize_simple_namespace(data)

if klass in self.PRIMITIVE_TYPES:
return self.__deserialize_primitive(data, klass)
Expand All @@ -327,7 +330,7 @@ def __deserialize(self, data, klass):
elif klass == datetime.datetime:
return self.__deserialize_datetime(data)
else:
return self.__deserialize_model(data, klass)
return data

def files_parameters(self, files=None):
"""Build form parameters.
Expand Down Expand Up @@ -489,3 +492,20 @@ def __deserialize_model(self, data, klass):
"""

return klass.from_dict(data)

def __deserialize_simple_namespace(self, data):
"""Deserializes dict to a `SimpleNamespace`.
:param data: str.
:return: SimpleNamespace.
"""
if type(data) is list:
return list(map(self.__deserialize_simple_namespace, data))
elif type(data) is dict:
sns = SimpleNamespace()
for key, value in data.items():
setattr(sns, key, self.__deserialize_simple_namespace(value))
return sns
else:
return data
2 changes: 1 addition & 1 deletion waylay/api/api_config.py → src/waylay/api/api_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from waylay.config import WaylayConfig

from waylay import __version__
from waylay.client import __version__


class ApiConfig:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 7 additions & 5 deletions waylay/client.py → src/waylay/client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""REST client for the Waylay Platform."""

__version__ = '0.0.0'

from typing import List, Optional


from .api.api_client import ApiClient
from .api.api_config import ApiConfig
from waylay.api.api_client import ApiClient
from waylay.api.api_config import ApiConfig
from .exceptions import ConfigError

from .service.base import WaylayService
from .service.registry import RegistryService, registry_available
from waylay.services._base import WaylayService
from waylay.services._loader import RegistryService, registry_available

from .config import (
WaylayConfig,
Expand Down Expand Up @@ -114,7 +116,7 @@ def load_services(self, config: WaylayConfig):
"""Load all services that are installed."""
self.api_client = ApiClient(ApiConfig(config))

self.registry = RegistryService(self.api_client)
self.registry = RegistryService(self.api_client, name='registry')
if registry_available:
self._services.append(self.registry)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion waylay/service/base.py → src/waylay/services/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, *args, **kwargs):

def __getattr__(self, name):
"""Get attribute."""
raise ImportError('{0} service is not installed'.format(
raise ImportError('Waylay {0} service is not installed'.format(
self.name.capitalize() if self.name else 'This'))

def __bool__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
"""Waylay Function Registry Service."""

from typing import TYPE_CHECKING
from ._base import WaylayServiceStub

from ..base import WaylayServiceStub, WaylayService

# registry service
try:
from registry.service import RegistryService
import registry.api as api
# from registry import *
from waylay.services.registry.service import RegistryService
registry_available = True

except ImportError:
registry_available = False
if not TYPE_CHECKING:
RegistryService = WaylayServiceStub

try:
import registry.models as models
import registry.queries as queries
from waylay.services.registry import models, queries
registry_types_available = True

except ImportError:
registry_types_available = False
2 changes: 1 addition & 1 deletion test/integration/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from waylay import ClientCredentials, WaylayClient
from waylay.client import ClientCredentials, WaylayClient
from waylay.auth import WaylayCredentials, WaylayTokenAuth


Expand Down
3 changes: 1 addition & 2 deletions test/integration/test_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Integration tests for waylay.auth module."""
from waylay import (
from waylay.client import (
WaylayClient
)
import re

import waylay.auth_interactive
from waylay.config import (
Expand Down
2 changes: 1 addition & 1 deletion test/unit/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from datetime import datetime

from waylay import WaylayConfig
from waylay.config import WaylayConfig
from waylay.exceptions import ConfigError
from waylay.auth import (
DEFAULT_GATEWAY_URL, WaylayTokenAuth, WaylayToken,
Expand Down
7 changes: 0 additions & 7 deletions waylay/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions waylay/service/__init__.py

This file was deleted.

0 comments on commit b32a6ec

Please sign in to comment.