Use oslo.config for configuration.

Cleaning openstack.common.

Change-Id: Ifaa02bd6e7408aca4109f505b876149d681bd8a0
This commit is contained in:
François Rossigneux 2013-02-27 11:52:44 +01:00
parent a5ad06da44
commit 9d49090d31
23 changed files with 68 additions and 1930 deletions

View File

@ -17,8 +17,10 @@
import sys
from oslo.config import cfg
from kwapi.plugins.api import app
from kwapi.openstack.common import cfg, log
from kwapi.openstack.common import log
app_opts = [
cfg.IntOpt('api_port',

View File

@ -18,8 +18,10 @@
import sys
import signal
from oslo.config import cfg
from kwapi.drivers import driver_manager
from kwapi.openstack.common import cfg, log
from kwapi.openstack.common import log
if __name__ == "__main__":
cfg.CONF(sys.argv[1:], project='kwapi', default_config_files=['/etc/kwapi/drivers.conf'])

View File

@ -17,8 +17,10 @@
import sys
from oslo.config import cfg
from kwapi.plugins.rrd import app
from kwapi.openstack.common import cfg, log
from kwapi.openstack.common import log
app_opts = [
cfg.IntOpt('rrd_port',

View File

@ -17,9 +17,10 @@
import json
from threading import Thread, Event
from oslo.config import cfg
import zmq
from kwapi.openstack.common import cfg, log
from kwapi.openstack.common import log
from kwapi import security
LOG = log.getLogger(__name__)

View File

@ -21,9 +21,10 @@ import signal
import thread
from threading import Timer
from oslo.config import cfg
import zmq
from kwapi.openstack.common import cfg, log
from kwapi.openstack.common import log
LOG = log.getLogger(__name__)

View File

@ -1,19 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# TODO(jaypipes) Code in this module is intended to be ported to the eventual
# kwapi.openstack.common library

File diff suppressed because it is too large Load Diff

View File

@ -16,9 +16,9 @@
# under the License.
"""
gettext for kwapi.openstack.common modules.
gettext for openstack-common modules.
Usual usage in an kwapi.openstack.common module:
Usual usage in an openstack.common module:
from kwapi.openstack.common.gettextutils import _
"""
@ -26,7 +26,7 @@ Usual usage in an kwapi.openstack.common module:
import gettext
t = gettext.translation('kwapi.openstack.common', 'locale', fallback=True)
t = gettext.translation('openstack-common', 'locale', fallback=True)
def _(msg):

View File

@ -1,130 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 OpenStack LLC.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
class ParseError(Exception):
def __init__(self, message, lineno, line):
self.msg = message
self.line = line
self.lineno = lineno
def __str__(self):
return 'at line %d, %s: %r' % (self.lineno, self.msg, self.line)
class BaseParser(object):
lineno = 0
parse_exc = ParseError
def _assignment(self, key, value):
self.assignment(key, value)
return None, []
def _get_section(self, line):
if line[-1] != ']':
return self.error_no_section_end_bracket(line)
if len(line) <= 2:
return self.error_no_section_name(line)
return line[1:-1]
def _split_key_value(self, line):
colon = line.find(':')
equal = line.find('=')
if colon < 0 and equal < 0:
return self.error_invalid_assignment(line)
if colon < 0 or (equal >= 0 and equal < colon):
key, value = line[:equal], line[equal + 1:]
else:
key, value = line[:colon], line[colon + 1:]
value = value.strip()
if ((value and value[0] == value[-1]) and
(value[0] == "\"" or value[0] == "'")):
value = value[1:-1]
return key.strip(), [value]
def parse(self, lineiter):
key = None
value = []
for line in lineiter:
self.lineno += 1
line = line.rstrip()
if not line:
# Blank line, ends multi-line values
if key:
key, value = self._assignment(key, value)
continue
elif line[0] in (' ', '\t'):
# Continuation of previous assignment
if key is None:
self.error_unexpected_continuation(line)
else:
value.append(line.lstrip())
continue
if key:
# Flush previous assignment, if any
key, value = self._assignment(key, value)
if line[0] == '[':
# Section start
section = self._get_section(line)
if section:
self.new_section(section)
elif line[0] in '#;':
self.comment(line[1:].lstrip())
else:
key, value = self._split_key_value(line)
if not key:
return self.error_empty_key(line)
if key:
# Flush previous assignment, if any
self._assignment(key, value)
def assignment(self, key, value):
"""Called when a full assignment is parsed"""
raise NotImplementedError()
def new_section(self, section):
"""Called when a new section is started"""
raise NotImplementedError()
def comment(self, comment):
"""Called when a comment is parsed"""
pass
def error_invalid_assignment(self, line):
raise self.parse_exc("No ':' or '=' found in assignment",
self.lineno, line)
def error_empty_key(self, line):
raise self.parse_exc('Key cannot be empty', self.lineno, line)
def error_unexpected_continuation(self, line):
raise self.parse_exc('Unexpected continuation line',
self.lineno, line)
def error_no_section_end_bracket(self, line):
raise self.parse_exc('Invalid section (must end with ])',
self.lineno, line)
def error_no_section_name(self, line):
raise self.parse_exc('Empty section name', self.lineno, line)

View File

@ -34,15 +34,21 @@ This module provides a few things:
import datetime
import functools
import inspect
import itertools
import json
import logging
import xmlrpclib
from kwapi.openstack.common.gettextutils import _
from kwapi.openstack.common import timeutils
LOG = logging.getLogger(__name__)
def to_primitive(value, convert_instances=False, level=0):
def to_primitive(value, convert_instances=False, convert_datetime=True,
level=0, max_depth=3):
"""Convert a complex object into primitives.
Handy for JSON serialization. We can optionally handle instances,
@ -78,12 +84,19 @@ def to_primitive(value, convert_instances=False, level=0):
if getattr(value, '__module__', None) == 'mox':
return 'mock'
if level > 3:
if level > max_depth:
LOG.error(_('Max serialization depth exceeded on object: %d %s'),
level, value)
return '?'
# The try block may not be necessary after the class check above,
# but just in case ...
try:
recursive = functools.partial(to_primitive,
convert_instances=convert_instances,
convert_datetime=convert_datetime,
level=level,
max_depth=max_depth)
# It's not clear why xmlrpclib created their own DateTime type, but
# for our purposes, make it a datetime type which is explicitly
# handled
@ -91,33 +104,19 @@ def to_primitive(value, convert_instances=False, level=0):
value = datetime.datetime(*tuple(value.timetuple())[:6])
if isinstance(value, (list, tuple)):
o = []
for v in value:
o.append(to_primitive(v, convert_instances=convert_instances,
level=level))
return o
return [recursive(v) for v in value]
elif isinstance(value, dict):
o = {}
for k, v in value.iteritems():
o[k] = to_primitive(v, convert_instances=convert_instances,
level=level)
return o
elif isinstance(value, datetime.datetime):
return dict((k, recursive(v)) for k, v in value.iteritems())
elif convert_datetime and isinstance(value, datetime.datetime):
return timeutils.strtime(value)
elif hasattr(value, 'iteritems'):
return to_primitive(dict(value.iteritems()),
convert_instances=convert_instances,
level=level + 1)
return recursive(dict(value.iteritems()), level=level + 1)
elif hasattr(value, '__iter__'):
return to_primitive(list(value),
convert_instances=convert_instances,
level=level)
return recursive(list(value))
elif convert_instances and hasattr(value, '__dict__'):
# Likely an instance of something. Watch for cycles.
# Ignore class member vars.
return to_primitive(value.__dict__,
convert_instances=convert_instances,
level=level + 1)
return recursive(value.__dict__, level=level + 1)
else:
return value
except TypeError:

View File

@ -26,6 +26,9 @@ class WeakLocal(corolocal.local):
def __getattribute__(self, attr):
rval = corolocal.local.__getattribute__(self, attr)
if rval:
# NOTE(mikal): this bit is confusing. What is stored is a weak
# reference, not the value itself. We therefore need to lookup
# the weak reference and return the inner value here.
rval = rval()
return rval
@ -34,4 +37,12 @@ class WeakLocal(corolocal.local):
return corolocal.local.__setattr__(self, attr, value)
# NOTE(mikal): the name "store" should be deprecated in the future
store = WeakLocal()
# A "weak" store uses weak references and allows an object to fall out of scope
# when it falls out of scope in the code that uses the thread local storage. A
# "strong" store will hold a reference to the object so that it never falls out
# of scope.
weak_store = WeakLocal()
strong_store = corolocal.local

View File

@ -40,7 +40,8 @@ import stat
import sys
import traceback
from kwapi.openstack.common import cfg
from oslo.config import cfg
from kwapi.openstack.common.gettextutils import _
from kwapi.openstack.common import jsonutils
from kwapi.openstack.common import local
@ -324,16 +325,11 @@ def _create_logging_excepthook(product_name):
def setup(product_name):
"""Setup logging."""
sys.excepthook = _create_logging_excepthook(product_name)
if CONF.log_config:
try:
logging.config.fileConfig(CONF.log_config)
except Exception:
traceback.print_exc()
raise
logging.config.fileConfig(CONF.log_config)
else:
_setup_logging_from_conf(product_name)
sys.excepthook = _create_logging_excepthook(product_name)
def set_defaults(logging_context_format_string):

View File

@ -15,7 +15,8 @@
import uuid
from kwapi.openstack.common import cfg
from oslo.config import cfg
from kwapi.openstack.common import context
from kwapi.openstack.common.gettextutils import _
from kwapi.openstack.common import importutils

View File

@ -13,8 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo.config import cfg
from kwapi.openstack.common import cfg
from kwapi.openstack.common import jsonutils
from kwapi.openstack.common import log as logging

View File

@ -13,8 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo.config import cfg
from kwapi.openstack.common import cfg
from kwapi.openstack.common import context as req_context
from kwapi.openstack.common.gettextutils import _
from kwapi.openstack.common import log as logging

View File

@ -15,7 +15,8 @@
'''messaging based notification driver, with message envelopes'''
from kwapi.openstack.common import cfg
from oslo.config import cfg
from kwapi.openstack.common import context as req_context
from kwapi.openstack.common.gettextutils import _
from kwapi.openstack.common import log as logging

View File

@ -179,4 +179,4 @@ def is_soon(dt, window):
:return: True if expiration is within the given duration
"""
soon = (utcnow() + datetime.timedelta(seconds=window))
return normalize_time(dt) < soon
return normalize_time(dt) <= soon

View File

@ -18,8 +18,7 @@
import flask
from keystoneclient.v2_0.client import Client
from kwapi.openstack.common import cfg
from oslo.config import cfg
acl_opts = [
cfg.StrOpt('acl_auth_url',

View File

@ -17,8 +17,9 @@
"""Set up the API server application instance."""
import flask
from oslo.config import cfg
from kwapi.openstack.common import cfg, log
from kwapi.openstack.common import log
import acl
from collector import Collector
import v1

View File

@ -18,9 +18,10 @@ import json
import threading
import time
from oslo.config import cfg
import zmq
from kwapi.openstack.common import cfg, log
from kwapi.openstack.common import log
from kwapi import security
LOG = log.getLogger(__name__)

View File

@ -24,10 +24,11 @@ import os
import time
import uuid
from oslo.config import cfg
import rrdtool
import zmq
from kwapi.openstack.common import cfg, log
from kwapi.openstack.common import log
from kwapi import security
LOG = log.getLogger(__name__)

View File

@ -19,7 +19,7 @@
import flask
from jinja2 import TemplateNotFound
from kwapi.openstack.common import cfg
from oslo.config import cfg
import rrd
web_opts = [

View File

@ -1,3 +1,3 @@
[DEFAULT]
modules=cfg,log,iniparser,gettextutils,jsonutils,timeutils,local,notifier
modules=timeutils,log,gettextutils,jsonutils,local,notifier
base=kwapi