GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( 669e30...6bd570 )
by Plexxi
11:47 queued 06:09
created

lowercase_value()   B

Complexity

Conditions 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
dl 0
loc 19
rs 8
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import os
17
import sys
18
import collections
19
20
import six
21
22
__all__ = [
23
    'prefix_dict_keys',
24
    'compare_path_file_name',
25
    'lowercase_value'
26
]
27
28
29
def prefix_dict_keys(dictionary, prefix='_'):
30
    """
31
    Prefix dictionary keys with a provided prefix.
32
33
    :param dictionary: Dictionary whose keys to prefix.
34
    :type dictionary: ``dict``
35
36
    :param prefix: Key prefix.
37
    :type prefix: ``str``
38
39
    :rtype: ``dict``:
40
    """
41
    result = {}
42
43
    for key, value in six.iteritems(dictionary):
44
        result['%s%s' % (prefix, key)] = value
45
46
    return result
47
48
49
def compare_path_file_name(file_path_a, file_path_b):
50
    """
51
    Custom compare function which compares full absolute file paths just using
52
    the file name.
53
54
    This function can be used with ``sorted`` or ``list.sort`` function.
55
    """
56
    file_name_a = os.path.basename(file_path_a)
57
    file_name_b = os.path.basename(file_path_b)
58
59
    return file_name_a < file_name_b
60
61
62
def strip_shell_chars(input_str):
63
    """
64
    Strips the last '\r' or '\n' or '\r\n' string at the end of
65
    the input string. This is typically used to strip ``stdout``
66
    and ``stderr`` streams of those characters.
67
68
    :param input_str: Input string to be stripped.
69
    :type input_str: ``str``
70
71
    :rtype: ``str``
72
    """
73
    stripped_str = rstrip_last_char(input_str, '\n')
74
    stripped_str = rstrip_last_char(stripped_str, '\r')
75
    return stripped_str
76
77
78
def rstrip_last_char(input_str, char_to_strip):
79
    """
80
    Strips the last `char_to_strip` from input_str if
81
    input_str ends with `char_to_strip`.
82
83
    :param input_str: Input string to be stripped.
84
    :type input_str: ``str``
85
86
    :rtype: ``str``
87
    """
88
    if not input_str:
89
        return input_str
90
91
    if not char_to_strip:
92
        return input_str
93
94
    if input_str.endswith(char_to_strip):
95
        return input_str[:-len(char_to_strip)]
96
97
    return input_str
98
99
100
def deep_update(d, u):
101
    """
102
    Perform deep merge / update of the target dict.
103
    """
104
105
    for k, v in u.iteritems():
106
        if isinstance(v, collections.Mapping):
107
            r = deep_update(d.get(k, {}), v)
108
            d[k] = r
109
        else:
110
            d[k] = u[k]
111
112
    return d
113
114
115
def get_normalized_file_path(file_path):
116
    """
117
    Return a full normalized file path for the provided path string.
118
119
    :rtype: ``str``
120
    """
121
    if hasattr(sys, 'frozen'):  # support for py2exe
122
        file_path = 'logging%s__init__%s' % (os.sep, file_path[-4:])
123
    elif file_path[-4:].lower() in ['.pyc', '.pyo']:
124
        file_path = file_path[:-4] + '.py'
125
    else:
126
        file_path = file_path
127
128
    file_path = os.path.normcase(file_path)
129
    return file_path
130
131
132
def lowercase_value(value):
133
    """
134
    Lowercase the provided value.
135
136
    In case of a list, all the string item values are lowercases and in case of a dictionary, all
137
    of the string keys and values are lowercased.
138
    """
139
    if isinstance(value, six.string_types):
140
        result = value.lower()
141
    elif isinstance(value, (list, tuple)):
142
        result = [str(item).lower() for item in value]
143
    elif isinstance(value, dict):
144
        result = {}
145
        for key, value in six.iteritems(value):
146
            result[key.lower()] = str(value).lower()
147
    else:
148
        result = value
149
150
    return result
151