Skip to content

Commit

Permalink
[PY3] Add unicode_literals to salt.utils modules (R-S)
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalmage committed Jan 14, 2018
1 parent dcac4d4 commit 54f9785
Show file tree
Hide file tree
Showing 29 changed files with 265 additions and 225 deletions.
6 changes: 3 additions & 3 deletions salt/utils/raetevent.py
Expand Up @@ -7,7 +7,7 @@
# pylint: disable=3rd-party-module-not-gated

# Import python libs
from __future__ import absolute_import
from __future__ import absolute_import, print_function, unicode_literals
import os
import logging
import time
Expand Down Expand Up @@ -68,7 +68,7 @@ def __prep_stack(self, listen):
self.stack = transport.jobber_stack
else:
self.stack = transport.jobber_stack = self._setup_stack(ryn=self.ryn)
log.debug("RAETEvent Using Jobber Stack at = {0}\n".format(self.stack.ha))
log.debug("RAETEvent Using Jobber Stack at = %s\n", self.stack.ha)
if listen:
self.subscribe()

Expand Down Expand Up @@ -220,7 +220,7 @@ def fire_event(self, data, tag, timeout=1000):
identifier "tag"
'''
# Timeout is retained for compat with zeromq events
if not str(tag): # no empty tags allowed
if not six.text_type(tag): # no empty tags allowed
raise ValueError('Empty tag.')

if not isinstance(data, MutableMapping): # data must be dict
Expand Down
14 changes: 9 additions & 5 deletions salt/utils/raetlane.py
Expand Up @@ -49,7 +49,7 @@
raise ValueError("Timed out out waiting for response")
'''
# Import python libs
from __future__ import absolute_import
from __future__ import absolute_import, print_function, unicode_literals

try:
from raet import raeting, nacling
Expand Down Expand Up @@ -124,15 +124,19 @@ def _setup(opts, ryn='manor'):
sockdirpath=opts['sock_dir'])

lane_stack.Pk = raeting.PackKind.pack.value
log.debug("Created new LaneStack and local Yard named {0} at {1}\n"
"".format(lane_stack.name, lane_stack.ha))
log.debug(
'Created new LaneStack and local Yard named %s at %s\n',
lane_stack.name, lane_stack.ha
)
remote_yard = RemoteYard(stack=lane_stack,
name=ryn,
lanename=lanename,
dirpath=opts['sock_dir'])
lane_stack.addRemote(remote_yard)
log.debug("Added to LaneStack {0} remote Yard named {1} at {2}\n"
"".format(lane_stack.name, remote_yard.name, remote_yard.ha))
log.debug(
'Added to LaneStack %s remote Yard named %s at %s\n',
lane_stack.name, remote_yard.name, remote_yard.ha
)

def transmit(msg):
'''
Expand Down
46 changes: 18 additions & 28 deletions salt/utils/reactor.py
Expand Up @@ -5,7 +5,7 @@


# Import python libs
from __future__ import absolute_import
from __future__ import absolute_import, print_function, unicode_literals
import fnmatch
import glob
import logging
Expand Down Expand Up @@ -83,7 +83,7 @@ def render_reaction(self, glob_ref, tag, data):
glob_ref = self.minion.functions['cp.cache_file'](glob_ref) or ''
globbed_ref = glob.glob(glob_ref)
if not globbed_ref:
log.error('Can not render SLS {0} for tag {1}. File missing or not found.'.format(glob_ref, tag))
log.error('Can not render SLS %s for tag %s. File missing or not found.', glob_ref, tag)
for fn_ in globbed_ref:
try:
res = self.render_template(
Expand All @@ -98,32 +98,24 @@ def render_reaction(self, glob_ref, tag, data):

react.update(res)
except Exception:
log.error('Failed to render "{0}": '.format(fn_), exc_info=True)
log.exception('Failed to render "%s": ', fn_)
return react

def list_reactors(self, tag):
'''
Take in the tag from an event and return a list of the reactors to
process
'''
log.debug('Gathering reactors for tag {0}'.format(tag))
log.debug('Gathering reactors for tag %s', tag)
reactors = []
if isinstance(self.opts['reactor'], six.string_types):
try:
with salt.utils.files.fopen(self.opts['reactor']) as fp_:
react_map = salt.utils.yaml.safe_load(fp_)
except (OSError, IOError):
log.error(
'Failed to read reactor map: "{0}"'.format(
self.opts['reactor']
)
)
log.error('Failed to read reactor map: "%s"', self.opts['reactor'])
except Exception:
log.error(
'Failed to parse YAML in reactor map: "{0}"'.format(
self.opts['reactor']
)
)
log.error('Failed to parse YAML in reactor map: "%s"', self.opts['reactor'])
else:
react_map = self.opts['reactor']
for ropt in react_map:
Expand All @@ -145,22 +137,17 @@ def list_all(self):
Return a list of the reactors
'''
if isinstance(self.minion.opts['reactor'], six.string_types):
log.debug('Reading reactors from yaml {0}'.format(self.opts['reactor']))
log.debug('Reading reactors from yaml %s', self.opts['reactor'])
try:
with salt.utils.files.fopen(self.opts['reactor']) as fp_:
react_map = salt.utils.yaml.safe_load(fp_)
except (OSError, IOError):
log.error(
'Failed to read reactor map: "{0}"'.format(
self.opts['reactor']
)
)
log.error('Failed to read reactor map: "%s"', self.opts['reactor'])
except Exception:
log.error(
'Failed to parse YAML in reactor map: "{0}"'.format(
self.opts['reactor']
)
)
'Failed to parse YAML in reactor map: "%s"',
self.opts['reactor']
)
else:
log.debug('Not reading reactors from yaml')
react_map = self.minion.opts['reactor']
Expand Down Expand Up @@ -206,7 +193,7 @@ def reactions(self, tag, data, reactors):
'''
Render a list of reactor files and returns a reaction struct
'''
log.debug('Compiling reactions for tag {0}'.format(tag))
log.debug('Compiling reactions for tag %s', tag)
high = {}
chunks = []
try:
Expand All @@ -215,12 +202,15 @@ def reactions(self, tag, data, reactors):
if high:
errors = self.verify_high(high)
if errors:
log.error(('Unable to render reactions for event {0} due to '
'errors ({1}) in one or more of the sls files ({2})').format(tag, errors, reactors))
log.error(
'Unable to render reactions for event %s due to '
'errors (%s) in one or more of the sls files (%s)',
tag, errors, reactors
)
return [] # We'll return nothing since there was an error
chunks = self.order_chunks(self.compile_high_data(high))
except Exception as exc:
log.error('Exception trying to compile reactions: {0}'.format(exc), exc_info=True)
log.exception('Exception encountered while compiling reactions')

self.resolve_aliases(chunks)
return chunks
Expand Down
2 changes: 1 addition & 1 deletion salt/utils/reclass.py
Expand Up @@ -3,7 +3,7 @@
Common utility functions for the reclass adapters
http://reclass.pantsfullofunix.net
'''
from __future__ import absolute_import
from __future__ import absolute_import, print_function, unicode_literals

# Import python libs
import sys
Expand Down
2 changes: 1 addition & 1 deletion salt/utils/rsax931.py
Expand Up @@ -4,7 +4,7 @@
'''

