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

Commit

Permalink
fix: multiple bugs in manual scripts (#11)
Browse files Browse the repository at this point in the history
* chore: add index to load-docs

* fix: multiple bugs in manage scripts

* chore: bumping version from 1.1.0 to 1.1.1
  • Loading branch information
sralloza authored Oct 14, 2021
1 parent 1335114 commit 37d5360
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "meal-planner"
version = "1.1.0"
version = "1.1.1"
description = ""
authors = ["Diego Alloza González <[email protected]>"]
packages = [
Expand Down
5 changes: 3 additions & 2 deletions scripts/load-docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ def create_md(week: int, weekly_meals: List[Meal]):
def rebuild_mkdocs_yml(weeks: List[int]):
with MKDOCS_YML_PATH.open("rt", encoding="utf8") as fh:
yml_content = yml.load(fh)
yml_content["nav"] = [{f"Semana {x}": f"{x}.md"} for x in weeks]
yml_content["nav"] = [{"Índice": "index.md"}]
yml_content["nav"] += [{f"Semana {x}": f"{x}.md"} for x in weeks]

with MKDOCS_YML_PATH.open("wt", encoding="utf8") as fh:
yml.dump(yml_content, fh,)
yml.dump(yml_content, fh)


if __name__ == "__main__":
Expand Down
37 changes: 23 additions & 14 deletions scripts/manage.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import datetime
import json
import locale
import re
from datetime import datetime
from pathlib import Path
from typing import List
from urllib.parse import urljoin

import click
import requests
from dateutil.parser import parse
from fastapi.encoders import jsonable_encoder
from pydantic import parse_file_as, parse_obj_as
from ruamel.yaml import YAML

from app.core.config import settings
from app.schemas.meal import Meal

PATTERN = r"## (?P<weekday>[\wáéó]+)\n(?P<lunch>[-\wáéó\n ]+)\n(?P<dinner>[-\wáéó ]+)\n"
PATTERN = r"## (?P<weekday>[\wáéó]+)\n(?P<lunch>[\[\]\-\/()\wáéó\n ]+)\n(?P<dinner>[\[\]\-\/()\wáéó\n ]+)\n"
MD_DIR = Path(__file__).parent.parent / "docs"
MD_FILES = [x.stem for x in MD_DIR.iterdir() if x.suffix == ".md"]
YML_FILES = [x.stem for x in MD_DIR.iterdir() if x.suffix == ".yml"]
Expand All @@ -22,6 +26,10 @@
yaml = YAML()


class ModifiedMeal(Meal):
date: datetime.date


@click.group()
def cli():
pass
Expand All @@ -36,7 +44,7 @@ def convert_md_to_yml(date: str):

matches = re.finditer(PATTERN, joined)

current_year = datetime.now().year
current_year = datetime.datetime.now().year
try:
first_date = parse(date).date()
week_number = first_date.isocalendar()[1]
Expand All @@ -47,7 +55,7 @@ def convert_md_to_yml(date: str):
for match in matches:
parsed = match.groupdict()
weekday = parsed["weekday"]
meal_date = datetime.strptime(
meal_date = datetime.datetime.strptime(
f"{weekday} {week_number} {current_year}", "%A %W %Y"
).date()
lunch = [x.strip("- ") for x in parsed["lunch"].splitlines()]
Expand All @@ -68,7 +76,9 @@ def convert_md_to_yml(date: str):
click.secho("Not modified", fg="bright_yellow")
return

click.confirm("YML file exists and will be modified. Continue?", abort=True)
click.confirm(
"YML file exists and will be modified. Continue?", abort=True, default=True
)

with yaml_path.open("wt", encoding="utf8") as fh:
current_data = yaml.dump(objs, fh)
Expand All @@ -83,27 +93,26 @@ def convert_yml_json(date: str):
with filepath.open("rt", encoding="utf8") as fh:
objs = yaml.load(fh)

for obj in objs:
obj["id"] = str(obj["date"])
del obj["id"]
parsed_data = parse_obj_as(List[ModifiedMeal], objs)

json_path = MD_DIR / f"{date}.json"
if json_path.is_file():
with json_path.open("rt", encoding="utf8") as fh:
current_data = json.load(fh)
current_data = parse_file_as(List[ModifiedMeal], json_path, encoding="utf8")

if current_data == objs:
if current_data == parsed_data:
click.secho("Not modified", fg="bright_yellow")
return

click.confirm("YML file exists and will be modified. Continue?", abort=True)
click.confirm(
"YML file exists and will be modified. Continue?", abort=True, default=True
)

with json_path.open("wt", encoding="utf8") as fh:
current_data = json.dump(objs, fh, indent=2, ensure_ascii=False)
json.dump(jsonable_encoder(parsed_data), fh, indent=2, ensure_ascii=False)


@cli.command("upload-json")
@click.argument("date", metavar="DATE", type=click.Choice(MD_FILES))
@click.argument("date", metavar="DATE", type=click.Choice(JSON_FILES))
@click.argument("API_URL", envvar="MEAL_PLANNER_API_URL")
def upload_json(date: str, api_url: str):
filepath = MD_DIR / f"{date}.json"
Expand Down

0 comments on commit 37d5360

Please sign in to comment.