Run pyupgrade to clean up Python 2 syntaxes

Update all .py source files by
 $ pyupgrade --py3-only $(git ls-files | grep ".py$")
to modernize the code according to Python 3 syntaxes.

pep8 errors are fixed by
 $ autopep8 --select=E127,E128,E501 --max-line-length 79 -r \
    --in-place os_apply_config

Change-Id: I89888a4e007af7c35f75a01a144cfbab71a08c1e
This commit is contained in:
Takashi Kajinami 2024-10-19 23:35:56 +09:00
parent abff7141db
commit 53eef8b5c2
7 changed files with 17 additions and 17 deletions

View File

@ -113,7 +113,7 @@ def print_key(
return
else:
raise exc.ConfigException(
'key %s does not exist in %s' % (key, config_path))
'key {} does not exist in {}'.format(key, config_path))
value_types.ensure_type(str(config), type_name)
if isinstance(config, (dict, list, bool)):
print(json.dumps(config))
@ -349,8 +349,8 @@ def main(argv=sys.argv):
opts.metadata = os.environ['OS_CONFIG_FILES'].split(':')
else:
opts.metadata = load_list_from_json(opts.os_config_files)
if ((not opts.metadata and opts.os_config_files ==
OS_CONFIG_FILES_PATH)):
if (not opts.metadata and opts.os_config_files ==
OS_CONFIG_FILES_PATH):
logger.warning('DEPRECATED: falling back to %s' %
OS_CONFIG_FILES_PATH_OLD)
opts.metadata = load_list_from_json(OS_CONFIG_FILES_PATH_OLD)

View File

@ -27,7 +27,7 @@ def read_configs(config_files):
try:
with open(input_path) as input_file:
yield (input_file.read(), input_path)
except IOError as e:
except OSError as e:
raise exc.ConfigException('Could not open %s for reading. %s' %
(input_path, e))

View File

@ -19,7 +19,7 @@ import pwd
from os_apply_config import config_exception as exc
class OacFile(object):
class OacFile:
DEFAULTS = {
'allow_empty': True,
'mode': None,
@ -28,7 +28,7 @@ class OacFile(object):
}
def __init__(self, body, **kwargs):
super(OacFile, self).__init__()
super().__init__()
self.body = body
for k, v in self.DEFAULTS.items():
@ -53,7 +53,7 @@ class OacFile(object):
for key, default in self.DEFAULTS.items():
value = getattr(self, key)
if value != default:
a.append("%s=%s" % (key, repr(value)))
a.append("{}={}".format(key, repr(value)))
return ", ".join(a) + ")"
def set(self, key, value):

View File

@ -33,11 +33,11 @@ class JsonRenderer(pystache.Renderer):
return u
if escape is None:
escape = escape_noop
return super(JsonRenderer, self).__init__(file_encoding,
string_encoding,
decode_errors, search_dirs,
file_extension, escape,
partials, missing_tags)
return super().__init__(file_encoding,
string_encoding,
decode_errors, search_dirs,
file_extension, escape,
partials, missing_tags)
def str_coerce(self, val):
if val is None:

View File

@ -86,7 +86,7 @@ class TestRunOSConfigApplier(testtools.TestCase):
"""Tests the commandline options."""
def setUp(self):
super(TestRunOSConfigApplier, self).setUp()
super().setUp()
self.useFixture(fixtures.NestedTempfile())
self.stdout = self.useFixture(fixtures.StringStream('stdout')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.stdout))
@ -234,7 +234,7 @@ class TestRunOSConfigApplier(testtools.TestCase):
class OSConfigApplierTestCase(testtools.TestCase):
def setUp(self):
super(OSConfigApplierTestCase, self).setUp()
super().setUp()
self.logger = self.useFixture(fixtures.FakeLogger('os-apply-config'))
self.useFixture(fixtures.NestedTempfile())
@ -329,7 +329,7 @@ class OSConfigApplierTestCase(testtools.TestCase):
apply_config.render_moustache("ab{{x.a}}cd", {"x": {"a": "123"}}))
def test_render_moustache_bad_key(self):
self.assertEqual(u'', apply_config.render_moustache("{{badkey}}", {}))
self.assertEqual('', apply_config.render_moustache("{{badkey}}", {}))
def test_render_moustache_none(self):
self.assertEqual('foo: ',

View File

@ -35,4 +35,4 @@ class JsonRendererTestCase(testtools.TestCase):
self.assertEqual(desire_structure, result_structure)
result = x.render('{{a.c}}', context)
self.addDetail('result', content.text_content(result))
self.assertEqual(u'the quick brown fox', result)
self.assertEqual('the quick brown fox', result)

View File

@ -39,6 +39,6 @@ def ensure_type(string_value, type_name='default'):
"requested validation of unknown type: %s" % type_name)
if not re.match(TYPES[type_name], string_value):
exception = config_exception.ConfigException
raise exception("cannot interpret value '%s' as type %s" % (
raise exception("cannot interpret value '{}' as type {}".format(
string_value, type_name))
return string_value