Skip to content

Commit 49f331a

Browse files
committed
Deprecation of old logs/warnings
All logs and warnings are now handled by the logger
1 parent d807a92 commit 49f331a

3 files changed

Lines changed: 23 additions & 66 deletions

File tree

backend/config.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
import time
1919
from datetime import datetime, timedelta
2020

21-
import click
2221
import shapely.wkt
2322

23+
from backend.logging import Loggable
24+
2425
from .bounding_polygons import get_county_polygon
2526
from .connectors.nmbgmr.source import (
2627
NMBGMRSiteSource,
@@ -91,7 +92,7 @@ def get_source(source):
9192
return None
9293

9394

94-
class Config(object):
95+
class Config(Loggable):
9596
site_limit: int = 0
9697
dry: bool = False
9798

@@ -135,10 +136,10 @@ class Config(object):
135136
use_csv: bool = True
136137
use_geojson: bool = False
137138

138-
logs: list = []
139-
warnings: list = []
140-
141139
def __init__(self, model=None, payload=None):
140+
# need to initialize logger
141+
super().__init__()
142+
142143
self.bbox = {}
143144
if model:
144145
if model.wkt:
@@ -316,32 +317,22 @@ def now_ms(self, days=0):
316317
# return current time in milliseconds
317318
return int((datetime.now() - td).timestamp() * 1000)
318319

319-
def report(self, log_report: bool = False):
320+
def report(self):
320321
def _report_attributes(title, attrs):
321322
s = f"---- {title} --------------------------------------------------"
322-
click.secho(s, fg="yellow")
323-
if log_report:
324-
self.logs.append(s)
323+
self.log(s)
325324

326325
for k in attrs:
327326
v = getattr(self, k)
328327
s = f"{k}: {v}"
329-
click.secho(s, fg="yellow")
330-
331-
if log_report:
332-
self.logs.append(s)
328+
self.log(s)
333329

334330
s = ""
335-
click.secho(s, fg="yellow")
331+
self.log(s)
336332

337-
if log_report:
338-
self.logs.append(s)
339333

340334
s = "---- Begin configuration -------------------------------------\n"
341-
click.secho(s, fg="yellow")
342-
343-
if log_report:
344-
self.logs.append(s)
335+
self.log(s)
345336

346337
sources = [f"use_source_{s}" for s in SOURCE_KEYS]
347338
attrs = [
@@ -373,26 +364,23 @@ def _report_attributes(title, attrs):
373364
)
374365

375366
s = "---- End configuration -------------------------------------\n"
376-
click.secho(s, fg="yellow")
377-
378-
if log_report:
379-
self.logs.append(s)
367+
self.log(s)
380368

381369
def validate(self):
382370
if not self._validate_bbox():
383-
click.secho("Invalid bounding box", fg="red")
371+
self.warn("Invalid bounding box")
384372
sys.exit(2)
385373

386374
if not self._validate_county():
387-
click.secho("Invalid county", fg="red")
375+
self.warn("Invalid county")
388376
sys.exit(2)
389377

390378
if not self._validate_date(self.start_date):
391-
click.secho(f"Invalid start date {self.start_date}", fg="red")
379+
self.warn(f"Invalid start date {self.start_date}")
392380
sys.exit(2)
393381

394382
if not self._validate_date(self.end_date):
395-
click.secho("Invalid end date", fg="red")
383+
self.warn(f"Invalid end date {self.end_date}")
396384
sys.exit(2)
397385

398386
def _extract_date(self, d):

backend/persister.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
import os
1919
import shutil
2020

21-
import click
2221
import pandas as pd
2322
import geopandas as gpd
2423

2524
from backend.logging import Loggable
26-
from backend.record import SiteRecord
2725

2826
try:
2927
from google.cloud import storage
@@ -40,8 +38,6 @@ def __init__(self):
4038
self.combined = []
4139
self.timeseries = []
4240
self.sites = []
43-
self.logs = []
44-
self.warnings = []
4541

4642
super().__init__()
4743
# self.keys = record_klass.keys
@@ -98,24 +94,6 @@ def dump_sites(self, path: str):
9894
else:
9995
self.log("no sites to dump", fg="red")
10096

101-
def dump_logs(self, path: str):
102-
if self.logs:
103-
self.log(f"dumping logs to {os.path.abspath(path)}")
104-
with open(path, "w") as f:
105-
for l in self.logs:
106-
f.write(f"{l}\n")
107-
else:
108-
self.log("no logs to dump")
109-
110-
def dump_warnings(self, path: str):
111-
if self.warnings:
112-
self.log(f"dumping warnings to {os.path.abspath(path)}")
113-
with open(path, "w") as f:
114-
for w in self.warnings:
115-
f.write(f"{w}\n")
116-
else:
117-
self.log("no warnings to dump")
118-
11997
def save(self, path: str):
12098
if self.records:
12199
path = self.add_extension(path)

backend/unifier.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def health_check(source: BaseSiteSource) -> bool:
4141

4242

4343
def unify_sites(config):
44-
print("Unifying sites")
44+
print("Unifying sites\n")
4545

4646
# def func(config, persister):
4747
# for source in config.site_sources():
@@ -52,8 +52,8 @@ def unify_sites(config):
5252

5353

5454
def unify_analytes(config):
55-
print("Unifying analytes")
56-
config.report(log_report=True)
55+
print("Unifying analytes\n")
56+
# config.report() -- report is done in cli.py, no need to do it twice
5757
config.validate()
5858

5959
if not config.dry:
@@ -63,9 +63,9 @@ def unify_analytes(config):
6363

6464

6565
def unify_waterlevels(config):
66-
print("Unifying waterlevels")
66+
print("Unifying waterlevels\n")
6767

68-
config.report(log_report=True)
68+
# config.report() -- report is done in cli.py, no need to do it twice
6969
config.validate()
7070

7171
if not config.dry:
@@ -182,14 +182,9 @@ def _site_wrapper(site_source, parameter_source, persister, config):
182182
import traceback
183183

184184
exc = traceback.format_exc()
185-
print(exc)
186-
print(f"Failed to unify {site_source}")
185+
config.warn(exc)
186+
config.warn(f"Failed to unify {site_source}")
187187

188-
config.logs.append(exc)
189-
config.logs.append(f"Failed to unify {site_source}")
190-
191-
config.warnings.append(exc)
192-
config.warnings.append(f"Failed to unify {site_source}")
193188

194189

195190
def _unify_parameter(
@@ -209,10 +204,6 @@ def _unify_parameter(
209204
persister.dump_combined(f"{config.output_path}.combined")
210205
persister.dump_timeseries(f"{config.output_path}_timeseries")
211206

212-
persister.logs.extend(config.logs)
213-
persister.warnings.extend(config.warnings)
214-
persister.dump_logs(f"{config.output_path}.logs.txt")
215-
persister.dump_warnings(f"{config.output_path}.warnings.txt")
216207
persister.finalize(config.output_name)
217208

218209

0 commit comments

Comments
 (0)