Correct the assumed order of the name formats.

This commit corrects the ordering to the one in
Gramps 5.1 when run on Python 3.3.
Unlike the implementation in 5.1, this
implementation does not rely on dict ordering
and will therefore be consistent across Python
versions.

The assumed order of ascending positives followed
by negatives in reverse order, was based on tests
run on Python 3.7. However, the current ordering
in Gramps 5.1 is depended on dict ordering which
has changed between Python versions.
The expected ordering is therefore taken as the
one in Gramps 5.1 run on Python 3.3, which is
what the CI environment tests on.
This ordering is positives followed by negatives
both in ascending order.
I.e. -2, -3, -1,  0,  1,  2,  3,  4,  5
  =>  0,  1,  2,  3,  4,  5, -1, -2, -3
This commit is contained in:
Baizley 2020-01-26 17:42:58 +01:00 committed by Nick Hall
parent 3cb4115ae4
commit 080d1d1dca

View File

@ -60,6 +60,7 @@ Specific symbols for parts of a name are defined:
import re import re
import logging import logging
from functools import cmp_to_key from functools import cmp_to_key
from math import copysign
LOG = logging.getLogger(".gramps.gen") LOG = logging.getLogger(".gramps.gen")
@ -493,34 +494,32 @@ class NameDisplay:
only_custom=False, only_custom=False,
only_active=True): only_active=True):
""" """
Get a list of tuples (num, name,fmt_str,act) Returns a list of name formats as tuples on
the form (index, name,fmt_str,act).
The will contain standard formats followed
by custom formats both in ascending order on
their indices.
""" """
formats = [ custom_formats = sorted([
(index, name, format_string, active) (index, name, format_string, active)
for index, (name, format_string, active, *_) in self.name_formats.items() for index, (name, format_string, active, *_) in self.name_formats.items()
if (also_default or index) and if index < 0 and
(not only_custom or index < 0) and
(not only_active or active) (not only_active or active)
] ])
return self.sort_by_ascending_positives_followed_by_negatives_in_reverse_order(formats) if only_custom:
return custom_formats
standard_formats = sorted([
(index, name, format_string, active)
for index, (name, format_string, active, *_) in self.name_formats.items()
if index >= 0 and
(also_default or index) and
(not only_active or active)
])
def sort_by_ascending_positives_followed_by_negatives_in_reverse_order(self, formats): return standard_formats + custom_formats
"""
Sorts the formats with positive keys first,
and negative keys last.
The positives will be in ascending order and
the negatives in the reverse of their original order
in the iterable.
E.g. key ordering: -3, -1, -2, 1, 0, 2, 3 => 0, 1, 2, 3, -2, -1, -3
"""
def compare_function(t, k):
x, y = t[0], k[0]
return x - y if x >= 0 and y >= 0 else y
return sorted(formats, key=cmp_to_key(compare_function))
def _is_format_valid(self, num): def _is_format_valid(self, num):
try: try: