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 osprofiler

Also add the pyupgrade hook to pre-commit to avoid merging additional
Python 2 syntaxes.

Change-Id: Id5028ca9fbb04d6dad9729bc13fe71ab8b391138
This commit is contained in:
Takashi Kajinami
2025-02-10 22:15:53 +09:00
parent da144ae968
commit 0827580347
33 changed files with 81 additions and 77 deletions

View File

@@ -27,3 +27,8 @@ repos:
rev: 1.7.10 rev: 1.7.10
hooks: hooks:
- id: bandit - id: bandit
- repo: https://github.com/asottile/pyupgrade
rev: v3.18.0
hooks:
- id: pyupgrade
args: [--py3-only]

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Red Hat, Inc. # Copyright (C) 2020 Red Hat, Inc.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # Licensed under the Apache License, Version 2.0 (the "License"); you may

View File

@@ -31,7 +31,8 @@ def split(text, strip=True):
if isinstance(text, (tuple, list)): if isinstance(text, (tuple, list)):
return text return text
if not isinstance(text, str): if not isinstance(text, str):
raise TypeError("Unknown how to split '%s': %s" % (text, type(text))) raise TypeError(
"Unknown how to split '{}': {}".format(text, type(text)))
if strip: if strip:
return [t.strip() for t in text.split(",") if t.strip()] return [t.strip() for t in text.split(",") if t.strip()]
else: else:
@@ -145,7 +146,7 @@ def import_modules_from_package(package):
if filename.startswith("__") or not filename.endswith(".py"): if filename.startswith("__") or not filename.endswith(".py"):
continue continue
new_package = ".".join(root.split(os.sep)).split("....")[1] new_package = ".".join(root.split(os.sep)).split("....")[1]
module_name = "%s.%s" % (new_package, filename[:-3]) module_name = "{}.{}".format(new_package, filename[:-3])
__import__(module_name) __import__(module_name)

View File

@@ -25,7 +25,7 @@ from osprofiler.drivers import base
from osprofiler import exc from osprofiler import exc
class BaseCommand(object): class BaseCommand:
group_name = None group_name = None
@@ -136,12 +136,12 @@ class TraceCommands(BaseCommand):
if name == "wsgi": if name == "wsgi":
req = info["meta.raw_payload.wsgi-start"]["info"]["request"] req = info["meta.raw_payload.wsgi-start"]["info"]["request"]
label = "%s\\n%s %s.." % (label, req["method"], label = "{}\\n{} {}..".format(label, req["method"],
req["path"][:30]) req["path"][:30])
elif name == "rpc" or name == "driver": elif name == "rpc" or name == "driver":
raw = info["meta.raw_payload.%s-start" % name] raw = info["meta.raw_payload.%s-start" % name]
fn_name = raw["info"]["function"]["name"] fn_name = raw["info"]["function"]["name"]
label = "%s\\n%s" % (label, fn_name.split(".")[-1]) label = "{}\\n{}".format(label, fn_name.split(".")[-1])
node_id = str(next_id[0]) node_id = str(next_id[0])
next_id[0] += 1 next_id[0] += 1

View File

@@ -30,7 +30,7 @@ from osprofiler import exc
from osprofiler import opts from osprofiler import opts
class OSProfilerShell(object): class OSProfilerShell:
def __init__(self, argv): def __init__(self, argv):
args = self._get_base_parser().parse_args(argv) args = self._get_base_parser().parse_args(argv)

View File

