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