Skip to content

Commit

Permalink
Added support for hocon config
Browse files Browse the repository at this point in the history
Signed-off-by: Kharude, Sachin <sachin.kharude@here.com>
  • Loading branch information
Kharude, Sachin authored and sackh committed May 7, 2021
1 parent debea1e commit 343e87d
Show file tree
Hide file tree
Showing 24 changed files with 119 additions and 43 deletions.
8 changes: 8 additions & 0 deletions docs/notebooks/data/xyz_configuration.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
credentials: {
XYZ_TOKEN: "MY-XYZ-TOKEN"
}
http_headers: {
Authorization: "Bearer MY-XYZ-TOKEN"
Content-Type: "application/geo+json"
}
url: "https://xyz.api.here.com"
10 changes: 0 additions & 10 deletions docs/notebooks/data/xyz_configuration.json

This file was deleted.

2 changes: 1 addition & 1 deletion docs/notebooks/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"from xyzspaces.apis import HubApi\n",
"from xyzspaces.datasets import get_countries_data\n",
"import xyzspaces\n",
"from xyzspaces.config.default_config import XYZConfig"
"from xyzspaces.config.default import XYZConfig"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks/spaces_class_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"import warnings\n",
"from xyzspaces.datasets import get_countries_data\n",
"import xyzspaces\n",
"from xyzspaces.config.default_config import XYZConfig"
"from xyzspaces.config.default import XYZConfig"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks/talk_pyml_geopython_2020.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"import xyzspaces.datasets\n",
"from xyzspaces.apis import HubApi\n",
"from xyzspaces import XYZ\n",
"from xyzspaces.config.default_config import XYZConfig"
"from xyzspaces.config.default import XYZConfig"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks/visual_clipping.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"from xyzspaces.tools import subset_geojson\n",
"from xyzspaces.utils import feature_to_bbox\n",
"import xyzspaces\n",
"from xyzspaces.config.default_config import XYZConfig"
"from xyzspaces.config.default import XYZConfig"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks/xyz_pro_features_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"from xyzspaces.datasets import get_chicago_parks_data, get_countries_data\n",
"from xyzspaces.exceptions import ApiError\n",
"import xyzspaces\n",
"from xyzspaces.config.default_config import XYZConfig"
"from xyzspaces.config.default import XYZConfig"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions docs/notebooks/xyzspaces_configurations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"outputs": [],
"source": [
"from xyzspaces import XYZ\n",
"from xyzspaces.config.default_config import XYZConfig"
"from xyzspaces.config.default import XYZConfig"
]
},
{
Expand Down Expand Up @@ -92,7 +92,7 @@
"id": "confidential-newspaper",
"metadata": {},
"source": [
"## User defined configuration using json file\n",
"## User defined configuration using config file\n",
"You can read configuration from json file."
]
},
Expand All @@ -104,9 +104,9 @@
"outputs": [],
"source": [
"from xyzspaces import XYZ\n",
"from xyzspaces.config.default_config import XYZConfig\n",
"from xyzspaces.config.default import XYZConfig\n",
"\n",
"xyz_config = XYZConfig.from_file(\"data/xyz_configuration.json\")"
"xyz_config = XYZConfig.from_file(\"data/xyz_configuration.conf\")"
]
},
{
Expand Down
8 changes: 8 additions & 0 deletions docs/source/xyzspaces.config.default.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
xyzspaces.config.default module
===============================

.. automodule:: xyzspaces.config.default
:members:
:undoc-members:
:show-inheritance:
:private-members:
8 changes: 0 additions & 8 deletions docs/source/xyzspaces.config.default_config.rst

This file was deleted.

2 changes: 1 addition & 1 deletion docs/source/xyzspaces.config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Submodules
.. toctree::
:maxdepth: 4

xyzspaces.config.default_config
xyzspaces.config.default
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ geopandas
turfpy>=0.0.3
geobuf
ijson==3.1.1
pyhocon
Empty file added tests/config/__init__.py
Empty file.
75 changes: 75 additions & 0 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""This module tests custom configurations for xyzspaces."""

import os
from pathlib import Path

from xyzspaces.config.default import DEFAULT_CONFIG, XYZConfig


def test_default_config():
"""Test default config."""
xyzconfig = XYZConfig.from_default()
assert xyzconfig.config["credentials"]["XYZ_TOKEN"] == os.environ.get(
"XYZ_TOKEN"
)
assert xyzconfig.config["credentials"]["HERE_USER"] == os.environ.get(
"HERE_USER"
)
assert xyzconfig.config["credentials"]["HERE_PASSWORD"] == os.environ.get(
"HERE_PASSWORD"
)
assert xyzconfig.config["url"] == "https://xyz.api.here.com"
assert (
xyzconfig.config["http_headers"]["Authorization"]
== f"Bearer {os.environ.get('XYZ_TOKEN')}"
)
assert (
xyzconfig.config["http_headers"]["Content-Type"]
== "application/geo+json"
)


def test_config_object():
"""Test configurations using config object."""
xyzconfig = XYZConfig(**DEFAULT_CONFIG)
assert xyzconfig.config["credentials"]["XYZ_TOKEN"] == os.environ.get(
"XYZ_TOKEN"
)
assert xyzconfig.config["credentials"]["HERE_USER"] == os.environ.get(
"HERE_USER"
)
assert xyzconfig.config["credentials"]["HERE_PASSWORD"] == os.environ.get(
"HERE_PASSWORD"
)
assert xyzconfig.config["url"] == "https://xyz.api.here.com"
assert (
xyzconfig.config["http_headers"]["Authorization"]
== f"Bearer {os.environ.get('XYZ_TOKEN')}"
)
assert (
xyzconfig.config["http_headers"]["Content-Type"]
== "application/geo+json"
)


def test_config_from_file():
"""Test configurations using file."""
root = Path(__file__).parent.parent.parent
file_path = (
root
/ Path("docs")
/ Path("notebooks")
/ Path("data")
/ "xyz_configuration.conf"
)
xyzconfig = XYZConfig.from_file(file_path)
assert xyzconfig.config["credentials"]["XYZ_TOKEN"] == "MY-XYZ-TOKEN"
assert xyzconfig.config["url"] == "https://xyz.api.here.com"
assert (
xyzconfig.config["http_headers"]["Authorization"]
== "Bearer MY-XYZ-TOKEN"
)
assert (
xyzconfig.config["http_headers"]["Content-Type"]
== "application/geo+json"
)
2 changes: 1 addition & 1 deletion tests/hub/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pytest

from xyzspaces.apis import HubApi
from xyzspaces.config.default_config import XYZConfig
from xyzspaces.config.default import XYZConfig
from xyzspaces.datasets import get_chicago_parks_data, get_countries_data


Expand Down
2 changes: 1 addition & 1 deletion tests/project/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pytest

from xyzspaces.apis import ProjectApi
from xyzspaces.config.default_config import XYZConfig
from xyzspaces.config.default import XYZConfig


@pytest.fixture()
Expand Down
2 changes: 1 addition & 1 deletion tests/space/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import pytest

from xyzspaces.apis import HubApi
from xyzspaces.config.default_config import XYZConfig
from xyzspaces.config.default import XYZConfig
from xyzspaces.datasets import get_chicago_parks_data, get_countries_data
from xyzspaces.spaces import Space

Expand Down
2 changes: 1 addition & 1 deletion tests/token/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pytest

from xyzspaces.apis import TokenApi
from xyzspaces.config.default_config import XYZConfig
from xyzspaces.config.default import XYZConfig
from xyzspaces.exceptions import AuthenticationError


Expand Down
2 changes: 1 addition & 1 deletion tests/tools/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import pytest

from xyzspaces.config.default_config import XYZConfig
from xyzspaces.config.default import XYZConfig
from xyzspaces.datasets import get_countries_data
from xyzspaces.tools import subset_geojson
from xyzspaces.utils import feature_to_bbox, get_xyz_token
Expand Down
2 changes: 1 addition & 1 deletion xyzspaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from xyzspaces.spaces import Space

from .apis import HubApi
from .config.default_config import XYZConfig
from .config.default import XYZConfig


class XYZ:
Expand Down
6 changes: 3 additions & 3 deletions xyzspaces/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import xyzspaces.curl as curl

from .auth import get_auth_cookies
from .config.default_config import XYZConfig
from .config.default import XYZConfig
from .exceptions import ApiError, TooManyRequestsException
from .utils import join_string_lists

Expand Down Expand Up @@ -310,7 +310,7 @@ class TokenApi(Api):
Example:
>>> from xyzspaces.apis import TokenApi
>>> from xyzspaces.config.default_config import XYZConfig
>>> from xyzspaces.config.default import XYZConfig
>>> config = XYZConfig.from_default()
>>> api = TokenApi(config=config)
>>> api.get_tokens()
Expand Down Expand Up @@ -403,7 +403,7 @@ class HubApi(Api):
Example:
>>> from xyzspaces.apis import HubApi
>>> from xyzspaces.config.default_config import XYZConfig
>>> from xyzspaces.config.default import XYZConfig
>>> config = XYZConfig.from_default()
>>> api = HubApi(config=config)
>>> api.get_spaces()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""This module defines classes for default configuration for the project."""

import json
import os
from pathlib import Path
from typing import Union

from pyhocon import ConfigFactory

# : This config dictionary is used by default to create object of XYZConfig class
# : if user does not provide config object for project level configurations.
Expand Down Expand Up @@ -31,8 +34,7 @@ def from_default(cls) -> "XYZConfig":
return cls(**DEFAULT_CONFIG)

@classmethod
def from_file(cls, path: str) -> "XYZConfig":
def from_file(cls, path: Union[str, Path]) -> "XYZConfig":
"""Return the config from file path provided."""
with open(path) as fh:
config_data = json.load(fh)
config_data = ConfigFactory.parse_file(path)
return cls(**config_data)
2 changes: 1 addition & 1 deletion xyzspaces/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from geojson import Feature, GeoJSON

from .apis import HubApi
from .config.default_config import XYZConfig
from .config.default import XYZConfig
from .utils import divide_bbox, flatten_geometry, grouper, wkt_to_geojson

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion xyzspaces/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from typing import List, Optional

from .apis import HubApi
from .config.default_config import XYZConfig
from .config.default import XYZConfig


def subset_geojson(
Expand Down

0 comments on commit 343e87d

Please sign in to comment.