Add nasty formatting hacks for table displays to look nice
This commit is contained in:
parent
60a23b6c81
commit
fd4652ded6
|
@ -1,20 +1,21 @@
|
|||
import csv
|
||||
import functools
|
||||
import itertools
|
||||
import time
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from io import StringIO
|
||||
from typing import List, Tuple
|
||||
from typing import Tuple
|
||||
|
||||
import cachetools.func
|
||||
import httpx
|
||||
import tabulate
|
||||
import typer
|
||||
from pydantic import BaseModel, BaseSettings, Field
|
||||
from tabulate import tabulate as tab
|
||||
from tabulate import tabulate_formats
|
||||
|
||||
app = typer.Typer()
|
||||
tabulate_formats = set(tabulate.tabulate_formats)
|
||||
tabulate_formats = set(tabulate_formats)
|
||||
|
||||
AllowedFormat = Enum(
|
||||
"AllowedFormat", {"json": "json", "csv": "csv", **{v: v for v in tabulate_formats}}
|
||||
|
@ -61,7 +62,7 @@ def parse_data(data: Tuple[str]) -> Tuple[FedSite]:
|
|||
|
||||
|
||||
@functools.lru_cache()
|
||||
def analyze_data(data: Tuple[FedSite]) -> Tuple[FedSite]:
|
||||
def analyze_data(data: Tuple[FedSite]) -> Summary:
|
||||
usps = {x for x in data if "tools.usps.com" in x.page}
|
||||
usps_visitors = max(x.active_visitors for x in usps)
|
||||
hot = tuple(
|
||||
|
@ -73,7 +74,7 @@ def analyze_data(data: Tuple[FedSite]) -> Tuple[FedSite]:
|
|||
return Summary(sites=hot)
|
||||
|
||||
|
||||
def format_response(summary: Summary, format: AllowedFormat) -> str:
|
||||
def format_response(summary: Summary, format: AllowedFormat, count: int) -> str:
|
||||
if format == AllowedFormat.json:
|
||||
return summary.json(indent=4)
|
||||
elif format == AllowedFormat.csv:
|
||||
|
@ -81,22 +82,32 @@ def format_response(summary: Summary, format: AllowedFormat) -> str:
|
|||
to_write = summary.dict()["sites"]
|
||||
headers = FedSite.schema()["properties"].keys()
|
||||
writer = csv.DictWriter(data, fieldnames=headers)
|
||||
writer.writeheader()
|
||||
writer.writerows(summary.dict()["sites"])
|
||||
if count == 0:
|
||||
writer.writeheader()
|
||||
writer.writerows(to_write)
|
||||
return data.getvalue()
|
||||
else:
|
||||
return tab(summary.dict()["sites"], tablefmt=format.value, headers="keys")
|
||||
data = summary.dict()["sites"]
|
||||
for site in data:
|
||||
site["active_visitors"] = site["active_visitors"].rjust(19)
|
||||
site["page_title"] = site["page_title"].rjust(25)
|
||||
site["page"] = site["page"].rjust(26)
|
||||
if count == 0:
|
||||
return tab(data, tablefmt=format.value, headers="keys")
|
||||
else:
|
||||
return tab(data, tablefmt=format.value)
|
||||
|
||||
|
||||
@app.command()
|
||||
def main(format: AllowedFormat = typer.Option(default="json", show_choices=True)):
|
||||
prev = None
|
||||
counter = itertools.count()
|
||||
while True:
|
||||
data = get_data(config.base_url)
|
||||
data = parse_data(data)
|
||||
summary = analyze_data(data)
|
||||
if summary != prev:
|
||||
if summary:
|
||||
typer.echo(format_response(summary, format))
|
||||
typer.echo(format_response(summary, format, next(counter)))
|
||||
prev = summary
|
||||
time.sleep(config.poll)
|
||||
|
|
Loading…
Reference in New Issue