Bug 1075090 -- Fixing log messages in python source code to support internationalization.

Change-Id: I6b50abaa82effad8feaaac8d85086ca8b5d42590
This commit is contained in:
Nachiappan VR N 2012-11-14 10:01:59 -08:00
parent 75277cf1ae
commit 84a0b2df59
15 changed files with 80 additions and 72 deletions

View File

@ -106,7 +106,7 @@ class TemplatedCatalog(kvs.Catalog):
try: try:
self.templates = parse_templates(open(template_file)) self.templates = parse_templates(open(template_file))
except IOError: except IOError:
LOG.critical('Unable to open template file %s' % template_file) LOG.critical(_('Unable to open template file %s') % template_file)
raise raise
def get_catalog(self, user_id, tenant_id, metadata=None): def get_catalog(self, user_id, tenant_id, metadata=None):

View File

@ -41,17 +41,19 @@ def format_url(url, data):
except AttributeError: except AttributeError:
return None return None
except KeyError as e: except KeyError as e:
LOG.error("Malformed endpoint %s - unknown key %s" % LOG.error(_("Malformed endpoint %(url)s - unknown key %(keyerror)s") %
(url, str(e))) {"url": url,
"keyerror": str(e)})
raise exception.MalformedEndpoint(endpoint=url) raise exception.MalformedEndpoint(endpoint=url)
except TypeError as e: except TypeError as e:
LOG.error("Malformed endpoint %s - type mismatch %s \ LOG.error(_("Malformed endpoint %(url)s - unknown key %(keyerror)s"
(are you missing brackets ?)" % "(are you missing brackets ?)") %
(url, str(e))) {"url": url,
"keyerror": str(e)})
raise exception.MalformedEndpoint(endpoint=url) raise exception.MalformedEndpoint(endpoint=url)
except ValueError as e: except ValueError as e:
LOG.error("Malformed endpoint %s - incomplete format \ LOG.error(_("Malformed endpoint %s - incomplete format \
(are you missing a type notifier ?)" % url) (are you missing a type notifier ?)") % url)
raise exception.MalformedEndpoint(endpoint=url) raise exception.MalformedEndpoint(endpoint=url)
return result return result

View File

@ -20,20 +20,21 @@ from keystone import exception
def check_length(property_name, value, min_length=1, max_length=64): def check_length(property_name, value, min_length=1, max_length=64):
if len(value) < min_length: if len(value) < min_length:
if min_length == 1: if min_length == 1:
msg = "%s cannot be empty." % property_name msg = _("%s cannot be empty.") % property_name
else: else:
msg = ("%(property_name)s cannot be less than " msg = (_("%(property_name)s cannot be less than "
"%(min_length)s characters.") % locals() "%(min_length)s characters.")) % locals()
raise exception.ValidationError(msg) raise exception.ValidationError(msg)
if len(value) > max_length: if len(value) > max_length:
msg = ("%(property_name)s should not be greater than " msg = (_("%(property_name)s should not be greater than "
"%(max_length)s characters.") % locals() "%(max_length)s characters.")) % locals()
raise exception.ValidationError(msg) raise exception.ValidationError(msg)
def check_type(property_name, value, expected_type, display_expected_type): def check_type(property_name, value, expected_type, display_expected_type):
if not isinstance(value, expected_type): if not isinstance(value, expected_type):
msg = "%(property_name)s is not a %(display_expected_type)s" % locals() msg = _("%(property_name)s is not a"
"%(display_expected_type)s") % locals()
raise exception.ValidationError(msg) raise exception.ValidationError(msg)

View File

