Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Add Dashboard sheet #15

Merged
merged 10 commits into from
Sep 4, 2019
1 change: 1 addition & 0 deletions isogeotoxlsx/i18n/english.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
I18N_EN = {
# worksheets
"attributes": "Feature Attributes",
"dashboard": "Dashboard",
"raster": "Rasters",
"resource": "Ressources",
"service": "Services",
Expand Down
1 change: 1 addition & 0 deletions isogeotoxlsx/i18n/french.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
I18N_FR = {
# worksheets
"attributes": "Attributs",
"dashboard": "Tableau de bord",
"raster": "Rasters",
"resource": "Ressources",
"service": "Services",
Expand Down
97 changes: 45 additions & 52 deletions isogeotoxlsx/isogeo2xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

# Standard library
import logging
from collections import Counter
from collections.abc import KeysView
from pathlib import Path
from urllib.parse import urlparse
Expand Down Expand Up @@ -168,13 +167,7 @@ def set_worksheets(

# SHEETS & HEADERS
if dashboard:
self.ws_d = self.create_sheet(title="Tableau de bord")
# headers
# self.ws_f.append([i for i in self.cols_v])
# styling
# for i in self.cols_v:
# self.ws_v.cell(row=1,
# column=self.cols_v.index(i) + 1).style = "Headline 2"
self.ws_d = self.create_sheet(title=self.tr.get("dashboard"))
# initialize line counter
self.idx_d = 1
# log
Expand Down Expand Up @@ -319,14 +312,6 @@ def store_metadatas(self, metadata: Metadata, share: Share = None):
"Type of metadata is not recognized/handled: {}".format(metadata.type)
)

# special analisis
if hasattr(self, "ws_fa"):
self.analisis_attributes()
# style it
self.column_width(self.ws_fa, self.columns_attributes)
self.row_height(self.ws_fa)
self.styling_cells(self.ws_fa, self.columns_attributes)

def store_md_generic(self, md: Metadata, ws: Worksheet, idx: int):
"""Exports generic metadata attributes into Excel worksheet with some dynamic
adaptations based on metadata type.
Expand Down Expand Up @@ -478,10 +463,12 @@ def store_md_generic(self, md: Metadata, ws: Worksheet, idx: int):
ws["{}{}".format(col.get("format").letter, idx)] = "{0} ({1} - {2})".format(
format_lbl, md.formatVersion, md.encoding
)
self.stats.li_data_formats.append(format_lbl)
elif md.format:
ws["{}{}".format(col.get("format").letter, idx)] = "{0} {1}".format(
md.format, md.formatVersion
)
self.stats.li_data_formats.append(md.format)
else:
pass

Expand Down Expand Up @@ -605,12 +592,21 @@ def store_md_generic(self, md: Metadata, ws: Worksheet, idx: int):
ws["{}{}".format(col.get("_created").letter, idx)] = utils.hlpr_datetimes(
md._created
)
# add creation date (not datetime) for later stats
self.stats.li_dates_md_created.append(
utils.hlpr_datetimes(md._created).date()
)

# last update
if md._modified:
ws["{}{}".format(col.get("_modified").letter, idx)] = utils.hlpr_datetimes(
md._modified
)
# add modification date (not datetime) for later stats, only if different from the creation date
if md._modified != md._created:
self.stats.li_dates_md_modified.append(
utils.hlpr_datetimes(md._modified).date()
)

# edit
ws["{}{}".format(col.get("linkEdit").letter, idx)] = utils.get_edit_url(md)
Expand All @@ -629,36 +625,22 @@ def store_md_generic(self, md: Metadata, ws: Worksheet, idx: int):
)

# ------------ Analisis --------------------------------------------------
def analisis_attributes(self):
"""Perform feature attributes analisis and write results into the
dedicatedWworksheet."""
# local arrays
fa_names = []
fa_types = []
fa_alias = []
fa_descr = []

# parsing
for dico_fa in self.fa_all:
for fa in dico_fa:
fa_names.append(fa.get("name"))
# fa_alias.append(fa.get("alias", "NR"))
# fa_types.append(fa.get("dataType"))
# fa_descr.append(fa.get("description", "NR"))
del fa

# stats
frq_names = Counter(fa_names)
frq_alias = Counter(fa_alias)
frq_types = Counter(fa_types)
frq_descr = Counter(fa_descr)

# write
ws = self.ws_fa
for fa in frq_names:
self.idx_fa += 1
ws["A{}".format(self.idx_fa)] = fa
ws["B{}".format(self.idx_fa)] = frq_names.get(fa)
def launch_analisis(self):
"""Launches special analisis, using the stats submodule."""
# special analisis
if hasattr(self, "ws_fa"):
self.stats.attributes(all_attributes=self.fa_all, ws_attributes=self.ws_fa)
# style it
self.column_width(self.ws_fa, self.columns_attributes)
self.row_height(self.ws_fa)
self.styling_cells(self.ws_fa, self.columns_attributes)
logger.info("Analisis of feature attributes sheet has been added.")

if hasattr(self, "ws_d"):
self.stats.pie_types(self.ws_d)
self.stats.pie_formats(self.ws_d)
self.stats.line_dates(self.ws_d)
logger.info("Dashboard sheet has been added.")

# ------------ Customize worksheet ----------------------------------------
def headers_writer(self, ws: Worksheet, columns: ColumnPattern):
Expand Down Expand Up @@ -720,9 +702,19 @@ def styling_cells(self, ws: Worksheet, columns: ColumnPattern):
# print(cell.style)
cell.style = v.style

def tunning_worksheets(self):
"""Automate"""
def tunning_worksheets(self, excluded_sheets: tuple = ("dashboard",)):
"""Applies some adjustments to the sheets of the workbook: filters, frozen panels,
print settings, etc.

:param tuple excluded_sheets: sheets name to be excluded from the tunning.
"""
for sheet in self.worksheets:
# exclude specified sheets
if sheet.title in [self.tr.get(i, "") for i in excluded_sheets]:
logger.debug(
"'{}' sheet has been exclude from the tunning.".format(sheet.title)
)
continue
# Freezing panes
c_freezed = sheet["B2"]
sheet.freeze_panes = c_freezed
Expand Down Expand Up @@ -813,21 +805,22 @@ def tunning_worksheets(self):

isogeo.close() # close session

print(
"{}/{} metadata ready to be exported.".format(len(search.results), search.total)
)
print("{}/{} metadata to be exported.".format(len(search.results), search.total))

# instanciate th final workbook
out_workbook = Isogeo2xlsx(
lang=isogeo.lang, url_base_edit=isogeo.app_url, url_base_view=isogeo.oc_url
)
# add needed worksheets
out_workbook.set_worksheets(auto=search.tags.keys(), attributes=1)
out_workbook.set_worksheets(auto=search.tags.keys(), attributes=1, dashboard=1)

# parse search results
for md in map(Metadata.clean_attributes, search.results):
out_workbook.store_metadatas(md)

# launch analisis
out_workbook.launch_analisis()

# apply filters
out_workbook.tunning_worksheets()

Expand Down
Loading