add csv exporter command
This commit is contained in:
parent
ca5619976f
commit
96562c0895
3 changed files with 100 additions and 2 deletions
|
|
@ -3,6 +3,8 @@ import click
|
|||
import importlib
|
||||
|
||||
from rec.districts import get_districts
|
||||
from data_access import Listing
|
||||
import csv_exporter
|
||||
|
||||
dump_listings_module = importlib.import_module('1_dump_listings')
|
||||
dump_detail_module = importlib.import_module('2_dump_detail')
|
||||
|
|
@ -92,5 +94,39 @@ def routing(ctx: click.core.Context):
|
|||
routing_module.calculate_route(listing_paths)
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.option(
|
||||
'--columns',
|
||||
'-C',
|
||||
help='Columns to include in the CSV file',
|
||||
type=click.Choice(
|
||||
Listing.ALL_COLUMNS,
|
||||
case_sensitive=False,
|
||||
),
|
||||
multiple=True,
|
||||
default=Listing.ALL_COLUMNS,
|
||||
)
|
||||
@click.option(
|
||||
'--output-file',
|
||||
'-O',
|
||||
help='Path to the output CSV file',
|
||||
required=True,
|
||||
type=click.Path(
|
||||
writable=True,
|
||||
file_okay=True,
|
||||
dir_okay=False,
|
||||
resolve_path=True,
|
||||
),
|
||||
)
|
||||
@click.pass_context
|
||||
def export_csv(ctx: click.core.Context, output_file: str, columns: tuple[str]):
|
||||
data_dir = ctx.obj['data_dir']
|
||||
click.echo(f'Exporting data to {output_file} using {data_dir=}')
|
||||
output_file_path = pathlib.Path(output_file)
|
||||
listing_paths = sorted(list(pathlib.Path(data_dir).glob("*/listing.json")))
|
||||
listings = Listing.get_all_listings(listing_paths)
|
||||
csv_exporter.export_to_csv(listings, output_file_path, list(columns))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue