From 298c86f1e6f2c7e7feba63580b327542d40e8dad Mon Sep 17 00:00:00 2001 From: haixin Date: Wed, 30 Sep 2020 15:26:27 +0800 Subject: Remove all usage of six library Replace six with Python 3 style code. Change-Id: I3d0c35e237484409d8410601ec482fac0dacf30d --- oslo_policy/_checks.py | 8 +++----- oslo_policy/_parser.py | 9 +++------ oslo_policy/policy.py | 7 +++---- oslo_policy/tests/base.py | 4 ++-- oslo_policy/tests/test_external.py | 2 +- oslo_policy/tests/test_parser.py | 4 +--- oslo_policy/tests/test_policy.py | 13 ++++--------- 7 files changed, 17 insertions(+), 30 deletions(-) (limited to 'oslo_policy') diff --git a/oslo_policy/_checks.py b/oslo_policy/_checks.py index b7e7b91..c813af6 100644 --- a/oslo_policy/_checks.py +++ b/oslo_policy/_checks.py @@ -19,7 +19,6 @@ import abc import ast import inspect -import six import stevedore if hasattr(inspect, 'getfullargspec'): @@ -86,8 +85,7 @@ def _check(rule, target, creds, enforcer, current_rule): return rule(*rule_args) -@six.add_metaclass(abc.ABCMeta) -class BaseCheck(object): +class BaseCheck(metaclass=abc.ABCMeta): """Abstract base class for Check classes.""" @abc.abstractmethod @@ -316,7 +314,7 @@ class GenericCheck(Check): ''' if len(path_segments) == 0: - return match == six.text_type(test_value) + return match == str(test_value) key, path_segments = path_segments[0], path_segments[1:] try: test_value = test_value[key] @@ -341,7 +339,7 @@ class GenericCheck(Check): try: # Try to interpret self.kind as a literal test_value = ast.literal_eval(self.kind) - return match == six.text_type(test_value) + return match == str(test_value) except ValueError: pass diff --git a/oslo_policy/_parser.py b/oslo_policy/_parser.py index 06dae07..29e76a5 100644 --- a/oslo_policy/_parser.py +++ b/oslo_policy/_parser.py @@ -18,8 +18,6 @@ import logging import re -import six - from oslo_policy import _checks @@ -72,8 +70,7 @@ class ParseStateMeta(type): return super(ParseStateMeta, mcs).__new__(mcs, name, bases, cls_dict) -@six.add_metaclass(ParseStateMeta) -class ParseState(object): +class ParseState(metaclass=ParseStateMeta): """Implement the core of parsing the policy language. Uses a greedy reduction algorithm to reduce a sequence of tokens into @@ -246,7 +243,7 @@ def _parse_list_rule(rule): continue # Handle bare strings - if isinstance(inner_rule, six.string_types): + if isinstance(inner_rule, str): inner_rule = [inner_rule] # Parse the inner rules into Check objects @@ -350,6 +347,6 @@ def parse_rule(rule): """Parses a policy rule into a tree of :class:`.Check` objects.""" # If the rule is a string, it's in the policy language - if isinstance(rule, six.string_types): + if isinstance(rule, str): return _parse_text_rule(rule) return _parse_list_rule(rule) diff --git a/oslo_policy/policy.py b/oslo_policy/policy.py index 781502c..9495b0e 100644 --- a/oslo_policy/policy.py +++ b/oslo_policy/policy.py @@ -231,7 +231,6 @@ from oslo_config import cfg from oslo_context import context from oslo_serialization import jsonutils from oslo_utils import strutils -import six import yaml from oslo_policy import _cache_handler @@ -387,7 +386,7 @@ def parse_file_contents(data): except yaml.YAMLError as e: # For backwards-compatibility, convert yaml error to ValueError, # which is what JSON loader raised. - raise ValueError(six.text_type(e)) + raise ValueError(str(e)) return parsed or {} @@ -456,7 +455,7 @@ class Rules(dict): if self.default_rule not in self: raise KeyError(key) - elif isinstance(self.default_rule, six.string_types): + elif isinstance(self.default_rule, str): return self[self.default_rule] def __str__(self): @@ -1176,7 +1175,7 @@ class RuleDefault(object): if not isinstance(scope_types, list): raise ValueError(msg) for scope_type in scope_types: - if not isinstance(scope_type, six.string_types): + if not isinstance(scope_type, str): raise ValueError(msg) if scope_types.count(scope_type) > 1: raise ValueError( diff --git a/oslo_policy/tests/base.py b/oslo_policy/tests/base.py index 670ef4b..698e4dd 100644 --- a/oslo_policy/tests/base.py +++ b/oslo_policy/tests/base.py @@ -14,6 +14,7 @@ # under the License. import codecs +import io import os import os.path import sys @@ -21,7 +22,6 @@ import sys import fixtures from oslo_config import fixture as config from oslotest import base as test_base -from six import moves from oslo_policy import _checks from oslo_policy import policy @@ -55,7 +55,7 @@ class PolicyBaseTestCase(test_base.BaseTestCase): f.write(contents) def _capture_stdout(self): - self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) + self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) return sys.stdout diff --git a/oslo_policy/tests/test_external.py b/oslo_policy/tests/test_external.py index 80fdec8..478920b 100644 --- a/oslo_policy/tests/test_external.py +++ b/oslo_policy/tests/test_external.py @@ -18,7 +18,7 @@ from unittest import mock from oslo_serialization import jsonutils from requests_mock.contrib import fixture as rm_fixture -import six.moves.urllib.parse as urlparse +from urllib import parse as urlparse from oslo_policy import _external from oslo_policy import opts diff --git a/oslo_policy/tests/test_parser.py b/oslo_policy/tests/test_parser.py index f1ac36b..3ca73b4 100644 --- a/oslo_policy/tests/test_parser.py +++ b/oslo_policy/tests/test_parser.py @@ -16,7 +16,6 @@ from unittest import mock from oslotest import base as test_base -import six from oslo_policy import _checks from oslo_policy import _parser @@ -173,8 +172,7 @@ class ParseStateMetaTestCase(test_base.BaseTestCase): self.assertEqual([['d', 'e', 'f'], ['a', 'b', 'c']], spam.reducers) def test_parse_state_meta(self): - @six.add_metaclass(_parser.ParseStateMeta) - class FakeState(object): + class FakeState(metaclass=_parser.ParseStateMeta): @_parser.reducer('a', 'b', 'c') @_parser.reducer('d', 'e', 'f') diff --git a/oslo_policy/tests/test_policy.py b/oslo_policy/tests/test_policy.py index 7b05362..d9f6fa0 100644 --- a/oslo_policy/tests/test_policy.py +++ b/oslo_policy/tests/test_policy.py @@ -23,7 +23,6 @@ from oslo_config import cfg from oslo_context import context from oslo_serialization import jsonutils from oslotest import base as test_base -import six from oslo_policy import _cache_handler from oslo_policy import _checks @@ -349,8 +348,7 @@ class EnforcerTest(base.PolicyBaseTestCase): self.enforcer.load_rules(False) self.assertIsNotNone(self.enforcer.rules) - old = six.next(six.itervalues( - self.enforcer._policy_dir_mtimes)) + old = next(iter(self.enforcer._policy_dir_mtimes)) self.assertEqual(1, len(self.enforcer._policy_dir_mtimes)) # Touch the file @@ -361,8 +359,7 @@ class EnforcerTest(base.PolicyBaseTestCase): self.enforcer.load_rules(False) self.assertEqual(1, len(self.enforcer._policy_dir_mtimes)) - self.assertEqual(old, six.next(six.itervalues( - self.enforcer._policy_dir_mtimes))) + self.assertEqual(old, next(iter(self.enforcer._policy_dir_mtimes))) loaded_rules = jsonutils.loads(str(self.enforcer.rules)) self.assertEqual('is_admin:True', loaded_rules['admin']) @@ -385,14 +382,12 @@ class EnforcerTest(base.PolicyBaseTestCase): self.enforcer.load_rules(False) self.assertIsNotNone(self.enforcer.rules) - old = six.next(six.itervalues( - self.enforcer._policy_dir_mtimes)) + old = next(iter(self.enforcer._policy_dir_mtimes)) self.assertEqual(1, len(self.enforcer._policy_dir_mtimes)) self.enforcer.load_rules(False) self.assertEqual(1, len(self.enforcer._policy_dir_mtimes)) - self.assertEqual(old, six.next(six.itervalues( - self.enforcer._policy_dir_mtimes))) + self.assertEqual(old, next(iter(self.enforcer._policy_dir_mtimes))) loaded_rules = jsonutils.loads(str(self.enforcer.rules)) self.assertEqual('is_admin:True', loaded_rules['admin']) -- cgit v1.2.1