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
Draft

Conversation

diamant3
Copy link

@diamant3 diamant3 commented Jan 12, 2024

  • use list and loop only for automatic search path of the figlet share directory
  • add current share directory used when you type pyfiglet -l like this:
image

fix for #86

@pwaller
Copy link
Owner

pwaller commented Jan 12, 2024

Would it be better to have this behave as a search path? In other words, each of the directories in the list are searched in turn when a font is requested. That way, if the font is found in any of the directories, it is found (rather than only considering the first existing directory?).

My suggestion is to make this a list of paths, and update the logic which opens a path to loop over all the paths until a hit is found.

What do you think?

@diamant3
Copy link
Author

Thank you for your reply! And I think the suggestion you said, that is what the code does. It loops every path in the list until it hits a path, depending on their OS and figlet installation(they can submit a PR to add a directory to search for on that list) to make it cross-platform. correct me if I'm wrong because english is not my primary language, thanks again!

@pwaller
Copy link
Owner

pwaller commented Jan 12, 2024

Hi, No problem. Happy to explain so feel free to keep asking if you still don't understand, and there is no urgency so we can take our time.

What I understand you've done is change the initialisation so that it chooses the first directory which is found as the directory which is searched. Given a font lookup, there is exactly one directory checked to see if it has that font. What I think users may prefer is if all of the directories are checked.

To achieve this I think SHARED_DIRECTORY = "..." should become SEARCHED_DIRECTORIES = [...], and whatever logic uses SHARED_DIRECTORY should be updated so that it checks each directory in turn until a font is found or there are no more directories to check. Hope that makes sense.

If I become unresponsive, please feel free to ping the thread once weekly (ideally late on a Thursday or early on a Friday).

@diamant3
Copy link
Author

Thank you so much for your consideration and time! sure, no worries I will do that

@peterbrittain
Copy link
Collaborator

Note that this isn't the only place SHARED_DIRECTORY is used. You will need to fix up all references to it. In particular, the logic in installFont() will need to pick a specific option from the list.

@diamant3
Copy link
Author

Thank you for the reply! Yes, I took note of that the SHARED_DIRECTORY is used even inside the methods of the classes, but I don't think that it can affect their logic. The code is still the same as before, except for the logic of automatic search.

By the way please bear with me, I'm just learning about your codebase and always open to learn from more experienced people. Thanks again for the note you gave!

@pwaller
Copy link
Owner

pwaller commented Jan 13, 2024

@diamant3 I think you've not understood what is being asked for.

What @peterbrittain and myself are thinking is that there needs to be a function, findfont(...) which returns the path to a font. The way this function works is by consulting each directory in turn. Anything which previously used SHARED_DIRECTORY should use this new function.

There shouldn't be any module-level execution, only a module level variable, SEARCHED_DIRECTORIES.

Does that make sense, and do you see how this is different from what you have implemented so far?

@diamant3
Copy link
Author

diamant3 commented Jan 13, 2024

I'm sorry, Yes it's so huge different on my implementation, thank you so much! I will rewrite this and make an update next week.

@diamant3 diamant3 changed the title improve font dir loader improve font loader Jan 13, 2024
pyfiglet/__init__.py Outdated Show resolved Hide resolved
@diamant3
Copy link
Author

Hello, Can i request a review and test my PR, thank you!

Copy link
Collaborator

@peterbrittain peterbrittain left a comment

Choose a reason for hiding this comment

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

Looks good! Just a few quick tweaks please...

pyfiglet/__init__.py Outdated Show resolved Hide resolved
pyfiglet/__init__.py Outdated Show resolved Hide resolved
pyfiglet/__init__.py Outdated Show resolved Hide resolved
pyfiglet/__init__.py Outdated Show resolved Hide resolved
@diamant3
Copy link
Author

diamant3 commented Jan 19, 2024

Thank you so much! sure no worries, just a minute

edit: it's done, thanks!

@pwaller
Copy link
Owner

pwaller commented Jan 19, 2024

Sorry if I wasn't clear before but this is still not quite what I envisaged. You've made this into a function which gets called when it's needed, that's good; but the function needs to take a font to search for as a parameter. Because the font may not exist in "the first directory found", but may exist in a later one, and different fonts may be found in different directories.

Listing all fonts should likewise consult all directories and return the union of all fonts installed in all directories. Does what I'm asking for seem reasonable?

@peterbrittain
Copy link
Collaborator

Listing all fonts should likewise consult all directories and return the union of all fonts installed in all directories. Does what I'm asking for seem reasonable?

Yeah - I wondered about this, but stuck with simplicity. Happy for you to override that as owner of the project, though.

I see two issues:

  1. The logic to list the fonts will need replacing to create a unified set of fonts.
  2. The logic to install a font needs to pick a directory where the font shouldn't already exist.

Both possible to do, but both will need testing.

@diamant3
Copy link
Author

That's on me, my bad. Yes, that's reasonable no worries. So this findFont(font) function with a parameter font will gather the font found in every directory, but what do I need to return if it exists? the directories it found with a font like this /usr/local/pyfiglet/<font> or just a path to that directory like this one /usr/local/pyfiglet/ for example, and it will result in a list of directories if it exists in another directory, am i right?

Happy for you to override that as owner of the project

Thank you so much for the permission!, and got it that issues i need to take note and needs to change according to findFont function, am i right?

@pwaller
Copy link
Owner

pwaller commented Jan 20, 2024

It's not necessarily one function. You need to consider three pieces of functionality of the software:

  1. Normal running of pyfiglet (rendering text). This needs to find a font. My ideal implementation of this would change whatever the current logic is for 'open(font)' and instead do something analogous to 'loadFont(font)' -- under the hood this function would iterate over each directory attempting to open fonts and return as soon as it has one which does not raise a FileNotFoundError.

  2. Listing fonts. This needs to make a set containing all available fonts. This should operate by calling os.listdir on each of the paths, ignoring paths which don't exist.

  3. Installing fonts. In the existing version, this installs to SHARED_DIRECTORY, which is /usr/local/share/pyfiglet on non-win32. Retaining this existing behaviour is OK, and as @peterbrittain says maybe we actually don't want to consider other paths for installation since those aren't "managed" by pyfiglet.

@peterbrittain
Copy link
Collaborator

Heavy plus 1 for what @pwaller said!

@diamant3
Copy link
Author

Hello, so I implement the logic from pwaller's said to my best understanding, can i request again for review and as always, thank youu.

@diamant3 diamant3 marked this pull request as draft January 31, 2024 05:42
Copy link
Owner

@pwaller pwaller left a comment

Choose a reason for hiding this comment

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

Apologies the ball got dropped on this one. I see you marked the PR as draft (rather than ready to review), is that your intended state? I've left another comment.

@@ -142,12 +144,20 @@ def preloadFont(cls, font):
if path.exists():
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants