Unify string formatting

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

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

View File

@ -34,17 +34,17 @@ class IllegalObjectIdError(cdist.Error):
self.message = message or 'Illegal object id' self.message = message or 'Illegal object id'
def __str__(self): def __str__(self):
return '%s: %s' % (self.message, self.object_id) return '{}: {}'.format(self.message, self.object_id)
class MissingObjectIdError(cdist.Error): class MissingObjectIdError(cdist.Error):
def __init__(self, type_name): def __init__(self, type_name):
self.type_name = type_name self.type_name = type_name
self.message = ("Type %s requires object id (is not a " self.message = ("Type {} requires object id (is not a "
"singleton type)") % self.type_name "singleton type)").format(self.type_name)
def __str__(self): def __str__(self):
return '%s' % (self.message) return '{}'.format(self.message)
class CdistObject: class CdistObject:
@ -142,7 +142,7 @@ class CdistObject:
if self.object_marker in self.object_id.split(os.sep): if self.object_marker in self.object_id.split(os.sep):
raise IllegalObjectIdError( raise IllegalObjectIdError(
self.object_id, ('object_id may not contain ' self.object_id, ('object_id may not contain '
'\'%s\'') % self.object_marker) '\'{}\'').format(self.object_marker))
if '//' in self.object_id: if '//' in self.object_id:
raise IllegalObjectIdError( raise IllegalObjectIdError(
self.object_id, 'object_id may not contain //') self.object_id, 'object_id may not contain //')
@ -189,7 +189,7 @@ class CdistObject:
object_id=object_id) object_id=object_id)
def __repr__(self): def __repr__(self):
return '<CdistObject %s>' % self.name return '<CdistObject {}>'.format(self.name)
def __eq__(self, other): def __eq__(self, other):
"""define equality as 'name is the same'""" """define equality as 'name is the same'"""
@ -277,7 +277,7 @@ class CdistObject:
os.makedirs(path, exist_ok=allow_overwrite) os.makedirs(path, exist_ok=allow_overwrite)
except EnvironmentError as error: except EnvironmentError as error:
raise cdist.Error(('Error creating directories for cdist object: ' raise cdist.Error(('Error creating directories for cdist object: '
'%s: %s') % (self, error)) '{}: {}').format(self, error))
def requirements_unfinished(self, requirements): def requirements_unfinished(self, requirements):
"""Return state whether requirements are satisfied""" """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) self.source_path = os.path.realpath(self.type_absolute_path)
def __str__(self): 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) self.type_path, self.type_absolute_path, self.source_path)
@ -109,7 +109,7 @@ class CdistType:
return cls._instances[name] return cls._instances[name]
def __repr__(self): def __repr__(self):
return '<CdistType %s>' % self.name return '<CdistType {}>'.format(self.name)
def __eq__(self, other): def __eq__(self, other):
return isinstance(other, self.__class__) and self.name == other.name 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): def _run_gencode(self, cdist_object, which):
cdist_type = cdist_object.cdist_type cdist_type = cdist_object.cdist_type
script = os.path.join(self.local.type_path, gencode_attr = getattr(cdist_type, 'gencode_{}_path'.format(which))
getattr(cdist_type, 'gencode_%s_path' % which)) script = os.path.join(self.local.type_path, gencode_attr)
if os.path.isfile(script): if os.path.isfile(script):
env = os.environ.copy() env = os.environ.copy()
env.update(self.env) env.update(self.env)
@ -167,8 +167,8 @@ class Code:
def _run_code(self, cdist_object, which, env=None): def _run_code(self, cdist_object, which, env=None):
which_exec = getattr(self, which) which_exec = getattr(self, which)
script = os.path.join(which_exec.object_path, code_attr = getattr(cdist_object, 'code_{}_path'.format(which))
getattr(cdist_object, 'code_%s_path' % which)) script = os.path.join(which_exec.object_path, code_attr)
if which_exec.save_output_streams: if which_exec.save_output_streams:
stderr_path = os.path.join(cdist_object.stderr_path, stderr_path = os.path.join(cdist_object.stderr_path,
'code-' + which) 'code-' + which)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -93,7 +93,7 @@ class Trigger(object):
time.sleep(self.sleeptime) time.sleep(self.sleeptime)
def trigger(self, interface): 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) log.debug("Sending request on %s", interface)
send(packet, verbose=self.verbose) send(packet, verbose=self.verbose)

View File

@ -65,7 +65,7 @@ class Shell:
def _init_environment(self): def _init_environment(self):
self.env = os.environ.copy() self.env = os.environ.copy()
additional_env = { 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 # for use in type emulator
'__cdist_type_base_path': self.local.type_path, '__cdist_type_base_path': self.local.type_path,
'__cdist_manifest': "cdist shell", '__cdist_manifest': "cdist shell",

View File

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

View File

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

View File

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

View File

@ -67,7 +67,7 @@ class MessageTestCase(test.CdistTestCase):
def test_message_merge_prefix(self): def test_message_merge_prefix(self):
"""Ensure messages are merged and are prefixed""" """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'] out = self.message.env['__messages_out']

View File

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