@ -99,8 +99,8 @@ class BufferedHTTPConnection(HTTPConnection):
def getresponse(self): def getresponse(self):
response = HTTPConnection.getresponse(self) response = HTTPConnection.getresponse(self)
LOG.debug('HTTP PERF: %(time).5f seconds to %(method)s ' LOG.debug(_('HTTP PERF: %(time).5f seconds to %(method)s '
'%(host)s:%(port)s %(path)s)', '%(host)s:%(port)s %(path)s)'),
{'time': time.time() - self._connected_time, {'time': time.time() - self._connected_time,
'method': self._method, 'method': self._method,
'host': self.host, 'host': self.host,

View File

@ -41,7 +41,7 @@ def cms_verify(formatted, signing_cert_file_name, ca_file_name):
output, err = process.communicate(formatted) output, err = process.communicate(formatted)
retcode = process.poll() retcode = process.poll()
if retcode: if retcode:
LOG.error('Verify error: %s' % err) LOG.error(_('Verify error: %s') % err)
raise subprocess.CalledProcessError(retcode, "openssl", output=err) raise subprocess.CalledProcessError(retcode, "openssl", output=err)
return output return output
@ -131,7 +131,7 @@ def cms_sign_text(text, signing_cert_file_name, signing_key_file_name):
output, err = process.communicate(text) output, err = process.communicate(text)
retcode = process.poll() retcode = process.poll()
if retcode or "Error" in err: if retcode or "Error" in err:
LOG.error('Signing error: %s' % err) LOG.error(_('Signing error: %s') % err)
raise subprocess.CalledProcessError(retcode, "openssl") raise subprocess.CalledProcessError(retcode, "openssl")
return output return output

View File

@ -168,8 +168,8 @@ class BaseLdap(object):
pass pass
else: else:
raise exception.Conflict(type=self.options_name, raise exception.Conflict(type=self.options_name,
details='Duplicate name, %s.' % details=_('Duplicate name, %s.') %
values['name']) values['name'])
if values.get('id') is not None: if values.get('id') is not None:
try: try:
@ -178,12 +178,13 @@ class BaseLdap(object):
pass pass
else: else:
raise exception.Conflict(type=self.options_name, raise exception.Conflict(type=self.options_name,
details='Duplicate ID, %s.' % details=_('Duplicate ID, %s.') %
values['id']) values['id'])
def create(self, values): def create(self, values):
if not self.allow_create: if not self.allow_create:
msg = 'LDAP backend does not allow %s create' % self.options_name msg = _('LDAP backend does not allow %s create') \
% self.options_name
raise exception.ForbiddenAction(msg) raise exception.ForbiddenAction(msg)
conn = self.get_connection() conn = self.get_connection()
@ -289,7 +290,8 @@ class BaseLdap(object):
def update(self, id, values, old_obj=None): def update(self, id, values, old_obj=None):
if not self.allow_update: if not self.allow_update:
msg = 'LDAP backend does not allow %s update' % self.options_name msg = _('LDAP backend does not allow %s update') \
% self.options_name
raise exception.ForbiddenAction(msg) raise exception.ForbiddenAction(msg)
if old_obj is None: if old_obj is None:
@ -316,7 +318,8 @@ class BaseLdap(object):
def delete(self, id): def delete(self, id):
if not self.allow_delete: if not self.allow_delete:
msg = 'LDAP backend does not allow %s delete' % self.options_name msg = _('LDAP backend does not allow %s delete') \
% self.options_name
raise exception.ForbiddenAction(msg) raise exception.ForbiddenAction(msg)
conn = self.get_connection() conn = self.get_connection()

View File

@ -145,7 +145,7 @@ class FakeLdap(object):
__prefix = 'ldap:' __prefix = 'ldap:'
def __init__(self, url): def __init__(self, url):
LOG.debug('FakeLdap initialize url=%s', url) LOG.debug(_('FakeLdap initialize url=%s'), url)
if url == 'fake://memory': if url == 'fake://memory':
self.db = FakeShelve.get_instance() self.db = FakeShelve.get_instance()
else: else:
@ -155,26 +155,27 @@ class FakeLdap(object):
"""This method is ignored, but provided for compatibility.""" """This method is ignored, but provided for compatibility."""
if server_fail: if server_fail:
raise ldap.SERVER_DOWN raise ldap.SERVER_DOWN
LOG.debug('FakeLdap bind dn=%s', dn) LOG.debug(_('FakeLdap bind dn=%s'), dn)
if dn == 'cn=Admin' and password == 'password': if dn == 'cn=Admin' and password == 'password':
return return
try: try:
attrs = self.db['%s%s' % (self.__prefix, dn)] attrs = self.db['%s%s' % (self.__prefix, dn)]
except KeyError: except KeyError:
LOG.error('FakeLdap bind fail: dn=%s not found', dn) LOG.error(_('FakeLdap bind fail: dn=%s not found'), dn)
raise ldap.NO_SUCH_OBJECT raise ldap.NO_SUCH_OBJECT
db_password = None db_password = None
try: try:
db_password = attrs['userPassword'][0] db_password = attrs['userPassword'][0]
except (KeyError, IndexError): except (KeyError, IndexError):
LOG.error('FakeLdap bind fail: password for dn=%s not found', dn) LOG.error(_('FakeLdap bind fail: password for dn=%s not found'),
dn)
raise ldap.INAPPROPRIATE_AUTH raise ldap.INAPPROPRIATE_AUTH
if not utils.ldap_check_password(password, db_password): if not utils.ldap_check_password(password, db_password):
LOG.error('FakeLdap bind fail: password for dn=%s does' LOG.error(_('FakeLdap bind fail: password for dn=%s does'
' not match' % dn) ' not match') % dn)
raise ldap.INVALID_CREDENTIALS raise ldap.INVALID_CREDENTIALS
def unbind_s(self): def unbind_s(self):
@ -188,10 +189,10 @@ class FakeLdap(object):
raise ldap.SERVER_DOWN raise ldap.SERVER_DOWN
key = '%s%s' % (self.__prefix, dn) key = '%s%s' % (self.__prefix, dn)
LOG.debug('FakeLdap add item: dn=%s, attrs=%s', dn, attrs) LOG.debug(_('FakeLdap add item: dn=%s, attrs=%s'), dn, attrs)
if key in self.db: if key in self.db:
LOG.error('FakeLdap add item failed: dn=%s is' LOG.error(_('FakeLdap add item failed: dn=%s is'
' already in store.', dn) ' already in store.'), dn)
raise ldap.ALREADY_EXISTS(dn) raise ldap.ALREADY_EXISTS(dn)
self.db[key] = dict([(k, v if isinstance(v, list) else [v]) self.db[key] = dict([(k, v if isinstance(v, list) else [v])
@ -204,11 +205,11 @@ class FakeLdap(object):
raise ldap.SERVER_DOWN raise ldap.SERVER_DOWN
key = '%s%s' % (self.__prefix, dn) key = '%s%s' % (self.__prefix, dn)
LOG.debug('FakeLdap delete item: dn=%s', dn) LOG.debug(_('FakeLdap delete item: dn=%s'), dn)
try: try:
del self.db[key] del self.db[key]
except KeyError: except KeyError:
LOG.error('FakeLdap delete item failed: dn=%s not found.', dn) LOG.error(_('FakeLdap delete item failed: dn=%s not found.'), dn)
raise ldap.NO_SUCH_OBJECT raise ldap.NO_SUCH_OBJECT
self.db.sync() self.db.sync()
@ -218,11 +219,11 @@ class FakeLdap(object):
raise ldap.SERVER_DOWN raise ldap.SERVER_DOWN
key = '%s%s' % (self.__prefix, dn) key = '%s%s' % (self.__prefix, dn)
LOG.debug('FakeLdap delete item: dn=%s', dn) LOG.debug(_('FakeLdap delete item: dn=%s'), dn)
try: try:
del self.db[key] del self.db[key]
except KeyError: except KeyError:
LOG.error('FakeLdap delete item failed: dn=%s not found.', dn) LOG.error(_('FakeLdap delete item failed: dn=%s not found.'), dn)
raise ldap.NO_SUCH_OBJECT raise ldap.NO_SUCH_OBJECT
self.db.sync() self.db.sync()
@ -237,11 +238,11 @@ class FakeLdap(object):
raise ldap.SERVER_DOWN raise ldap.SERVER_DOWN
key = '%s%s' % (self.__prefix, dn) key = '%s%s' % (self.__prefix, dn)
LOG.debug('FakeLdap modify item: dn=%s attrs=%s', dn, attrs) LOG.debug(_('FakeLdap modify item: dn=%s attrs=%s'), dn, attrs)
try: try:
entry = self.db[key] entry = self.db[key]
except KeyError: except KeyError:
LOG.error('FakeLdap modify item failed: dn=%s not found.', dn) LOG.error(_('FakeLdap modify item failed: dn=%s not found.'), dn)
raise ldap.NO_SUCH_OBJECT raise ldap.NO_SUCH_OBJECT
for cmd, k, v in attrs: for cmd, k, v in attrs:
@ -258,8 +259,8 @@ class FakeLdap(object):
elif cmd == ldap.MOD_DELETE: elif cmd == ldap.MOD_DELETE:
if v is None: if v is None:
if len(values) == 0: if len(values) == 0:
LOG.error('FakeLdap modify item failed: ' LOG.error(_('FakeLdap modify item failed: '
'item has no attribute "%s" to delete', k) 'item has no attribute "%s" to delete'), k)
raise ldap.NO_SUCH_ATTRIBUTE raise ldap.NO_SUCH_ATTRIBUTE
values[:] = [] values[:] = []
else: else:
@ -269,15 +270,15 @@ class FakeLdap(object):
try: try:
values.remove(val) values.remove(val)
except ValueError: except ValueError:
LOG.error('FakeLdap modify item failed:' LOG.error(_('FakeLdap modify item failed:'
' item has no attribute "%s" with' ' item has no attribute "%s" with'
' value "%s" to delete', k, val) ' value "%s" to delete'), k, val)
raise ldap.NO_SUCH_ATTRIBUTE raise ldap.NO_SUCH_ATTRIBUTE
else: else:
LOG.error('FakeLdap modify item failed: unknown' LOG.error(_('FakeLdap modify item failed: unknown'
' command %s', cmd) ' command %s'), cmd)
raise NotImplementedError('modify_s action %s not implemented' raise NotImplementedError(_('modify_s action %s not'
% cmd) ' implemented') % cmd)
self.db[key] = entry self.db[key] = entry
self.db.sync() self.db.sync()
@ -294,13 +295,14 @@ class FakeLdap(object):
if server_fail: if server_fail:
raise ldap.SERVER_DOWN raise ldap.SERVER_DOWN
LOG.debug('FakeLdap search at dn=%s scope=%s query=%s', LOG.debug(_('FakeLdap search at dn=%s scope=%s query=%s'),
dn, SCOPE_NAMES.get(scope, scope), query) dn, SCOPE_NAMES.get(scope, scope), query)
if scope == ldap.SCOPE_BASE: if scope == ldap.SCOPE_BASE:
try: try:
item_dict = self.db['%s%s' % (self.__prefix, dn)] item_dict = self.db['%s%s' % (self.__prefix, dn)]
except KeyError: except KeyError:
LOG.debug('FakeLdap search fail: dn not found for SCOPE_BASE') LOG.debug(_('FakeLdap search fail: dn not found for'
' SCOPE_BASE'))
raise ldap.NO_SUCH_OBJECT raise ldap.NO_SUCH_OBJECT
results = [(dn, item_dict)] results = [(dn, item_dict)]
elif scope == ldap.SCOPE_SUBTREE: elif scope == ldap.SCOPE_SUBTREE:
@ -313,7 +315,7 @@ class FakeLdap(object):
if re.match('%s\w+=[^,]+,%s' % (self.__prefix, dn), k)] if re.match('%s\w+=[^,]+,%s' % (self.__prefix, dn), k)]
else: else:
LOG.error('FakeLdap search fail: unknown scope %s', scope) LOG.error('FakeLdap search fail: unknown scope %s', scope)
raise NotImplementedError('Search scope %s not implemented.' raise NotImplementedError(_('Search scope %s not implemented.')
% scope) % scope)
objects = [] objects = []

View File

@ -160,7 +160,7 @@ class MySQLPingListener(object):
dbapi_con.cursor().execute('select 1') dbapi_con.cursor().execute('select 1')
except dbapi_con.OperationalError as e: except dbapi_con.OperationalError as e:
if e.args[0] in (2006, 2013, 2014, 2045, 2055): if e.args[0] in (2006, 2013, 2014, 2045, 2055):
logging.warn('Got mysql server has gone away: %s', e) logging.warn(_('Got mysql server has gone away: %s'), e)
raise DisconnectionError("Database server went away") raise DisconnectionError("Database server went away")
else: else:
raise raise

View File

@ -171,4 +171,4 @@ class LegacyMigration(object):
try: try:
self.ec2_driver.create_credential(None, new_dict) self.ec2_driver.create_credential(None, new_dict)
except exc.IntegrityError: except exc.IntegrityError:
LOG.exception('Cannot migrate EC2 credential: %s' % x) LOG.exception(_('Cannot migrate EC2 credential: %s') % x)

View File

@ -55,7 +55,7 @@ def _create_tenants(api, tenants):
'enabled': True, 'enabled': True,
} }
tenant_map[tenant['id']] = tenant_dict['id'] tenant_map[tenant['id']] = tenant_dict['id']
LOG.debug('Create tenant %s' % tenant_dict) LOG.debug(_('Create tenant %s') % tenant_dict)
api.create_tenant(tenant_dict['id'], tenant_dict) api.create_tenant(tenant_dict['id'], tenant_dict)
return tenant_map return tenant_map
@ -71,7 +71,7 @@ def _create_users(api, users):
'enabled': True, 'enabled': True,
} }
user_map[user['id']] = user_dict['id'] user_map[user['id']] = user_dict['id']
LOG.debug('Create user %s' % user_dict) LOG.debug(_('Create user %s') % user_dict)
api.create_user(user_dict['id'], user_dict) api.create_user(user_dict['id'], user_dict)
return user_map return user_map
@ -80,7 +80,7 @@ def _create_memberships(api, memberships, user_map, tenant_map):
for membership in memberships: for membership in memberships:
user_id = user_map[membership['user_id']] user_id = user_map[membership['user_id']]
tenant_id = tenant_map[membership['tenant_id']] tenant_id = tenant_map[membership['tenant_id']]
LOG.debug('Add user %s to tenant %s' % (user_id, tenant_id)) LOG.debug(_('Add user %s to tenant %s') % (user_id, tenant_id))
api.add_user_to_tenant(tenant_id, user_id) api.add_user_to_tenant(tenant_id, user_id)
@ -88,14 +88,14 @@ def _create_roles(api, roles):
role_map = dict((r['name'], r['id']) for r in api.list_roles()) role_map = dict((r['name'], r['id']) for r in api.list_roles())
for role in roles: for role in roles:
if role in role_map: if role in role_map:
LOG.debug('Ignoring existing role %s' % role) LOG.debug(_('Ignoring existing role %s') % role)
continue continue
role_dict = { role_dict = {
'id': _generate_uuid(), 'id': _generate_uuid(),
'name': role, 'name': role,
} }
role_map[role] = role_dict['id'] role_map[role] = role_dict['id']
LOG.debug('Create role %s' % role_dict) LOG.debug(_('Create role %s') % role_dict)
api.create_role(role_dict['id'], role_dict) api.create_role(role_dict['id'], role_dict)
return role_map return role_map
@ -105,7 +105,7 @@ def _assign_roles(api, assignments, role_map, user_map, tenant_map):
role_id = role_map[assignment['role']] role_id = role_map[assignment['role']]
user_id = user_map[assignment['user_id']] user_id = user_map[assignment['user_id']]
tenant_id = tenant_map[assignment['tenant_id']] tenant_id = tenant_map[assignment['tenant_id']]
LOG.debug('Assign role %s to user %s on tenant %s' % LOG.debug(_('Assign role %s to user %s on tenant %s') %
(role_id, user_id, tenant_id)) (role_id, user_id, tenant_id))
api.add_role_to_user_and_tenant(user_id, tenant_id, role_id) api.add_role_to_user_and_tenant(user_id, tenant_id, role_id)
@ -120,6 +120,6 @@ def _create_ec2_creds(ec2_api, identity_api, ec2_creds, user_map):
'user_id': user_id, 'user_id': user_id,
'tenant_id': tenant_id, 'tenant_id': tenant_id,
} }
LOG.debug('Creating ec2 cred for user %s and tenant %s' % LOG.debug(_('Creating ec2 cred for user %s and tenant %s') %
(user_id, tenant_id)) (user_id, tenant_id))
ec2_api.create_credential(None, cred_dict) ec2_api.create_credential(None, cred_dict)

