mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
Migrate basestring to a python3 compatible type (#17199)
This commit is contained in:
@@ -133,54 +133,10 @@ except ImportError:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
# Python 2.6+
|
||||
from ast import literal_eval
|
||||
except ImportError:
|
||||
# a replacement for literal_eval that works with python 2.4. from:
|
||||
# https://mail.python.org/pipermail/python-list/2009-September/551880.html
|
||||
# which is essentially a cut/paste from an earlier (2.6) version of python's
|
||||
# ast.py
|
||||
from compiler import ast, parse
|
||||
|
||||
def literal_eval(node_or_string):
|
||||
"""
|
||||
Safely evaluate an expression node or a string containing a Python
|
||||
expression. The string or node provided may only consist of the following
|
||||
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
|
||||
and None.
|
||||
"""
|
||||
_safe_names = {'None': None, 'True': True, 'False': False}
|
||||
# Okay to use basestring and long here because this is only for
|
||||
# python 2.4 and 2.5
|
||||
if isinstance(node_or_string, basestring):
|
||||
node_or_string = parse(node_or_string, mode='eval')
|
||||
if isinstance(node_or_string, ast.Expression):
|
||||
node_or_string = node_or_string.node
|
||||
|
||||
def _convert(node):
|
||||
if isinstance(node, ast.Const) and isinstance(node.value, (basestring, int, float, long, complex)):
|
||||
return node.value
|
||||
elif isinstance(node, ast.Tuple):
|
||||
return tuple(map(_convert, node.nodes))
|
||||
elif isinstance(node, ast.List):
|
||||
return list(map(_convert, node.nodes))
|
||||
elif isinstance(node, ast.Dict):
|
||||
return dict((_convert(k), _convert(v)) for k, v in node.items())
|
||||
elif isinstance(node, ast.Name):
|
||||
if node.name in _safe_names:
|
||||
return _safe_names[node.name]
|
||||
elif isinstance(node, ast.UnarySub):
|
||||
return -_convert(node.expr)
|
||||
raise ValueError('malformed string')
|
||||
return _convert(node_or_string)
|
||||
|
||||
_literal_eval = literal_eval
|
||||
|
||||
from ansible.module_utils.pycompat24 import get_exception, literal_eval
|
||||
from ansible.module_utils.six import (PY2, PY3, b, binary_type, integer_types,
|
||||
iteritems, text_type, string_types)
|
||||
from ansible.module_utils.six.moves import map, reduce
|
||||
from ansible.module_utils.pycompat24 import get_exception
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
_NUMBERTYPES = tuple(list(integer_types) + [float])
|
||||
@@ -213,6 +169,8 @@ except NameError:
|
||||
# Python 3
|
||||
basestring = string_types
|
||||
|
||||
_literal_eval = literal_eval
|
||||
|
||||
# End of deprecated names
|
||||
|
||||
# Internal global holding passed in params. This is consulted in case
|
||||
|
||||
@@ -49,6 +49,7 @@ try:
|
||||
except:
|
||||
HAS_LOOSE_VERSION = False
|
||||
|
||||
from ansible.module_utils.six import string_types
|
||||
|
||||
class AnsibleAWSError(Exception):
|
||||
pass
|
||||
@@ -343,7 +344,7 @@ def ansible_dict_to_boto3_filter_list(filters_dict):
|
||||
filters_list = []
|
||||
for k,v in filters_dict.iteritems():
|
||||
filter_dict = {'Name': k}
|
||||
if isinstance(v, basestring):
|
||||
if isinstance(v, string_types):
|
||||
filter_dict['Values'] = [v]
|
||||
else:
|
||||
filter_dict['Values'] = v
|
||||
@@ -438,7 +439,7 @@ def get_ec2_security_group_ids_from_names(sec_group_list, ec2_connection, vpc_id
|
||||
|
||||
sec_group_id_list = []
|
||||
|
||||
if isinstance(sec_group_list, basestring):
|
||||
if isinstance(sec_group_list, string_types):
|
||||
sec_group_list = [sec_group_list]
|
||||
|
||||
# Get all security groups
|
||||
|
||||
@@ -22,6 +22,7 @@ from distutils.version import LooseVersion
|
||||
from ansible.module_utils.basic import AnsibleModule, env_fallback, get_exception
|
||||
from ansible.module_utils.shell import Shell, ShellError, HAS_PARAMIKO
|
||||
from ansible.module_utils.netcfg import parse
|
||||
from ansible.module_utils.six import string_types
|
||||
|
||||
try:
|
||||
from jnpr.junos import Device
|
||||
@@ -70,7 +71,7 @@ def to_list(val):
|
||||
|
||||
|
||||
def xml_to_json(val):
|
||||
if isinstance(val, basestring):
|
||||
if isinstance(val, string_types):
|
||||
return jxmlease.parse(val)
|
||||
else:
|
||||
return jxmlease.parse_etree(val)
|
||||
|
||||
@@ -33,6 +33,7 @@ import shlex
|
||||
import itertools
|
||||
|
||||
from ansible.module_utils.basic import BOOLEANS_TRUE, BOOLEANS_FALSE
|
||||
from ansible.module_utils.six import string_types
|
||||
|
||||
DEFAULT_COMMENT_TOKENS = ['#', '!', '/*', '*/']
|
||||
|
||||
@@ -197,7 +198,7 @@ class NetworkConfig(object):
|
||||
self.load(open(filename).read())
|
||||
|
||||
def get(self, path):
|
||||
if isinstance(path, basestring):
|
||||
if isinstance(path, string_types):
|
||||
path = [path]
|
||||
for item in self._config:
|
||||
if item.text == path[-1]:
|
||||
|
||||
@@ -32,6 +32,7 @@ import itertools
|
||||
import shlex
|
||||
|
||||
from ansible.module_utils.basic import BOOLEANS_TRUE, BOOLEANS_FALSE
|
||||
from ansible.module_utils.six import string_types
|
||||
|
||||
def to_list(val):
|
||||
if isinstance(val, (list, tuple)):
|
||||
@@ -75,7 +76,7 @@ class Cli(object):
|
||||
elif isinstance(command, dict):
|
||||
output = cmd.get('output') or output
|
||||
cmd = cmd['command']
|
||||
if isinstance(prompt, basestring):
|
||||
if isinstance(prompt, string_types):
|
||||
prompt = re.compile(re.escape(prompt))
|
||||
return Command(command, output, prompt=prompt, response=response)
|
||||
|
||||
|
||||
@@ -42,3 +42,47 @@ def get_exception():
|
||||
|
||||
"""
|
||||
return sys.exc_info()[1]
|
||||
|
||||
try:
|
||||
# Python 2.6+
|
||||
from ast import literal_eval
|
||||
except ImportError:
|
||||
# a replacement for literal_eval that works with python 2.4. from:
|
||||
# https://mail.python.org/pipermail/python-list/2009-September/551880.html
|
||||
# which is essentially a cut/paste from an earlier (2.6) version of python's
|
||||
# ast.py
|
||||
from compiler import ast, parse
|
||||
from ansible.module_utils.six import binary_type, string_types, text_type
|
||||
|
||||
def literal_eval(node_or_string):
|
||||
"""
|
||||
Safely evaluate an expression node or a string containing a Python
|
||||
expression. The string or node provided may only consist of the following
|
||||
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
|
||||
and None.
|
||||
"""
|
||||
_safe_names = {'None': None, 'True': True, 'False': False}
|
||||
if isinstance(node_or_string, string_types):
|
||||
node_or_string = parse(node_or_string, mode='eval')
|
||||
if isinstance(node_or_string, ast.Expression):
|
||||
node_or_string = node_or_string.node
|
||||
|
||||
def _convert(node):
|
||||
# Okay to use long here because this is only for python 2.4 and 2.5
|
||||
if isinstance(node, ast.Const) and isinstance(node.value, (text_type, binary_type, int, float, long, complex)):
|
||||
return node.value
|
||||
elif isinstance(node, ast.Tuple):
|
||||
return tuple(map(_convert, node.nodes))
|
||||
elif isinstance(node, ast.List):
|
||||
return list(map(_convert, node.nodes))
|
||||
elif isinstance(node, ast.Dict):
|
||||
return dict((_convert(k), _convert(v)) for k, v in node.items())
|
||||
elif isinstance(node, ast.Name):
|
||||
if node.name in _safe_names:
|
||||
return _safe_names[node.name]
|
||||
elif isinstance(node, ast.UnarySub):
|
||||
return -_convert(node.expr)
|
||||
raise ValueError('malformed string')
|
||||
return _convert(node_or_string)
|
||||
|
||||
__all__ = ('get_exception', 'literal_eval')
|
||||
|
||||
@@ -33,6 +33,7 @@ import re
|
||||
from uuid import UUID
|
||||
|
||||
from ansible.module_utils.basic import BOOLEANS
|
||||
from ansible.module_utils.six import text_type, binary_type
|
||||
|
||||
FINAL_STATUSES = ('ACTIVE', 'ERROR')
|
||||
VOLUME_STATUS = ('available', 'attaching', 'creating', 'deleting', 'in-use',
|
||||
@@ -44,7 +45,7 @@ CLB_PROTOCOLS = ['DNS_TCP', 'DNS_UDP', 'FTP', 'HTTP', 'HTTPS', 'IMAPS',
|
||||
'IMAPv4', 'LDAP', 'LDAPS', 'MYSQL', 'POP3', 'POP3S', 'SMTP',
|
||||
'TCP', 'TCP_CLIENT_FIRST', 'UDP', 'UDP_STREAM', 'SFTP']
|
||||
|
||||
NON_CALLABLES = (basestring, bool, dict, int, list, type(None))
|
||||
NON_CALLABLES = (text_type, binary_type, bool, dict, int, list, type(None))
|
||||
PUBLIC_NET_ID = "00000000-0000-0000-0000-000000000000"
|
||||
SERVICE_NET_ID = "11111111-1111-1111-1111-111111111111"
|
||||
|
||||
|
||||
@@ -34,6 +34,8 @@ import select
|
||||
import subprocess
|
||||
import json
|
||||
|
||||
from ansible.module_utils.six import text_type, binary_type
|
||||
|
||||
class Service(object):
|
||||
"""
|
||||
This is the generic Service manipulation class that is subclassed based on system.
|
||||
@@ -112,7 +114,7 @@ class Service(object):
|
||||
os._exit(0)
|
||||
|
||||
# Start the command
|
||||
if isinstance(cmd, basestring):
|
||||
if isinstance(cmd, (text_type, binary_type)):
|
||||
cmd = shlex.split(cmd)
|
||||
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=lambda: os.close(pipe[1]))
|
||||
stdout = ""
|
||||
|
||||
Reference in New Issue
Block a user