Unify string formatting

Use one way of string formatting: replace old `%` style with new `str.format`.

Resolve .
This commit is contained in:
Darko Poljak 2021-03-30 07:56:38 +02:00
parent f984a918b9
commit 4c2d273f07
17 changed files with 67 additions and 65 deletions

View file

@ -190,7 +190,7 @@ class Config:
fd.write(sys.stdin.read())
except (IOError, OSError) as e:
raise cdist.Error(("Creating tempfile for stdin data "
"failed: %s" % e))
"failed: {}").format(e))
args.manifest = initial_manifest_temp_path
atexit.register(lambda: os.remove(initial_manifest_temp_path))
@ -764,7 +764,7 @@ class Config:
raise cdist.UnresolvableRequirementsError(
("The requirements of the following objects could not be "
"resolved:\n%s") % ("\n".join(info_string)))
"resolved:\n{}").format("\n".join(info_string)))
def _handle_deprecation(self, cdist_object):
cdist_type = cdist_object.cdist_type

View file

@ -34,17 +34,17 @@ class IllegalObjectIdError(cdist.Error):
self.message = message or 'Illegal object id'
def __str__(self):
return '%s: %s' % (self.message, self.object_id)
return '{}: {}'.format(self.message, self.object_id)
class MissingObjectIdError(cdist.Error):
def __init__(self, type_name):
self.type_name = type_name
self.message = ("Type %s requires object id (is not a "
"singleton type)") % self.type_name
self.message = ("Type {} requires object id (is not a "
"singleton type)").format(self.type_name)
def __str__(self):
return '%s' % (self.message)
return '{}'.format(self.message)
class CdistObject:
@ -142,7 +142,7 @@ class CdistObject:
if self.object_marker in self.object_id.split(os.sep):
raise IllegalObjectIdError(
self.object_id, ('object_id may not contain '
'\'%s\'') % self.object_marker)
'\'{}\'').format(self.object_marker))
if '//' in self.object_id:
raise IllegalObjectIdError(
self.object_id, 'object_id may not contain //')
@ -189,7 +189,7 @@ class CdistObject:
object_id=object_id)
def __repr__(self):
return '<CdistObject %s>' % self.name
return '<CdistObject {}>'.format(self.name)
def __eq__(self, other):
"""define equality as 'name is the same'"""
@ -277,7 +277,7 @@ class CdistObject:
os.makedirs(path, exist_ok=allow_overwrite)
except EnvironmentError as error:
raise cdist.Error(('Error creating directories for cdist object: '
'%s: %s') % (self, error))
'{}: {}').format(self, error))
def requirements_unfinished(self, requirements):
"""Return state whether requirements are satisfied"""

View file

@ -34,7 +34,7 @@ class InvalidTypeError(cdist.Error):
self.source_path = os.path.realpath(self.type_absolute_path)
def __str__(self):
return "Invalid type '%s' at '%s' defined at '%s'" % (
return "Invalid type '{}' at '{}' defined at '{}'".format(
self.type_path, self.type_absolute_path, self.source_path)
@ -109,7 +109,7 @@ class CdistType:
return cls._instances[name]
def __repr__(self):
return '<CdistType %s>' % self.name
return '<CdistType {}>'.format(self.name)
def __eq__(self, other):
return isinstance(other, self.__class__) and self.name == other.name

View file

@ -122,8 +122,8 @@ class Code:
def _run_gencode(self, cdist_object, which):
cdist_type = cdist_object.cdist_type
script = os.path.join(self.local.type_path,
getattr(cdist_type, 'gencode_%s_path' % which))
gencode_attr = getattr(cdist_type, 'gencode_{}_path'.format(which))
script = os.path.join(self.local.type_path, gencode_attr)
if os.path.isfile(script):
env = os.environ.copy()
env.update(self.env)
@ -167,8 +167,8 @@ class Code:
def _run_code(self, cdist_object, which, env=None):
which_exec = getattr(self, which)
script = os.path.join(which_exec.object_path,
getattr(cdist_object, 'code_%s_path' % which))
code_attr = getattr(cdist_object, 'code_{}_path'.format(which))
script = os.path.join(which_exec.object_path, code_attr)
if which_exec.save_output_streams:
stderr_path = os.path.join(cdist_object.stderr_path,
'code-' + which)

View file

@ -160,8 +160,8 @@ class Explorer:
self.remote.transfer(self.local.global_explorer_path,
self.remote.global_explorer_path,
self.jobs)
self.remote.run(["chmod", "0700",
"%s/*" % (self.remote.global_explorer_path)])
self.remote.run(["chmod", "0700", "{}/*".format(
self.remote.global_explorer_path)])
def run_global_explorer(self, explorer):
"""Run the given global explorer and return it's output."""
@ -242,7 +242,7 @@ class Explorer:
destination = os.path.join(self.remote.type_path,
cdist_type.explorer_path)
self.remote.transfer(source, destination)
self.remote.run(["chmod", "0700", "%s/*" % (destination)])
self.remote.run(["chmod", "0700", "{}/*".format(destination)])
self._type_explorers_transferred.append(cdist_type.name)
def transfer_object_parameters(self, cdist_object):

View file

@ -80,13 +80,12 @@ class NoInitialManifestError(cdist.Error):
if user_supplied:
if os.path.islink(manifest_path):
self.message = "%s: %s -> %s" % (
msg_header, manifest_path,
os.path.realpath(manifest_path))
self.message = "{}: {} -> {}".format(
msg_header, manifest_path, os.path.realpath(manifest_path))
else:
self.message = "%s: %s" % (msg_header, manifest_path)
self.message = "{}: {}".format(msg_header, manifest_path)
else:
self.message = "%s" % (msg_header)
self.message = "{}".format(msg_header)
def __str__(self):
return repr(self.message)
@ -107,7 +106,7 @@ class Manifest:
self._open_logger()
self.env = {
'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']),
'PATH': "{}:{}".format(self.local.bin_path, os.environ['PATH']),
# for use in type emulator
'__cdist_type_base_path': self.local.type_path,
'__global': self.local.base_path,

View file

@ -36,8 +36,8 @@ from cdist.core.manifest import Manifest
class MissingRequiredEnvironmentVariableError(cdist.Error):
def __init__(self, name):
self.name = name
self.message = ("Emulator requires the environment variable %s to be "
"setup" % self.name)
self.message = ("Emulator requires the environment variable {} to be "
"setup").format(self.name)
def __str__(self):
return self.message
@ -231,13 +231,13 @@ class Emulator:
if self.cdist_object.exists and 'CDIST_OVERRIDE' not in self.env:
obj_params = self._object_params_in_context()
if obj_params != self.parameters:
errmsg = ("Object %s already exists with conflicting "
"parameters:\n%s: %s\n%s: %s" % (
errmsg = ("Object {} already exists with conflicting "
"parameters:\n{}: {}\n{}: {}").format(
self.cdist_object.name,
" ".join(self.cdist_object.source),
obj_params,
self.object_source,
self.parameters))
self.parameters)
raise cdist.Error(errmsg)
else:
if self.cdist_object.exists:
@ -292,7 +292,7 @@ class Emulator:
fd.write(chunk)
chunk = self._read_stdin()
except EnvironmentError as e:
raise cdist.Error('Failed to read from stdin: %s' % e)
raise cdist.Error('Failed to read from stdin: {}'.format(e))
def record_requirement(self, requirement):
"""record requirement and return recorded requirement"""

View file

