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

PB-227: Helptext translations #76

Merged
merged 16 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
18 changes: 18 additions & 0 deletions app/helpers/check_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from flask import abort

from app.helpers.description import find_descripton_file
from app.icon_set import get_icon_set

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -88,3 +89,20 @@ def get_and_check_icon(icon_set, icon_name):
logger.error("The icon doesn't exist: %s", path)
abort(404, "Icon not found in icon set")
return icon


def check_if_descripton_file_exists(icon_set):
"""
Checks that the icon set has a corresponding dictionary containing description for all available
languages.
if not raises a flask error and abort the current request.

Args:
icon_set: (IconSet) the icon set in which belongs the icon we want
icon_name: (str) the name of the icon
Copy link
Member

Choose a reason for hiding this comment

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

Currently only icon_set is an argument

Copy link
Member

@hansmannj hansmannj Aug 30, 2024

Choose a reason for hiding this comment

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

tbd @pakb @LukasJoss : Do we want to check the existence of keys in the dict for all languages here, or in the front-end?

Copy link
Member

Choose a reason for hiding this comment

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

Probably front-end could handle the case when a certain language is missing in the returned dict, by simply skipping the mouseover text in that case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We would probably need to define first which languages are required in the first place, but handling it on the front-end is probably fine

"""
# checking that the icon exists in the icon set's folder
path = find_descripton_file(icon_set)
if not os.path.isfile(path):
logger.error("The description dictionary doesn't exist: %s", path)
abort(404, "Description dictionary not found")
45 changes: 45 additions & 0 deletions app/helpers/description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json
import os

from app.settings import DESCRIPTION_FOLDER


def get_icon_set_description(icon_set=''):
'''
Return json containing the description in all available languages for all icons of the
provided icon set
'''
path = find_descripton_file(icon_set)
if not os.path.isfile(path):
return None

with open(path, encoding='utf-8') as f:
icon_set_descriptions = json.load(f)

return icon_set_descriptions


def get_icon_description(icon_name='', icon_set=''):
'''
Return json containing the description in all available languages for the specified icon of the
provided icon set
Copy link
Member

Choose a reason for hiding this comment

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

of the provided icon, not icon set

'''
path = find_descripton_file(icon_set)
if not os.path.isfile(path):
return None

with open(path, encoding='utf-8') as f:
df = json.load(f)

icon_description = df[icon_name]
return icon_description


def find_descripton_file(icon_set):
'''
Return file path of description file if it exists
Copy link
Member

Choose a reason for hiding this comment

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

please add or False in case it doen't

'''
path = os.path.abspath(os.path.join(DESCRIPTION_FOLDER, icon_set)) + '-dictionary.json'
if os.path.isfile(path):
return path
return False
4 changes: 3 additions & 1 deletion app/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from flask import url_for

from app.helpers.description import get_icon_description
from app.helpers.icons import get_icon_template_url
from app.helpers.url import get_base_url
from app.settings import DEFAULT_COLOR
Expand Down Expand Up @@ -97,5 +98,6 @@ def serialize(self):
"anchor": self.anchor,
"icon_set": self.icon_set.name,
"url": self.get_icon_url(),
"template_url": get_icon_template_url(get_base_url())
"template_url": get_icon_template_url(get_base_url()),
"description": get_icon_description(self.name, self.icon_set.name)
}
31 changes: 30 additions & 1 deletion app/icon_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from flask import url_for

from app.helpers.description import find_descripton_file
from app.helpers.description import get_icon_set_description
from app.helpers.icons import get_icon_set_template_url
from app.helpers.url import get_base_url
from app.icon import Icon
Expand Down Expand Up @@ -76,6 +78,19 @@ def get_icons_url(self):
"""
return url_for('icons_from_icon_set', icon_set_name=self.name, _external=True)

def get_icon_set_description_url(self):
"""
Generate and return the URL that will list the description in all available lanaguages
of all available icons of this icon set.

Returns:
the URL to the description in all available languages of all icons in this icon set if
it exists, otherwise return None
"""
if find_descripton_file(self.name):
return url_for('description_from_icon_set', icon_set_name=self.name, _external=True)
return None

def get_icon(self, icon_name):
"""
Generate and return the URL to access the metadata of one specific icon of this icon set
Expand Down Expand Up @@ -112,6 +127,19 @@ def get_all_icons(self):
icons.append(self.get_icon(name_without_extension))
return icons

def get_description(self):
"""
Generate a dictionary containing the description in all available languages of all icons
belonging to this icon set.

Returns:
A dictionary of all icon description in all available languages from this icon set it
it exists, otherwise return None
"""
if not self.is_valid():
return None
return get_icon_set_description(self.name)

def serialize(self):
"""
As we want to add "icons_url" to the __dict__, we can't really use a json.dumps to generate
Expand All @@ -126,5 +154,6 @@ def serialize(self):
"name": self.name,
"colorable": self.colorable,
"icons_url": self.get_icons_url(),
"template_url": get_icon_set_template_url(get_base_url())
"template_url": get_icon_set_template_url(get_base_url()),
"description_url": self.get_icon_set_description_url()
}
8 changes: 8 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from app import app
from app.helpers.check_functions import check_color_channels
from app.helpers.check_functions import check_if_descripton_file_exists
from app.helpers.check_functions import check_scale
from app.helpers.check_functions import get_and_check_icon
from app.helpers.check_functions import get_and_check_icon_set
Expand Down Expand Up @@ -58,6 +59,13 @@ def icons_from_icon_set(icon_set_name):
return make_api_compliant_response(icon_set.get_all_icons())


@app.route('/sets/<string:icon_set_name>/description', methods=['GET'])
def description_from_icon_set(icon_set_name):
icon_set = get_and_check_icon_set(icon_set_name)
check_if_descripton_file_exists(icon_set_name)
return make_api_compliant_response(icon_set.get_description())


@app.route('/sets/<string:icon_set_name>/icons/<string:icon_name>', methods=['GET'])
def icon_metadata(icon_set_name, icon_name):
icon_set = get_and_check_icon_set(icon_set_name)
Expand Down
3 changes: 3 additions & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
load_dotenv(ENV_FILE, override=True, verbose=True)

IMAGE_FOLDER = os.path.abspath(os.path.join(os.path.dirname(__file__), '../static/images/'))
DESCRIPTION_FOLDER = os.path.abspath(
os.path.join(os.path.dirname(__file__), '../metadata/description/')
)

COLORABLE_ICON_SETS = ['default']
DEFAULT_COLOR = {"r": '255', "g": '0', "b": '0'}
Expand Down
Loading