Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve font loader #138

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
35 changes: 25 additions & 10 deletions pyfiglet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,23 @@

RESET_COLORS = b'\033[0m'

if sys.platform == 'win32':
SHARED_DIRECTORY = os.path.join(os.environ["APPDATA"], "pyfiglet")
else:
SHARED_DIRECTORY = '/usr/local/share/pyfiglet/'

SHARED_DIRECTORIES = [
'/usr/local/share/figlet',
'/usr/local/share/pyfiglet',
diamant3 marked this conversation as resolved.
Show resolved Hide resolved
'/usr/share/figlet',
'/usr/share/pyfiglet',
# add figlet/pyfiglet shared directory path
]

def find_shared_dir():
path = ''
diamant3 marked this conversation as resolved.
Show resolved Hide resolved
if sys.platform == 'win32':
path = os.path.join(os.environ["APPDATA"], "pyfiglet")
else:
for directory in SHARED_DIRECTORIES:
if os.path.isdir(directory):
path = directory
return path

def figlet_format(text, font=DEFAULT_FONT, **kwargs):
fig = Figlet(font, **kwargs)
Expand Down Expand Up @@ -143,7 +155,8 @@ def preloadFont(cls, font):
font_path = path
break
else:
for location in ("./", SHARED_DIRECTORY):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This previously checked $PWD as well as SHARED_DIRECTORY. I think we should preserve the existing behaviour.

shared_dir = find_shared_dir()
diamant3 marked this conversation as resolved.
Show resolved Hide resolved
for location in ("./", shared_dir):
full_name = os.path.join(location, fn)
if os.path.isfile(full_name):
font_path = pathlib.Path(full_name)
Expand Down Expand Up @@ -172,7 +185,8 @@ def isValidFont(cls, font):
if not font.endswith(('.flf', '.tlf')):
return False
f = None
full_file = os.path.join(SHARED_DIRECTORY, font)
shared_dir = find_shared_dir()
diamant3 marked this conversation as resolved.
Show resolved Hide resolved
full_file = os.path.join(shared_dir, font)
if os.path.isfile(font):
f = open(font, 'rb')
elif os.path.isfile(full_file):
Expand All @@ -197,8 +211,9 @@ def isValidFont(cls, font):
@classmethod
def getFonts(cls):
all_files = importlib.resources.files('pyfiglet.fonts').iterdir()
if os.path.isdir(SHARED_DIRECTORY):
all_files = itertools.chain(all_files, pathlib.Path(SHARED_DIRECTORY).iterdir())
shared_dir = find_shared_dir()
if os.path.isdir(shared_dir):
all_files = itertools.chain(all_files, pathlib.Path(shared_dir).iterdir())
return [font.name.split('.', 2)[0] for font
in all_files
if font.is_file() and cls.isValidFont(font.name)]
Expand Down Expand Up @@ -234,7 +249,7 @@ def installFonts(file_name):
location = str(importlib.resources.files('pyfiglet.fonts'))
else:
# Figlet is installed using a zipped resource - don't try to upload to it.
location = SHARED_DIRECTORY
location = find_shared_dir()

print("Installing {} to {}".format(file_name, location))

Expand Down