Skip to content

Commit

Permalink
Enable dynamic templates from repo
Browse files Browse the repository at this point in the history
  • Loading branch information
tanertopal committed Apr 6, 2024
1 parent 1336aa9 commit 9a63ba4
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/py/flwr/cli/new/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
"""Flower command line interface `new` command."""

import os
import urllib.request
from enum import Enum
from string import Template
from typing import Dict, Optional
from typing import Dict, Optional, cast

import typer
from typing_extensions import Annotated
Expand All @@ -37,16 +38,19 @@ class TemplateNotFound(Exception):
"""Raised when template does not exist."""


def load_template(name: str) -> str:
"""Load template from template directory and return as text."""
tpl_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "templates"))
tpl_file_path = os.path.join(tpl_dir, name)
TEMPLATES_BASE_URL = (
"https://raw.githubusercontent.com/adap/flower/main/src/py/flwr/cli/new/templates"
)

if not os.path.isfile(tpl_file_path):
raise TemplateNotFound(f"Template '{name}' not found")

with open(tpl_file_path, encoding="utf-8") as tpl_file:
return tpl_file.read()
def load_template(name: str) -> str:
"""Load template from template directory and return as text."""
try:
with urllib.request.urlopen(f"{TEMPLATES_BASE_URL}/{name}") as res:
content = res.read().decode("utf-8")
return cast(str, content)
except Exception as ex:
raise TemplateNotFound(f"Template '{name}' not found") from ex


def render_template(template: str, data: Dict[str, str]) -> str:
Expand Down

0 comments on commit 9a63ba4

Please sign in to comment.