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

Tidy up fonts and provide a font installation option #61

Merged
merged 7 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions pyfiglet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import os
import pkg_resources
import re
import shutil
import sys
import zipfile
from optparse import OptionParser

from .version import __version__
Expand Down Expand Up @@ -46,6 +48,8 @@

RESET_COLORS = b'\033[0m'

SHARED_DIRECTORY = '%APPDATA%/pyfiglet/' if sys.platform == 'win32' else '/usr/local/share/pyfiglet/'
peterbrittain marked this conversation as resolved.
Show resolved Hide resolved


def figlet_format(text, font=DEFAULT_FONT, **kwargs):
fig = Figlet(font, **kwargs)
Expand Down Expand Up @@ -123,9 +127,12 @@ def preloadFont(cls, font):
data = pkg_resources.resource_string('pyfiglet.fonts', fn)
data = data.decode('UTF-8', 'replace')
return data
elif os.path.isfile(font):
with open(font, 'rb') as f:
return f.read().decode('UTF-8', 'replace')
else:
for location in ("./", SHARED_DIRECTORY):
full_name = os.path.join(location, fn)
if os.path.isfile(full_name):
with open(full_name, 'rb') as f:
return f.read().decode('UTF-8', 'replace')
else:
raise FontNotFound(font)

Expand All @@ -134,8 +141,11 @@ def isValidFont(cls, font):
if not font.endswith(('.flf', '.tlf')):
return False
f = None
full_file = os.path.join(SHARED_DIRECTORY, font)
if os.path.isfile(font):
f = open(font, 'rb')
elif os.path.isfile(full_file):
f = open(full_file, 'rb')
else:
f = pkg_resources.resource_stream('pyfiglet.fonts', font)
header = f.readline().decode('UTF-8', 'replace')
Expand All @@ -144,8 +154,11 @@ def isValidFont(cls, font):

@classmethod
def getFonts(cls):
all_files = pkg_resources.resource_listdir('pyfiglet', 'fonts')
if os.path.isdir(SHARED_DIRECTORY):
all_files += os.listdir(SHARED_DIRECTORY)
return [font.rsplit('.', 2)[0] for font
in pkg_resources.resource_listdir('pyfiglet', 'fonts')
in all_files
if cls.isValidFont(font)]

@classmethod
Expand All @@ -169,6 +182,38 @@ def infoFont(cls, font, short=False):
infos.append(line)
return '\n'.join(infos) if not short else infos[0]

@staticmethod
def installFonts(file_name):
"""
Install the specified font file to this system.
"""
if isinstance(pkg_resources.get_provider('pyfiglet'), pkg_resources.ZipProvider):
# Figlet is installed using a zipped resource - don't try to upload to it.
location = SHARED_DIRECTORY
else:
# Figlet looks like a standard directory - so lets use that to install new fonts.
location = pkg_resources.resource_filename('pyfiglet', 'fonts')

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

# Make sure the required destination directory exists
if not os.path.exists(location):
os.makedirs(location)

# Copy the font definitions - unpacking any zip files as needed.
if os.path.splitext(file_name)[1].lower() == ".zip":
# Ignore any structure inside the ZIP file.
with zipfile.ZipFile(file_name) as zip_file:
for font in zip_file.namelist():
font_file = os.path.basename(font)
if not font_file:
continue
src = zip_file.open(font)
dest = open(os.path.join(location, font_file), "wb")
peterbrittain marked this conversation as resolved.
Show resolved Hide resolved
shutil.copyfileobj(src, dest)
else:
shutil.copy(file_name, location)

def loadFont(self):
"""
Parse loaded font data for the rendering engine to consume
Expand Down Expand Up @@ -833,6 +878,8 @@ def main():
help='show installed fonts list')
parser.add_option('-i', '--info_font', action='store_true', default=False,
help='show font\'s information, use with -f FONT')
parser.add_option('-L', '--load', default=None,
help='load and install the specified font definition')
parser.add_option('-c', '--color', default=':',
help='''prints text with passed foreground color,
--color=foreground:background
Expand All @@ -854,6 +901,10 @@ def main():
print(FigletFont.infoFont(opts.font))
exit(0)

if opts.load:
FigletFont.installFonts(opts.load)
exit(0)

if len(args) == 0:
parser.print_help()
return 1
Expand Down
218 changes: 0 additions & 218 deletions pyfiglet/fonts/1row.flf

This file was deleted.

Loading