Add global "locale" config option and make key-ordering rule locale-aware
Support sorting by locale with strcoll(). Properly handle case and accents. Note: this is a second implementation, for context see: https://github.com/adrienverge/yamllint/pull/280 https://github.com/adrienverge/yamllint/issues/285 https://github.com/adrienverge/yamllint/pull/288
This commit is contained in:
@@ -18,6 +18,7 @@ from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import io
|
||||
import locale
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
@@ -175,6 +176,9 @@ def run(argv=None):
|
||||
print(e, file=sys.stderr)
|
||||
sys.exit(-1)
|
||||
|
||||
if conf.locale is not None:
|
||||
locale.setlocale(locale.LC_ALL, conf.locale)
|
||||
|
||||
max_level = 0
|
||||
|
||||
for file in find_files_recursively(args.files, conf):
|
||||
|
||||
@@ -35,6 +35,8 @@ class YamlLintConfig(object):
|
||||
self.yaml_files = pathspec.PathSpec.from_lines(
|
||||
'gitwildmatch', ['*.yaml', '*.yml', '.yamllint'])
|
||||
|
||||
self.locale = None
|
||||
|
||||
if file is not None:
|
||||
with open(file) as f:
|
||||
content = f.read()
|
||||
@@ -111,6 +113,12 @@ class YamlLintConfig(object):
|
||||
self.yaml_files = pathspec.PathSpec.from_lines('gitwildmatch',
|
||||
conf['yaml-files'])
|
||||
|
||||
if 'locale' in conf:
|
||||
if not isinstance(conf['locale'], str):
|
||||
raise YamlLintConfigError(
|
||||
'invalid config: locale should be a string')
|
||||
self.locale = conf['locale']
|
||||
|
||||
def validate(self):
|
||||
for id in self.rules:
|
||||
try:
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
|
||||
"""
|
||||
Use this rule to enforce alphabetical ordering of keys in mappings. The sorting
|
||||
order uses the Unicode code point number. As a result, the ordering is
|
||||
case-sensitive and not accent-friendly (see examples below).
|
||||
order uses the Unicode code point number as a default. As a result, the
|
||||
ordering is case-sensitive and not accent-friendly (see examples below).
|
||||
This can be changed by setting the global ``locale`` option. This allows to
|
||||
sort case and accents properly.
|
||||
|
||||
.. rubric:: Examples
|
||||
|
||||
@@ -63,8 +65,24 @@ case-sensitive and not accent-friendly (see examples below).
|
||||
|
||||
- haïr: true
|
||||
hais: true
|
||||
|
||||
#. With global option ``locale: "en_US.UTF-8"`` and rule ``key-ordering: {}``
|
||||
|
||||
as opposed to before, the following code snippet would now **PASS**:
|
||||
::
|
||||
|
||||
- t-shirt: 1
|
||||
T-shirt: 2
|
||||
t-shirts: 3
|
||||
T-shirts: 4
|
||||
- hair: true
|
||||
haïr: true
|
||||
hais: true
|
||||
haïssable: true
|
||||
"""
|
||||
|
||||
from locale import strcoll
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint.linter import LintProblem
|
||||
@@ -101,7 +119,8 @@ def check(conf, token, prev, next, nextnext, context):
|
||||
# This check is done because KeyTokens can be found inside flow
|
||||
# sequences... strange, but allowed.
|
||||
if len(context['stack']) > 0 and context['stack'][-1].type == MAP:
|
||||
if any(next.value < key for key in context['stack'][-1].keys):
|
||||
if any(strcoll(next.value, key) < 0
|
||||
for key in context['stack'][-1].keys):
|
||||
yield LintProblem(
|
||||
next.start_mark.line + 1, next.start_mark.column + 1,
|
||||
'wrong ordering of key "%s" in mapping' % next.value)
|
||||
|
||||
Reference in New Issue
Block a user