@@ -49,7 +49,7 @@ def get_driver(connection_string, *args, **kwargs):
"%s" % connection_string) "%s" % connection_string)
class Driver(object): class Driver:
"""Base Driver class. """Base Driver class.
This class provides protected common methods that This class provides protected common methods that
@@ -94,7 +94,7 @@ class Driver(object):
With parent_id and trace_id it's quite simple to build With parent_id and trace_id it's quite simple to build
tree of trace elements, which simplify analyze of trace. tree of trace elements, which simplify analyze of trace.
""" """
raise NotImplementedError("{0}: This method is either not supported " raise NotImplementedError("{}: This method is either not supported "
"or has to be overridden".format( "or has to be overridden".format(
self.get_name())) self.get_name()))
@@ -103,7 +103,7 @@ class Driver(object):
:param base_id: Base id of trace elements. :param base_id: Base id of trace elements.
""" """
raise NotImplementedError("{0}: This method is either not supported " raise NotImplementedError("{}: This method is either not supported "
"or has to be overridden".format( "or has to be overridden".format(
self.get_name())) self.get_name()))
@@ -120,7 +120,7 @@ class Driver(object):
:returns: List of traces, where each trace is a dictionary containing :returns: List of traces, where each trace is a dictionary containing
at least `base_id` and `timestamp`. at least `base_id` and `timestamp`.
""" """
raise NotImplementedError("{0}: This method is either not supported " raise NotImplementedError("{}: This method is either not supported "
"or has to be overridden".format( "or has to be overridden".format(
self.get_name())) self.get_name()))
@@ -130,7 +130,7 @@ class Driver(object):
:return List of traces, where each trace is a dictionary containing :return List of traces, where each trace is a dictionary containing
`base_id` and `timestamp`. `base_id` and `timestamp`.
""" """
raise NotImplementedError("{0}: This method is either not supported " raise NotImplementedError("{}: This method is either not supported "
"or has to be overridden".format( "or has to be overridden".format(
self.get_name())) self.get_name()))

View File

@@ -27,7 +27,7 @@ class ElasticsearchDriver(base.Driver):
**kwargs): **kwargs):
"""Elasticsearch driver for OSProfiler.""" """Elasticsearch driver for OSProfiler."""
super(ElasticsearchDriver, self).__init__(connection_str, super().__init__(connection_str,
project=project, project=project,
service=service, service=service,
host=host, host=host,

View File

@@ -31,7 +31,7 @@ class Jaeger(base.Driver):
conf=cfg.CONF, **kwargs): conf=cfg.CONF, **kwargs):
"""Jaeger driver for OSProfiler.""" """Jaeger driver for OSProfiler."""
super(Jaeger, self).__init__(connection_str, project=project, super().__init__(connection_str, project=project,
service=service, host=host, service=service, host=host,
conf=conf, **kwargs) conf=conf, **kwargs)
try: try:

View File

@@ -49,7 +49,7 @@ class LogInsightDriver(base.Driver):
def __init__( def __init__(
self, connection_str, project=None, service=None, host=None, self, connection_str, project=None, service=None, host=None,
**kwargs): **kwargs):
super(LogInsightDriver, self).__init__(connection_str, super().__init__(connection_str,
project=project, project=project,
service=service, service=service,
host=host) host=host)
@@ -126,7 +126,7 @@ class LogInsightDriver(base.Driver):
return self._parse_results() return self._parse_results()
class LogInsightClient(object): class LogInsightClient:
"""A minimal Log Insight client.""" """A minimal Log Insight client."""
LI_OSPROFILER_AGENT_ID = "F52D775B-6017-4787-8C8A-F21AE0AEC057" LI_OSPROFILER_AGENT_ID = "F52D775B-6017-4787-8C8A-F21AE0AEC057"
@@ -174,7 +174,7 @@ class LogInsightClient(object):
def _send_request( def _send_request(
self, method, scheme, path, headers=None, body=None, params=None): self, method, scheme, path, headers=None, body=None, params=None):
url = "%s/%s" % (self._build_base_url(scheme), path) url = "{}/{}".format(self._build_base_url(scheme), path)
headers = headers or {} headers = headers or {}
headers["content-type"] = "application/json" headers["content-type"] = "application/json"
@@ -239,10 +239,11 @@ class LogInsightClient(object):
# the operator is "CONTAINS". # the operator is "CONTAINS".
constraints = [] constraints = []
for field, value in params.items(): for field, value in params.items():
constraints.append("%s/CONTAINS+%s" % (field, value)) constraints.append("{}/CONTAINS+{}".format(field, value))
constraints.append("timestamp/GT+0") constraints.append("timestamp/GT+0")
path = "%s/%s" % (self.QUERY_EVENTS_BASE_PATH, "/".join(constraints)) path = "{}/{}".format(self.QUERY_EVENTS_BASE_PATH,
"/".join(constraints))
def _query_events(): def _query_events():
return self._send_request("get", return self._send_request("get",

View File

@@ -49,7 +49,7 @@ class Messaging(base.Driver):
raise ValueError("Oslo.messaging library is required for " raise ValueError("Oslo.messaging library is required for "
"messaging driver") "messaging driver")
super(Messaging, self).__init__(connection_str, project=project, super().__init__(connection_str, project=project,
service=service, host=host) service=service, host=host)
self.context = context self.context = context
@@ -167,7 +167,7 @@ class Messaging(base.Driver):
return self._parse_results() return self._parse_results()
class NotifyEndpoint(object): class NotifyEndpoint:
def __init__(self, oslo_messaging, base_id): def __init__(self, oslo_messaging, base_id):
self.received_messages = [] self.received_messages = []

View File

@@ -22,7 +22,7 @@ class MongoDB(base.Driver):
service=None, host=None, **kwargs): service=None, host=None, **kwargs):
"""MongoDB driver for OSProfiler.""" """MongoDB driver for OSProfiler."""
super(MongoDB, self).__init__(connection_str, project=project, super().__init__(connection_str, project=project,
service=service, host=host, **kwargs) service=service, host=host, **kwargs)
try: try:
from pymongo import MongoClient from pymongo import MongoClient

