Text's .translate() is easier to use than bytes
Text strings and byte strings both have a translate method but the byte string version is harder to use. It requires a mapping of all 256 bytes to a translation value. Text strings only require a mapping from the characters that are changing to the new string. Switching to text strings on both py2 and py3 allows us to state what we're getting rid of simply without having to rely on the maketrans() helper function.
This commit is contained in:
parent
ecb7f13119
commit
ee14e0cc2a
1 changed files with 2 additions and 9 deletions
|
@ -37,13 +37,6 @@ from ansible.module_utils.six import PY3, iteritems
|
||||||
from ansible.module_utils.six.moves import configparser, StringIO
|
from ansible.module_utils.six.moves import configparser, StringIO
|
||||||
from ansible.module_utils._text import to_native
|
from ansible.module_utils._text import to_native
|
||||||
|
|
||||||
try:
|
|
||||||
# python2
|
|
||||||
from string import maketrans
|
|
||||||
except ImportError:
|
|
||||||
# python3
|
|
||||||
maketrans = str.maketrans # TODO: is this really identical?
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import selinux
|
import selinux
|
||||||
HAVE_SELINUX=True
|
HAVE_SELINUX=True
|
||||||
|
@ -1647,8 +1640,8 @@ class OpenBSDHardware(Hardware):
|
||||||
# total: 69268k bytes allocated = 0k used, 69268k available
|
# total: 69268k bytes allocated = 0k used, 69268k available
|
||||||
rc, out, err = self.module.run_command("/sbin/swapctl -sk")
|
rc, out, err = self.module.run_command("/sbin/swapctl -sk")
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
swaptrans = maketrans('kmg', ' ')
|
swaptrans = { ord(u'k'): None, ord(u'm'): None, ord(u'g'): None}
|
||||||
data = out.split()
|
data = to_text(out, errors='surrogate_or_strict').split()
|
||||||
self.facts['swapfree_mb'] = int(data[-2].translate(swaptrans)) // 1024
|
self.facts['swapfree_mb'] = int(data[-2].translate(swaptrans)) // 1024
|
||||||
self.facts['swaptotal_mb'] = int(data[1].translate(swaptrans)) // 1024
|
self.facts['swaptotal_mb'] = int(data[1].translate(swaptrans)) // 1024
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue