Skip to content

Commit

Permalink
fix: language order as we dump taxonomies (#512)
Browse files Browse the repository at this point in the history
Fix language order to match taxonomy linter preferred order.

Fixes: #511

---------

Co-authored-by: Pierre Slamich <pierre.slamich@gmail.com>
  • Loading branch information
alexgarel and teolemon authored Jul 17, 2024
1 parent d36b7d3 commit 2bc56e6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions parser/openfoodfacts_taxonomy_parser/unparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,30 @@ def list_tags_lc(self, node):
for property in node:
if property.startswith(key):
lc_list.append(property.split("_", dash_before_lc)[dash_before_lc])
lc_list.sort()
# we sort, but with a priority for xx and en language codes
priority = {"en": 1, "xx": 0}
lc_list.sort(key=lambda name: (priority.get(name[:2], 100), name))
return lc_list

def get_tags_line(self, node, lc):
"""return a string that should look like the original line"""
line = (", ").join(node["tags_" + lc])
return lc + ":" + line

@staticmethod
def property_sort_key(property):
name, lang_code, *_ = property.split("_", 2)
# give priority to xx and en language codes
priority = {"en": 1, "xx": 0}
return (name, priority.get(lang_code, 100), lang_code)

def list_property_and_lc(self, node):
"""return an ordered list of properties with their language code (lc)"""
# there is no rule for the order of properties
# properties will be arranged in alphabetical order
return sorted([property[5:] for property in node if property.startswith("prop_") and not property.endswith("_comments")])
values = [property[5:] for property in node if property.startswith("prop_") and not property.endswith("_comments")]
# note: using the fact that we are sure to find language code after the first underscore
return sorted(values, key=self.property_sort_key)

def get_property_line(self, node, property):
"""return a string that should look like the original property line"""
Expand Down

0 comments on commit 2bc56e6

Please sign in to comment.