From 95706800ba3c91cfd5825aa9ebcf8531f4cb7944 Mon Sep 17 00:00:00 2001 From: Barbier--Darnal Joseph Date: Mon, 16 Sep 2024 17:11:00 +0200 Subject: [PATCH 1/3] remove google font feature --- pyfonts/get_font.py | 11 ----------- pyfonts/is_valid.py | 13 ------------- pyfonts/main.py | 29 +++++++++-------------------- pyfonts/preview_font.py | 5 +---- req.py | 17 ++--------------- requirements-dev.txt | 2 -- requirements.txt | 1 + tests/test_is_valid.py | 14 +------------- tests/test_load_font.py | 30 ------------------------------ 9 files changed, 14 insertions(+), 108 deletions(-) delete mode 100644 requirements-dev.txt diff --git a/pyfonts/get_font.py b/pyfonts/get_font.py index 1145c2e..983ccb3 100644 --- a/pyfonts/get_font.py +++ b/pyfonts/get_font.py @@ -74,14 +74,3 @@ def _get_local_font(font_location: str) -> FontProperties: raise ValueError(f"Font file not found at : '{font_location}'") return font - - -def _get_font_from_google( - font_location: str, weight: str, style: str -) -> FontProperties: - raise ValueError("Feature not available yet") - - # font_dir = font_location.lower() - # available_files = _get_github_dir_files(path=f"ofl/{font_dir}") - # available_font_files = _filter_font_files(available_files) - # raw_tag = "?raw=true" diff --git a/pyfonts/is_valid.py b/pyfonts/is_valid.py index e2fdacb..ca6547e 100644 --- a/pyfonts/is_valid.py +++ b/pyfonts/is_valid.py @@ -41,16 +41,3 @@ def _is_valid_raw_url(url: str) -> bool: return True return False - - -def _is_safe_input(font_name, font_url, font_path) -> bool: - if font_name and (font_url or font_path): - return False - elif font_url and (font_name or font_path): - return False - elif font_path and (font_name or font_url): - return False - elif not font_name and not font_url and not font_path: - return False - else: - return True diff --git a/pyfonts/main.py b/pyfonts/main.py index 0436136..4660485 100644 --- a/pyfonts/main.py +++ b/pyfonts/main.py @@ -1,46 +1,35 @@ from matplotlib.font_manager import FontProperties from typing import Optional -from .get_font import _get_font_from_url, _get_local_font, _get_font_from_google -from .is_valid import _is_safe_input +from .get_font import _get_font_from_url, _get_local_font def load_font( font_url: Optional[str] = None, font_path: Optional[str] = None, - font_name: Optional[str] = None, - weight: Optional[str] = None, - style: Optional[str] = None, ) -> FontProperties: """ Loads a font from one of the following: - An url that points to a binary font file if `font_url` - A locally stored font if `font_path` - - A font from Google font repo if `font_name` Parameters: - - font_url (Optional[str]): A URL pointing to a binary font file. + - font_url (Optional[str]): A URL pointing to a binary font file from Github. - font_path (Optional[str]): The local file path of the font. - - font_name (Optional[str]): The name of the font to load from Google Fonts. - - weight (Optional[str]): The weight of the font to load. - - style (Optional[str]): Returns: - matplotlib.font_manager.FontProperties: A FontProperties object containing the loaded font. """ - - if not _is_safe_input(font_name, font_url, font_path): + if font_url and font_path: raise ValueError( - "You must provide only one of the following: `font_name`, `font_url` or `font_path`." + "You must provide only one of the following: `font_url` or `font_path`." ) - - if font_url: + elif font_url: font = _get_font_from_url(font_url) - elif font_path: font = _get_local_font(font_path) - - elif font_name: - font = _get_font_from_google(font_name, weight=weight, style=style) - + else: + raise ValueError( + "You must provide one of the following: `font_url` or `font_path`." + ) return font diff --git a/pyfonts/preview_font.py b/pyfonts/preview_font.py index dc31645..2425d44 100644 --- a/pyfonts/preview_font.py +++ b/pyfonts/preview_font.py @@ -6,14 +6,11 @@ def preview_font( font_url: Optional[str] = None, font_path: Optional[str] = None, - font_name: Optional[str] = None, - weight: Optional[str] = None, - style: Optional[str] = None, ): """ Preview a font. """ - font = load_font(font_url, font_path, font_name, weight, style) + font = load_font(font_url, font_path) plt.figure(figsize=(10, 5)) plt.text( diff --git a/req.py b/req.py index 6211962..96cc407 100644 --- a/req.py +++ b/req.py @@ -1,9 +1,7 @@ import subprocess -packages = ["matplotlib"] -packages_dev = packages + ["pytest"] +packages = ["matplotlib", "pytest"] REQUIREMENTS_FILE = "requirements.txt" -REQUIREMENTS_FILE_DEV = "requirements-dev.txt" def get_package_version(package_name: str) -> str: @@ -34,15 +32,4 @@ def get_package_version(package_name: str) -> str: file.write(packageVersion + "\n") else: print(f"Version not found for package {package}") - print(f"{REQUIREMENTS_FILE} file updated\n\n") - -with open(REQUIREMENTS_FILE_DEV, "w") as file: - for package in packages_dev: - version = get_package_version(package) - if version: - packageVersion = f"{package}=={version}" - print(packageVersion) - file.write(packageVersion + "\n") - else: - print(f"Version not found for package {package}") - print(f"{REQUIREMENTS_FILE_DEV} file updated") + print(f"\n{REQUIREMENTS_FILE} file updated") diff --git a/requirements-dev.txt b/requirements-dev.txt deleted file mode 100644 index 94c0518..0000000 --- a/requirements-dev.txt +++ /dev/null @@ -1,2 +0,0 @@ -matplotlib==3.9.1 -pytest==8.3.2 diff --git a/requirements.txt b/requirements.txt index 5f7ba6c..94c0518 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ matplotlib==3.9.1 +pytest==8.3.2 diff --git a/tests/test_is_valid.py b/tests/test_is_valid.py index e833f0f..fe4b3f2 100644 --- a/tests/test_is_valid.py +++ b/tests/test_is_valid.py @@ -1,17 +1,5 @@ import pytest -from pyfonts.is_valid import _is_valid_raw_url, _is_url, _is_safe_input - - -def test_safe_input(): - assert _is_safe_input("font_name", None, None) == True - assert _is_safe_input(None, "font_url", None) == True - assert _is_safe_input(None, None, "font_path") == True - assert _is_safe_input("font_name", "font_url", None) == False - assert _is_safe_input(None, "font_url", "font_path") == False - assert _is_safe_input("font_name", None, "font_path") == False - assert _is_safe_input("font_name", "font_url", "font_path") == False - assert _is_safe_input(None, None, None) == False - assert _is_safe_input("font_name", "font_url", "font_path") == False +from pyfonts.is_valid import _is_valid_raw_url, _is_url @pytest.mark.parametrize( diff --git a/tests/test_load_font.py b/tests/test_load_font.py index c0f1075..c7d63d8 100644 --- a/tests/test_load_font.py +++ b/tests/test_load_font.py @@ -4,28 +4,6 @@ from pyfonts import load_font -@pytest.fixture(autouse=True) -def mock_imported_functions(): - with patch("pyfonts.is_valid._is_safe_input") as mock_is_safe_input, patch( - "pyfonts.get_font._get_font_from_url" - ) as mock_get_font_from_url, patch( - "pyfonts.get_font._get_local_font" - ) as mock_get_local_font, patch( - "pyfonts.get_font._get_font_from_google" - ) as mock_get_font_from_google: - mock_is_safe_input.return_value = True - mock_get_font_from_url.return_value = FontProperties() - mock_get_local_font.return_value = FontProperties() - mock_get_font_from_google.return_value = FontProperties() - - yield ( - mock_is_safe_input, - mock_get_font_from_url, - mock_get_local_font, - mock_get_font_from_google, - ) - - def test_load_font_with_url(): font = load_font( font_url="https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/tests/Ultra-Regular.ttf?raw=true" @@ -46,11 +24,3 @@ def test_load_font_invalid_input(): def test_load_font_no_input(): with pytest.raises(ValueError): load_font() - - -def test_load_font_unsafe_input(mock_imported_functions): - mock_is_safe_input, _, _, _ = mock_imported_functions - mock_is_safe_input.return_value = False - - with pytest.raises(ValueError): - load_font(font_url="http://example.com/font.ttf") From b0b05db312eaeef8e087870ac36c6cc79fd61414 Mon Sep 17 00:00:00 2001 From: Barbier--Darnal Joseph Date: Mon, 16 Sep 2024 17:16:12 +0200 Subject: [PATCH 2/3] update doc and set up --- .pre-commit-config.yaml | 20 ++++++++++++--- README.md | 56 ++--------------------------------------- pyproject.toml | 4 +-- 3 files changed, 21 insertions(+), 59 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0185d72..ce477ee 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,3 @@ -# Install pre-commit hooks via -# pre-commit install - repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c # frozen: v4.6.0 @@ -46,6 +43,23 @@ repos: hooks: - id: codespell args: ["-w", "-L", "ist,cant,connexion,multline,checkin"] + + - repo: local + hooks: + - id: custom-script + name: Verify requirements file + entry: python req.py + language: system + types: [python] + + - repo: local + hooks: + - id: tests + name: Run tests + entry: pytest tests/ + language: system + types: [python] + ci: autofix_prs: false autofix_commit_msg: "[pre-commit.ci 🤖] Apply code format tools to PR" diff --git a/README.md b/README.md index 4c5edcc..d388de3 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,6 @@ You can install `pyfonts` directly from PyPI with: pip install pyfonts ``` -(not recommended) Alternatively you can install the **development version** with: - -``` -pip install git+https://github.com/JosephBARBIERDARNAL/pyfonts.git -``` -