@ -152,7 +152,7 @@ class Local:
def _setup_object_marker_file(self):
with open(self.object_marker_file, 'w') as fd:
fd.write("%s\n" % self.object_marker_name)
fd.write("{}\n".format(self.object_marker_name))
self.log.trace("Object marker %s saved in %s",
self.object_marker_name, self.object_marker_file)
@ -184,7 +184,7 @@ class Local:
"""
assert isinstance(command, (list, tuple)), (
"list or tuple argument expected, got: %s" % command)
"list or tuple argument expected, got: {}".format(command))
quiet = self.quiet_mode or quiet_mode
do_save_output = save_output and not quiet and self.save_output_streams
@ -352,7 +352,8 @@ class Local:
try:
os.symlink(src, dst)
except OSError as e:
raise cdist.Error("Linking %s %s to %s failed: %s" % (
raise cdist.Error(
"Linking {} {} to {} failed: {}".format(
sub_dir, src, dst, e.__str__()))
def _link_types_for_emulator(self):
@ -366,5 +367,5 @@ class Local:
os.symlink(src, dst)
except OSError as e:
raise cdist.Error(
"Linking emulator from %s to %s failed: %s" % (
"Linking emulator from {} to {} failed: {}".format(
src, dst, e.__str__()))

View file

@ -24,12 +24,10 @@ import os
import glob
import subprocess
import logging
import multiprocessing
import cdist
import cdist.exec.util as util
import cdist.util.ipaddr as ipaddr
from cdist.mputil import mp_pool_run
def _wrap_addr(addr):
@ -262,9 +260,10 @@ class Remote:
# remotely in e.g. csh and setting up CDIST_REMOTE_SHELL to e.g.
# /bin/csh will execute this script in the right way.
if env:
remote_env = [" export %s=%s;" % item for item in env.items()]
string_cmd = ("/bin/sh -c '" + " ".join(remote_env) +
" ".join(command) + "'")
remote_env = [" export {env[0]}={env[1]};".format(env=item)
for item in env.items()]
string_cmd = ("/bin/sh -c '{}{}'").format(" ".join(remote_env),
" ".join(command))
cmd.append(string_cmd)
else:
cmd.extend(command)
@ -278,7 +277,7 @@ class Remote:
"""
assert isinstance(command, (list, tuple)), (
"list or tuple argument expected, got: %s" % command)
"list or tuple argument expected, got: {}".format(command))
close_stdout = False
close_stderr = False

View file

@ -70,7 +70,7 @@ class Message:
with open(self.global_messages, 'a') as fd:
for line in content:
fd.write("%s:%s" % (self.prefix, line))
fd.write("{}:{}".format(self.prefix, line))
def merge_messages(self):
self._merge_messages()

View file

@ -93,7 +93,7 @@ class Trigger(object):
time.sleep(self.sleeptime)
def trigger(self, interface):
packet = IPv6(dst=f"ff02::1%{interface}") / ICMPv6EchoRequest()
packet = IPv6(dst="ff02::1{}".format(interface)) / ICMPv6EchoRequest()
log.debug("Sending request on %s", interface)
send(packet, verbose=self.verbose)

View file

@ -65,7 +65,7 @@ class Shell:
def _init_environment(self):
self.env = os.environ.copy()
additional_env = {
'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']),
'PATH': "{}:{}".format(self.local.bin_path, os.environ['PATH']),
# for use in type emulator
'__cdist_type_base_path': self.local.type_path,
'__cdist_manifest': "cdist shell",

View file

@ -86,8 +86,7 @@ class ObjectClassTestCase(test.CdistTestCase):
def test_create_singleton(self):
"""Check whether creating an object without id (singleton) works"""
singleton = self.expected_objects[0].object_from_name(
"__test_singleton")
self.expected_objects[0].object_from_name("__test_singleton")
# came here - everything fine
def test_create_singleton_not_singleton_type(self):
@ -126,16 +125,16 @@ class ObjectIdTestCase(test.CdistTestCase):
def test_object_id_contains_object_marker(self):
cdist_type = core.CdistType(type_base_path, '__third')
illegal_object_id = (
'object_id/may/not/contain/%s/anywhere' % OBJECT_MARKER_NAME)
illegal_object_id = 'object_id/may/not/contain/{}/anywhere'.format(
OBJECT_MARKER_NAME)
with self.assertRaises(core.IllegalObjectIdError):
core.CdistObject(cdist_type, self.object_base_path,
OBJECT_MARKER_NAME, illegal_object_id)
def test_object_id_contains_object_marker_string(self):
cdist_type = core.CdistType(type_base_path, '__third')
illegal_object_id = (
'object_id/may/contain_%s_in_filename' % OBJECT_MARKER_NAME)
illegal_object_id = 'object_id/may/contain_{}_in_filename'.format(
OBJECT_MARKER_NAME)
core.CdistObject(cdist_type, self.object_base_path,
OBJECT_MARKER_NAME, illegal_object_id)
# if we get here, the test passed
@ -195,28 +194,32 @@ class ObjectTestCase(test.CdistTestCase):
def test_path(self):
self.assertEqual(self.cdist_object.path,
"__third/moon/%s" % OBJECT_MARKER_NAME)
"__third/moon/{}".format(OBJECT_MARKER_NAME))
def test_absolute_path(self):
self.assertEqual(self.cdist_object.absolute_path,
os.path.join(self.object_base_path,
"__third/moon/%s" % OBJECT_MARKER_NAME))
"__third/moon/{}".format(
OBJECT_MARKER_NAME)))
def test_code_local_path(self):
self.assertEqual(self.cdist_object.code_local_path,
"__third/moon/%s/code-local" % OBJECT_MARKER_NAME)
"__third/moon/{}/code-local".format(
OBJECT_MARKER_NAME))
def test_code_remote_path(self):
self.assertEqual(self.cdist_object.code_remote_path,
"__third/moon/%s/code-remote" % OBJECT_MARKER_NAME)
"__third/moon/{}/code-remote".format(
OBJECT_MARKER_NAME))
def test_parameter_path(self):
self.assertEqual(self.cdist_object.parameter_path,
"__third/moon/%s/parameter" % OBJECT_MARKER_NAME)
"__third/moon/{}/parameter".format(
OBJECT_MARKER_NAME))
def test_explorer_path(self):
self.assertEqual(self.cdist_object.explorer_path,
"__third/moon/%s/explorer" % OBJECT_MARKER_NAME)
"__third/moon/{}/explorer".format(OBJECT_MARKER_NAME))
def test_parameters(self):
expected_parameters = {'planet': 'Saturn', 'name': 'Prometheus'}

View file

@ -84,8 +84,8 @@ class EmulatorTestCase(test.CdistTestCase):
def test_illegal_object_id_requirement(self):
argv = ['__file', '/tmp/foobar']
self.env['require'] = (
"__file/bad/id/with/%s/inside") % self.local.object_marker_name
self.env['require'] = "__file/bad/id/with/{}/inside".format(
self.local.object_marker_name)
emu = emulator.Emulator(argv, env=self.env)
self.assertRaises(core.IllegalObjectIdError, emu.run)

View file

@ -47,8 +47,8 @@ class RemoteTestCase(test.CdistTestCase):
args = (self.target_host,)
kwargs.setdefault('base_path', self.base_path)
user = getpass.getuser()
kwargs.setdefault('remote_exec', 'ssh -o User=%s -q' % user)
kwargs.setdefault('remote_copy', 'scp -o User=%s -q' % user)
kwargs.setdefault('remote_exec', 'ssh -o User={} -q'.format(user))
kwargs.setdefault('remote_copy', 'scp -o User={} -q'.format(user))
if 'stdout_base_path' not in kwargs:
stdout_path = os.path.join(self.temp_dir, 'stdout')
os.makedirs(stdout_path, exist_ok=True)
@ -170,7 +170,7 @@ class RemoteTestCase(test.CdistTestCase):
r = self.create_remote(remote_exec=remote_exec,
remote_copy=remote_copy)
self.assertEqual(r.run('true', return_output=True),
"%s\n" % self.target_host[0])
"{}\n".format(self.target_host[0]))
def test_run_script_target_host_in_env(self):
handle, remote_exec_path = self.mkstemp(dir=self.temp_dir)
@ -185,7 +185,7 @@ class RemoteTestCase(test.CdistTestCase):
with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "true"])
self.assertEqual(r.run_script(script, return_output=True),
"%s\n" % self.target_host[0])
"{}\n".format(self.target_host[0]))
def test_run_script_with_env_target_host_in_env(self):
handle, script = self.mkstemp(dir=self.temp_dir)

View file

@ -67,7 +67,7 @@ class MessageTestCase(test.CdistTestCase):
def test_message_merge_prefix(self):
"""Ensure messages are merged and are prefixed"""
expectedcontent = "%s:%s" % (self.prefix, self.content)
expectedcontent = "{}:{}".format(self.prefix, self.content)
out = self.message.env['__messages_out']

View file

@ -30,7 +30,7 @@ class AbsolutePathRequiredError(cdist.Error):
self.path = path
def __str__(self):
return 'Absolute path required, got: %s' % self.path
return 'Absolute path required, got: {}'.format(self.path)
class FileList(collections.MutableSequence):
@ -218,7 +218,7 @@ class FileBasedProperty:
def _get_attribute(self, instance, owner):
name = self._get_property_name(owner)
attribute_name = '__%s' % name
attribute_name = '__{}'.format(name)
if not hasattr(instance, attribute_name):
path = self._get_path(instance)
attribute_instance = self.attribute_class(path)