View File

@@ -28,7 +28,7 @@ class OTLP(base.Driver):
conf=cfg.CONF, **kwargs): conf=cfg.CONF, **kwargs):
"""OTLP driver using OTLP exporters.""" """OTLP driver using OTLP exporters."""
super(OTLP, self).__init__(connection_str, project=project, super().__init__(connection_str, project=project,
service=service, host=host, service=service, host=host,
conf=conf, **kwargs) conf=conf, **kwargs)
try: try:

View File

@@ -33,7 +33,7 @@ class Redis(base.Driver):
service=None, host=None, conf=cfg.CONF, **kwargs): service=None, host=None, conf=cfg.CONF, **kwargs):
"""Redis driver for OSProfiler.""" """Redis driver for OSProfiler."""
super(Redis, self).__init__(connection_str, project=project, super().__init__(connection_str, project=project,
service=service, host=host, service=service, host=host,
conf=conf, **kwargs) conf=conf, **kwargs)
try: try:
@@ -149,8 +149,7 @@ class Redis(base.Driver):
match=self.namespace + base_id + "*"): # legacy match=self.namespace + base_id + "*"): # legacy
yield self.db.get(key) yield self.db.get(key)
for event in self.db.lrange(self.namespace_opt + base_id, 0, -1): yield from self.db.lrange(self.namespace_opt + base_id, 0, -1)
yield event
for data in iterate_events(): for data in iterate_events():
n = jsonutils.loads(data) n = jsonutils.loads(data)
@@ -177,7 +176,7 @@ class RedisSentinel(Redis, base.Driver):
service=None, host=None, conf=cfg.CONF, **kwargs): service=None, host=None, conf=cfg.CONF, **kwargs):
"""Redis driver for OSProfiler.""" """Redis driver for OSProfiler."""
super(RedisSentinel, self).__init__(connection_str, project=project, super().__init__(connection_str, project=project,
service=service, host=host, service=service, host=host,
conf=conf, **kwargs) conf=conf, **kwargs)
try: try:

View File

@@ -26,7 +26,7 @@ LOG = logging.getLogger(__name__)
class SQLAlchemyDriver(base.Driver): class SQLAlchemyDriver(base.Driver):
def __init__(self, connection_str, project=None, service=None, host=None, def __init__(self, connection_str, project=None, service=None, host=None,
**kwargs): **kwargs):
super(SQLAlchemyDriver, self).__init__(connection_str, project=project, super().__init__(connection_str, project=project,
service=service, host=host) service=service, host=host)
try: try:

