Skip to content

Commit

Permalink
Updated common module from oslo
Browse files Browse the repository at this point in the history
This patch updated common module from oslo for known issue.

Fixed bug: #1175808
Fixed bug: #1072917

Change-Id: Ic1de6b786a9d67b39d186e31a37049729d367464
  • Loading branch information
guohliu committed Jun 19, 2013
1 parent 28528ba commit fee94d2
Show file tree
Hide file tree
Showing 27 changed files with 512 additions and 364 deletions.
3 changes: 2 additions & 1 deletion heat/openstack/common/context.py
Expand Up @@ -33,7 +33,8 @@ def generate_request_id():

class RequestContext(object):

"""
"""Helper class to represent useful information about a request context.
Stores information about the security context under which the user
accesses the system, as well as additional request information.
"""
Expand Down
3 changes: 1 addition & 2 deletions heat/openstack/common/exception.py
Expand Up @@ -110,8 +110,7 @@ def _wrap(*args, **kw):


class OpenstackException(Exception):
"""
Base Exception
"""Base Exception class.
To correctly use this class, inherit from it and define
a 'message' property. That message will get printf'd
Expand Down
176 changes: 176 additions & 0 deletions heat/openstack/common/gettextutils.py
Expand Up @@ -2,6 +2,7 @@

# Copyright 2012 Red Hat, Inc.
# All Rights Reserved.
# Copyright 2013 IBM Corp.
#
# 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
Expand All @@ -23,8 +24,11 @@
from heat.openstack.common.gettextutils import _
"""

import copy
import gettext
import logging.handlers
import os
import UserString

_localedir = os.environ.get('heat'.upper() + '_LOCALEDIR')
_t = gettext.translation('heat', localedir=_localedir, fallback=True)
Expand All @@ -48,3 +52,175 @@ def install(domain):
gettext.install(domain,
localedir=os.environ.get(domain.upper() + '_LOCALEDIR'),
unicode=True)


"""
Lazy gettext functionality.
The following is an attempt to introduce a deferred way
to do translations on messages in OpenStack. We attempt to
override the standard _() function and % (format string) operation
to build Message objects that can later be translated when we have
more information. Also included is an example LogHandler that
translates Messages to an associated locale, effectively allowing
many logs, each with their own locale.
"""


def get_lazy_gettext(domain):
"""Assemble and return a lazy gettext function for a given domain.
Factory method for a project/module to get a lazy gettext function
for its own translation domain (i.e. nova, glance, cinder, etc.)
"""

def _lazy_gettext(msg):
"""Create and return a Message object.
Message encapsulates a string so that we can translate it later when
needed.
"""
return Message(msg, domain)

return _lazy_gettext


class Message(UserString.UserString, object):
"""Class used to encapsulate translatable messages."""
def __init__(self, msg, domain):
# _msg is the gettext msgid and should never change
self._msg = msg
self._left_extra_msg = ''
self._right_extra_msg = ''
self.params = None
self.locale = None
self.domain = domain

@property
def data(self):
# NOTE(mrodden): this should always resolve to a unicode string
# that best represents the state of the message currently

localedir = os.environ.get(self.domain.upper() + '_LOCALEDIR')
if self.locale:
lang = gettext.translation(self.domain,
localedir=localedir,
languages=[self.locale],
fallback=True)
else:
# use system locale for translations
lang = gettext.translation(self.domain,
localedir=localedir,
fallback=True)

full_msg = (self._left_extra_msg +
lang.ugettext(self._msg) +
self._right_extra_msg)

if self.params is not None:
full_msg = full_msg % self.params

return unicode(full_msg)

def _save_parameters(self, other):
# we check for None later to see if
# we actually have parameters to inject,
# so encapsulate if our parameter is actually None
if other is None:
self.params = (other, )
else:
self.params = copy.deepcopy(other)

return self

# overrides to be more string-like
def __unicode__(self):
return self.data

def __str__(self):
return self.data.encode('utf-8')

def __getstate__(self):
to_copy = ['_msg', '_right_extra_msg', '_left_extra_msg',
'domain', 'params', 'locale']
new_dict = self.__dict__.fromkeys(to_copy)
for attr in to_copy:
new_dict[attr] = copy.deepcopy(self.__dict__[attr])

return new_dict

def __setstate__(self, state):
for (k, v) in state.items():
setattr(self, k, v)

# operator overloads
def __add__(self, other):
copied = copy.deepcopy(self)
copied._right_extra_msg += other.__str__()
return copied

def __radd__(self, other):
copied = copy.deepcopy(self)
copied._left_extra_msg += other.__str__()
return copied

def __mod__(self, other):
# do a format string to catch and raise
# any possible KeyErrors from missing parameters
self.data % other
copied = copy.deepcopy(self)
return copied._save_parameters(other)

def __mul__(self, other):
return self.data * other

def __rmul__(self, other):
return other * self.data

def __getitem__(self, key):
return self.data[key]

def __getslice__(self, start, end):
return self.data.__getslice__(start, end)

def __getattribute__(self, name):
# NOTE(mrodden): handle lossy operations that we can't deal with yet
# These override the UserString implementation, since UserString
# uses our __class__ attribute to try and build a new message
# after running the inner data string through the operation.
# At that point, we have lost the gettext message id and can just
# safely resolve to a string instead.
ops = ['capitalize', 'center', 'decode', 'encode',
'expandtabs', 'ljust', 'lstrip', 'replace', 'rjust', 'rstrip',
'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
if name in ops:
return getattr(self.data, name)
else:
return UserString.UserString.__getattribute__(self, name)


class LocaleHandler(logging.Handler):
"""Handler that can have a locale associated to translate Messages.
A quick example of how to utilize the Message class above.
LocaleHandler takes a locale and a target logging.Handler object
to forward LogRecord objects to after translating the internal Message.
"""

def __init__(self, locale, target):
"""Initialize a LocaleHandler
:param locale: locale to use for translating messages
:param target: logging.Handler object to forward
LogRecord objects to after translation
"""
logging.Handler.__init__(self)
self.locale = locale
self.target = target

def emit(self, record):
if isinstance(record.msg, Message):
# set the locale and resolve to a string
record.msg.locale = self.locale

self.target.emit(record)
7 changes: 4 additions & 3 deletions heat/openstack/common/importutils.py
Expand Up @@ -24,7 +24,7 @@


def import_class(import_str):
"""Returns a class from a string including module and class"""
"""Returns a class from a string including module and class."""
mod_str, _sep, class_str = import_str.rpartition('.')
try:
__import__(mod_str)
Expand All @@ -41,8 +41,9 @@ def import_object(import_str, *args, **kwargs):


def import_object_ns(name_space, import_str, *args, **kwargs):
"""
Import a class and return an instance of it, first by trying
"""Tries to import object from default namespace.
Imports a class and return an instance of it, first by trying
to find the class in a default namespace, then failing back to
a full path if not found in the default namespace.
"""
Expand Down
7 changes: 4 additions & 3 deletions heat/openstack/common/log.py
Expand Up @@ -459,10 +459,11 @@ def getLogger(name='unknown', version='unknown'):


def getLazyLogger(name='unknown', version='unknown'):
"""
create a pass-through logger that does not create the real logger
"""Returns lazy logger.
Creates a pass-through logger that does not create the real logger
until it is really needed and delegates all calls to the real logger
once it is created
once it is created.
"""
return LazyAdapter(name, version)

Expand Down
4 changes: 2 additions & 2 deletions heat/openstack/common/network_utils.py
Expand Up @@ -26,8 +26,8 @@


def parse_host_port(address, default_port=None):
"""
Interpret a string as a host:port pair.
"""Interpret a string as a host:port pair.
An IPv6 address MUST be escaped if accompanied by a port,
because otherwise ambiguity ensues: 2001:db8:85a3::8a2e:370:7334
means both [2001:db8:85a3::8a2e:370:7334] and
Expand Down
2 changes: 1 addition & 1 deletion heat/openstack/common/notifier/api.py
Expand Up @@ -56,7 +56,7 @@ class BadPriorityException(Exception):


def notify_decorator(name, fn):
""" decorator for notify which is used from utils.monkey_patch()
"""Decorator for notify which is used from utils.monkey_patch().
:param name: name of the function
:param function: - object of the function
Expand Down
4 changes: 3 additions & 1 deletion heat/openstack/common/notifier/log_notifier.py
Expand Up @@ -24,7 +24,9 @@

def notify(_context, message):
"""Notifies the recipient of the desired event given the model.
Log notifications using openstack's default logging system"""
Log notifications using openstack's default logging system.
"""

priority = message.get('priority',
CONF.default_notification_level)
Expand Down
2 changes: 1 addition & 1 deletion heat/openstack/common/notifier/no_op_notifier.py
Expand Up @@ -15,5 +15,5 @@


def notify(_context, message):
"""Notifies the recipient of the desired event given the model"""
"""Notifies the recipient of the desired event given the model."""
pass
2 changes: 1 addition & 1 deletion heat/openstack/common/notifier/rpc_notifier.py
Expand Up @@ -31,7 +31,7 @@


def notify(context, message):
"""Sends a notification via RPC"""
"""Sends a notification via RPC."""
if not context:
context = req_context.get_admin_context()
priority = message.get('priority',
Expand Down
2 changes: 1 addition & 1 deletion heat/openstack/common/notifier/rpc_notifier2.py
Expand Up @@ -37,7 +37,7 @@


def notify(context, message):
"""Sends a notification via RPC"""
"""Sends a notification via RPC."""
if not context:
context = req_context.get_admin_context()
priority = message.get('priority',
Expand Down

0 comments on commit fee94d2

Please sign in to comment.