Skip to content

Commit

Permalink
fix errors on windows (#11)
Browse files Browse the repository at this point in the history
* added tests on mac os

* add windows test

* attempt fix for url issues on windows

* tweak to base url path for font download

* specify encoding of font metadata file

* update version number
  • Loading branch information
gillenbrown committed Jul 23, 2023
1 parent 5457c09 commit 177e6cb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ jobs:
needs: black
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.7", "3.8", "3.9", "3.10"]
runs-on: ubuntu-latest
include:
- os: macos-latest
python-version: "3.10"
- os: windows-latest
python-version: "3.10"
runs-on: ${{ matrix.os }}
steps:
- name: checkout
uses: actions/checkout@v3
Expand Down
19 changes: 10 additions & 9 deletions betterplotlib/styles.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import urllib.request
import os
from pathlib import Path

import matplotlib
Expand Down Expand Up @@ -220,15 +219,17 @@ def download_font(fontname, dir):
# format the font as it is in the github repo
fontname = fontname.lower().replace(" ", "")

base_url = "https://raw.githubusercontent.com/google/fonts/master/"
# we don't know what license the font uses, so we need to search for it
base_url = "https://raw.githubusercontent.com/google/fonts/master"
# we don't know what license the font uses, so we need to search for it.
# Note that we do not use os.path.join to attach components to URLs when trying
# to find a font. That is system dependent, while URLs will always use slashes here
licenses = ["apache", "ofl", "ufl"]
found = False
for l in licenses:
font_dir_url = os.path.join(base_url, l, fontname)
font_dir_url = "/".join([base_url, l, fontname])

# try to get the metadata file
metadata_url = os.path.join(font_dir_url, "METADATA.pb")
metadata_url = "/".join([font_dir_url, "METADATA.pb"])
# the download will throw an error if it's not found
try:
_download_file(metadata_url, dir)
Expand All @@ -243,7 +244,7 @@ def download_font(fontname, dir):
# then get the filenames from the metadata file. We do need to watch out
# for variable fonts.
metadata_path = dir / "METADATA.pb"
with open(metadata_path, "r") as metadata_file:
with open(metadata_path, "r", encoding="utf-8") as metadata_file:
metadata = [line.strip() for line in metadata_file]

# remove the metadata file
Expand All @@ -261,7 +262,7 @@ def download_font(fontname, dir):
# this will have quotes, remove them
filename = filename.replace('"', "")
# then we can just download it
ttf_url = os.path.join(font_dir_url, filename)
ttf_url = "/".join([font_dir_url, filename])
_download_file(ttf_url, dir)
print(" - Downloading {}".format(filename))

Expand Down Expand Up @@ -300,7 +301,7 @@ def download_font(fontname, dir):
for italic in ["Italic", ""]:
ttf_name = "{}-{}{}.ttf".format(file_base_name, weight, italic)

ttf_url = os.path.join(font_dir_url, "static", ttf_name)
ttf_url = "/".join([font_dir_url, "static", ttf_name])

# some of these will not exist, which is fine. Grab what exists
try:
Expand All @@ -319,6 +320,6 @@ def download_font(fontname, dir):
# this will have quotes, remove them
filename = filename.replace('"', "")
# then we can just download it
ttf_url = os.path.join(font_dir_url, filename)
ttf_url = "/".join([font_dir_url, filename])
_download_file(ttf_url, dir)
print(" - Downloading {}".format(filename))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages = ["betterplotlib"]

[project]
name = "betterplotlib"
version = "1.12.5"
version = "1.12.6"
description = "Some wrappers for matplotlib to make plotting easier and nicer."
readme = "README.md"
license = {file = "LICENSE"}
Expand Down

0 comments on commit 177e6cb

Please sign in to comment.