Uses explicit imports for _

Previously `_` was monkeypatched in tests/core.py and bin/keystone-*.
This meant that if a developer was not running the tests exactly as
the documentation described they would not work. Even importing
certain modules in a interactive Python interpreter would fail unless
keystone.tests was imported first. Monkeypatching was removed and
explicit import for `_` was added.

Co-Authored-By: David Stanek <dstanek@dstanek.com>
Change-Id: I8b25b5b6d83fb873e25a8fab7686babf1d2261fa
Closes-Bug: #1255518
This commit is contained in:
Ilya Pekelny 2013-11-29 16:46:08 +02:00 committed by Morgan Fainberg
parent d906f57748
commit 5f3fcf1123
63 changed files with 84 additions and 36 deletions

View File

@ -35,11 +35,11 @@ from paste import deploy
import pbr.version
from keystone.openstack.common import gettextutils
# NOTE(blk-u):
# gettextutils.install() must run to set _ before importing any modules that
# contain static translated strings.
gettextutils.install('keystone', lazy=True)
# NOTE(dstanek): gettextutils.enable_lazy() must be called before
# gettextutils._() is called to ensure it has the desired lazy lookup
# behavior. This includes cases, like keystone.exceptions, where
# gettextutils._() is called at import time.
gettextutils.enable_lazy()
from keystone.common import dependency
from keystone.common import environment

View File