# Quick start @@ -36,7 +30,6 @@ font = load_font( font_url="https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf" ) -# check how the font looks on a minimalist example fig, ax = plt.subplots(figsize=(10, 6)) ax.text( x=0.5, @@ -61,53 +54,8 @@ Check out [the online documentation](https://python-graph-gallery.com/pyfonts/). # Contributions -Contributions (and feedback) are welcome. - -### TODO features: - -- check the [issues](https://github.com/JosephBARBIERDARNAL/pyfonts/issues) for ideas -- suggest something (: - -### Installation for contributions - -_Follow the steps below for your OS._ - -1. **Fork the Repository:** - Fork this repository to your GitHub account. - -2. **Clone the Repository:** - - ```bash - git clone https://github.com/yourusername/pyfonts.git - cd pyfonts - ``` - -3. **Set Up a Virtual Environment:** - - - **Mac/Linux:** - ```bash - python -m venv venv - source venv/bin/activate - ``` - - **Windows:** - ```cmd - python -m venv venv - venv\Scripts\activate - ``` - -4. **Install Dependencies:** - - ```bash - pip install -r requirements-dev.txt - pip install -e . - ``` - -5. **Create a Feature Branch:** - - ```bash - git checkout -b feature-name - ``` +Contributions and feedback are welcome. -6. **Start Coding!** +There's not much that needs to be implemented at the moment. If you've found a bug or want to request a new feature, open an [issue](https://github.com/JosephBARBIERDARNAL/pyfonts/issues).


