From fbe2f00418eb099fc1dd63bedb2b4fbfd8af404a Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 21 May 2013 11:12:36 +0200 Subject: [PATCH] Move recursive_keypairs into utils Change-Id: I61ffe093cc4205ab306adc29c5e6c67c0247b276 Signed-off-by: Julien Danjou --- ceilometer/publisher/meter.py | 20 ++------------ ceilometer/utils.py | 17 ++++++++++++ tests/publisher/test_meter_publisher.py | 15 ----------- tests/test_utils.py | 36 +++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 33 deletions(-) create mode 100644 tests/test_utils.py diff --git a/ceilometer/publisher/meter.py b/ceilometer/publisher/meter.py index cc1773896..eab71aafc 100644 --- a/ceilometer/publisher/meter.py +++ b/ceilometer/publisher/meter.py @@ -28,6 +28,7 @@ from oslo.config import cfg from ceilometer.openstack.common import log from ceilometer.openstack.common import rpc from ceilometer import publisher +from ceilometer import utils LOG = log.getLogger(__name__) @@ -55,28 +56,11 @@ def register_opts(config): register_opts(cfg.CONF) -def recursive_keypairs(d): - """Generator that produces sequence of keypairs for nested dictionaries. - """ - for name, value in sorted(d.iteritems()): - if isinstance(value, dict): - for subname, subvalue in recursive_keypairs(value): - yield ('%s:%s' % (name, subname), subvalue) - elif isinstance(value, (tuple, list)): - # When doing a pair of JSON encode/decode operations to the tuple, - # the tuple would become list. So we have to generate the value as - # list here. - yield name, list(map(lambda x: unicode(x).encode('utf-8'), - value)) - else: - yield name, value - - def compute_signature(message, secret): """Return the signature for a message dictionary. """ digest_maker = hmac.new(secret, '', hashlib.sha256) - for name, value in recursive_keypairs(message): + for name, value in utils.recursive_keypairs(message): if name == 'message_signature': # Skip any existing signature value, which would not have # been part of the original message. diff --git a/ceilometer/utils.py b/ceilometer/utils.py index 11bcdfe8d..f6c676aa0 100644 --- a/ceilometer/utils.py +++ b/ceilometer/utils.py @@ -42,3 +42,20 @@ def read_cached_file(filename, cache_info, reload_func=None): if reload_func: reload_func(cache_info['data']) return cache_info['data'] + + +def recursive_keypairs(d): + """Generator that produces sequence of keypairs for nested dictionaries. + """ + for name, value in sorted(d.iteritems()): + if isinstance(value, dict): + for subname, subvalue in recursive_keypairs(value): + yield ('%s:%s' % (name, subname), subvalue) + elif isinstance(value, (tuple, list)): + # When doing a pair of JSON encode/decode operations to the tuple, + # the tuple would become list. So we have to generate the value as + # list here. + yield name, list(map(lambda x: unicode(x).encode('utf-8'), + value)) + else: + yield name, value diff --git a/tests/publisher/test_meter_publisher.py b/tests/publisher/test_meter_publisher.py index 574e1f805..d2ac32e56 100644 --- a/tests/publisher/test_meter_publisher.py +++ b/tests/publisher/test_meter_publisher.py @@ -86,21 +86,6 @@ def test_verify_signature_incorrect(): assert not meter.verify_signature(data, 'not-so-secret') -def test_recursive_keypairs(): - data = {'a': 'A', - 'b': 'B', - 'nested': {'a': 'A', - 'b': 'B', - }, - } - pairs = list(meter.recursive_keypairs(data)) - assert pairs == [('a', 'A'), - ('b', 'B'), - ('nested:a', 'A'), - ('nested:b', 'B'), - ] - - def test_verify_signature_nested(): data = {'a': 'A', 'b': 'B', diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 000000000..2d53290a7 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,36 @@ +# -*- encoding: utf-8 -*- +# +# Copyright © 2012 New Dream Network, LLC (DreamHost) +# +# Author: Doug Hellmann +# +# 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. +"""Tests for ceilometer/utils.py +""" + +from ceilometer import utils + + +def test_recursive_keypairs(): + data = {'a': 'A', + 'b': 'B', + 'nested': {'a': 'A', + 'b': 'B', + }, + } + pairs = list(utils.recursive_keypairs(data)) + assert pairs == [('a', 'A'), + ('b', 'B'), + ('nested:a', 'A'), + ('nested:b', 'B'), + ]