From a0c966f4e1927dcab633ec31d8b8b483bf2386b4 Mon Sep 17 00:00:00 2001 From: "Julien M." Date: Thu, 6 Jun 2024 12:46:30 +0200 Subject: [PATCH] refacto(serializer): move serializer from paremeters as class method of serializer meta class --- dicogis/cli/cmd_inventory.py | 66 +++------------------------ dicogis/export/base_serializer.py | 70 +++++++++++++++++++++++++++++ dicogis/georeaders/process_files.py | 44 ++++++++---------- 3 files changed, 94 insertions(+), 86 deletions(-) diff --git a/dicogis/cli/cmd_inventory.py b/dicogis/cli/cmd_inventory.py index 65eb678b..a8b3ad26 100644 --- a/dicogis/cli/cmd_inventory.py +++ b/dicogis/cli/cmd_inventory.py @@ -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 @@ -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", @@ -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, @@ -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, @@ -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, diff --git a/dicogis/export/base_serializer.py b/dicogis/export/base_serializer.py index 81d2700a..241b2fb2 100644 --- a/dicogis/export/base_serializer.py +++ b/dicogis/export/base_serializer.py @@ -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 # ############################################################################ @@ -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 diff --git a/dicogis/georeaders/process_files.py b/dicogis/georeaders/process_files.py index ff7b289a..4dcd50e1 100644 --- a/dicogis/georeaders/process_files.py +++ b/dicogis/georeaders/process_files.py @@ -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 @@ -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], @@ -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 @@ -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}") @@ -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 " @@ -322,7 +309,7 @@ def add_files_to_process_queue( dataset_format: _description_ Returns: - _description_ + list of files to process """ out_list: list = [] @@ -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)