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:
parent
da144ae968
commit
0827580347
@ -27,3 +27,8 @@ repos:
|
||||
rev: 1.7.10
|
||||
hooks:
|
||||
- id: bandit
|
||||
- repo: https://github.com/asottile/pyupgrade
|
||||
rev: v3.18.0
|
||||
hooks:
|
||||
- id: pyupgrade
|
||||
args: [--py3-only]
|
||||
|
@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2020 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
|
@ -31,7 +31,8 @@ def split(text, strip=True):
|
||||
if isinstance(text, (tuple, list)):
|
||||
return text
|
||||
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:
|
||||
return [t.strip() for t in text.split(",") if t.strip()]
|
||||
else:
|
||||
@ -145,7 +146,7 @@ def import_modules_from_package(package):
|
||||
if filename.startswith("__") or not filename.endswith(".py"):
|
||||
continue
|
||||
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)
|
||||
|
||||
|
||||
|
@ -25,7 +25,7 @@ from osprofiler.drivers import base
|
||||
from osprofiler import exc
|
||||
|
||||
|
||||
class BaseCommand(object):
|
||||
class BaseCommand:
|
||||
group_name = None
|
||||
|
||||
|
||||
@ -136,12 +136,12 @@ class TraceCommands(BaseCommand):
|
||||
|
||||
if name == "wsgi":
|
||||
req = info["meta.raw_payload.wsgi-start"]["info"]["request"]
|
||||
label = "%s\\n%s %s.." % (label, req["method"],
|
||||
req["path"][:30])
|
||||
label = "{}\\n{} {}..".format(label, req["method"],
|
||||
req["path"][:30])
|
||||
elif name == "rpc" or name == "driver":
|
||||
raw = info["meta.raw_payload.%s-start" % 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])
|
||||
next_id[0] += 1
|
||||
|
@ -30,7 +30,7 @@ from osprofiler import exc
|
||||
from osprofiler import opts
|
||||
|
||||
|
||||
class OSProfilerShell(object):
|
||||
class OSProfilerShell:
|
||||
|
||||
def __init__(self, argv):
|
||||
args = self._get_base_parser().parse_args(argv)
|
||||
|
@ -49,7 +49,7 @@ def get_driver(connection_string, *args, **kwargs):
|
||||
"%s" % connection_string)
|
||||
|
||||
|
||||
class Driver(object):
|
||||
class Driver:
|
||||
"""Base Driver class.
|
||||
|
||||
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
|
||||
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(
|
||||
self.get_name()))
|
||||
|
||||
@ -103,7 +103,7 @@ class Driver(object):
|
||||
|
||||
: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(
|
||||
self.get_name()))
|
||||
|
||||
@ -120,7 +120,7 @@ class Driver(object):
|
||||
:returns: List of traces, where each trace is a dictionary containing
|
||||
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(
|
||||
self.get_name()))
|
||||
|
||||
@ -130,7 +130,7 @@ class Driver(object):
|
||||
:return List of traces, where each trace is a dictionary containing
|
||||
`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(
|
||||
self.get_name()))
|
||||
|
||||
|
@ -27,12 +27,12 @@ class ElasticsearchDriver(base.Driver):
|
||||
**kwargs):
|
||||
"""Elasticsearch driver for OSProfiler."""
|
||||
|
||||
super(ElasticsearchDriver, self).__init__(connection_str,
|
||||
project=project,
|
||||
service=service,
|
||||
host=host,
|
||||
conf=conf,
|
||||
**kwargs)
|
||||
super().__init__(connection_str,
|
||||
project=project,
|
||||
service=service,
|
||||
host=host,
|
||||
conf=conf,
|
||||
**kwargs)
|
||||
try:
|
||||
from elasticsearch import Elasticsearch
|
||||
except ImportError:
|
||||
|
@ -31,9 +31,9 @@ class Jaeger(base.Driver):
|
||||
conf=cfg.CONF, **kwargs):
|
||||
"""Jaeger driver for OSProfiler."""
|
||||
|
||||
super(Jaeger, self).__init__(connection_str, project=project,
|
||||
service=service, host=host,
|
||||
conf=conf, **kwargs)
|
||||
super().__init__(connection_str, project=project,
|
||||
service=service, host=host,
|
||||
conf=conf, **kwargs)
|
||||
try:
|
||||
import jaeger_client
|
||||
self.jaeger_client = jaeger_client
|
||||
|
@ -49,10 +49,10 @@ class LogInsightDriver(base.Driver):
|
||||
def __init__(
|
||||
self, connection_str, project=None, service=None, host=None,
|
||||
**kwargs):
|
||||
super(LogInsightDriver, self).__init__(connection_str,
|
||||
project=project,
|
||||
service=service,
|
||||
host=host)
|
||||
super().__init__(connection_str,
|
||||
project=project,
|
||||
service=service,
|
||||
host=host)
|
||||
|
||||
parsed_connection = urlparse.urlparse(connection_str)
|
||||
try:
|
||||
@ -126,7 +126,7 @@ class LogInsightDriver(base.Driver):
|
||||
return self._parse_results()
|
||||
|
||||
|
||||
class LogInsightClient(object):
|
||||
class LogInsightClient:
|
||||
"""A minimal Log Insight client."""
|
||||
|
||||
LI_OSPROFILER_AGENT_ID = "F52D775B-6017-4787-8C8A-F21AE0AEC057"
|
||||
@ -174,7 +174,7 @@ class LogInsightClient(object):
|
||||
|
||||
def _send_request(
|
||||
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["content-type"] = "application/json"
|
||||
@ -239,10 +239,11 @@ class LogInsightClient(object):
|
||||
# the operator is "CONTAINS".
|
||||
constraints = []
|
||||
for field, value in params.items():
|
||||
constraints.append("%s/CONTAINS+%s" % (field, value))
|
||||
constraints.append("{}/CONTAINS+{}".format(field, value))
|
||||
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():
|
||||
return self._send_request("get",
|
||||
|
@ -49,8 +49,8 @@ class Messaging(base.Driver):
|
||||
raise ValueError("Oslo.messaging library is required for "
|
||||
"messaging driver")
|
||||
|
||||
super(Messaging, self).__init__(connection_str, project=project,
|
||||
service=service, host=host)
|
||||
super().__init__(connection_str, project=project,
|
||||
service=service, host=host)
|
||||
|
||||
self.context = context
|
||||
|
||||
@ -167,7 +167,7 @@ class Messaging(base.Driver):
|
||||
return self._parse_results()
|
||||
|
||||
|
||||
class NotifyEndpoint(object):
|
||||
class NotifyEndpoint:
|
||||
|
||||
def __init__(self, oslo_messaging, base_id):
|
||||
self.received_messages = []
|
||||
|
@ -22,8 +22,8 @@ class MongoDB(base.Driver):
|
||||
service=None, host=None, **kwargs):
|
||||
"""MongoDB driver for OSProfiler."""
|
||||
|
||||
super(MongoDB, self).__init__(connection_str, project=project,
|
||||
service=service, host=host, **kwargs)
|
||||
super().__init__(connection_str, project=project,
|
||||
service=service, host=host, **kwargs)
|
||||
try:
|
||||
from pymongo import MongoClient
|
||||
except ImportError:
|
||||
|
@ -28,9 +28,9 @@ class OTLP(base.Driver):
|
||||
conf=cfg.CONF, **kwargs):
|
||||
"""OTLP driver using OTLP exporters."""
|
||||
|
||||
super(OTLP, self).__init__(connection_str, project=project,
|
||||
service=service, host=host,
|
||||
conf=conf, **kwargs)
|
||||
super().__init__(connection_str, project=project,
|
||||
service=service, host=host,
|
||||
conf=conf, **kwargs)
|
||||
try:
|
||||
from opentelemetry import trace as trace_api
|
||||
|
||||
|
@ -33,9 +33,9 @@ class Redis(base.Driver):
|
||||
service=None, host=None, conf=cfg.CONF, **kwargs):
|
||||
"""Redis driver for OSProfiler."""
|
||||
|
||||
super(Redis, self).__init__(connection_str, project=project,
|
||||
service=service, host=host,
|
||||
conf=conf, **kwargs)
|
||||
super().__init__(connection_str, project=project,
|
||||
service=service, host=host,
|
||||
conf=conf, **kwargs)
|
||||
try:
|
||||
from redis import Redis as _Redis
|
||||
except ImportError:
|
||||
@ -149,8 +149,7 @@ class Redis(base.Driver):
|
||||
match=self.namespace + base_id + "*"): # legacy
|
||||
yield self.db.get(key)
|
||||
|
||||
for event in self.db.lrange(self.namespace_opt + base_id, 0, -1):
|
||||
yield event
|
||||
yield from self.db.lrange(self.namespace_opt + base_id, 0, -1)
|
||||
|
||||
for data in iterate_events():
|
||||
n = jsonutils.loads(data)
|
||||
@ -177,9 +176,9 @@ class RedisSentinel(Redis, base.Driver):
|
||||
service=None, host=None, conf=cfg.CONF, **kwargs):
|
||||
"""Redis driver for OSProfiler."""
|
||||
|
||||
super(RedisSentinel, self).__init__(connection_str, project=project,
|
||||
service=service, host=host,
|
||||
conf=conf, **kwargs)
|
||||
super().__init__(connection_str, project=project,
|
||||
service=service, host=host,
|
||||
conf=conf, **kwargs)
|
||||
try:
|
||||
from redis.sentinel import Sentinel
|
||||
except ImportError:
|
||||
|
@ -26,8 +26,8 @@ LOG = logging.getLogger(__name__)
|
||||
class SQLAlchemyDriver(base.Driver):
|
||||
def __init__(self, connection_str, project=None, service=None, host=None,
|
||||
**kwargs):
|
||||
super(SQLAlchemyDriver, self).__init__(connection_str, project=project,
|
||||
service=service, host=host)
|
||||
super().__init__(connection_str, project=project,
|
||||
service=service, host=host)
|
||||
|
||||
try:
|
||||
from sqlalchemy import create_engine
|
||||
|
@ -292,7 +292,7 @@ class TracedMeta(type):
|
||||
traced - E.g. wsgi, rpc, db, etc...
|
||||
"""
|
||||
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_private = trace_args.pop("trace_private", False)
|
||||
@ -321,7 +321,7 @@ class TracedMeta(type):
|
||||
attr_name)))
|
||||
|
||||
|
||||
class Trace(object):
|
||||
class Trace:
|
||||
|
||||
def __init__(self, name, info=None):
|
||||
"""With statement way to use profiler start()/stop().
|
||||
@ -355,7 +355,7 @@ class Trace(object):
|
||||
stop()
|
||||
|
||||
|
||||
class _Profiler(object):
|
||||
class _Profiler:
|
||||
|
||||
def __init__(self, hmac_key, base_id=None, parent_id=None):
|
||||
self.hmac_key = hmac_key
|
||||
|
@ -30,7 +30,7 @@ LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@profiler.trace_cls("rpc", hide_args=True)
|
||||
class Foo(object):
|
||||
class Foo:
|
||||
def bar(self, x):
|
||||
return self.baz(x, x)
|
||||
|
||||
@ -44,7 +44,7 @@ class DriverTestCase(test.FunctionalTestCase):
|
||||
PROJECT = "project"
|
||||
|
||||
def setUp(self):
|
||||
super(DriverTestCase, self).setUp()
|
||||
super().setUp()
|
||||
CONF(["--config-file", os.path.dirname(__file__) + "/config.cfg"])
|
||||
opts.set_defaults(CONF,
|
||||
enabled=True,
|
||||
|
@ -28,6 +28,6 @@ class FunctionalTestCase(TestCase):
|
||||
"""Base for functional tests"""
|
||||
|
||||
def setUp(self):
|
||||
super(FunctionalTestCase, self).setUp()
|
||||
super().setUp()
|
||||
|
||||
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
|
||||
|
@ -32,16 +32,16 @@ class ShellTestCase(test.TestCase):
|
||||
TRACE_ID = "c598094d-bbee-40b6-b317-d76003b679d3"
|
||||
|
||||
def setUp(self):
|
||||
super(ShellTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self.old_environment = os.environ.copy()
|
||||
|
||||
def tearDown(self):
|
||||
super(ShellTestCase, self).tearDown()
|
||||
super().tearDown()
|
||||
os.environ = self.old_environment
|
||||
|
||||
def _trace_show_cmd(self, format_=None):
|
||||
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("osprofiler.cmd.shell.OSProfilerShell")
|
||||
|
@ -88,7 +88,7 @@ class TitlesTestCase(test.TestCase):
|
||||
trailing_spaces = re.findall(" +$", line)
|
||||
self.assertEqual(
|
||||
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):
|
||||
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)
|
||||
|
||||
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),
|
||||
"%s is not a directory" % spec_dir)
|
||||
|
@ -22,7 +22,7 @@ from osprofiler.tests import test
|
||||
class ElasticsearchTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(ElasticsearchTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self.elasticsearch = ElasticsearchDriver(
|
||||
"elasticsearch://localhost:9001/")
|
||||
self.elasticsearch.project = "project"
|
||||
|
@ -27,7 +27,7 @@ from jaeger_client import Config
|
||||
class JaegerTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(JaegerTestCase, self).setUp()
|
||||
super().setUp()
|
||||
|
||||
opts.set_defaults(cfg.CONF)
|
||||
cfg.CONF.set_default(
|
||||
|
@ -29,7 +29,7 @@ class LogInsightDriverTestCase(test.TestCase):
|
||||
BASE_ID = "8d28af1e-acc0-498c-9890-6908e33eff5f"
|
||||
|
||||
def setUp(self):
|
||||
super(LogInsightDriverTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self._client = mock.Mock(spec=loginsight.LogInsightClient)
|
||||
self._project = "cinder"
|
||||
self._service = "osapi_volume"
|
||||
@ -160,7 +160,7 @@ class LogInsightDriverTestCase(test.TestCase):
|
||||
class LogInsightClientTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(LogInsightClientTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self._host = "localhost"
|
||||
self._username = "username"
|
||||
self._password = "password" # nosec
|
||||
|
@ -21,7 +21,7 @@ from osprofiler.tests import test
|
||||
|
||||
class MongoDBParserTestCase(test.TestCase):
|
||||
def setUp(self):
|
||||
super(MongoDBParserTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self.mongodb = MongoDB("mongodb://localhost")
|
||||
|
||||
def test_build_empty_tree(self):
|
||||
|
@ -24,7 +24,7 @@ from osprofiler.tests import test
|
||||
class OTLPTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(OTLPTestCase, self).setUp()
|
||||
super().setUp()
|
||||
|
||||
opts.set_defaults(cfg.CONF)
|
||||
|
||||
|
@ -24,7 +24,7 @@ from osprofiler.tests import test
|
||||
|
||||
class RedisParserTestCase(test.TestCase):
|
||||
def setUp(self):
|
||||
super(RedisParserTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self.redisdb = Redis("redis://localhost:6379")
|
||||
|
||||
def test_build_empty_tree(self):
|
||||
|
@ -24,7 +24,7 @@ class NotifierTestCase(test.TestCase):
|
||||
def tearDown(self):
|
||||
notifier.set(notifier._noop_notifier) # restore defaults
|
||||
notifier.clear_notifier_cache()
|
||||
super(NotifierTestCase, self).tearDown()
|
||||
super().tearDown()
|
||||
|
||||
def test_set(self):
|
||||
|
||||
|
@ -23,7 +23,7 @@ from osprofiler.tests import test
|
||||
|
||||
class ConfigTestCase(test.TestCase):
|
||||
def setUp(self):
|
||||
super(ConfigTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self.conf_fixture = self.useFixture(fixture.Config())
|
||||
|
||||
def test_options_defaults(self):
|
||||
|
@ -285,7 +285,7 @@ class TraceDecoratorTestCase(test.TestCase):
|
||||
mock_stop.assert_called_once_with(info=stop_info)
|
||||
|
||||
|
||||
class FakeTracedCls(object):
|
||||
class FakeTracedCls:
|
||||
|
||||
def method1(self, a, b, c=10):
|
||||
return a + b + c
|
||||
@ -488,7 +488,7 @@ class TraceClsDecoratorTestCase(test.TestCase):
|
||||
self.assertFalse(mock_stop.called)
|
||||
|
||||
|
||||
class FakeTraceWithMetaclassBase(object, metaclass=profiler.TracedMeta):
|
||||
class FakeTraceWithMetaclassBase(metaclass=profiler.TracedMeta):
|
||||
__trace_args__ = {"name": "rpc",
|
||||
"info": {"a": 10}}
|
||||
|
||||
|
@ -137,7 +137,7 @@ class UtilsTestCase(test.TestCase):
|
||||
|
||||
def test_itersubclasses(self):
|
||||
|
||||
class A(object):
|
||||
class A:
|
||||
pass
|
||||
|
||||
class B(A):
|
||||
|
@ -31,7 +31,7 @@ def dummy_app(environ, response):
|
||||
class WebTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(WebTestCase, self).setUp()
|
||||
super().setUp()
|
||||
profiler.clean()
|
||||
self.addCleanup(profiler.clean)
|
||||
|
||||
@ -61,7 +61,7 @@ class WebTestCase(test.TestCase):
|
||||
|
||||
class WebMiddlewareTestCase(test.TestCase):
|
||||
def setUp(self):
|
||||
super(WebMiddlewareTestCase, self).setUp()
|
||||
super().setUp()
|
||||
profiler.clean()
|
||||
# it's default state of _ENABLED param, so let's set it here
|
||||
web._ENABLED = None
|
||||
@ -69,7 +69,7 @@ class WebMiddlewareTestCase(test.TestCase):
|
||||
|
||||
def tearDown(self):
|
||||
web.enable()
|
||||
super(WebMiddlewareTestCase, self).tearDown()
|
||||
super().tearDown()
|
||||
|
||||
def test_factory(self):
|
||||
mock_app = mock.MagicMock()
|
||||
|
@ -65,7 +65,7 @@ def enable(hmac_keys=None):
|
||||
_HMAC_KEYS = utils.split(hmac_keys or "")
|
||||
|
||||
|
||||
class WsgiMiddleware(object):
|
||||
class WsgiMiddleware:
|
||||
"""WSGI Middleware that enables tracing for an application."""
|
||||
|
||||
def __init__(self, application, hmac_keys=None, enabled=False, **kwargs):
|
||||
|
@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
|
@ -37,7 +37,7 @@ def main(argv):
|
||||
os.path.join(root, 'test-requirements.txt'),
|
||||
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'
|
||||
install = install_venv.InstallVenv(root, venv, pip_requires, test_requires,
|
||||
py_version, project)
|
||||
|
Loading…
x
Reference in New Issue
Block a user