From 85ca44e20e62cf25438de8e7dacd46811943936e Mon Sep 17 00:00:00 2001 From: diamant3 Date: Fri, 12 Jan 2024 20:20:17 +0800 Subject: [PATCH 01/15] improve font dir loader --- pyfiglet/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 9ff838b..1d3e8ec 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -61,7 +61,16 @@ SHARED_DIRECTORY = os.path.join(os.environ["APPDATA"], "pyfiglet") else: SHARED_DIRECTORY = '/usr/local/share/pyfiglet/' + shared_dir_paths = [ + '/usr/share/figlet/', + '/usr/local/share/figlet/', + # add figlet shared directory + ] + for path in shared_dir_paths: + if os.path.isdir(path): + SHARED_DIRECTORY = path + break def figlet_format(text, font=DEFAULT_FONT, **kwargs): fig = Figlet(font, **kwargs) @@ -972,6 +981,7 @@ def main(): opts, args = parser.parse_args() if opts.list_fonts: + print(f'Current shared directory: {SHARED_DIRECTORY}') print('\n'.join(sorted(FigletFont.getFonts()))) exit(0) From 1c3da359915227de49cd833790f49eff56e7ef95 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sat, 13 Jan 2024 09:59:21 +0800 Subject: [PATCH 02/15] update logic to check all share directories --- pyfiglet/__init__.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 1d3e8ec..1c67954 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -60,17 +60,27 @@ if sys.platform == 'win32': SHARED_DIRECTORY = os.path.join(os.environ["APPDATA"], "pyfiglet") else: - SHARED_DIRECTORY = '/usr/local/share/pyfiglet/' - shared_dir_paths = [ + SEARCHED_DIRECTORIES = [ '/usr/share/figlet/', '/usr/local/share/figlet/', - # add figlet shared directory + '/usr/local/share/pyfiglet/', + # add pyfiglet/figlet shared directory ] - for path in shared_dir_paths: - if os.path.isdir(path): - SHARED_DIRECTORY = path - break + DIRECTORIES = [] + directory_count = (len(SEARCHED_DIRECTORIES) - 1) + index = 0 + while index <= directory_count: + directory = SEARCHED_DIRECTORIES[index] + if os.path.isdir(directory): + DIRECTORIES.append(directory) + index += 1 + + if len(DIRECTORIES) > 0: + SHARED_DIRECTORY = DIRECTORIES[0] # get the first directory found + else: + print('No share directory found.') + exit(0) def figlet_format(text, font=DEFAULT_FONT, **kwargs): fig = Figlet(font, **kwargs) @@ -982,6 +992,7 @@ def main(): if opts.list_fonts: print(f'Current shared directory: {SHARED_DIRECTORY}') + print('-------------------------') print('\n'.join(sorted(FigletFont.getFonts()))) exit(0) From 58161506104de2bdbc318bb87c04b140fdecf320 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Wed, 17 Jan 2024 20:22:40 +0800 Subject: [PATCH 03/15] implement `find_shared_dir` function --- pyfiglet/__init__.py | 54 ++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 1c67954..612d7ed 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -57,30 +57,23 @@ RESET_COLORS = b'\033[0m' -if sys.platform == 'win32': - SHARED_DIRECTORY = os.path.join(os.environ["APPDATA"], "pyfiglet") -else: - SEARCHED_DIRECTORIES = [ - '/usr/share/figlet/', - '/usr/local/share/figlet/', - '/usr/local/share/pyfiglet/', - # add pyfiglet/figlet shared directory - ] - - DIRECTORIES = [] - directory_count = (len(SEARCHED_DIRECTORIES) - 1) - index = 0 - while index <= directory_count: - directory = SEARCHED_DIRECTORIES[index] - if os.path.isdir(directory): - DIRECTORIES.append(directory) - index += 1 - - if len(DIRECTORIES) > 0: - SHARED_DIRECTORY = DIRECTORIES[0] # get the first directory found +SHARED_DIRECTORIES = [ + '/usr/share/figlet', + '/usr/share/pyfiglet', + '/usr/local/share/figlet', + '/usr/local/share/pyfiglet', + # add figlet/pyfiglet shared directory path +] + +def find_shared_dir(): + path = '' + if sys.platform == 'win32': + path = os.path.join(os.environ["APPDATA"], "pyfiglet") else: - print('No share directory found.') - exit(0) + 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) @@ -162,7 +155,8 @@ def preloadFont(cls, font): font_path = path break else: - for location in ("./", SHARED_DIRECTORY): + shared_dir = find_shared_dir() + for location in ("./", shared_dir): full_name = os.path.join(location, fn) if os.path.isfile(full_name): font_path = pathlib.Path(full_name) @@ -191,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() + full_file = os.path.join(shared_dir, font) if os.path.isfile(font): f = open(font, 'rb') elif os.path.isfile(full_file): @@ -216,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)] @@ -253,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)) @@ -991,8 +987,6 @@ def main(): opts, args = parser.parse_args() if opts.list_fonts: - print(f'Current shared directory: {SHARED_DIRECTORY}') - print('-------------------------') print('\n'.join(sorted(FigletFont.getFonts()))) exit(0) From 233c5fd5440a5a1dc4cc1b03939389f67b9b4729 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Thu, 18 Jan 2024 07:19:21 +0800 Subject: [PATCH 04/15] prioritize have `local` paths --- pyfiglet/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 612d7ed..c3a42c4 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -58,10 +58,10 @@ RESET_COLORS = b'\033[0m' SHARED_DIRECTORIES = [ - '/usr/share/figlet', - '/usr/share/pyfiglet', '/usr/local/share/figlet', '/usr/local/share/pyfiglet', + '/usr/share/figlet', + '/usr/share/pyfiglet', # add figlet/pyfiglet shared directory path ] From f0686963e30785353a2a9c23112563d37837d988 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Fri, 19 Jan 2024 17:25:40 +0800 Subject: [PATCH 05/15] fix path selection and add some tweaks --- pyfiglet/__init__.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index c3a42c4..db92026 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -58,15 +58,15 @@ RESET_COLORS = b'\033[0m' SHARED_DIRECTORIES = [ - '/usr/local/share/figlet', '/usr/local/share/pyfiglet', - '/usr/share/figlet', '/usr/share/pyfiglet', + '/usr/local/share/figlet', + '/usr/share/figlet', # add figlet/pyfiglet shared directory path ] def find_shared_dir(): - path = '' + path = '/usr/local/share/pyfiglet' # default path if sys.platform == 'win32': path = os.path.join(os.environ["APPDATA"], "pyfiglet") else: @@ -155,8 +155,7 @@ def preloadFont(cls, font): font_path = path break else: - shared_dir = find_shared_dir() - for location in ("./", shared_dir): + for location in ("./", find_shared_dir()): full_name = os.path.join(location, fn) if os.path.isfile(full_name): font_path = pathlib.Path(full_name) @@ -185,8 +184,7 @@ def isValidFont(cls, font): if not font.endswith(('.flf', '.tlf')): return False f = None - shared_dir = find_shared_dir() - full_file = os.path.join(shared_dir, font) + full_file = os.path.join(find_shared_dir(), font) if os.path.isfile(font): f = open(font, 'rb') elif os.path.isfile(full_file): From c0ca8209f0438c1467478d029bb5116c80ef2832 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sat, 20 Jan 2024 22:47:01 +0800 Subject: [PATCH 06/15] improve font loader --- pyfiglet/__init__.py | 49 +++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index db92026..64a3b60 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -65,15 +65,22 @@ # add figlet/pyfiglet shared directory path ] -def find_shared_dir(): - path = '/usr/local/share/pyfiglet' # default path - 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 openFont(font): + font_path = '/usr/local/share/pyfiglet' # default path + for directory in SHARED_DIRECTORIES: + if os.path.isdir(directory): + for file in os.listdir(directory): + if file.split('.')[0] == font.split('.')[0]: + font_path = os.path.join(directory, file) + break + # in case of same name but not the same file extension + for extension in ('tlf', 'flf'): + fn = os.path.join(font, extension) + path = os.path.join(font_path, fn) + if os.path.isfile(path): + font_path = path + break + return pathlib.Path(font_path) def figlet_format(text, font=DEFAULT_FONT, **kwargs): fig = Figlet(font, **kwargs) @@ -155,11 +162,10 @@ def preloadFont(cls, font): font_path = path break else: - for location in ("./", find_shared_dir()): - full_name = os.path.join(location, fn) - if os.path.isfile(full_name): - font_path = pathlib.Path(full_name) - break + font_path = openFont(fn) + if not os.path.isfile(font_path): + raise FontNotFound(font) + break # Unzip the first file if this file/stream looks like a ZIP file. if font_path: @@ -184,7 +190,7 @@ def isValidFont(cls, font): if not font.endswith(('.flf', '.tlf')): return False f = None - full_file = os.path.join(find_shared_dir(), font) + full_file = openFont(font) if os.path.isfile(font): f = open(font, 'rb') elif os.path.isfile(full_file): @@ -209,9 +215,14 @@ def isValidFont(cls, font): @classmethod def getFonts(cls): all_files = importlib.resources.files('pyfiglet.fonts').iterdir() - shared_dir = find_shared_dir() - if os.path.isdir(shared_dir): - all_files = itertools.chain(all_files, pathlib.Path(shared_dir).iterdir()) + shared_dir = set() + for directory in SHARED_DIRECTORIES: + if os.path.isdir(directory): + shared_dir.update(pathlib.Path(directory).iterdir()) + if shared_dir: + all_files = itertools.chain(all_files, shared_dir) + else: + all_files = itertools.chain(all_files) return [font.name.split('.', 2)[0] for font in all_files if font.is_file() and cls.isValidFont(font.name)] @@ -247,7 +258,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 = find_shared_dir() + location = '/usr/local/share/pyfiglet' print("Installing {} to {}".format(file_name, location)) From 6f281a70a759ad71f0a4337e7006d08e611798e7 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sat, 20 Jan 2024 22:49:34 +0800 Subject: [PATCH 07/15] forgot win32 check --- pyfiglet/__init__.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 64a3b60..044cdea 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -67,19 +67,22 @@ def openFont(font): font_path = '/usr/local/share/pyfiglet' # default path - for directory in SHARED_DIRECTORIES: - if os.path.isdir(directory): - for file in os.listdir(directory): - if file.split('.')[0] == font.split('.')[0]: - font_path = os.path.join(directory, file) - break - # in case of same name but not the same file extension - for extension in ('tlf', 'flf'): - fn = os.path.join(font, extension) - path = os.path.join(font_path, fn) - if os.path.isfile(path): - font_path = path - break + if sys.platform == 'win32': + font_path = os.path.join(os.environ["APPDATA"], "pyfiglet") + else: + for directory in SHARED_DIRECTORIES: + if os.path.isdir(directory): + for file in os.listdir(directory): + if file.split('.')[0] == font.split('.')[0]: + font_path = os.path.join(directory, file) + break + # in case of same name but not the same file extension + for extension in ('tlf', 'flf'): + fn = os.path.join(font, extension) + path = os.path.join(font_path, fn) + if os.path.isfile(path): + font_path = path + break return pathlib.Path(font_path) def figlet_format(text, font=DEFAULT_FONT, **kwargs): From 2c06ca40b8da4bc7270b71f25690b8598f07675e Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sat, 20 Jan 2024 22:51:47 +0800 Subject: [PATCH 08/15] check all file extension --- pyfiglet/__init__.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 044cdea..5f62be5 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -76,13 +76,13 @@ def openFont(font): if file.split('.')[0] == font.split('.')[0]: font_path = os.path.join(directory, file) break - # in case of same name but not the same file extension - for extension in ('tlf', 'flf'): - fn = os.path.join(font, extension) - path = os.path.join(font_path, fn) - if os.path.isfile(path): - font_path = path - break + # in case of same name but not the same file extension + for extension in ('tlf', 'flf'): + fn = os.path.join(font, extension) + path = os.path.join(font_path, fn) + if os.path.isfile(path): + font_path = path + break return pathlib.Path(font_path) def figlet_format(text, font=DEFAULT_FONT, **kwargs): From b85986301866e50dfcc7b28b7399dddd15990496 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sat, 20 Jan 2024 22:57:05 +0800 Subject: [PATCH 09/15] fix file extension check --- pyfiglet/__init__.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 5f62be5..c9de4e9 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -69,6 +69,13 @@ def openFont(font): font_path = '/usr/local/share/pyfiglet' # default path if sys.platform == 'win32': font_path = os.path.join(os.environ["APPDATA"], "pyfiglet") + # in case of same name but not the same file extension + for extension in ('tlf', 'flf'): + fn = os.path.join(font, extension) + path = os.path.join(font_path, fn) + if os.path.isfile(path): + font_path = path + break else: for directory in SHARED_DIRECTORIES: if os.path.isdir(directory): @@ -76,13 +83,14 @@ def openFont(font): if file.split('.')[0] == font.split('.')[0]: font_path = os.path.join(directory, file) break - # in case of same name but not the same file extension - for extension in ('tlf', 'flf'): - fn = os.path.join(font, extension) - path = os.path.join(font_path, fn) - if os.path.isfile(path): - font_path = path - break + # in case of same name but not the same file extension + for extension in ('tlf', 'flf'): + fn = os.path.join(font, extension) + path = os.path.join(font_path, fn) + if os.path.isfile(path): + font_path = path + break + return pathlib.Path(font_path) def figlet_format(text, font=DEFAULT_FONT, **kwargs): From afc37fe768274a10395c3ae10c9bcebefbca6674 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sun, 21 Jan 2024 09:15:49 +0800 Subject: [PATCH 10/15] remove excess file ext check --- pyfiglet/__init__.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index c9de4e9..e6fc96e 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -69,13 +69,6 @@ def openFont(font): font_path = '/usr/local/share/pyfiglet' # default path if sys.platform == 'win32': font_path = os.path.join(os.environ["APPDATA"], "pyfiglet") - # in case of same name but not the same file extension - for extension in ('tlf', 'flf'): - fn = os.path.join(font, extension) - path = os.path.join(font_path, fn) - if os.path.isfile(path): - font_path = path - break else: for directory in SHARED_DIRECTORIES: if os.path.isdir(directory): @@ -83,13 +76,13 @@ def openFont(font): if file.split('.')[0] == font.split('.')[0]: font_path = os.path.join(directory, file) break - # in case of same name but not the same file extension - for extension in ('tlf', 'flf'): - fn = os.path.join(font, extension) - path = os.path.join(font_path, fn) - if os.path.isfile(path): - font_path = path - break + # in case of same name but not the same file extension + for extension in ('tlf', 'flf'): + fn = os.path.join(font, extension) + path = os.path.join(font_path, fn) + if os.path.isfile(path): + font_path = path + break return pathlib.Path(font_path) From 293fb2525ed9e0284e9a1d52ced8a22dd0aff730 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sun, 21 Jan 2024 11:00:15 +0800 Subject: [PATCH 11/15] get the filename only --- pyfiglet/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index e6fc96e..20d5c29 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -78,7 +78,7 @@ def openFont(font): break # in case of same name but not the same file extension for extension in ('tlf', 'flf'): - fn = os.path.join(font, extension) + fn = os.path.join(font.split('.')[0], extension) path = os.path.join(font_path, fn) if os.path.isfile(path): font_path = path From 970b0c45383fb881d7ed5c4418b36334f5885965 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sun, 21 Jan 2024 11:55:11 +0800 Subject: [PATCH 12/15] fix win32 font file ext check --- pyfiglet/__init__.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 20d5c29..5e8b528 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -68,7 +68,12 @@ def openFont(font): font_path = '/usr/local/share/pyfiglet' # default path if sys.platform == 'win32': - font_path = os.path.join(os.environ["APPDATA"], "pyfiglet") + directory = os.path.join(os.environ["APPDATA"], "pyfiglet") + if os.path.isdir(directory): + for file in os.listdir(directory): + if file.split('.')[0] == font.split('.')[0]: + font_path = os.path.join(directory, file) + break else: for directory in SHARED_DIRECTORIES: if os.path.isdir(directory): @@ -76,14 +81,6 @@ def openFont(font): if file.split('.')[0] == font.split('.')[0]: font_path = os.path.join(directory, file) break - # in case of same name but not the same file extension - for extension in ('tlf', 'flf'): - fn = os.path.join(font.split('.')[0], extension) - path = os.path.join(font_path, fn) - if os.path.isfile(path): - font_path = path - break - return pathlib.Path(font_path) def figlet_format(text, font=DEFAULT_FONT, **kwargs): From afe03ebcf24ada18fd026dcb73854b3af48ff46d Mon Sep 17 00:00:00 2001 From: diamant3 Date: Mon, 22 Jan 2024 20:25:12 +0800 Subject: [PATCH 13/15] fix cant detect fonts from pyfiglet fonts resources --- pyfiglet/__init__.py | 49 ++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 5e8b528..27d801b 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -65,24 +65,6 @@ # add figlet/pyfiglet shared directory path ] -def openFont(font): - font_path = '/usr/local/share/pyfiglet' # default path - if sys.platform == 'win32': - directory = os.path.join(os.environ["APPDATA"], "pyfiglet") - if os.path.isdir(directory): - for file in os.listdir(directory): - if file.split('.')[0] == font.split('.')[0]: - font_path = os.path.join(directory, file) - break - else: - for directory in SHARED_DIRECTORIES: - if os.path.isdir(directory): - for file in os.listdir(directory): - if file.split('.')[0] == font.split('.')[0]: - font_path = os.path.join(directory, file) - break - return pathlib.Path(font_path) - def figlet_format(text, font=DEFAULT_FONT, **kwargs): fig = Figlet(font, **kwargs) return fig.renderText(text) @@ -148,6 +130,24 @@ def __init__(self, font=DEFAULT_FONT): self.data = self.preloadFont(font) self.loadFont() + def openFont(font): + font_path = '/usr/local/share/pyfiglet' # default path + if sys.platform == 'win32': + directory = os.path.join(os.environ["APPDATA"], "pyfiglet") + if os.path.isdir(directory): + for file in os.listdir(directory): + if file.split('.')[0] == font.split('.')[0]: + font_path = os.path.join(directory, file) + break + else: + for directory in SHARED_DIRECTORIES: + if os.path.isdir(directory): + for file in os.listdir(directory): + if file.split('.')[0] == font.split('.')[0]: + font_path = os.path.join(directory, file) + break + return pathlib.Path(font_path) + @classmethod def preloadFont(cls, font): """ @@ -156,17 +156,18 @@ def preloadFont(cls, font): # Find a plausible looking font file. data = None font_path = None + is_found = False for extension in ('tlf', 'flf'): fn = '%s.%s' % (font, extension) path = importlib.resources.files('pyfiglet.fonts').joinpath(fn) if path.exists(): font_path = path + is_found = True break - else: - font_path = openFont(fn) - if not os.path.isfile(font_path): - raise FontNotFound(font) - break + if not is_found: + font_path = FigletFont.openFont(fn) + if not os.path.isfile(font_path): + raise FontNotFound(font) # Unzip the first file if this file/stream looks like a ZIP file. if font_path: @@ -191,7 +192,7 @@ def isValidFont(cls, font): if not font.endswith(('.flf', '.tlf')): return False f = None - full_file = openFont(font) + full_file = FigletFont.openFont(font) if os.path.isfile(font): f = open(font, 'rb') elif os.path.isfile(full_file): From 1e33639525005367be7c141d4732f47b7770ec2f Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sun, 28 Jan 2024 22:00:07 +0800 Subject: [PATCH 14/15] simplify --- pyfiglet/__init__.py | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 27d801b..03de281 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -130,24 +130,6 @@ def __init__(self, font=DEFAULT_FONT): self.data = self.preloadFont(font) self.loadFont() - def openFont(font): - font_path = '/usr/local/share/pyfiglet' # default path - if sys.platform == 'win32': - directory = os.path.join(os.environ["APPDATA"], "pyfiglet") - if os.path.isdir(directory): - for file in os.listdir(directory): - if file.split('.')[0] == font.split('.')[0]: - font_path = os.path.join(directory, file) - break - else: - for directory in SHARED_DIRECTORIES: - if os.path.isdir(directory): - for file in os.listdir(directory): - if file.split('.')[0] == font.split('.')[0]: - font_path = os.path.join(directory, file) - break - return pathlib.Path(font_path) - @classmethod def preloadFont(cls, font): """ @@ -156,18 +138,19 @@ def preloadFont(cls, font): # Find a plausible looking font file. data = None font_path = None - is_found = False for extension in ('tlf', 'flf'): fn = '%s.%s' % (font, extension) path = importlib.resources.files('pyfiglet.fonts').joinpath(fn) if path.exists(): font_path = path - is_found = True break - if not is_found: - font_path = FigletFont.openFont(fn) - if not os.path.isfile(font_path): - raise FontNotFound(font) + else: + for directory in SHARED_DIRECTORIES: + if os.path.isdir(directory): + path = os.path.join(directory, fn) + if os.path.isfile(path): + font_path = pathlib.Path(path) + break # Unzip the first file if this file/stream looks like a ZIP file. if font_path: @@ -192,7 +175,13 @@ def isValidFont(cls, font): if not font.endswith(('.flf', '.tlf')): return False f = None - full_file = FigletFont.openFont(font) + full_file = '' + for directory in SHARED_DIRECTORIES: + if os.path.isdir(directory): + path = os.path.join(directory, font) + if os.path.isfile(path): + full_file = pathlib.Path(path) + break if os.path.isfile(font): f = open(font, 'rb') elif os.path.isfile(full_file): From a8b0c1fdbfb66aaa62fe9b0940ad5d7f9e66e34a Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sun, 28 Jan 2024 22:33:24 +0800 Subject: [PATCH 15/15] add win32 check --- pyfiglet/__init__.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 03de281..3553a1f 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -144,6 +144,13 @@ def preloadFont(cls, font): if path.exists(): font_path = path break + if sys.platform == 'win32': + path = os.path.join(os.environ["APPDATA"], "pyfiglet") + if os.path.exists(path): + path = os.path.join(path, fn) + if os.path.isfile(path): + font_path = pathlib.Path(path) + break else: for directory in SHARED_DIRECTORIES: if os.path.isdir(directory): @@ -176,12 +183,19 @@ def isValidFont(cls, font): return False f = None full_file = '' - for directory in SHARED_DIRECTORIES: - if os.path.isdir(directory): - path = os.path.join(directory, font) - if os.path.isfile(path): - full_file = pathlib.Path(path) - break + if sys.platform == 'win32': + path = os.path.join(os.environ["APPDATA"], "pyfiglet") + if os.path.isdir(path): + path = os.path.join(path, font) + if os.path.isfile(path): + full_file = pathlib.Path(path) + else: + for directory in SHARED_DIRECTORIES: + if os.path.isdir(directory): + path = os.path.join(directory, font) + if os.path.isfile(path): + full_file = pathlib.Path(path) + break if os.path.isfile(font): f = open(font, 'rb') elif os.path.isfile(full_file): @@ -207,9 +221,14 @@ def isValidFont(cls, font): def getFonts(cls): all_files = importlib.resources.files('pyfiglet.fonts').iterdir() shared_dir = set() - for directory in SHARED_DIRECTORIES: - if os.path.isdir(directory): - shared_dir.update(pathlib.Path(directory).iterdir()) + if sys.platform == 'win32': + path = os.path.join(os.environ["APPDATA"], "pyfiglet") + if os.path.isdir(path): + shared_dir.update(pathlib.Path(path).iterdir()) + else: + for directory in SHARED_DIRECTORIES: + if os.path.isdir(directory): + shared_dir.update(pathlib.Path(directory).iterdir()) if shared_dir: all_files = itertools.chain(all_files, shared_dir) else: