Skip to content

Commit

Permalink
Merge branch 'main' into fr/release-work
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioRosado authored Mar 4, 2024
2 parents 6f959e6 + 5031c25 commit a841e5b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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",
Expand Down
36 changes: 28 additions & 8 deletions src/pyscript/_generator.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/pyscript/plugins/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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",
),
Expand Down
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit a841e5b

Please sign in to comment.