Skip to content

Commit

Permalink
Running and adding ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
whubsch committed Aug 9, 2024
1 parent b3d0f56 commit abbad0e
Show file tree
Hide file tree
Showing 19 changed files with 409 additions and 142 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/black.yml

This file was deleted.

12 changes: 12 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: formatter

on: [push, pull_request]

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
with:
args: "format --check"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ pip-delete-this-directory.txt
scripts/test*.py
.pypirc
.coverage
.vscode
.vscode
.DS-Store
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Convert Overture's `places`, `buildings`, and `addresses` features to OSM tags."""
34 changes: 22 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ license = "MIT"
keywords = ["overture", "osm", "openstreetmap", "mapping", "overture maps"]
authors = [{ name = "Will", email = "wahubsch@gmail.com" }]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"License :: OSI Approved :: MIT License",
"Typing :: Typed",
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"License :: OSI Approved :: MIT License",
"Typing :: Typed",
]
dependencies = ["pydantic"]

Expand Down Expand Up @@ -52,3 +52,13 @@ tests = ["tests", "*/overturetoosm/tests"]

[tool.coverage.report]
exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"]

[tool.ruff.lint]
select = ["D", "E", "F", "I"]
pydocstyle.convention = "google"
exclude = ["tests/*", "scripts/*"]
isort.split-on-trailing-comma = false

[tool.ruff.format]
docstring-code-format = true
skip-magic-trailing-comma = true
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pydantic>=2.8.2
pytest>=8.3.2
17 changes: 7 additions & 10 deletions src/overturetoosm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""`overturetoosm` is a Python package to convert objects tagged in the
Overture schema for use in OSM. Only Overture's `places`, `buildings`,
"""Convert Overture's `places`, `buildings`, and `addresses` features to OSM tags.
`overturetoosm` is a Python package to convert objects tagged in the
Overture schema for use in OSM. Only Overture's `places`, `buildings`,
and `addresses` layers are currently supported.
Links:
Expand All @@ -8,16 +10,11 @@
* [PyPI](https://pypi.org/project/overturetoosm/)
"""

from .places import process_place
from .buildings import process_building
from . import addresses, buildings, objects, places, resources, utils
from .addresses import process_address
from .buildings import process_building
from .places import process_place
from .utils import process_geojson
from . import places
from . import buildings
from . import addresses
from . import objects
from . import utils
from . import resources

