mirror of https://github.com/ansible/ansible.git
Try to get best usable locale (#75033)
specially for when you have parameters in unicode but need to scrape responses, C is still the fallback Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>pull/75078/head
parent
f05bcf5693
commit
415e08c297
@ -0,0 +1,2 @@
|
||||
minor_changes:
|
||||
- added new function to module utils to choose best possible locale.
|
@ -0,0 +1,49 @@
|
||||
# Copyright (c), Ansible Project
|
||||
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
|
||||
def get_best_parsable_locale(module, preferences=None):
|
||||
'''
|
||||
Attempts to return the best possible locale for parsing output in English
|
||||
useful for scraping output with i18n tools. When this raises an exception
|
||||
and the caller wants to continue, it should use the 'C' locale.
|
||||
|
||||
:param module: an AnsibleModule instance
|
||||
:param preferences: A list of preferred locales, in order of preference
|
||||
:returns: The first matched preferred locale or 'C' which is the default
|
||||
'''
|
||||
|
||||
locale = module.get_bin_path("locale")
|
||||
if not locale:
|
||||
# not using required=true as that forces fail_json
|
||||
raise RuntimeWarning("Could not find 'locale' tool")
|
||||
|
||||
available = []
|
||||
found = 'C' # default posix, its ascii but always there
|
||||
|
||||
if preferences is None:
|
||||
# new POSIX standard or English cause those are messages core team expects
|
||||
# yes, the last 2 are the same but some systems are weird
|
||||
preferences = ['C.utf8', 'en_US.utf8', 'C', 'POSIX']
|
||||
|
||||
rc, out, err = module.run_command([locale, '-a'])
|
||||
|
||||
if rc == 0:
|
||||
if out:
|
||||
available = out.strip().splitlines()
|
||||
else:
|
||||
raise RuntimeWarning("No output from locale, rc=%s: %s" % (rc, to_native(err)))
|
||||
else:
|
||||
raise RuntimeWarning("Unable to get locale information, rc=%s: %s" % (rc, to_native(err)))
|
||||
|
||||
if available:
|
||||
for pref in preferences:
|
||||
if pref in available:
|
||||
found = pref
|
||||
break
|
||||
return found
|
@ -0,0 +1,42 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
from units.compat.mock import MagicMock
|
||||
|
||||
from ansible.module_utils.common.locale import get_best_parsable_locale
|
||||
|
||||
|
||||
class TestLocale:
|
||||
"""Tests for get_best_paresable_locale"""
|
||||
|
||||
mock_module = MagicMock()
|
||||
mock_module.get_bin_path = MagicMock(return_value='/usr/bin/locale')
|
||||
|
||||
def test_finding_best(self):
|
||||
self.mock_module.run_command = MagicMock(return_value=(0, "C.utf8\nen_US.utf8\nC\nPOSIX\n", ''))
|
||||
locale = get_best_parsable_locale(self.mock_module)
|
||||
assert locale == 'C.utf8'
|
||||
|
||||
def test_finding_last(self):
|
||||
self.mock_module.run_command = MagicMock(return_value=(0, "fr_FR.utf8\nen_UK.utf8\nC\nPOSIX\n", ''))
|
||||
locale = get_best_parsable_locale(self.mock_module)
|
||||
assert locale == 'C'
|
||||
|
||||
def test_finding_middle(self):
|
||||
self.mock_module.run_command = MagicMock(return_value=(0, "fr_FR.utf8\nen_US.utf8\nC\nPOSIX\n", ''))
|
||||
locale = get_best_parsable_locale(self.mock_module)
|
||||
assert locale == 'en_US.utf8'
|
||||
|
||||
def test_finding_prefered(self):
|
||||
self.mock_module.run_command = MagicMock(return_value=(0, "es_ES.utf8\nMINE\nC\nPOSIX\n", ''))
|
||||
locale = get_best_parsable_locale(self.mock_module, preferences=['MINE', 'C.utf8'])
|
||||
assert locale == 'MINE'
|
||||
|
||||
def test_finding_C_on_no_match(self):
|
||||
self.mock_module.run_command = MagicMock(return_value=(0, "fr_FR.UTF8\nMINE\n", ''))
|
||||
locale = get_best_parsable_locale(self.mock_module)
|
||||
assert locale == 'C'
|
Loading…
Reference in New Issue