Skip to content

Commit

Permalink
add cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
sliu008 committed Dec 3, 2024
1 parent 8c6c6ed commit 4a7cb85
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
23 changes: 14 additions & 9 deletions podaac/podaac_forge_tig_config_generator/generate_config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pandas as pd
import click
import ast
import json
import os
import numpy as np
from jsonschema import validate, Draft7Validator
from typing import Dict, Any, Optional
import ast
import click
import numpy as np
import pandas as pd
from jsonschema import validate

class HiTideConfigGenerator:
REQUIRED_SETTINGS_SHEET = "required-settings"
Expand Down Expand Up @@ -42,7 +42,7 @@ def recursive_filter(current_config, current_schema):
filtered_obj[key] = filtered_value
elif prop_schema.get('type') == 'array' and isinstance(value, list):
filtered_obj[key] = [
recursive_filter(item, prop_schema.get('items', {}))
recursive_filter(item, prop_schema.get('items', {}))
for item in value if item
]
else:
Expand Down Expand Up @@ -98,7 +98,7 @@ def read_sheet_as_dict(cls, file_path: str, sheet_name: str) -> Optional[Dict[st
df = pd.read_excel(file_path, sheet_name=sheet_name, engine='openpyxl')
if df.empty:
raise ValueError(f"Sheet '{sheet_name}' is empty")

first_row = df.iloc[0]
return {k: cls._convert_value(v) for k, v in first_row.items() if pd.notna(v)}
except Exception as e:
Expand Down Expand Up @@ -132,7 +132,7 @@ def generate_hitide_config(file: str):
"""Generate and validate HiTide configuration."""
generator = HiTideConfigGenerator()
config = generator.generate_configuration(file)

if not config:
click.echo("Configuration generation failed.", err=True)
return
Expand All @@ -141,8 +141,13 @@ def generate_hitide_config(file: str):
schema = generator.load_schema()
validate(instance=config, schema=schema)
click.echo(json.dumps(config, indent=2))
short_name = config.get('shortName')
file = f"{short_name}.cfg"
with open(file, 'w') as file:
json.dump(config, file, indent=4)

except Exception as e:
click.echo(f"Validation error: {e}", err=True)

if __name__ == '__main__':
generate_hitide_config()
generate_hitide_config()
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ click = "^8.1"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
generate_config = 'podaac.podaac_forge_tig_config_generator.generate_config:generate_hitide_config'

0 comments on commit 4a7cb85

Please sign in to comment.