View File

@ -121,7 +121,7 @@ class Ec2Signer(object):
def _calc_signature_2(self, params, verb, server_string, path): def _calc_signature_2(self, params, verb, server_string, path):
"""Generate AWS signature version 2 string.""" """Generate AWS signature version 2 string."""
LOG.debug('using _calc_signature_2') LOG.debug(_('using _calc_signature_2'))
string_to_sign = '%s\n%s\n%s\n' % (verb, server_string, path) string_to_sign = '%s\n%s\n%s\n' % (verb, server_string, path)
if self.hmac_256: if self.hmac_256:
current_hmac = self.hmac_256 current_hmac = self.hmac_256
@ -137,13 +137,13 @@ class Ec2Signer(object):
val = urllib.quote(val, safe='-_~') val = urllib.quote(val, safe='-_~')
pairs.append(urllib.quote(key, safe='') + '=' + val) pairs.append(urllib.quote(key, safe='') + '=' + val)
qs = '&'.join(pairs) qs = '&'.join(pairs)
LOG.debug('query string: %s', qs) LOG.debug(_('query string: %s'), qs)
string_to_sign += qs string_to_sign += qs
LOG.debug('string_to_sign: %s', string_to_sign) LOG.debug(_('string_to_sign: %s'), string_to_sign)
current_hmac.update(string_to_sign) current_hmac.update(string_to_sign)
b64 = base64.b64encode(current_hmac.digest()) b64 = base64.b64encode(current_hmac.digest())
LOG.debug('len(b64)=%d', len(b64)) LOG.debug(_('len(b64)=%d'), len(b64))
LOG.debug('base64 encoded digest: %s', b64) LOG.debug(_('base64 encoded digest: %s'), b64)
return b64 return b64

