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

Fix: Postgis processing #287

Merged
merged 4 commits into from
Jun 4, 2024
Merged
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
89 changes: 61 additions & 28 deletions dicogis/cli/cmd_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,51 @@
# ##################################


def get_serializer_from_parameters(
output_format: str,
output_path: Path,
opt_raw_path: bool,
opt_prettify_size: bool,
localized_strings: dict | None = None,
) -> MetadatasetSerializerJson | MetadatasetSerializerXlsx:
"""Initiate the adequat serializer depending on parameters.

Args:
output_format: output format
output_path: output path
opt_raw_path: option to serialize dataset raw path without any sugar syntax
opt_prettify_size: option to prettify size in octets (typically: 1 ko instead
of 1024 octects)
localized_strings: localized texts. Defaults to None.

Returns:
_description_
"""
if output_format == "excel":
# creating the Excel workbook
output_serializer = MetadatasetSerializerXlsx(
opt_raw_path=opt_raw_path,
opt_size_prettify=opt_prettify_size,
output_path=output_path,
localized_strings=localized_strings,
)
elif output_format == "json":
output_serializer = MetadatasetSerializerJson(
output_path=output_path,
localized_strings=localized_strings,
opt_size_prettify=opt_prettify_size,
)
else:
logger.error(
NotImplementedError(
f"Specified output format '{output_format}' is not available."
)
)
typer.Exit(1)

return output_serializer


def determine_output_path(
output_path: Path | str | None,
output_format: str = "excel",
Expand All @@ -54,6 +99,7 @@ def determine_output_path(
output_path: output path passed to inventory CLI
output_format: input output format passed to inventory CLI
input_folder: input folder passed to inventory CLI
pg_services: list of ppostgres services names to use

Raises:
ValueError: if output format is not supported
Expand Down Expand Up @@ -249,25 +295,6 @@ def inventory(
localized_strings = TextsManager().load_texts(language_code=language)

# output format
if output_format == "excel":
# creating the Excel workbook
output_serializer = MetadatasetSerializerXlsx(
translated_texts=localized_strings,
opt_raw_path=opt_raw_path,
opt_size_prettify=opt_prettify_size,
output_path=output_path,
)
elif output_format == "json":
output_serializer = MetadatasetSerializerJson(
translated_texts=localized_strings, output_path=output_path
)
else:
logger.error(
NotImplementedError(
f"Specified output format '{output_format}' is not available."
)
)
typer.Exit(1)

# look for geographic data files
if input_folder is not None:
Expand All @@ -277,6 +304,13 @@ def inventory(
input_folder=input_folder,
)

output_serializer = get_serializer_from_parameters(
output_format=output_format,
output_path=output_path,
opt_prettify_size=opt_prettify_size,
opt_raw_path=opt_raw_path,
)

li_vectors = []
(
num_folders,
Expand Down Expand Up @@ -391,15 +425,22 @@ def inventory(
output_path = determine_output_path(
output_path=output_path,
output_format=output_format,
input_folder=input_folder,
pg_services=pg_services,
)

output_serializer = get_serializer_from_parameters(
output_format=output_format,
output_path=output_path,
opt_prettify_size=opt_prettify_size,
opt_raw_path=opt_raw_path,
)

print("Start looking for geographic table in PostGIS...")
# configure output workbook
output_serializer.pre_serializing(has_sgbd=True)

for pg_service in pg_services:
print(f"Start processing using PostgreSQL service: {pg_service}")

# testing connection settings
sgbd_reader = ReadPostGIS(service=pg_service)
Expand Down Expand Up @@ -427,14 +468,6 @@ def inventory(
logger.debug("Layer metadata stored into workbook.")

output_serializer.post_serializing()
saved = Utilities.safe_save(
output_object=output_serializer,
dest_dir=f"{output_path.parent.resolve()}",
dest_filename=f"{output_path.resolve()}",
ftype="Excel Workbook",
gui=False,
)
logger.info(f"Workbook saved: {saved[1]}")

send_system_notify(
notification_title="DicoGIS analysis ended",
Expand Down
9 changes: 7 additions & 2 deletions dicogis/export/base_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import logging
from pathlib import Path

# project
from dicogis.utils.texts import TextsManager

# ############################################################################
# ######### Globals ############
# ##############################
Expand All @@ -25,13 +28,15 @@ class MetadatasetSerializerBase:

def __init__(
self,
translated_texts: dict,
localized_strings: dict | None = None,
output_path: Path | None = None,
opt_raw_path: bool = False,
opt_size_prettify: bool = True,
) -> None:
"""Initialize object."""
self.translated_texts = translated_texts
self.localized_strings = localized_strings
if self.localized_strings is None:
self.localized_strings = TextsManager().load_texts()

# output path
self.output_path = output_path
Expand Down
4 changes: 2 additions & 2 deletions dicogis/export/to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class MetadatasetSerializerJson(MetadatasetSerializerBase):

def __init__(
self,
translated_texts: dict,
localized_strings: dict | None = None,
output_path: Path | None = None,
opt_size_prettify: bool = True,
) -> None:
"""Store metadata into JSON files."""
output_path.mkdir(parents=True, exist_ok=True)

super().__init__(
translated_texts=translated_texts,
localized_strings=localized_strings,
opt_size_prettify=opt_size_prettify,
output_path=output_path,
)
Expand Down
Loading
Loading