1 |
On 11/07/2016 12:32 PM, Brian Dolbec wrote: |
2 |
> On Sun, 6 Nov 2016 14:18:12 -0800 |
3 |
> Zac Medico <zmedico@g.o> wrote: |
4 |
> |
5 |
>> Descriptions may exist for multiple languages, so prefer English |
6 |
>> language descriptions for use.local.desc content. |
7 |
>> |
8 |
>> X-Gentoo-Bug: 599060 |
9 |
>> X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=599060 |
10 |
>> --- |
11 |
>> pym/portage/xml/metadata.py | 28 +++++++++++++++++++++++++++- |
12 |
>> 1 file changed, 27 insertions(+), 1 deletion(-) |
13 |
>> |
14 |
>> [PATCH v3] sort languages by preference |
15 |
>> |
16 |
>> diff --git a/pym/portage/xml/metadata.py b/pym/portage/xml/metadata.py |
17 |
>> index 4940bfb..39aa738 100644 |
18 |
>> --- a/pym/portage/xml/metadata.py |
19 |
>> +++ b/pym/portage/xml/metadata.py |
20 |
>> @@ -61,7 +61,7 @@ except (ImportError, SystemError, RuntimeError, |
21 |
>> Exception): import re |
22 |
>> import xml.etree.ElementTree |
23 |
>> from portage import _encodings, _unicode_encode |
24 |
>> -from portage.util import unique_everseen |
25 |
>> +from portage.util import cmp_sort_key, unique_everseen |
26 |
>> |
27 |
>> if sys.hexversion >= 0x3000000: |
28 |
>> # pylint: disable=W0622 |
29 |
>> @@ -430,6 +430,19 @@ class MetaDataXML(object): |
30 |
>> maint_str = " ".join(maintainers) |
31 |
>> return maint_str |
32 |
>> |
33 |
>> +# lang with higher value is preferred |
34 |
>> +_lang_pref = { |
35 |
>> + "" : 0, |
36 |
>> + "en": 1, |
37 |
>> +} |
38 |
>> + |
39 |
>> + |
40 |
>> +def _cmp_lang(a, b): |
41 |
>> + a_score = _lang_pref.get(a.get("lang", ""), -1) |
42 |
>> + b_score = _lang_pref.get(b.get("lang", ""), -1) |
43 |
>> + |
44 |
>> + return a_score - b_score |
45 |
>> + |
46 |
>> |
47 |
>> def parse_metadata_use(xml_tree): |
48 |
>> """ |
49 |
>> @@ -443,6 +456,9 @@ def parse_metadata_use(xml_tree): |
50 |
>> if not usetags: |
51 |
>> return uselist |
52 |
>> |
53 |
>> + # Sort by language preference in descending order. |
54 |
>> + usetags.sort(key=cmp_sort_key(_cmp_lang), reverse=True) |
55 |
>> + |
56 |
>> # It's possible to have multiple 'use' elements. |
57 |
>> for usetag in usetags: |
58 |
>> flags = usetag.findall("flag") |
59 |
>> @@ -455,6 +471,16 @@ def parse_metadata_use(xml_tree): |
60 |
>> if pkg_flag is not None: |
61 |
>> flag_restrict = flag.get("restrict") |
62 |
>> |
63 |
>> + # Descriptions may exist for |
64 |
>> multiple languages, so |
65 |
>> + # ignore all except the first |
66 |
>> description found for a |
67 |
>> + # particular value of restrict (see |
68 |
>> bug 599060). |
69 |
>> + try: |
70 |
>> + |
71 |
>> uselist[pkg_flag][flag_restrict] |
72 |
>> + except KeyError: |
73 |
>> + pass |
74 |
>> + else: |
75 |
>> + continue |
76 |
>> + |
77 |
>> # emulate the Element.itertext() |
78 |
>> method from python-2.7 inner_text = [] |
79 |
>> stack = [] |
80 |
> |
81 |
> third time's the charm ;) LGTM |
82 |
> |
83 |
|
84 |
Thanks, pushed: |
85 |
|
86 |
https://gitweb.gentoo.org/proj/portage.git/commit/?id=db0dddfda68fbeed2dc6c48d9f8168fc2b42b8da |
87 |
-- |
88 |
Thanks, |
89 |
Zac |