diff --git a/pyproject.toml b/pyproject.toml index 4153aa5..9dc0be2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ authors = [ ] description = "A simple way to load fonts for matplotlib" readme = "README.md" -requires-python = ">=3.8" +requires-python = ">=3.9" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", @@ -23,4 +23,4 @@ build-backend = "setuptools.build_meta" [project.urls] Homepage = "https://github.com/JosephBARBIERDARNAL/pyfonts" -Issues = "https://github.com/JosephBARBIERDARNAL/pyfonts/issues" \ No newline at end of file +Issues = "https://github.com/JosephBARBIERDARNAL/pyfonts/issues" From 2655f16399819249fa7765e74838df8c716c8af6 Mon Sep 17 00:00:00 2001 From: Barbier--Darnal Joseph Date: Mon, 16 Sep 2024 17:56:37 +0200 Subject: [PATCH 3/3] update readme + basic updates --- README.md | 2 +- pyfonts/get_font.py | 4 ++-- pyfonts/main.py | 8 +++----- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d388de3..bb545da 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ pyfonts logo -A simple way to load fonts for matplotlib. +A simple (and reproducible) way to load fonts for `matplotlib`. Check out [the online documentation](https://python-graph-gallery.com/pyfonts/). diff --git a/pyfonts/get_font.py b/pyfonts/get_font.py index 983ccb3..9558967 100644 --- a/pyfonts/get_font.py +++ b/pyfonts/get_font.py @@ -13,7 +13,7 @@ def _get_font_from_url(font_location: str) -> FontProperties: font found and returns a FontProperties. Params: - - font_location (str): url that points to the binary font file on Github + - font_location: url that points to the binary font file on Github Returns: - matplotlib.font_manager.FontProperties: A FontProperties object containing the loaded font. @@ -57,7 +57,7 @@ def _get_local_font(font_location: str) -> FontProperties: Retrieves a font from a local path. Params: - - font_location (str): path to a font file. + - font_location: path to a font file. Returns: - matplotlib.font_manager.FontProperties: A FontProperties object containing the loaded font. diff --git a/pyfonts/main.py b/pyfonts/main.py index 4660485..546799e 100644 --- a/pyfonts/main.py +++ b/pyfonts/main.py @@ -9,13 +9,11 @@ def load_font( font_path: Optional[str] = None, ) -> FontProperties: """ - Loads a font from one of the following: - - An url that points to a binary font file if `font_url` - - A locally stored font if `font_path` + Loads a FontProperties object from a remote Github repo or a local file. Parameters: - - font_url (Optional[str]): A URL pointing to a binary font file from Github. - - font_path (Optional[str]): The local file path of the font. + - font_url: A URL pointing to a binary font file from Github. + - font_path: The local file path of the font. Returns: - matplotlib.font_manager.FontProperties: A FontProperties object containing the loaded font.