Skip to content

Commit

Permalink
refacto(serializer): cahnge code strcuture to handle new serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed May 24, 2024
1 parent 20c256d commit 2d93ef3
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 134 deletions.
14 changes: 7 additions & 7 deletions dicogis/cli/cmd_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# project
from dicogis.__about__ import __package_name__, __title__, __version__
from dicogis.constants import SUPPORTED_FORMATS, AvailableLocales, OutputFormats
from dicogis.export.to_xlsx import MetadataToXlsx
from dicogis.export.to_xlsx import MetadatasetSerializerXlsx
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 Down Expand Up @@ -188,7 +188,7 @@ def inventory(
# output format
if output_format == "excel":
# creating the Excel workbook
xl_workbook = MetadataToXlsx(
xl_workbook = MetadatasetSerializerXlsx(
translated_texts=localized_strings,
opt_size_prettify=opt_prettify_size,
)
Expand Down Expand Up @@ -255,7 +255,7 @@ def inventory(

# instanciate geofiles processor
geofiles_processor = ProcessingFiles(
output_workbook=xl_workbook,
format_or_serializer=xl_workbook,
localized_strings=localized_strings,
# list by tabs
li_vectors=li_vectors,
Expand Down Expand Up @@ -293,13 +293,13 @@ def inventory(
total_files = geofiles_processor.count_files_to_process()
print(f"Start analyzing {total_files} files...")

geofiles_processor.process_files_in_queue()
geofiles_processor.process_datasets_in_queue()

# output file path
if output_path is None:
output_path = Path(f"DicoGIS_{input_folder.name}_{date.today()}.xlsx")

xl_workbook.tunning_worksheets()
xl_workbook.post_serializing()
saved = Utilities.safe_save(
output_object=xl_workbook,
dest_dir=f"{output_path.parent.resolve()}",
Expand All @@ -325,7 +325,7 @@ def inventory(
raise typer.Exit(1)

# configure output workbook
xl_workbook.set_worksheets(has_sgbd=True)
xl_workbook.pre_serializing(has_sgbd=True)

for pg_service in pg_services:

Expand Down Expand Up @@ -358,7 +358,7 @@ def inventory(
if output_path is None:
output_path = Path(f"DicoGIS_PostGIS_{date.today()}.xlsx")

xl_workbook.tunning_worksheets()
xl_workbook.post_serializing()
saved = Utilities.safe_save(
output_object=xl_workbook,
dest_dir=f"{output_path.parent.resolve()}",
Expand Down
1 change: 1 addition & 0 deletions dicogis/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class OutputFormats(str, ExtendedEnum):
"""Supported output formats."""

excel = "excel"
json = "json"


class FormatsVector(ExtendedEnum):
Expand Down
51 changes: 51 additions & 0 deletions dicogis/export/base_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! python3 # noqa: E265


# ##############################################################################
# ########## Libraries #############
# ##################################

# Standard library
import logging

# ############################################################################
# ######### Globals ############
# ##############################

logger = logging.getLogger(__name__)

# ##############################################################################
# ########## Classes ###############
# ##################################


class MetadatasetSerializerBase:
"""Base class for metadataset serializers."""

def __init__(self, translated_texts: dict, opt_size_prettify: bool = True) -> None:
"""Store metadata into JSON files."""
self.translated_texts = translated_texts

# options
self.opt_size_prettify = opt_size_prettify

def pre_serializing(
self,
has_vector: bool = False,
has_raster: bool = False,
has_filedb: bool = False,
has_mapdocs: bool = False,
has_cad: bool = False,
has_sgbd: bool = False,
):
"""Set workbook's sheets accordingly to metadata types.
Args:
has_vector (bool, optional): _description_. Defaults to False.
has_raster (bool, optional): _description_. Defaults to False.
has_filedb (bool, optional): _description_. Defaults to False.
has_mapdocs (bool, optional): _description_. Defaults to False.
has_cad (bool, optional): _description_. Defaults to False.
has_sgbd (bool, optional): _description_. Defaults to False.
"""
pass
44 changes: 44 additions & 0 deletions dicogis/export/to_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#! python3 # noqa: E265

"""JSON serializer."""

# ##############################################################################
# ########## Libraries #############
# ##################################


# Standard library
import logging
from dataclasses import asdict

# project
from dicogis.export.base_serializer import MetadatasetSerializerBase
from dicogis.models.metadataset import MetaDataset

# 3rd party library


# ##############################################################################
# ############ Globals ############
# #################################

# LOG
logger = logging.getLogger(__name__)


# ##############################################################################
# ########## Classes ###############
# ##################################


class MetadatasetSerializerJson(MetadatasetSerializerBase):
"""Export to JSON."""

def __init__(self, translated_texts: dict, opt_size_prettify: bool = True) -> None:
"""Store metadata into JSON files."""
super().__init__(
translated_texts=translated_texts, opt_size_prettify=opt_size_prettify
)

def serialize_metadaset(self, metadataset: MetaDataset) -> dict:
return asdict(metadataset)
62 changes: 34 additions & 28 deletions dicogis/export/to_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from openpyxl.worksheet.worksheet import Worksheet

# project
from dicogis.export.base_serializer import MetadatasetSerializerBase
from dicogis.models.metadataset import (
MetaDatabaseFlat,
MetaDatabaseTable,
Expand All @@ -40,7 +41,7 @@
# ##################################


class MetadataToXlsx(Workbook):
class MetadatasetSerializerXlsx(MetadatasetSerializerBase):
"""Export into a XLSX worksheet."""

li_cols_vector = [
Expand Down Expand Up @@ -145,34 +146,32 @@ class MetadataToXlsx(Workbook):
"gdal_err",
]

def __init__(self, translated_texts: dict, opt_size_prettify: bool = True):
def __init__(self, translated_texts: dict, opt_size_prettify: bool = True) -> None:
"""Store metadata into Excel worksheets.
Args:
texts (dict, optional): dictionary of translated texts. Defaults to {}.
opt_size_prettify (bool, optional): option to prettify size or not. Defaults to False.
"""
super().__init__(iso_dates=True)
self.translated_texts = translated_texts

# options
self.opt_size_prettify = opt_size_prettify

super().__init__(
translated_texts=translated_texts, opt_size_prettify=opt_size_prettify
)
self.workbook = Workbook(iso_dates=True)
# styles
s_date = NamedStyle(name="date")
s_date.number_format = "dd/mm/yyyy"
s_wrap = NamedStyle(name="wrap")
s_wrap.alignment = Alignment(wrap_text=True)
self.add_named_style(s_date)
self.add_named_style(s_wrap)
self.workbook.add_named_style(s_date)
self.workbook.add_named_style(s_wrap)

# deleting the default worksheet
ws = self.active
self.remove(worksheet=ws)
ws = self.workbook.active
self.workbook.remove(worksheet=ws)

# ------------ Setting workbook ---------------------

def set_worksheets(
def pre_serializing(
self,
has_vector: bool = False,
has_raster: bool = False,
Expand All @@ -181,7 +180,7 @@ def set_worksheets(
has_cad: bool = False,
has_sgbd: bool = False,
):
"""Set workbook's sheets accordingly to metadata types.
"""Prepare workbooks.
Args:
has_vector (bool, optional): _description_. Defaults to False.
Expand All @@ -195,9 +194,10 @@ def set_worksheets(
# SHEETS & HEADERS
if (
has_vector
and self.translated_texts.get("sheet_vectors") not in self.sheetnames
and self.translated_texts.get("sheet_vectors")
not in self.workbook.sheetnames
):
self.sheet_vector_files = self.create_sheet(
self.sheet_vector_files = self.workbook.create_sheet(
title=self.translated_texts.get("sheet_vectors")
)
# headers
Expand All @@ -210,9 +210,10 @@ def set_worksheets(

if (
has_raster
and self.translated_texts.get("sheet_rasters") not in self.sheetnames
and self.translated_texts.get("sheet_rasters")
not in self.workbook.sheetnames
):
self.sheet_raster_files = self.create_sheet(
self.sheet_raster_files = self.workbook.create_sheet(
title=self.translated_texts.get("sheet_rasters")
)
# headers
Expand All @@ -225,9 +226,10 @@ def set_worksheets(

if (
has_filedb
and self.translated_texts.get("sheet_filedb") not in self.sheetnames
and self.translated_texts.get("sheet_filedb")
not in self.workbook.sheetnames
):
self.sheet_flat_geodatabases = self.create_sheet(
self.sheet_flat_geodatabases = self.workbook.create_sheet(
title=self.translated_texts.get("sheet_filedb")
)
# headers
Expand All @@ -240,9 +242,10 @@ def set_worksheets(

if (
has_mapdocs
and self.translated_texts.get("sheet_maplans") not in self.sheetnames
and self.translated_texts.get("sheet_maplans")
not in self.workbook.sheetnames
):
self.sheet_map_workspaces = self.create_sheet(
self.sheet_map_workspaces = self.workbook.create_sheet(
title=self.translated_texts.get("sheet_maplans")
)
# headers
Expand All @@ -253,8 +256,11 @@ def set_worksheets(
# initialize line counter
self.row_index_map_worskpaces = 1

if has_cad and self.translated_texts.get("sheet_cdao") not in self.sheetnames:
self.sheet_cad_files = self.create_sheet(
if (
has_cad
and self.translated_texts.get("sheet_cdao") not in self.workbook.sheetnames
):
self.sheet_cad_files = self.workbook.create_sheet(
title=self.translated_texts.get("sheet_cdao")
)
# headers
Expand All @@ -265,18 +271,18 @@ def set_worksheets(
# initialize line counter
self.row_index_cad_files = 1

if has_sgbd and "PostGIS" not in self.sheetnames:
self.sheet_server_geodatabases = self.create_sheet(title="PostGIS")
if has_sgbd and "PostGIS" not in self.workbook.sheetnames:
self.sheet_server_geodatabases = self.workbook.create_sheet(title="PostGIS")
# headers
self.sheet_server_geodatabases.append(
[self.translated_texts.get(i) for i in self.li_cols_sgbd]
)
# initialize line counter
self.row_index_server_geodatabases = 1

def tunning_worksheets(self):
def post_serializing(self):
"""Clean up and tunning worksheet."""
for sheet in self.worksheets:
for sheet in self.workbook.worksheets:
# Freezing panes
c_freezed = sheet["B2"]
sheet.freeze_panes = c_freezed
Expand Down
Loading

0 comments on commit 2d93ef3

Please sign in to comment.