Skip to content

Refactor all function based converters #158

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

### Changed

- Improve Converter template.py usability
- Remove functionality for function based converters

## [v0.10.0] - 2025-03-11

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ Install the pre-commit hook with `pre-commit install`, so you never commit incor

The following high-level description gives an idea how to implement a converter in fiboa CLI:

1. Create a new file in `fiboa_cli/datasets` based on the `template.py`
2. Implement the `convert()` function / test it / run it
1. Create a new file in `fiboa_cli/datasets/your_file.py` based on the `template.py`
2. Fill in the required variables / test it / run it
3. Add missing dependencies into a separate dependency group in `setup.py`
4. Add the converter to the list above
5. Create a PR to submit your converter for review
Expand Down
6 changes: 3 additions & 3 deletions fiboa_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ def converters(providers, sources, verbose):
"""
log(f"fiboa CLI {__version__} - List of Converters\n", "success")

columns = {"SHORT_NAME": "Short Title", "LICENSE": "License"}
columns = {"short_name": "Short Title", "license": "License"}
if providers:
columns["PROVIDERS"] = "Provider(s)"
columns["providers"] = "Provider(s)"
if sources:
columns["SOURCES"] = "Source(s)"
columns["sources"] = "Source(s)"

keys = list(columns.keys())
converters = list_all_converters(keys)
Expand Down
43 changes: 16 additions & 27 deletions fiboa_cli/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,15 @@ def list_all_converters(keys):
obj = {}
try:
converter = read_converter(id)
# todo remove this if once class-based converters have been fully implemented
if isinstance(converter, BaseConverter):
assert converter.__class__.__name__ != "TemplateConverter", (
f"Please change TemplateConverter for {id}"
)

for key in keys:
# todo remove this if once class-based converters have been fully implemented
if isinstance(converter, BaseConverter):
value = getattr(converter, key.lower())
else:
value = getattr(converter, key, None)
value = getattr(converter, key.lower())

if key == "SOURCES" and isinstance(value, dict):
if key == "sources" and isinstance(value, dict):
value = ", ".join(list(value.keys()))
elif key == "LICENSE" and isinstance(value, dict):
elif key == "license" and isinstance(value, dict):
value = value["href"]
elif key == "PROVIDERS" and isinstance(value, list):
elif key == "providers" and isinstance(value, list):
value = ", ".join(list(map(lambda x: x["name"], value)))

obj[key] = value
Expand All @@ -93,17 +84,15 @@ def list_all_converters(keys):
def read_converter(_id):
module_name = f".datasets.{_id}"
module = importlib.import_module(module_name, package="fiboa_cli")
# todo: remove conditional once class-based converters have been fully implemented
if not hasattr(module, "convert"):
try:
clazz = next(
v
for v in module.__dict__.values()
if type(v) is type
and issubclass(v, BaseConverter)
and "BaseConverter" not in v.__name__
)
return clazz()
except StopIteration:
log("Missing convert function or Converter class for module {_id}", "warning")
return module
try:
clazz = next(
v
for v in module.__dict__.values()
if type(v) is type
and issubclass(v, BaseConverter)
and "BaseConverter" not in v.__name__
)
return clazz()
except StopIteration:
log(f"Missing Converter class for module {_id}", "error")
raise ImportError(f"Missing Converter class for module {_id}")
77 changes: 1 addition & 76 deletions fiboa_cli/convert_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,73 +28,6 @@
from .version import fiboa_version


def convert(
output_file,
cache,
urls,
column_map,
id,
title,
description,
input_files=None,
bbox=None,
providers=[],
source_coop_url=None,
extensions=set(),
missing_schemas={},
column_additions={},
column_filters={},
column_migrations={},
migration=None,
file_migration=None,
layer_filter=None,
attribution=None,
store_collection=False,
license=None,
compression=None,
geoparquet1=False,
original_geometries=False,
index_as_id=False,
year=None, # noqa unused
**kwargs,
):
# this function is a (temporary) bridge from function-based converters to class-based converters
# todo: this convert-function should be removed once class-based converters have been fully implemented

converter = BaseConverter(
sources=urls,
columns=column_map,
id=id,
title=title,
description=description,
bbox=bbox,
providers=providers,
short_name=id,
extensions=extensions,
missing_schemas=missing_schemas,
column_additions=column_additions,
column_filters=column_filters,
column_migrations=column_migrations,
migrate=migration,
file_migration=file_migration,
layer_filter=layer_filter,
attribution=attribution,
license=license,
index_as_id=index_as_id,
)
converter.convert(
output_file,
cache,
input_files=input_files,
source_coop_url=source_coop_url,
store_collection=store_collection,
compression=compression,
geoparquet1=geoparquet1,
original_geometries=original_geometries,
**kwargs,
)


def add_asset_to_collection(collection, output_file, rows=None, columns=None):
c = collection.copy()
if "assets" not in c or not isinstance(c["assets"], dict):
Expand Down Expand Up @@ -184,11 +117,7 @@ class BaseConverter:

index_as_id = False

def __init__(self, **kwargs):
# This init method allows you to override all properties & methods
# It's a bit hacky but allows a gradual conversion from function-based to class-based converters
# todo remove this once class-based converters have been fully implemented
self.__dict__.update({k: v for k, v in kwargs.items() if v is not None})
def __init__(self):
for key in ("id", "short_name", "title", "license", "columns"):
assert getattr(self, key) is not None, (
f"{inspect.getfile(self.__class__)}:{self.__class__.__name__} misses required attribute {key}"
Expand All @@ -200,10 +129,6 @@ def __init__(self, **kwargs):
if not key.startswith("_") and isinstance(item, (list, dict, set)):
setattr(self, key, copy(item))

@property
def ID(self): # noqa backwards compatibility for function-based converters
return self.id

def migrate(self, gdf) -> gpd.GeoDataFrame:
return gdf

Expand Down
Loading