diff --git a/pyproject.toml b/pyproject.toml index bdd10ea..0fdf124 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,8 @@ dependencies = [ "rich==13.4.2", "toml<0.11", "typer==0.9.0", - "platformdirs<2.7" + "platformdirs<2.7", + "requests<=2.31.0" ] description = "Command Line Interface for PyScript" keywords = ["pyscript", "cli", "pyodide", "micropython", "pyscript-cli"] @@ -43,7 +44,8 @@ dev = [ "coverage<7.3", "mypy<=1.4.1", "pytest<7.5", - "types-toml<0.11" + "types-toml<0.11", + "types-requests" ] docs = [ "Sphinx<5.2", diff --git a/src/pyscript/_generator.py b/src/pyscript/_generator.py index 41b415c..dd8aef9 100644 --- a/src/pyscript/_generator.py +++ b/src/pyscript/_generator.py @@ -1,9 +1,9 @@ -import datetime import json from pathlib import Path from typing import Optional import jinja2 +import requests import toml from pyscript import LATEST_PYSCRIPT_VERSION, config @@ -19,7 +19,7 @@ def create_project_html( python_file_path: str, config_file_path: str, output_file_path: Path, - pyscript_version: str = LATEST_PYSCRIPT_VERSION, + pyscript_version: str, template: str = "basic.html", ) -> None: """Write a Python script string to an HTML file template. @@ -54,9 +54,9 @@ def save_config_file(config_file: Path, configuration: dict): Params: - - config_file(Path): path configuration file. (i.e.: "pyscript.toml"). Supported - formats: `toml` and `json`. - - configuration(dict): app configuration to be saved + - config_file(Path): path configuration file. (i.e.: "pyscript.toml"). Supported + formats: `toml` and `json`. + - configuration(dict): app configuration to be saved Return: (None) @@ -73,7 +73,7 @@ def create_project( app_description: str, author_name: str, author_email: str, - pyscript_version: str = LATEST_PYSCRIPT_VERSION, + pyscript_version: Optional[str] = None, project_type: str = "app", wrap: bool = False, command: Optional[str] = None, @@ -86,7 +86,6 @@ def create_project( main.py - a "Hello world" python starter module index.html - start page for the project """ - date_stamp = datetime.date.today() if wrap: if command: @@ -107,13 +106,16 @@ def create_project( # was complaining so let's add a default app_name = app_or_file_name or "my-pyscript-app" + if not pyscript_version: + pyscript_version = _get_latest_pyscript_version() + context = { "name": app_name, "description": app_description, "type": "app", "author_name": author_name, "author_email": author_email, - "version": f"{date_stamp.year}.{'{:02d}'.format(date_stamp.month)}.1", + "version": "latest", } app_dir = Path(".") / app_name @@ -155,3 +157,21 @@ def create_project( pyscript_version=pyscript_version, template=template, ) + + +def _get_latest_pyscript_version() -> str: + """Get the latest version of PyScript from GitHub.""" + url = "https://api.github.com/repos/pyscript/pyscript/releases/latest" + try: + response = requests.get(url) + + if not response.ok: + pyscript_version = LATEST_PYSCRIPT_VERSION + else: + + data = response.json() + pyscript_version = data["tag_name"] + except Exception: + pyscript_version = LATEST_PYSCRIPT_VERSION + + return pyscript_version diff --git a/src/pyscript/plugins/create.py b/src/pyscript/plugins/create.py index ffe62a0..cbc896a 100644 --- a/src/pyscript/plugins/create.py +++ b/src/pyscript/plugins/create.py @@ -2,7 +2,7 @@ import typer -from pyscript import LATEST_PYSCRIPT_VERSION, app, cli, plugins +from pyscript import app, cli, plugins from pyscript._generator import create_project @@ -15,7 +15,7 @@ def create( author_name: str = typer.Option(None, help="Name of the author"), author_email: str = typer.Option(None, help="Email of the author"), pyscript_version: str = typer.Option( - LATEST_PYSCRIPT_VERSION, + None, "--pyscript-version", help="If provided, defines what version of pyscript will be used to create the app", ), diff --git a/tests/conftest.py b/tests/conftest.py index 12d22d4..a18302f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,24 @@ from pathlib import Path from typing import Any +from unittest.mock import MagicMock, patch import pytest from _pytest.monkeypatch import MonkeyPatch +from pyscript import LATEST_PYSCRIPT_VERSION + + +@pytest.fixture(scope="session", autouse=True) +def requests(): + mocked_result = {"tag_name": LATEST_PYSCRIPT_VERSION} + + with patch("pyscript._generator.requests") as mocked_requests: + mocked_get = MagicMock() + mocked_get.ok = True + mocked_get.json = MagicMock(return_value=mocked_result) + mocked_requests.get.return_value = mocked_get + yield mocked_requests + @pytest.fixture def auto_enter(monkeypatch):