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
|
|
|
] |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def prefix_dict_keys(dictionary, prefix='_'): |
29
|
|
|
""" |
30
|
|
|
Prefix dictionary keys with a provided prefix. |
31
|
|
|
|
32
|
|
|
:param dictionary: Dictionary whose keys to prefix. |
33
|
|
|
:type dictionary: ``dict`` |
34
|
|
|
|
35
|
|
|
:param prefix: Key prefix. |
36
|
|
|
:type prefix: ``str`` |
37
|
|
|
|
38
|
|
|
:rtype: ``dict``: |
39
|
|
|
""" |
40
|
|
|
result = {} |
41
|
|
|
|
42
|
|
|
for key, value in six.iteritems(dictionary): |
43
|
|
|
result['%s%s' % (prefix, key)] = value |
44
|
|
|
|
45
|
|
|
return result |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def compare_path_file_name(file_path_a, file_path_b): |
49
|
|
|
""" |
50
|
|
|
Custom compare function which compares full absolute file paths just using |
51
|
|
|
the file name. |
52
|
|
|
|
53
|
|
|
This function can be used with ``sorted`` or ``list.sort`` function. |
54
|
|
|
""" |
55
|
|
|
file_name_a = os.path.basename(file_path_a) |
56
|
|
|
file_name_b = os.path.basename(file_path_b) |
57
|
|
|
|
58
|
|
|
return file_name_a < file_name_b |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def strip_shell_chars(input_str): |
62
|
|
|
""" |
63
|
|
|
Strips the last '\r' or '\n' or '\r\n' string at the end of |
64
|
|
|
the input string. This is typically used to strip ``stdout`` |
65
|
|
|
and ``stderr`` streams of those characters. |
66
|
|
|
|
67
|
|
|
:param input_str: Input string to be stripped. |
68
|
|
|
:type input_str: ``str`` |
69
|
|
|
|
70
|
|
|
:rtype: ``str`` |
71
|
|
|
""" |
72
|
|
|
stripped_str = rstrip_last_char(input_str, '\n') |
73
|
|
|
stripped_str = rstrip_last_char(stripped_str, '\r') |
74
|
|
|
return stripped_str |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def rstrip_last_char(input_str, char_to_strip): |
78
|
|
|
""" |
79
|
|
|
Strips the last `char_to_strip` from input_str if |
80
|
|
|
input_str ends with `char_to_strip`. |
81
|
|
|
|
82
|
|
|
:param input_str: Input string to be stripped. |
83
|
|
|
:type input_str: ``str`` |
84
|
|
|
|
85
|
|
|
:rtype: ``str`` |
86
|
|
|
""" |
87
|
|
|
if not input_str: |
88
|
|
|
return input_str |
89
|
|
|
|
90
|
|
|
if not char_to_strip: |
91
|
|
|
return input_str |
92
|
|
|
|
93
|
|
|
if input_str.endswith(char_to_strip): |
94
|
|
|
return input_str[:-len(char_to_strip)] |
95
|
|
|
|
96
|
|
|
return input_str |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def deep_update(d, u): |
100
|
|
|
""" |
101
|
|
|
Perform deep merge / update of the target dict. |
102
|
|
|
""" |
103
|
|
|
|
104
|
|
|
for k, v in u.iteritems(): |
105
|
|
|
if isinstance(v, collections.Mapping): |
106
|
|
|
r = deep_update(d.get(k, {}), v) |
107
|
|
|
d[k] = r |
108
|
|
|
else: |
109
|
|
|
d[k] = u[k] |
110
|
|
|
|
111
|
|
|
return d |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
def get_normalized_file_path(file_path): |
115
|
|
|
""" |
116
|
|
|
Return a full normalized file path for the provided path string. |
117
|
|
|
|
118
|
|
|
:rtype: ``str`` |
119
|
|
|
""" |
120
|
|
|
if hasattr(sys, 'frozen'): # support for py2exe |
121
|
|
|
file_path = 'logging%s__init__%s' % (os.sep, file_path[-4:]) |
122
|
|
|
elif file_path[-4:].lower() in ['.pyc', '.pyo']: |
123
|
|
|
file_path = file_path[:-4] + '.py' |
124
|
|
|
else: |
125
|
|
|
file_path = file_path |
126
|
|
|
|
127
|
|
|
file_path = os.path.normcase(file_path) |
128
|
|
|
return file_path |
129
|
|
|
|