Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate basestring to a python3 compatible type #17199

Merged
merged 1 commit into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CODING_GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ To test if something is a string, consider that it may be unicode.
if type(x) == str:

# yes
if isinstance(x, basestring):
from ansible.compat.six import string_types
if isinstance(x, string_types):

Cleverness
==========
Expand Down
3 changes: 2 additions & 1 deletion contrib/inventory/nsot.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
from pynsot.app import HttpServerError
from click.exceptions import UsageError

from six import string_types

def warning(*objs):
print("WARNING: ", *objs, file=sys.stderr)
Expand Down Expand Up @@ -251,7 +252,7 @@ def _inventory_group(self, group, contents):
obj[group]['hosts'] = []
obj[group]['vars'] = hostvars
try:
assert isinstance(query, basestring)
assert isinstance(query, string_types)
except:
sys.exit('ERR: Group queries must be a single string\n'
' Group: %s\n'
Expand Down
6 changes: 3 additions & 3 deletions contrib/inventory/vmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import time
import ConfigParser

from six import text_type
from six import text_type, string_types

# Disable logging message trigged by pSphere/suds.
try:
Expand Down Expand Up @@ -160,7 +160,7 @@ def _flatten_dict(self, d, parent_key='', sep='_'):
if isinstance(v, collections.MutableMapping):
items.extend(self._flatten_dict(v, new_key, sep).items())
elif isinstance(v, (list, tuple)):
if all([isinstance(x, basestring) for x in v]):
if all([isinstance(x, string_types) for x in v]):
items.append((new_key, v))
else:
items.append((new_key, v))
Expand Down Expand Up @@ -208,7 +208,7 @@ def _get_obj_info(self, obj, depth=99, seen=None):
if obj_info != ():
l.append(obj_info)
return l
elif isinstance(obj, (type(None), bool, int, long, float, basestring)):
elif isinstance(obj, (type(None), bool, int, long, float, string_types)):
return obj
else:
return ()
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/cli/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import traceback
import textwrap

from ansible.compat.six import iteritems
from ansible.compat.six import iteritems, string_types

from ansible import constants as C
from ansible.errors import AnsibleError, AnsibleOptionsError
Expand Down Expand Up @@ -309,13 +309,13 @@ def get_man_text(self, doc):

maintainers = set()
if 'author' in doc:
if isinstance(doc['author'], basestring):
if isinstance(doc['author'], string_types):
maintainers.add(doc['author'])
else:
maintainers.update(doc['author'])

if 'maintainers' in doc:
if isinstance(doc['maintainers'], basestring):
if isinstance(doc['maintainers'], string_types):
maintainers.add(doc['author'])
else:
maintainers.update(doc['author'])
Expand Down
20 changes: 11 additions & 9 deletions lib/ansible/galaxy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from urllib2 import quote as urlquote, HTTPError

import ansible.constants as C
from ansible.compat.six import string_types
from ansible.errors import AnsibleError
from ansible.module_utils.urls import open_url
from ansible.galaxy.token import GalaxyToken
Expand All @@ -41,22 +42,24 @@
from ansible.utils.display import Display
display = Display()


def g_connect(method):
''' wrapper to lazily initialize connection info to galaxy '''
def wrapped(self, *args, **kwargs):
if not self.initialized:
display.vvvv("Initial connection to galaxy_server: %s" % self._api_server)
server_version = self._get_server_api_version()
if not server_version in self.SUPPORTED_VERSIONS:
if server_version not in self.SUPPORTED_VERSIONS:
raise AnsibleError("Unsupported Galaxy server API version: %s" % server_version)

self.baseurl = '%s/api/%s' % (self._api_server, server_version)
self.version = server_version # for future use
self.version = server_version # for future use
display.vvvv("Base API: %s" % self.baseurl)
self.initialized = True
return method(self, *args, **kwargs)
return wrapped


class GalaxyAPI(object):
''' This class is meant to be used as a API client for an Ansible Galaxy server '''

Expand All @@ -77,7 +80,6 @@ def __init__(self, galaxy):
if galaxy.options.api_server != C.GALAXY_SERVER:
self._api_server = galaxy.options.api_server


def __auth_header(self):
token = self.token.get()
if token is None:
Expand Down Expand Up @@ -112,7 +114,7 @@ def _get_server_api_version(self):
"""
url = '%s/api/' % self._api_server
try:
return_data =open_url(url, validate_certs=self._validate_certs)
return_data = open_url(url, validate_certs=self._validate_certs)
except Exception as e:
raise AnsibleError("Failed to get data from the API server (%s): %s " % (url, to_str(e)))

Expand All @@ -121,7 +123,7 @@ def _get_server_api_version(self):
except Exception as e:
raise AnsibleError("Could not process data from the API server (%s): %s " % (url, to_str(e)))

if not 'current_version' in data:
if 'current_version' not in data:
raise AnsibleError("missing required 'current_version' from server response (%s)" % url)

return data['current_version']
Expand Down Expand Up @@ -159,9 +161,9 @@ def get_import_task(self, task_id=None, github_user=None, github_repo=None):
Check the status of an import task.
"""
url = '%s/imports/' % self.baseurl
if not task_id is None:
if task_id is not None:
url = "%s?id=%d" % (url,task_id)
elif not github_user is None and not github_repo is None:
elif github_user is not None and github_repo is not None:
url = "%s?github_user=%s&github_repo=%s" % (url,github_user,github_repo)
else:
raise AnsibleError("Expected task_id or github_user and github_repo")
Expand Down Expand Up @@ -249,11 +251,11 @@ def search_roles(self, search, **kwargs):
page_size = kwargs.get('page_size', None)
author = kwargs.get('author', None)

if tags and isinstance(tags, basestring):
if tags and isinstance(tags, string_types):
tags = tags.split(',')
search_url += '&tags_autocomplete=' + '+'.join(tags)

if platforms and isinstance(platforms, basestring):
if platforms and isinstance(platforms, string_types):
platforms = platforms.split(',')
search_url += '&platforms_autocomplete=' + '+'.join(platforms)

Expand Down
48 changes: 3 additions & 45 deletions lib/ansible/module_utils/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,54 +133,10 @@
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])
Expand Down Expand Up @@ -213,6 +169,8 @@ def _convert(node):
# Python 3
basestring = string_types

