summaryrefslogtreecommitdiffstats
path: root/python-discovery-client/discovery_client/util.py
blob: b59647a45c106e08f9462d9c52bc9fbff6ef05b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# org.onap.dcae
# ================================================================================
# Copyright (c) 2017 AT&T Intellectual Property. 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.
# ============LICENSE_END=========================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.

import collections
import logging, sys
import six

#####
# Module contains utility methods
#####

def update_json(src, key, value):
    """Updates a nested JSON value

    This method does a recursive lookup for a value given a compound key and then
    replaces that value with the passed in new value.

    For example, given a src json { "a": [ { "aa": 1 }, "foo" ], "b": "2 } and a
    key ("a", 0, "aa"), the value parameter would replace 1.

    :param src: json to update
    :type src: dict or list
    :param key: compound key used to lookup
    :type key: tuple
    :param value: new value used to replace
    :type value: object

    :return: updated json
    """
    if key:
        src[key[0]] = update_json(src[key[0]], key[1:], value)
    else:
        # We've found the value we want to replace regardless of whether or not
        # the object we are replacing is another copmlicated data structure.
        src = value
    return src

def _has_handlers(logger):
    """Check if logger has handlers"""
    if six.PY3:
        return logger.hasHandlers()
    else:
        # TODO: Not sure how to check if a handler has already been attached
        # WATCH: Downside is lines get printed multiple times
        return False

def get_logger(name, level=logging.INFO):
    """Get a logger with sensible defaults

    This method returns a logger from logging by name that has been set with sensible
    defaults if the logger hasn't already been setup with any handlers. The
    default handler is a stream handler to stdout.
    """
    logger = logging.getLogger(name)

    if not _has_handlers(logger):
        # No handlers attached which means logging hasn't been setup. Set
        # "sensible" defaults which means stdout, INFO
        logger.setLevel(level)
        logger.addHandler(logging.StreamHandler(stream=sys.stdout))

    return logger