# Import Python libs
from __future__ import absolute_import
from __future__ import absolute_import, print_function, unicode_literals
import glob
import sys
import os
Expand Down
29 changes: 16 additions & 13 deletions salt/utils/s3.py
Expand Up @@ -4,7 +4,7 @@
:depends: requests
'''
from __future__ import absolute_import
from __future__ import absolute_import, print_function, unicode_literals

# Import Python libs
import logging
Expand All @@ -23,6 +23,7 @@
import salt.utils.xmlutil as xml
from salt._compat import ElementTree as ET
from salt.exceptions import CommandExecutionError
from salt.ext import six

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -141,9 +142,9 @@ def query(key, keyid, method='GET', params=None, headers=None,
payload_hash=payload_hash,
)

log.debug('S3 Request: {0}'.format(requesturl))
log.debug('S3 Request: %s', requesturl)
log.debug('S3 Headers::')
log.debug(' Authorization: {0}'.format(headers['Authorization']))
log.debug(' Authorization: %s', headers['Authorization'])

if not data:
data = None
Expand Down Expand Up @@ -180,20 +181,22 @@ def query(key, keyid, method='GET', params=None, headers=None,
if result.status_code >= 400:
# On error the S3 API response should contain error message
err_text = result.content or 'Unknown error'
log.debug(' Response content: {0}'.format(err_text))
log.debug(' Response content: %s', err_text)

# Try to get err info from response xml
try:
err_data = xml.to_dict(ET.fromstring(err_text))
err_code = err_data['Code']
err_msg = err_data['Message']
except (KeyError, ET.ParseError) as err:
log.debug('Failed to parse s3 err response. {0}: {1}'.format(
type(err).__name__, err))
log.debug(
'Failed to parse s3 err response. %s: %s',
type(err).__name__, err
)
err_code = 'http-{0}'.format(result.status_code)
err_msg = err_text

log.debug('S3 Response Status Code: {0}'.format(result.status_code))
log.debug('S3 Response Status Code: %s', result.status_code)

if method == 'PUT':
if result.status_code != 200:
Expand All @@ -206,13 +209,13 @@ def query(key, keyid, method='GET', params=None, headers=None,
bucket, err_code, err_msg))

if local_file:
log.debug('Uploaded from {0} to {1}'.format(local_file, path))
log.debug('Uploaded from %s to %s', local_file, path)
else:
log.debug('Created bucket {0}'.format(bucket))
log.debug('Created bucket %s', bucket)
return

if method == 'DELETE':
if not str(result.status_code).startswith('2'):
if not six.text_type(result.status_code).startswith('2'):
if path:
raise CommandExecutionError(
'Failed to delete {0} from bucket {1}. {2}: {3}'.format(
Expand All @@ -222,9 +225,9 @@ def query(key, keyid, method='GET', params=None, headers=None,
bucket, err_code, err_msg))

if path:
log.debug('Deleted {0} from bucket {1}'.format(path, bucket))
log.debug('Deleted %s from bucket %s', path, bucket)
else:
log.debug('Deleted bucket {0}'.format(bucket))
log.debug('Deleted bucket %s', bucket)
return

# This can be used to save a binary object to disk
Expand All @@ -233,7 +236,7 @@ def query(key, keyid, method='GET', params=None, headers=None,
raise CommandExecutionError(
'Failed to get file. {0}: {1}'.format(err_code, err_msg))

log.debug('Saving to local file: {0}'.format(local_file))
log.debug('Saving to local file: %s', local_file)
with salt.utils.files.fopen(local_file, 'wb') as out:
for chunk in result.iter_content(chunk_size=chunk_size):
out.write(chunk)
Expand Down
16 changes: 8 additions & 8 deletions salt/utils/saltclass.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Import Python libs
from __future__ import absolute_import
from __future__ import absolute_import, print_function, unicode_literals
import os
import re
import logging
Expand All @@ -11,7 +11,7 @@
import salt.utils.yaml

# Import 3rd-party libs
from salt.ext.six import iteritems
from salt.ext import six

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,7 +59,7 @@ def get_class(_class, salt_data):
if sub_init in l_files:
return render_yaml(sub_init, salt_data)

log.warning('{0}: Class definition not found'.format(_class))
log.warning('%s: Class definition not found', _class)
return {}


Expand All @@ -86,7 +86,7 @@ def dict_merge(a, b, path=None):
else:
a[key].extend(b[key])
elif isinstance(a[key], dict) and isinstance(b[key], dict):
dict_merge(a[key], b[key], path + [str(key)])
dict_merge(a[key], b[key], path + [six.text_type(key)])
elif a[key] == b[key]:
pass
else:
Expand All @@ -98,7 +98,7 @@ def dict_merge(a, b, path=None):

# Recursive search and replace in a dict
def dict_search_and_replace(d, old, new, expanded):
for (k, v) in iteritems(d):
for (k, v) in six.iteritems(d):
if isinstance(v, dict):
dict_search_and_replace(d[k], old, new, expanded)
if v == old:
Expand All @@ -125,9 +125,9 @@ def expand_variables(a, b, expanded, path=None):
b = a.copy()
path = []

for (k, v) in iteritems(a):
for (k, v) in six.iteritems(a):
if isinstance(v, dict):
expand_variables(v, b, expanded, path + [str(k)])
expand_variables(v, b, expanded, path + [six.text_type(k)])
else:
if isinstance(v, str):
vre = re.search(r'(^|.)\$\{.*?\}', v)
Expand Down Expand Up @@ -230,7 +230,7 @@ def expanded_dict_from_minion(minion_id, salt_data):
if _file:
node_dict[minion_id] = render_yaml(_file, salt_data)
else:
log.warning('{0}: Node definition not found'.format(minion_id))
log.warning('%s: Node definition not found', minion_id)
node_dict[minion_id] = {}

# Get 2 ordered lists:
Expand Down
6 changes: 3 additions & 3 deletions salt/utils/sanitizers.py
Expand Up @@ -15,12 +15,12 @@
# limitations under the License.

# Import Python libs
from __future__ import absolute_import
from __future__ import absolute_import, print_function, unicode_literals
import re
import os.path

# Import Salt libs
from salt.ext.six import text_type as text
from salt.ext import six
from salt.exceptions import CommandExecutionError


Expand All @@ -36,7 +36,7 @@ def trim(value):
if not value:
raise CommandExecutionError("Empty value during sanitation")

return text(value)
return six.text_type(value)

@staticmethod
def filename(value):
Expand Down

0 comments on commit 54f9785

Please sign in to comment.