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

refacto(serializer): move serializer from parameters as class method of serializer meta class #298

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
66 changes: 6 additions & 60 deletions dicogis/cli/cmd_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
# project
from dicogis.__about__ import __package_name__, __title__
from dicogis.constants import SUPPORTED_FORMATS, AvailableLocales, OutputFormats
from dicogis.export.to_json import MetadatasetSerializerJson
from dicogis.export.to_xlsx import MetadatasetSerializerXlsx
from dicogis.export.base_serializer import MetadatasetSerializerBase
from dicogis.georeaders.process_files import ProcessingFiles
from dicogis.georeaders.read_postgis import ReadPostGIS
from dicogis.listing.geodata_listing import check_usable_pg_services, find_geodata_files
Expand All @@ -35,64 +34,11 @@
state = {"verbose": False}
logger = logging.getLogger(__name__)
default_formats = ",".join([f.name for f in SUPPORTED_FORMATS])

# ############################################################################
# ########## Functions #############
# ##################################


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,
)
elif output_format == "udata":
output_serializer = MetadatasetSerializerJson(
flavor="udata",
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 Down Expand Up @@ -311,8 +257,8 @@ def inventory(
input_folder=input_folder,
)

output_serializer = get_serializer_from_parameters(
output_format=output_format,
output_serializer = MetadatasetSerializerBase.get_serializer_from_parameters(
format_or_serializer=output_format,
output_path=output_path,
opt_prettify_size=opt_prettify_size,
opt_raw_path=opt_raw_path,
Expand Down Expand Up @@ -371,7 +317,7 @@ def inventory(

# instanciate geofiles processor
geofiles_processor = ProcessingFiles(
format_or_serializer=output_serializer,
serializer=output_serializer,
localized_strings=localized_strings,
# list by tabs
li_vectors=li_vectors,
Expand Down Expand Up @@ -435,8 +381,8 @@ def inventory(
pg_services=pg_services,
)

output_serializer = get_serializer_from_parameters(
output_format=output_format,
output_serializer = MetadatasetSerializerBase.get_serializer_from_parameters(
format_or_serializer=output_format,
output_path=output_path,
opt_prettify_size=opt_prettify_size,
opt_raw_path=opt_raw_path,
Expand Down
70 changes: 70 additions & 0 deletions dicogis/export/base_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
# ##################################

# Standard library
from __future__ import annotations

import logging
from pathlib import Path

# project
from dicogis import export
from dicogis.constants import OutputFormats
from dicogis.utils.texts import TextsManager

# ############################################################################
Expand Down Expand Up @@ -52,3 +56,69 @@ def pre_serializing(self, **kwargs):
def post_serializing(self, **kwargs):
"""Operations to run after serialization."""
pass

@classmethod
def get_serializer_from_parameters(
cls,
format_or_serializer: (
str
| OutputFormats.excel
| OutputFormats.json
| OutputFormats.udata
| MetadatasetSerializerBase
),
output_path: Path | None = None,
opt_raw_path: bool = False,
opt_prettify_size: bool = True,
localized_strings: dict | None = None,
) -> (
export.to_json.MetadatasetSerializerJson
| export.to_xlsx.MetadatasetSerializerXlsx
):
"""Initiate the adequat serializer depending on parameters.

Args:
format_or_serializer: output format or serializer
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:
serializer already initialized
"""
print("ho", type(format_or_serializer))
if isinstance(format_or_serializer, MetadatasetSerializerBase):
return format_or_serializer

if isinstance(format_or_serializer, str):
format_or_serializer = OutputFormats(format_or_serializer)

if format_or_serializer == OutputFormats.excel:
# creating the Excel workbook
output_serializer = export.to_xlsx.MetadatasetSerializerXlsx(
localized_strings=localized_strings,
opt_raw_path=opt_raw_path,
opt_size_prettify=opt_prettify_size,
output_path=output_path,
)
elif format_or_serializer == OutputFormats.json:
output_serializer = export.to_json.MetadatasetSerializerJson(
localized_strings=localized_strings,
opt_size_prettify=opt_prettify_size,
output_path=output_path,
)
elif format_or_serializer == OutputFormats.udata:
output_serializer = export.to_json.MetadatasetSerializerJson(
flavor="udata",
localized_strings=localized_strings,
opt_size_prettify=opt_prettify_size,
output_path=output_path,
)
else:
raise NotImplementedError(
f"Specified output format '{format_or_serializer}' is not available."
)

return output_serializer
44 changes: 18 additions & 26 deletions dicogis/georeaders/process_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
from os import path
from pathlib import Path
from tkinter import IntVar, StringVar
from typing import Callable, Optional, Union
from typing import Callable, Optional

# package
from dicogis.constants import OutputFormats
from dicogis.export.base_serializer import MetadatasetSerializerBase
from dicogis.export.to_json import MetadatasetSerializerJson
from dicogis.export.to_xlsx import MetadatasetSerializerXlsx
from dicogis.georeaders.read_dxf import ReadCadDxf
from dicogis.georeaders.read_raster import ReadRasters
from dicogis.georeaders.read_vector_flat_dataset import ReadVectorFlatDataset
Expand Down Expand Up @@ -77,7 +74,7 @@ class ProcessingFiles:

def __init__(
self,
format_or_serializer: OutputFormats | MetadatasetSerializerBase,
serializer: MetadatasetSerializerBase,
localized_strings: Optional[dict],
# input lists of files to process
li_cdao: Optional[Iterable],
Expand Down Expand Up @@ -115,8 +112,7 @@ def __init__(
# misc
opt_quick_fail: bool = False,
) -> None:
# -- STORE PARAMETERS AS ATTRIBUTES --
self.serializer = self.serializer_from_output_format(format_or_serializer)
self.serializer = serializer

# List of files
self.li_dxf = li_dxf
Expand Down Expand Up @@ -173,26 +169,8 @@ def __init__(
self.progress_counter = progress_counter
self.progress_callback_cmd = progress_callback_cmd

def serializer_from_output_format(
self, format_or_serializer: OutputFormats | MetadatasetSerializerBase
) -> Union[MetadatasetSerializerJson, MetadatasetSerializerXlsx]:
if isinstance(format_or_serializer, MetadatasetSerializerBase):
return format_or_serializer

if (
isinstance(format_or_serializer, OutputFormats)
and format_or_serializer.value == "excel"
):
return MetadatasetSerializerXlsx
elif (
isinstance(format_or_serializer, OutputFormats)
and format_or_serializer.value == "json"
):
return MetadatasetSerializerJson

def process_datasets_in_queue(self):
"""Process datasets in queue."""

for geofile in self.li_files_to_process:
if geofile.processed is True:
logger.warning(f"File has already been processed: {geofile.file_path}")
Expand Down Expand Up @@ -272,6 +250,15 @@ def export_metadataset(
dataset_to_process: DatasetToProcess,
metadataset_to_serialize: MetaDataset,
) -> tuple[DatasetToProcess, MetaDataset | None]:
"""Serialize metadataset in a generic way.

Args:
dataset_to_process: dataset to process used to store status
metadataset_to_serialize: metadataset to serialize

Returns:
dataset to process, metadataset or None if something went wrong
"""
if self.opt_quick_fail:
self.update_progress(
message_to_display="Exporting metadata of "
Expand Down Expand Up @@ -322,7 +309,7 @@ def add_files_to_process_queue(
dataset_format: _description_

Returns:
_description_
list of files to process
"""
out_list: list = []

Expand All @@ -343,6 +330,11 @@ def add_files_to_process_queue(
return out_list

def count_files_to_process(self) -> int:
"""Count number of files to process.

Returns:
total of files to process
"""
total_files: int = 0
if self.opt_analyze_shapefiles and len(self.li_shapefiles):
total_files += len(self.li_shapefiles)
Expand Down
Loading