View File

@@ -292,7 +292,7 @@ class TracedMeta(type):
traced - E.g. wsgi, rpc, db, etc... traced - E.g. wsgi, rpc, db, etc...
""" """
def __init__(cls, cls_name, bases, attrs): def __init__(cls, cls_name, bases, attrs):
super(TracedMeta, cls).__init__(cls_name, bases, attrs) super().__init__(cls_name, bases, attrs)
trace_args = dict(getattr(cls, "__trace_args__", {})) trace_args = dict(getattr(cls, "__trace_args__", {}))
trace_private = trace_args.pop("trace_private", False) trace_private = trace_args.pop("trace_private", False)
@@ -321,7 +321,7 @@ class TracedMeta(type):
attr_name))) attr_name)))
class Trace(object): class Trace:
def __init__(self, name, info=None): def __init__(self, name, info=None):
"""With statement way to use profiler start()/stop(). """With statement way to use profiler start()/stop().
@@ -355,7 +355,7 @@ class Trace(object):
stop() stop()
class _Profiler(object): class _Profiler:
def __init__(self, hmac_key, base_id=None, parent_id=None): def __init__(self, hmac_key, base_id=None, parent_id=None):
self.hmac_key = hmac_key self.hmac_key = hmac_key

View File

@@ -30,7 +30,7 @@ LOG = logging.getLogger(__name__)
@profiler.trace_cls("rpc", hide_args=True) @profiler.trace_cls("rpc", hide_args=True)
class Foo(object): class Foo:
def bar(self, x): def bar(self, x):
return self.baz(x, x) return self.baz(x, x)
@@ -44,7 +44,7 @@ class DriverTestCase(test.FunctionalTestCase):
PROJECT = "project" PROJECT = "project"
def setUp(self): def setUp(self):
super(DriverTestCase, self).setUp() super().setUp()
CONF(["--config-file", os.path.dirname(__file__) + "/config.cfg"]) CONF(["--config-file", os.path.dirname(__file__) + "/config.cfg"])
opts.set_defaults(CONF, opts.set_defaults(CONF,
enabled=True, enabled=True,

View File

@@ -28,6 +28,6 @@ class FunctionalTestCase(TestCase):
"""Base for functional tests""" """Base for functional tests"""
def setUp(self): def setUp(self):
super(FunctionalTestCase, self).setUp() super().setUp()
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

View File

@@ -32,16 +32,16 @@ class ShellTestCase(test.TestCase):
TRACE_ID = "c598094d-bbee-40b6-b317-d76003b679d3" TRACE_ID = "c598094d-bbee-40b6-b317-d76003b679d3"
def setUp(self): def setUp(self):
super(ShellTestCase, self).setUp() super().setUp()
self.old_environment = os.environ.copy() self.old_environment = os.environ.copy()
def tearDown(self): def tearDown(self):
super(ShellTestCase, self).tearDown() super().tearDown()
os.environ = self.old_environment os.environ = self.old_environment
def _trace_show_cmd(self, format_=None): def _trace_show_cmd(self, format_=None):
cmd = "trace show --connection-string redis:// %s" % self.TRACE_ID cmd = "trace show --connection-string redis:// %s" % self.TRACE_ID
return cmd if format_ is None else "%s --%s" % (cmd, format_) return cmd if format_ is None else "{} --{}".format(cmd, format_)
@mock.patch("sys.stdout", io.StringIO()) @mock.patch("sys.stdout", io.StringIO())
@mock.patch("osprofiler.cmd.shell.OSProfilerShell") @mock.patch("osprofiler.cmd.shell.OSProfilerShell")

View File

@@ -88,7 +88,7 @@ class TitlesTestCase(test.TestCase):
trailing_spaces = re.findall(" +$", line) trailing_spaces = re.findall(" +$", line)
self.assertEqual( self.assertEqual(
len(trailing_spaces), 0, len(trailing_spaces), 0,
"Found trailing spaces on line %s of %s" % (i + 1, tpl)) "Found trailing spaces on line {} of {}".format(i + 1, tpl))
def test_template(self): def test_template(self):
with open(os.path.join(self.specs_path, "template.rst")) as f: with open(os.path.join(self.specs_path, "template.rst")) as f:
@@ -98,7 +98,7 @@ class TitlesTestCase(test.TestCase):
template_titles = self._get_titles(spec) template_titles = self._get_titles(spec)
for d in ["implemented", "in-progress"]: for d in ["implemented", "in-progress"]:
spec_dir = "%s/%s" % (self.specs_path, d) spec_dir = "{}/{}".format(self.specs_path, d)
self.assertTrue(os.path.isdir(spec_dir), self.assertTrue(os.path.isdir(spec_dir),
"%s is not a directory" % spec_dir) "%s is not a directory" % spec_dir)

View File

@@ -22,7 +22,7 @@ from osprofiler.tests import test
class ElasticsearchTestCase(test.TestCase): class ElasticsearchTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(ElasticsearchTestCase, self).setUp() super().setUp()
self.elasticsearch = ElasticsearchDriver( self.elasticsearch = ElasticsearchDriver(
"elasticsearch://localhost:9001/") "elasticsearch://localhost:9001/")
self.elasticsearch.project = "project" self.elasticsearch.project = "project"

View File

@@ -27,7 +27,7 @@ from jaeger_client import Config
class JaegerTestCase(test.TestCase): class JaegerTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(JaegerTestCase, self).setUp() super().setUp()
opts.set_defaults(cfg.CONF) opts.set_defaults(cfg.CONF)
cfg.CONF.set_default( cfg.CONF.set_default(

View File

@@ -29,7 +29,7 @@ class LogInsightDriverTestCase(test.TestCase):
BASE_ID = "8d28af1e-acc0-498c-9890-6908e33eff5f" BASE_ID = "8d28af1e-acc0-498c-9890-6908e33eff5f"
def setUp(self): def setUp(self):
super(LogInsightDriverTestCase, self).setUp() super().setUp()
self._client = mock.Mock(spec=loginsight.LogInsightClient) self._client = mock.Mock(spec=loginsight.LogInsightClient)
self._project = "cinder" self._project = "cinder"
self._service = "osapi_volume" self._service = "osapi_volume"
@@ -160,7 +160,7 @@ class LogInsightDriverTestCase(test.TestCase):
class LogInsightClientTestCase(test.TestCase): class LogInsightClientTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(LogInsightClientTestCase, self).setUp() super().setUp()
self._host = "localhost" self._host = "localhost"
self._username = "username" self._username = "username"
self._password = "password" # nosec self._password = "password" # nosec

View File

@@ -21,7 +21,7 @@ from osprofiler.tests import test
class MongoDBParserTestCase(test.TestCase): class MongoDBParserTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(MongoDBParserTestCase, self).setUp() super().setUp()
self.mongodb = MongoDB("mongodb://localhost") self.mongodb = MongoDB("mongodb://localhost")
def test_build_empty_tree(self): def test_build_empty_tree(self):

View File

@@ -24,7 +24,7 @@ from osprofiler.tests import test
class OTLPTestCase(test.TestCase): class OTLPTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(OTLPTestCase, self).setUp() super().setUp()
opts.set_defaults(cfg.CONF) opts.set_defaults(cfg.CONF)

View File

@@ -24,7 +24,7 @@ from osprofiler.tests import test
class RedisParserTestCase(test.TestCase): class RedisParserTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(RedisParserTestCase, self).setUp() super().setUp()
self.redisdb = Redis("redis://localhost:6379") self.redisdb = Redis("redis://localhost:6379")
def test_build_empty_tree(self): def test_build_empty_tree(self):

View File

@@ -24,7 +24,7 @@ class NotifierTestCase(test.TestCase):
def tearDown(self): def tearDown(self):
notifier.set(notifier._noop_notifier) # restore defaults notifier.set(notifier._noop_notifier) # restore defaults
notifier.clear_notifier_cache() notifier.clear_notifier_cache()
super(NotifierTestCase, self).tearDown() super().tearDown()
def test_set(self): def test_set(self):

View File

@@ -23,7 +23,7 @@ from osprofiler.tests import test
class ConfigTestCase(test.TestCase): class ConfigTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(ConfigTestCase, self).setUp() super().setUp()
self.conf_fixture = self.useFixture(fixture.Config()) self.conf_fixture = self.useFixture(fixture.Config())
def test_options_defaults(self): def test_options_defaults(self):

View File

@@ -285,7 +285,7 @@ class TraceDecoratorTestCase(test.TestCase):
mock_stop.assert_called_once_with(info=stop_info) mock_stop.assert_called_once_with(info=stop_info)
class FakeTracedCls(object): class FakeTracedCls:
def method1(self, a, b, c=10): def method1(self, a, b, c=10):
return a + b + c return a + b + c
@@ -488,7 +488,7 @@ class TraceClsDecoratorTestCase(test.TestCase):
self.assertFalse(mock_stop.called) self.assertFalse(mock_stop.called)
class FakeTraceWithMetaclassBase(object, metaclass=profiler.TracedMeta): class FakeTraceWithMetaclassBase(metaclass=profiler.TracedMeta):
__trace_args__ = {"name": "rpc", __trace_args__ = {"name": "rpc",
"info": {"a": 10}} "info": {"a": 10}}

View File

@@ -137,7 +137,7 @@ class UtilsTestCase(test.TestCase):
def test_itersubclasses(self): def test_itersubclasses(self):
class A(object): class A:
pass pass
class B(A): class B(A):

View File

@@ -31,7 +31,7 @@ def dummy_app(environ, response):
class WebTestCase(test.TestCase): class WebTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(WebTestCase, self).setUp() super().setUp()
profiler.clean() profiler.clean()
self.addCleanup(profiler.clean) self.addCleanup(profiler.clean)
@@ -61,7 +61,7 @@ class WebTestCase(test.TestCase):
class WebMiddlewareTestCase(test.TestCase): class WebMiddlewareTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(WebMiddlewareTestCase, self).setUp() super().setUp()
profiler.clean() profiler.clean()
# it's default state of _ENABLED param, so let's set it here # it's default state of _ENABLED param, so let's set it here
web._ENABLED = None web._ENABLED = None
@@ -69,7 +69,7 @@ class WebMiddlewareTestCase(test.TestCase):
def tearDown(self): def tearDown(self):
web.enable() web.enable()
super(WebMiddlewareTestCase, self).tearDown() super().tearDown()
def test_factory(self): def test_factory(self):
mock_app = mock.MagicMock() mock_app = mock.MagicMock()

View File

@@ -65,7 +65,7 @@ def enable(hmac_keys=None):
_HMAC_KEYS = utils.split(hmac_keys or "") _HMAC_KEYS = utils.split(hmac_keys or "")
class WsgiMiddleware(object): class WsgiMiddleware:
"""WSGI Middleware that enables tracing for an application.""" """WSGI Middleware that enables tracing for an application."""
def __init__(self, application, hmac_keys=None, enabled=False, **kwargs): def __init__(self, application, hmac_keys=None, enabled=False, **kwargs):

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at

View File

@@ -37,7 +37,7 @@ def main(argv):
os.path.join(root, 'test-requirements.txt'), os.path.join(root, 'test-requirements.txt'),
os.path.join(root, 'tools', 'test-requires'), os.path.join(root, 'tools', 'test-requires'),
]) ])
py_version = "python%s.%s" % (sys.version_info[0], sys.version_info[1]) py_version = "python{}.{}".format(sys.version_info[0], sys.version_info[1])
project = 'oslo' project = 'oslo'
install = install_venv.InstallVenv(root, venv, pip_requires, test_requires, install = install_venv.InstallVenv(root, venv, pip_requires, test_requires,
py_version, project) py_version, project)