Skip to content

Commit

Permalink
Fix some formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrrock2 committed Sep 20, 2024
1 parent 55608d5 commit 6ae69ea
Show file tree
Hide file tree
Showing 23 changed files with 53 additions and 21 deletions.
8 changes: 7 additions & 1 deletion gerrydb/create.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""CLI for creating GerryDB resources."""

from typing import Optional

import click

from gerrydb import GerryDB
from gerrydb.exceptions import ResultError


@click.group()
def cli():
"""Creates GerryDB resources."""
Expand All @@ -27,6 +29,7 @@ def namespace(path: str, description: str, public: bool):
else:
raise e


@cli.command()
@click.argument("path")
@click.option("--description", required=True)
Expand All @@ -37,12 +40,15 @@ def geo_layer(path: str, description: str, namespace: str, source_url: Optional[
db = GerryDB(namespace=namespace)
with db.context(notes=f'Creating geographic layer "{path}" from CLI') as ctx:
try:
ctx.geo_layers.create(path=path, description=description, source_url=source_url)
ctx.geo_layers.create(
path=path, description=description, source_url=source_url
)
except ResultError as e:
if "Failed to create geographic layer" in e.args[0]:
print(f"Failed to create {path} layer, already exists")
else:
raise e


if __name__ == "__main__":
cli()
5 changes: 3 additions & 2 deletions gerrydb/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class CacheInitError(CacheError):

class ViewLoadError(GerryDBError):
"""Raised when a view cannot be loaded (e.g. from a GeoPackage)."""



class GerryPathError(GerryDBError):
"""Raised when an invalid path is provided. Generally, this means invalid characters are present"""
"""Raised when an invalid path is provided. Generally, this means invalid characters are present"""
1 change: 1 addition & 0 deletions gerrydb/repos/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""GerryDB API object repositories."""

from gerrydb.repos.column import ColumnRepo
from gerrydb.repos.column_set import ColumnSetRepo
from gerrydb.repos.geo_layer import GeoLayerRepo
Expand Down
1 change: 1 addition & 0 deletions gerrydb/repos/column_set.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Repository for column sets."""

from typing import Optional, Union

from gerrydb.exceptions import RequestError
Expand Down
9 changes: 6 additions & 3 deletions gerrydb/repos/geo_layer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Repository for geographic layers."""

from typing import Optional, Union

from gerrydb.repos.base import (
Expand Down Expand Up @@ -69,9 +70,11 @@ def map_locality(
response = self.ctx.client.put(
f"{self.base_url}/{layer.namespace}/{layer.path}",
params={
"locality": locality.canonical_path
if isinstance(locality, Locality)
else locality
"locality": (
locality.canonical_path
if isinstance(locality, Locality)
else locality
)
},
json=GeoSetCreate(
paths=[
Expand Down
2 changes: 1 addition & 1 deletion gerrydb/repos/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,4 @@ def all_paths(self, fips: str, layer_name: str) -> list[str]:
response.raise_for_status()
response_json = response.json()

return response_json
return response_json
30 changes: 17 additions & 13 deletions gerrydb/repos/locality.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Repository for localities."""

from dataclasses import dataclass
from typing import TYPE_CHECKING, Optional

from gerrydb.repos.base import ObjectRepo, err, normalize_path, online, write_context
from gerrydb.schemas import Locality, LocalityCreate, LocalityPatch
from gerrydb.exceptions import ResultError

if TYPE_CHECKING:
from gerrydb.client import GerryDB, WriteContext

Expand Down Expand Up @@ -35,9 +37,11 @@ def get(self, path: str) -> Optional[Locality]:
response.raise_for_status()
return Locality(**response.json())

@err("Failed to create locality") # Decorator for handling HTTP request and Pydantic validation errors
@write_context # Decorator for marking operations that require a write context
@online # Decorator for marking online-only operations
@err(
"Failed to create locality"
) # Decorator for handling HTTP request and Pydantic validation errors
@write_context # Decorator for marking operations that require a write context
@online # Decorator for marking online-only operations
def create(
self,
canonical_path: str,
Expand Down Expand Up @@ -104,24 +108,24 @@ def create_bulk(
Returns:
The new localities.
"""
loc_list = [-1]*len(locs)
loc_list = [-1] * len(locs)
for i, loc in enumerate(locs):
try:
loc_object = self.create(canonical_path=loc.canonical_path,
name=loc.name,
parent_path=loc.parent_path,
default_proj=loc.default_proj,
aliases=loc.aliases,)
loc_object = self.create(
canonical_path=loc.canonical_path,
name=loc.name,
parent_path=loc.parent_path,
default_proj=loc.default_proj,
aliases=loc.aliases,
)
loc_list[i] = loc_object
except ResultError as e:
if "Failed to create canonical path to new location(s)." in e.args[0]:
print(f"Failed to create {loc.name}, path already exists")
else:
raise e

return loc_list



# loc_list = [-1]*len(locs)
# for i, loc in enumerate(locs):
Expand All @@ -132,7 +136,7 @@ def create_bulk(
# response.raise_for_status()
# loc_list[i] = Locality(**response.json()[0])
# return(loc_list[i])

# response = self.ctx.client.post(
# "/localities/",
# json=[loc.dict() for loc in locs],
Expand Down
3 changes: 2 additions & 1 deletion gerrydb/repos/namespace.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Repository for namespaces."""

from dataclasses import dataclass
from typing import TYPE_CHECKING, Optional

Expand Down Expand Up @@ -86,7 +87,7 @@ def create(
)

response.raise_for_status()

return Namespace(**response.json())

def __getitem__(self, path: str) -> Optional[Namespace]:
Expand Down
1 change: 1 addition & 0 deletions gerrydb/repos/plan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Repository for districting plans."""

from typing import Optional, Union

from gerrydb.repos.base import (
Expand Down
1 change: 1 addition & 0 deletions tests/repos/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Fixtures for repository tests."""

import pytest


Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for base objects and utilities for GerryDB API object repositories."""

from dataclasses import dataclass
from typing import Optional

Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_column.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration/VCR tests for columns."""

import pytest
from shapely import box

Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_column_set.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration/VCR tests for columns."""

import pytest


Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_geo_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration/VCR tests for geographic import metadata."""

import pytest

# The `GeoImport` object is used for internal tracking, so we don't
Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_geo_layer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration/VCR tests for geographic layers."""

import pytest


Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_geography.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration/VCR tests for columns."""

from shapely import box


Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_locality.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration/VCR tests for localities."""

import pytest

from gerrydb.schemas import LocalityCreate
Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_namespace.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration/VCR tests for namespaces."""

import pytest


Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_plan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration/VCR tests for districting plans."""

import pytest

from gerrydb.exceptions import ResultError
Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_view.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for views."""

import pytest


Expand Down
1 change: 1 addition & 0 deletions tests/repos/test_view_template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration/VCR tests for view templates."""

import pytest


Expand Down
1 change: 1 addition & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for GerryDB's local caching layer."""

import uuid
from datetime import datetime, timedelta, timezone

Expand Down
1 change: 1 addition & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for GerryDB session management."""

import os
from unittest import mock

Expand Down

0 comments on commit 6ae69ea

Please sign in to comment.