__all__ = [
"process_place",
Expand Down
10 changes: 4 additions & 6 deletions src/overturetoosm/addresses.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Convert Overture's `addresses` features to OSM tags."""

from typing import Dict

from .objects import AddressProps


def process_address(
props: dict,
style: str = "US",
) -> Dict[str, str]:
def process_address(props: dict, style: str = "US") -> Dict[str, str]:
"""Convert Overture's address properties to OSM tags.
Args:
Expand All @@ -16,7 +14,7 @@ def process_address(
a pull request or issue to add support for other regions. Defaults to "US".
Returns:
Dict[str, str]: The reshaped and converted properties in OSM's flat str:str schema.
Dict[str, str]: The reshaped and converted properties in OSM's flat
str:str schema.
"""

return AddressProps(**props).to_osm(style)
10 changes: 4 additions & 6 deletions src/overturetoosm/buildings.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
"""Convert Overture's `buildings` features to OSM tags."""

from typing import Dict

from .objects import BuildingProps


def process_building(
props: dict,
confidence: float = 0.0,
) -> Dict[str, str]:
def process_building(props: dict, confidence: float = 0.0) -> Dict[str, str]:
"""Convert Overture's building properties to OSM tags.
Args:
props (dict): The feature properties from the Overture GeoJSON.
confidence (float, optional): The minimum confidence level. Defaults to 0.0.
Returns:
Dict[str, str]: The reshaped and converted properties in OSM's flat str:str schema.
Dict[str, str]: The reshaped and converted properties in OSM's flat
str:str schema.
Raises:
`overturetoosm.objects.ConfidenceError`: Raised if the confidence level is set
above a feature's confidence.
"""

return BuildingProps(**props).to_osm(confidence)
67 changes: 27 additions & 40 deletions src/overturetoosm/objects.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"""Pydantic models needed throughout the project."""

# pylint: disable=E1136, C0103
# ruff: noqa: D415

from enum import Enum
from typing import Dict, List, Optional
from pydantic import (
BaseModel,
ConfigDict,
Field,
field_validator,
)

from pydantic import BaseModel, ConfigDict, Field, field_validator

from .resources import places_tags


Expand Down Expand Up @@ -63,12 +60,7 @@ class Names(BaseModel):
"""Overture names model."""

primary: str
common: Optional[
Dict[
str,
str,
]
]
common: Optional[Dict[str, str]]
rules: Optional[List[Rules]]


Expand Down Expand Up @@ -115,13 +107,12 @@ class PlaceProps(OvertureBaseModel):
addresses: List[PlaceAddress]

def to_osm(
self,
confidence: float,
region_tag: str,
unmatched: str,
self, confidence: float, region_tag: str, unmatched: str
) -> Dict[str, str]:
"""Convert Overture's place properties to OSM tags. Used internally
by the `overturetoosm.process_place` function."""
"""Convert Overture's place properties to OSM tags.
Used internallyby the `overturetoosm.process_place` function.
"""
new_props = {}
if self.confidence < confidence:
raise ConfidenceError(confidence, self.confidence)
Expand Down Expand Up @@ -175,8 +166,7 @@ def to_osm(


class ConfidenceError(Exception):
"""
Confidence error exception.
"""Confidence error exception.
This exception is raised when the confidence level of an item is below the
user-defined level. It contains the original confidence level and the confidence
Expand All @@ -202,13 +192,13 @@ def __init__(

def __str__(self) -> str:
"""@private"""
conf = f"confidence_level={self.confidence_level}, confidence_item={self.confidence_item}"
return f"""{self.message} {conf}"""
lev = f"confidence_level={self.confidence_level}"
item = f"confidence_item={self.confidence_item}"
return f"""{self.message} {lev}, {item}"""


class UnmatchedError(Exception):
"""
Unmatched category error.
"""Unmatched category error.
This exception is raised when an item's Overture category does not have a
corresponding OSM definition. Edit
Expand All @@ -221,9 +211,7 @@ class UnmatchedError(Exception):
"""

def __init__(
self,
category: str,
message: str = "Overture category is unmatched.",
self, category: str, message: str = "Overture category is unmatched."
) -> None:
"""@private"""
self.category = category
Expand All @@ -238,7 +226,7 @@ def __str__(self) -> str:
class BuildingProps(OvertureBaseModel):
"""Overture building properties.
Use this model directly if you want to manipulate the `building` properties yourself.
Use this model if you want to manipulate the `building` properties yourself.
"""

class_: Optional[str] = Field(alias="class", default=None)
Expand Down Expand Up @@ -280,12 +268,11 @@ class BuildingProps(OvertureBaseModel):
serialization_alias="roof:height", default=None
)

def to_osm(
self,
confidence: float,
) -> Dict[str, str]:
"""Convert properties to OSM tags. Used internally by
`overturetoosm.process_building` function."""
def to_osm(self, confidence: float) -> Dict[str, str]:
"""Convert properties to OSM tags.
Used internally by`overturetoosm.process_building` function.
"""
new_props = {}
confidences = {source.confidence for source in self.sources}
if any(conf and conf < confidence for conf in confidences):
Expand Down Expand Up @@ -327,11 +314,11 @@ class AddressProps(OvertureBaseModel):
address_levels: Optional[List[AddressLevel]] = Field(default_factory=list)
sources: List[Sources]

def to_osm(
self,
style: str,
) -> Dict[str, str]:
"""Convert properties to OSM tags. Used internally by `overturetoosm.process_address`."""
def to_osm(self, style: str) -> Dict[str, str]:
"""Convert properties to OSM tags.
Used internally by `overturetoosm.process_address`.
"""
obj_dict = {
k: v
for k, v in self.model_dump(exclude_none=True, by_alias=True).items()
Expand Down
9 changes: 5 additions & 4 deletions src/overturetoosm/places.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Convert Overture's `places` features to OSM tags."""

from typing import Literal, Dict
from typing import Dict, Literal

from .objects import PlaceProps

Expand Down Expand Up @@ -38,11 +38,12 @@ def process_place(
only returns other properties. Defaults to "ignore".
Returns:
dict[str, str]: The reshaped and converted properties in OSM's flat str:str schema.
dict[str, str]: The reshaped and converted properties in OSM's flat str:str
schema.
Raises:
`overturetoosm.objects.UnmatchedError`: Raised if `unmatched` is set to `error` and
the Overture category has no OSM definition.
`overturetoosm.objects.UnmatchedError`: Raised if `unmatched` is set to `error`
and the Overture category has no OSM definition.
`overturetoosm.objects.ConfidenceError`: Raised if the confidence level is set
above a feature's confidence.
"""
Expand Down
1 change: 0 additions & 1 deletion src/overturetoosm/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from typing import Dict


places_tags: Dict[str, Dict[str, str]] = {
"eat_and_drink": {"amenity": "restaurant"},
"restaurant": {"amenity": "restaurant"},
Expand Down
Loading

0 comments on commit abbad0e

Please sign in to comment.