Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parser of "dim.json" file and type checker with pyright #17

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ jobs:
run: poetry install --no-interaction --no-root --with test

- name: Linting Python codes
run: poetry run flake8 dim
run: |
poetry run pflake8 dim
poetry run pyright --warnings

- name: run quality check tests
run: poetry run pytest
Expand Down
1 change: 1 addition & 0 deletions dim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# -*- coding: utf-8 -*-

from dim.__version__ import __version__ # noqa: F401
from dim.locker.locker import Locker # noqa: F401
2 changes: 1 addition & 1 deletion dim/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__version__ = "0.1.0"
__version__ = "0.2.0"
Empty file added dim/locker/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions dim/locker/_dim_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import Dict, List, Optional
from dataclasses import dataclass, field
from dataclasses_json import DataClassJsonMixin


@dataclass
class _DimJsonContent(DataClassJsonMixin):
"""Dataclass to parse the contents of dim.json file.

Args:
name: dataset title
url (optional): URL to retrieve the dataset
catalogUrl (optional): URL if dataset is available in CKAN
catalogResourceId (optional): dataset ID if dataset is available in CKAN
headers (optional): headers of the dataset

Note:
URL of CKAN is https://www.geospatial.jp/ckan/
"""
name: str
url: Optional[str] = field(default=None)
catalogUrl: Optional[str] = field(default=None)
catalogResourceId: Optional[str] = field(default=None)
postProcesses: List[str] = field(default_factory=list)
headers: Dict[str, str] = field(default_factory=dict)


@dataclass
class _DimJson(DataClassJsonMixin):
"""Dataclass to parse dim.json file.

Args:
fileVersion: version number of dim.json file structure, fixed to '1.1' at this time.
contents: list of database definitions
"""
fileVersion: str = field(init=False)
contents: List[_DimJsonContent] = field(default_factory=list)

def __post_init__(self):
self.fileVersion = "1.1"
62 changes: 62 additions & 0 deletions dim/locker/locker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import annotations
import json
from pathlib import Path
from typing import Any
from typing_extensions import Self
from dim.locker._dim_json import _DimJson


class Locker(object):
"""Class to parse dim.json and write dim-lock.json file.

Args:
directory: directory which has dim.json to parse
create_if_missing (optional): True (create {directory}/dim.json if not exist) or False (default)

Raises:
FileNotFoundError: {errors} is not "raise" and {directory}/dim.json does not exist
"""

def __init__(self, directory: str, create_if_missing: bool = False) -> None:
self._dim_json_path = Path(directory) / "dim.json"
if self._dim_json_path.exists():
self._read_dim_json()
elif create_if_missing:
self._write_empty_dim_json()
else:
raise FileNotFoundError(f"dim.json not found in {directory=}")

def __eq__(self, other: Self) -> bool:
return self.defined == other.defined

@property
def defined(self) -> dict[str, Any]:
"""Parameter values defined by dim.json file.
"""
return self._dim_json.to_dict()

def _read_dim_json(self) -> None:
"""Read {directory}/dim.json file.
"""
self._dim_json_path.parent.mkdir(exist_ok=True)
with self._dim_json_path.open("r") as fh:
content = json.load(fh)
self._dim_json = _DimJson.from_dict(content)

def _write_empty_dim_json(self) -> None:
"""Write empty {directory}/dim.json file.
"""
self._dim_json = _DimJson()
with self._dim_json_path.open("w") as fh:
json.dump(self._dim_json.to_dict(), fh, indent=4)

def run(self) -> Self:
"""Run locking and write dim-lock.json.

Raises:
NotImplementedError: implemented later
"""
raise NotImplementedError
153 changes: 150 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.8"
dataclasses-json = "^0.5.7"
typing-extensions = "^4.4.0"

[tool.poetry.group.test.dependencies]
pytest = "^7.1.3"
pytest-cov = "^4.0.0"
flake8 = "^5.0.4"
autopep8 = "^1.7.0"
pyproject-flake8 = "^5.0.4.post1"
pyright = "^1.1.274"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand All @@ -38,3 +41,6 @@ extend-ignore = ["E501"]
[tool.pytest.ini_options]
testpaths = ["tests"]
filterwarnings = ["error"]

[tool.pyright]
include = ["dim"]
Loading