View File

@ -70,7 +70,7 @@ class Server(object):
def start(self, key=None, backlog=128): def start(self, key=None, backlog=128):
"""Run a WSGI server with the given application.""" """Run a WSGI server with the given application."""
LOG.debug('Starting %(arg0)s on %(host)s:%(port)s' % LOG.debug(_('Starting %(arg0)s on %(host)s:%(port)s') %
{'arg0': sys.argv[0], {'arg0': sys.argv[0],
'host': self.host, 'host': self.host,
'port': self.port}) 'port': self.port})
@ -193,7 +193,7 @@ class Application(BaseApplication):
arg_dict = req.environ['wsgiorg.routing_args'][1] arg_dict = req.environ['wsgiorg.routing_args'][1]
action = arg_dict.pop('action') action = arg_dict.pop('action')
del arg_dict['controller'] del arg_dict['controller']
LOG.debug('arg_dict: %s', arg_dict) LOG.debug(_('arg_dict: %s'), arg_dict)
# allow middleware up the stack to provide context & params # allow middleware up the stack to provide context & params
context = req.environ.get(CONTEXT_ENV, {}) context = req.environ.get(CONTEXT_ENV, {})
@ -214,7 +214,7 @@ class Application(BaseApplication):
try: try:
result = method(context, **params) result = method(context, **params)
except exception.Unauthorized as e: except exception.Unauthorized as e:
LOG.warning("Authorization failed. %s from %s" LOG.warning(_("Authorization failed. %s from %s")
% (e, req.environ['REMOTE_ADDR'])) % (e, req.environ['REMOTE_ADDR']))
return render_exception(e) return render_exception(e)
except exception.Error as e: except exception.Error as e:

View File

@ -41,8 +41,8 @@ def setup_logging(conf):
logging.config.fileConfig(conf.log_config) logging.config.fileConfig(conf.log_config)
return return
else: else:
raise RuntimeError('Unable to locate specified logging ' raise RuntimeError(_('Unable to locate specified logging '
'config file: %s' % conf.log_config) 'config file: %s') % conf.log_config)
root_logger = logging.root root_logger = logging.root
if conf.debug: if conf.debug:

View File

@ -92,5 +92,5 @@ def enforce(credentials, action, target):
class Policy(policy.Driver): class Policy(policy.Driver):
def enforce(self, credentials, action, target): def enforce(self, credentials, action, target):
LOG.debug('enforce %s: %s', action, credentials) LOG.debug(_('enforce %s: %s'), action, credentials)
enforce(credentials, action, target) enforce(credentials, action, target)

View File

@ -88,7 +88,7 @@ def checkout_vendor(repo, rev):
with open(modcheck, 'w') as fd: with open(modcheck, 'w') as fd:
fd.write('1') fd.write('1')
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
LOG.warning('Failed to checkout %s', repo) LOG.warning(_('Failed to checkout %s'), repo)
cd(working_dir) cd(working_dir)
return revdir return revdir