@ -28,10 +28,11 @@ if os.path.exists(os.path.join(possible_topdir,
sys.path.insert(0, possible_topdir)
from keystone.openstack.common import gettextutils
# NOTE(blk-u): gettextutils.install() must run to set _ before importing any
# modules that contain static translated strings.
gettextutils.install('keystone', lazy=True)
# NOTE(dstanek): gettextutils.enable_lazy() must be called before
# gettextutils._() is called to ensure it has the desired lazy lookup
# behavior. This includes cases, like keystone.exceptions, where
# gettextutils._() is called at import time.
gettextutils.enable_lazy()
from keystone import cli
from keystone.common import environment

View File

@ -17,13 +17,6 @@ import os
from paste import deploy
from keystone.openstack.common import gettextutils
# NOTE(blk-u):
# gettextutils.install() must run to set _ before importing any modules that
# contain static translated strings.
gettextutils.install('keystone', lazy=True)
from keystone.common import dependency
from keystone.common import environment
from keystone.common import sql

View File

@ -16,6 +16,7 @@ from keystone import assignment
from keystone import clean
from keystone.common import kvs
from keystone import exception
from keystone.openstack.common.gettextutils import _
class Assignment(kvs.Base, assignment.Driver):
@ -85,7 +86,7 @@ class Assignment(kvs.Base, assignment.Driver):
metadata_keys = filter(lambda x: x.startswith("metadata_user-"),
self.db.keys())
for key in metadata_keys:
_, meta_project_or_domain_id, meta_user_id = key.split('-')
i, meta_project_or_domain_id, meta_user_id = key.split('-')
if meta_project_or_domain_id != tenant_id:
# target is not the project, so on to next metadata.
@ -138,7 +139,7 @@ class Assignment(kvs.Base, assignment.Driver):
metadata_keys = filter(lambda x: x.startswith('metadata_user-'),
self.db.keys())
for key in metadata_keys:
_, meta_project_or_domain_id, meta_user_id = key.split('-')
i, meta_project_or_domain_id, meta_user_id = key.split('-')
if meta_user_id != user_id:
# Not the user, so on to next metadata.
@ -219,7 +220,7 @@ class Assignment(kvs.Base, assignment.Driver):
self.db.keys())
for key in metadata_keys:
template = {}
_, meta_project_or_domain_id, template['user_id'] = key.split('-')
i, meta_project_or_domain_id, template['user_id'] = key.split('-')
try:
self.get_project(meta_project_or_domain_id)
template['project_id'] = meta_project_or_domain_id
@ -238,7 +239,7 @@ class Assignment(kvs.Base, assignment.Driver):
self.db.keys())
for key in metadata_keys:
template = {}
_, meta_project_or_domain_id, template['group_id'] = key.split('-')
i, meta_project_or_domain_id, template['group_id'] = key.split('-')
try:
self.get_project(meta_project_or_domain_id)
template['project_id'] = meta_project_or_domain_id
@ -374,7 +375,7 @@ class Assignment(kvs.Base, assignment.Driver):
metadata_keys = filter(lambda x: x.startswith('metadata_user-'),
self.db.keys())
for key in metadata_keys:
_, meta_project_or_domain_id, meta_user_id = key.split('-')
i, meta_project_or_domain_id, meta_user_id = key.split('-')
try:
self.delete_grant(role_id,
project_id=meta_project_or_domain_id,
@ -390,7 +391,7 @@ class Assignment(kvs.Base, assignment.Driver):
metadata_keys = filter(lambda x: x.startswith('metadata_group-'),
self.db.keys())
for key in metadata_keys:
_, meta_project_or_domain_id, meta_group_id = key.split('-')
i, meta_project_or_domain_id, meta_group_id = key.split('-')
try:
self.delete_grant(role_id,
project_id=meta_project_or_domain_id,

View File

@ -25,6 +25,7 @@ from keystone.common import models
from keystone import config
from keystone import exception
from keystone.identity.backends import ldap as ldap_identity
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
@ -293,7 +294,7 @@ class Assignment(assignment.Driver):
conn = self.group.get_connection()
roles = conn.search_s(dn, ldap.SCOPE_ONELEVEL,
query, ['%s' % '1.1'])
for role_dn, _ in roles:
for role_dn, i in roles:
conn.delete_s(role_dn)
except ldap.NO_SUCH_OBJECT:
pass

View File

@ -21,6 +21,7 @@ from keystone.common.sql import migration_helpers
from keystone import config
from keystone import exception
from keystone.openstack.common.db.sqlalchemy import migration
from keystone.openstack.common.gettextutils import _
CONF = config.CONF

View File

@ -27,6 +27,7 @@ from keystone.common import controller
from keystone.common import dependency
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -26,6 +26,7 @@ from keystone.common import manager
from keystone import config
from keystone import exception
from keystone import notifications
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -23,6 +23,7 @@ from keystone.common import wsgi
from keystone import config
from keystone.contrib import federation
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import importutils
from keystone.openstack.common import log
from keystone.openstack.common import timeutils

View File

@ -22,6 +22,7 @@ from keystone import auth
from keystone.common import config
from keystone.common import dependency
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import versionutils

View File

@ -17,6 +17,7 @@ from keystone.common import dependency
from keystone.contrib.oauth1 import core as oauth
from keystone.contrib.oauth1 import validator
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import timeutils

View File

@ -15,6 +15,7 @@
from keystone import auth
from keystone.common import dependency
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
METHOD_NAME = 'password'

View File

@ -20,6 +20,7 @@ from keystone.catalog.backends import kvs
from keystone.catalog import core
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import versionutils

View File

@ -21,6 +21,7 @@ from keystone.common import controller
from keystone.common import dependency
from keystone.common import wsgi
from keystone import exception
from keystone.openstack.common.gettextutils import _
INTERFACES = ['public', 'internal', 'admin']

View File

@ -24,6 +24,7 @@ from keystone.common import driver_hints
from keystone.common import manager
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -15,6 +15,7 @@
import six
from keystone import exception
from keystone.openstack.common.gettextutils import _
def check_length(property_name, value, min_length=1, max_length=64):

View File

@ -19,6 +19,7 @@
import collections
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -43,6 +43,8 @@ import string
import six
from six.moves import urllib
from keystone.openstack.common.gettextutils import _
class InvalidBase64Error(ValueError):
pass

View File

@ -20,6 +20,7 @@ from dogpile.cache import util as dp_util
import six
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import importutils
from keystone.openstack.common import log
from keystone.openstack.common import timeutils

View File

@ -20,6 +20,7 @@ from dogpile.cache import util
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import importutils
from keystone.openstack.common import log

View File

@ -22,6 +22,7 @@ from keystone.common import utils
from keystone.common import wsgi
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import versionutils

View File

@ -16,6 +16,7 @@ import functools
import os
from keystone.common import config
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
CONF = config.CONF

View File

@ -24,6 +24,7 @@ import eventlet
import eventlet.wsgi
import greenlet
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -27,6 +27,7 @@ from dogpile.cache.backends import memcached
from keystone.common import manager
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -19,6 +19,7 @@ import ldap.filter
import six
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
LOG = log.getLogger(__name__)

View File

@ -93,11 +93,13 @@ and consumption of PEM formatted data including:
"""
import base64
from keystone.common import base64utils
import re
import six
from keystone.common import base64utils
from keystone.openstack.common.gettextutils import _
PEM_TYPE_TO_HEADER = {
u'cms': u'CMS',

View File

@ -25,6 +25,8 @@ import re
import six
from keystone.openstack.common.gettextutils import _
DOCTYPE = '<?xml version="1.0" encoding="UTF-8"?>'
XMLNS = 'http://docs.openstack.org/identity/api/v2.0'

View File

@ -34,6 +34,7 @@ from keystone.openstack.common.db import exception as db_exception
from keystone.openstack.common.db import options as db_options
from keystone.openstack.common.db.sqlalchemy import models
from keystone.openstack.common.db.sqlalchemy import session as db_session
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import jsonutils

View File

@ -16,6 +16,7 @@ import sqlalchemy as sql
from keystone.common import utils
from keystone import exception
from keystone.openstack.common.gettextutils import _
def upgrade(migrate_engine):

View File

@ -25,6 +25,7 @@ from keystone.common import sql
from keystone import contrib
from keystone import exception
from keystone.openstack.common.db.sqlalchemy import migration
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import importutils

View File

@ -29,6 +29,7 @@ import six
from keystone.common import config
from keystone.common import environment
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import strutils
from six import moves

View File

@ -28,6 +28,7 @@ from keystone.common import dependency
from keystone.common import utils
from keystone import exception
from keystone.openstack.common import gettextutils
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import importutils
from keystone.openstack.common import jsonutils
from keystone.openstack.common import log

View File

@ -44,9 +44,9 @@ from keystone.common import dependency
from keystone.common import utils
from keystone.common import wsgi
from keystone import exception
from keystone import token
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import jsonutils
from keystone import token
@dependency.requires('assignment_api', 'catalog_api', 'credential_api',

View File

@ -17,6 +17,7 @@ from keystone.common.sql import migration_helpers
from keystone.contrib import endpoint_filter
from keystone import exception
from keystone.openstack.common.db.sqlalchemy import migration
from keystone.openstack.common.gettextutils import _
class ProjectEndpoint(sql.ModelBase, sql.DictBase):

View File

@ -17,6 +17,7 @@ from keystone.common import dependency
from keystone.common import manager
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -18,6 +18,7 @@ import jsonschema
import six
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -24,6 +24,7 @@ from keystone.contrib import oauth1
from keystone.contrib.oauth1 import core
from keystone import exception
from keystone.openstack.common.db.sqlalchemy import migration
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import jsonutils
from keystone.openstack.common import timeutils

View File

@ -21,6 +21,7 @@ from keystone import config
from keystone.contrib.oauth1 import core as oauth1
from keystone.contrib.oauth1 import validator
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import jsonutils
from keystone.openstack.common import timeutils

View File

@ -13,6 +13,7 @@
from keystone.common import controller
from keystone.common import dependency
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import timeutils

View File

@ -23,6 +23,7 @@ from keystone import config
from keystone.contrib.revoke import model
from keystone import exception
from keystone import notifications
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import timeutils

View File

@ -19,6 +19,7 @@ from keystone.common import controller
from keystone.common import dependency
from keystone.common import driver_hints
from keystone import exception
from keystone.openstack.common.gettextutils import _
@dependency.requires('credential_api')

View File

@ -22,6 +22,7 @@ from keystone.common import dependency
from keystone.common import manager
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -16,6 +16,7 @@ from keystone.common import kvs
from keystone.common import utils
from keystone import exception
from keystone import identity
from keystone.openstack.common.gettextutils import _
class _UserIdToDomainId(object):

View File

@ -25,6 +25,7 @@ from keystone.common import utils
from keystone import config
from keystone import exception
from keystone import identity
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -19,6 +19,7 @@ from keystone.common import utils
from keystone import exception
from keystone import identity
from keystone.openstack.common.db.sqlalchemy import migration
from keystone.openstack.common.gettextutils import _
# Import assignment sql to ensure that the models defined in there are
# available for the reference from User and Group to Domain.id.

View File

@ -24,6 +24,7 @@ from keystone.common import controller
from keystone.common import dependency
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import versionutils

View File

@ -28,6 +28,7 @@ from keystone.common import manager
from keystone import config
from keystone import exception
from keystone import notifications
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import importutils
from keystone.openstack.common import log
from keystone.openstack.common import versionutils

View File

@ -21,6 +21,7 @@ from keystone.common import serializer
from keystone.common import utils
from keystone.common import wsgi
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import jsonutils
from keystone.openstack.common import log
from keystone.openstack.common import versionutils

View File

@ -25,6 +25,7 @@ from pycadf import cadftype
from pycadf import eventfactory
from pycadf import resource
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
notifier_opts = [

View File

@ -20,6 +20,7 @@ import os.path
from keystone.common import utils
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import policy as common_policy
from keystone import policy

View File

@ -35,15 +35,6 @@ import webob
from keystone.openstack.common.fixture import mockpatch
from keystone.openstack.common import gettextutils
# NOTE(blk-u):
# gettextutils.install() must run to set _ before importing any modules that
# contain static translated strings.
#
# Configure gettextutils for deferred translation of messages
# so that error messages in responses can be translated according to the
# Accept-Language in the request rather than the Keystone server locale.
gettextutils.install('keystone', lazy=True)
# NOTE(ayoung)
# environment.use_eventlet must run before any of the code that will
# call the eventlet monkeypatching.
@ -63,6 +54,7 @@ from keystone import notifications
from keystone.openstack.common.db import options as db_options
from keystone.openstack.common.db.sqlalchemy import migration
from keystone.openstack.common.fixture import config as config_fixture
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone import service
from keystone.tests import fixtures as ksfixtures

View File

@ -30,6 +30,7 @@ import six
from six import moves
from keystone.common import utils
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -571,7 +571,7 @@ class KVSTest(tests.TestCase):
class TestMemcachedBackend(tests.TestCase):
@mock.patch('__builtin__._', six.text_type)
@mock.patch('keystone.common.kvs.backends.memcached._', six.text_type)
def test_invalid_backend_fails_initialization(self):
raises_valueerror = matchers.Raises(matchers.MatchesException(
ValueError, r'.*FakeBackend.*'))

View File

@ -23,6 +23,7 @@ from keystone.common import wsgi
from keystone import exception
from keystone.openstack.common.fixture import moxstubout
from keystone.openstack.common import gettextutils
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import jsonutils
from keystone import tests

View File

@ -23,6 +23,7 @@ import six
from keystone.common import kvs
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import timeutils
from keystone import token

View File

@ -23,6 +23,7 @@ from keystone.common import dependency
from keystone.common import wsgi
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import timeutils
from keystone.token import core

View File

@ -26,6 +26,7 @@ from keystone.common import dependency
from keystone.common import manager
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import timeutils
from keystone.openstack.common import versionutils

View File

@ -24,6 +24,7 @@ from keystone.common import manager
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import timeutils

View File

@ -22,6 +22,7 @@ from keystone.common import dependency
from keystone import config
from keystone.contrib import federation
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone import token
from keystone.token import provider
from keystone import trust

View File

@ -21,6 +21,7 @@ from keystoneclient.common import cms
from keystone.common import environment
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.token.providers import common

View File

@ -21,6 +21,7 @@ from keystone.common import controller
from keystone.common import dependency
from keystone import config
from keystone import exception
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log
from keystone.openstack.common import timeutils

View File

@ -23,6 +23,7 @@ from keystone.common import manager
from keystone import config
from keystone import exception
from keystone import notifications
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import log

View File

@ -49,3 +49,6 @@ commands=
[testenv:sample_config]
envdir = {toxworkdir}/venv
commands = {toxinidir}/tools/config/generate_sample.sh
[hacking]
import_exceptions = keystone.openstack.common.gettextutils._