_literal_eval = literal_eval

# End of deprecated names

# Internal global holding passed in params. This is consulted in case
Expand Down
5 changes: 3 additions & 2 deletions lib/ansible/module_utils/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
except:
HAS_LOOSE_VERSION = False

from ansible.module_utils.six import string_types

class AnsibleAWSError(Exception):
pass
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -438,7 +439,7 @@ def get_sg_id(sg, boto3):

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
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/module_utils/junos.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/module_utils/netcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import itertools

from ansible.module_utils.basic import BOOLEANS_TRUE, BOOLEANS_FALSE
from ansible.module_utils.six import string_types

DEFAULT_COMMENT_TOKENS = ['#', '!', '/*', '*/']

Expand Down Expand Up @@ -197,7 +198,7 @@ def load_from_file(self, filename):
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]:
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/module_utils/netcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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)):
Expand Down Expand Up @@ -75,7 +76,7 @@ def to_command(self, command, output=None, prompt=None, response=None):
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)

Expand Down
44 changes: 44 additions & 0 deletions lib/ansible/module_utils/pycompat24.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
3 changes: 2 additions & 1 deletion lib/ansible/module_utils/rax.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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',
Expand All @@ -44,7 +45,7 @@
'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"

Expand Down
4 changes: 3 additions & 1 deletion lib/ansible/module_utils/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
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.
Expand Down Expand Up @@ -112,7 +114,7 @@ def execute_command(self, cmd, daemonize=False):
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 = ""
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/playbook/taggable.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self):
def _load_tags(self, attr, ds):
if isinstance(ds, list):
return ds
elif isinstance(ds, basestring):
elif isinstance(ds, string_types):
value = ds.split(',')
if isinstance(value, list):
return [ x.strip() for x in value ]
Expand Down