Skip to content

Commit

Permalink
Version card (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfguerrerom authored Nov 24, 2023
2 parents 6b1c031 + f25905f commit 9fbf7b0
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 17 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ test = [
"Flake8-pyproject",
"nbmake",
"pytest-regressions",
"tomli",
]
doc = [
"sphinx<7",
Expand All @@ -95,6 +96,7 @@ doc = [
"m2r2>=0.3.3",
"sphinxcontrib-autoprogram",
"sphinx-favicon>=1.0.1",
"tomli",
]

[project.scripts]
Expand Down
7 changes: 6 additions & 1 deletion sepal_ui/message/en/locale.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
"navdrawer": {
"code": "Source code",
"wiki": "Wiki",
"bug": "Bug report"
"bug": "Bug report",
"changelog": {
"version": "Version: {}",
"title": "Changelog",
"close_btn": "Close"
}
},
"asset_select": {
"types": {
Expand Down
102 changes: 95 additions & 7 deletions sepal_ui/scripts/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"""All the helper function of sepal-ui."""

import configparser
import math
import os
import random
import re
import string
import warnings
from pathlib import Path
from typing import Any, Sequence, Union
from typing import Any, Sequence, Tuple, Union
from urllib.parse import urlparse

import ee
import httplib2
import ipyvuetify as v
import requests
import tomli
from anyascii import anyascii
from deprecated.sphinx import deprecated, versionadded
from matplotlib import colors as c
Expand All @@ -23,6 +26,9 @@
from sepal_ui.scripts import decorator as sd
from sepal_ui.scripts.warning import SepalWarning

# Types
Pathlike = Union[str, Path]


def hide_component(widget: v.VuetifyWidget) -> v.VuetifyWidget:
"""Hide a vuetify based component.
Expand Down Expand Up @@ -60,7 +66,7 @@ def show_component(widget: v.VuetifyWidget) -> v.VuetifyWidget:
return widget


def create_download_link(pathname: Union[str, Path]) -> str:
def create_download_link(pathname: Pathlike) -> str:
"""Create a clickable link to download the pathname target.
Args:
Expand Down Expand Up @@ -102,7 +108,7 @@ def random_string(string_length: int = 3) -> str:
return "".join(random.choice(letters) for i in range(string_length))


def get_file_size(filename: Union[str, Path]) -> str:
def get_file_size(filename: Pathlike) -> str:
"""Get the file size as string of 2 digit in the adapted scale (B, KB, MB....).
Args:
Expand All @@ -128,10 +134,8 @@ def init_ee() -> None:
"""
# only do the initialization if the credential are missing
if not ee.data._credentials:

# if the credentials token is asved in the environment use it
if "EARTHENGINE_TOKEN" in os.environ:

# write the token to the appropriate folder
ee_token = os.environ["EARTHENGINE_TOKEN"]
credential_folder_path = Path.home() / ".config" / "earthengine"
Expand Down Expand Up @@ -183,15 +187,13 @@ def to_colors(
out_color = "#000000" # default black color

if isinstance(in_color, tuple) and len(in_color) == 3:

# rescale color if necessary
if all(isinstance(item, int) for item in in_color):
in_color = [c / 255.0 for c in in_color]

return transform(in_color)

else:

# try to guess the color system
try:
return transform(in_color)
Expand Down Expand Up @@ -375,6 +377,92 @@ def check_input(input_: Any, msg: str = ms.utils.check_input.error) -> bool:
return init


def get_app_version(repo_folder: Pathlike = Path.cwd()) -> str:
"""Get the current version of the a github project using the pyproject.toml file in the root.
Returns:
the version of the repository
"""
# get the path to the pyproject.toml file
pyproject_path = repo_folder / "pyproject.toml"

# check if the file exist
if pyproject_path.exists():
# read the file using tomli
pyproject = tomli.loads(pyproject_path.read_text())

# get the version
return pyproject.get("project", {}).get("version", None)

return None


def get_repo_info(repo_folder: Pathlike = Path.cwd()) -> Tuple[str, str]:
"""Get the repository name and owner from the git config file."""
config = configparser.ConfigParser()
git_config_path = Path(repo_folder) / ".git/config"
config.read(git_config_path)

try:
remote_url = config.get('remote "origin"', "url")
except (configparser.NoSectionError, configparser.NoOptionError):
return "", ""

# Check if URL is likely SSH
if "git@" in remote_url:
match = re.search(r":(.*?)/(.*?)(?:\.git)?$", remote_url)

# Assume URL is HTTPS otherwise
else:
match = re.search(r"github\.com/(.*?)/(.*?)(?:\.git)?$", remote_url)

if match:
return match.groups()

else:
return "", ""


def get_changelog(repo_folder: Pathlike = Path.cwd()) -> str:
"""Check if the repository contains a changelog file and/or a remote release and return its content.
Returns:
str: the content of the release and/or changelog file
"""
changelog_text, release_text = "", ""
repo_owner, repo_name = get_repo_info(repo_folder)

release_url = (
f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/latest"
)

response = requests.get(release_url)
if all([repo_owner, repo_name]) and response.status_code == 200:
response_json = response.json()
name = response_json.get("name")

if name == f"v_{get_app_version()}":
release_text = response_json.get("body")

url_pattern = r"https://github\.com/[^ ]+/pull/\d+"

# Replace URLs with <a> tags
def wrap_url_in_a_tag(match):
url = match.group(0)
return f'<a href="{url}">{url}</a>'

release_text = re.sub(url_pattern, wrap_url_in_a_tag, release_text)

# get the path to the pyproject.toml file
changelog_path = Path(repo_folder) / "CHANGELOG.md"

# check if the file exist
if changelog_path.exists():
changelog_text = changelog_path.read_text()

return release_text, changelog_text


################################################################################
# the soon to be deprecated decorators
#
Expand Down
Loading

0 comments on commit 9fbf7b0

Please sign in to comment.