This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
_common.py
142 lines (106 loc) · 3.55 KB
/
_common.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""Copyright 2016 Google Inc. 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.
Common functions and utilities used across the logocert package.
This provides some common functions and classes that are needed by multiple
classes and modules in the Logo Certification Package.
"""
import time
import os
from _config import Constants
# Module level variable
_use_color_output = (Constants.TEST['FORCE_COLOR_OUTPUT'] or
'nt' not in os.name.lower())
def Sleep(wait_type):
sec = Constants.SLEEP[wait_type]
if 'POLL' not in wait_type:
print '[Configurable sleep] %s: %s seconds' %(wait_type, sec)
time.sleep(sec)
def GreenText(str):
"""Display text in green
Args:
str: string, the str to display, cannot be None.
"""
global _use_color_output
return str if not _use_color_output else '\033[92m'+str+'\033[0m'
def RedText(str):
"""Display text in red
Args:
str: string, the str to display, cannot be None.
"""
global _use_color_output
return str if not _use_color_output else '\033[91m'+str+'\033[0m'
def BlueText(str):
"""Display text in blue
Args:
str: string, the str to display, cannot be None.
"""
global _use_color_output
return str if not _use_color_output else '\033[94m'+str+'\033[0m'
def PurpleText(str):
"""Display text in purple
Args:
str: string, the str to display, cannot be None.
"""
global _use_color_output
return str if not _use_color_output else '\033[95m' + str + '\033[0m'
def PromptUserAction(msg):
"""Display text in warning color and beep
Args:
msg: string, the msg to prompt the user.
Returns:
string, prompt string
"""
print "\a" # Cross-platform beep
print PurpleText('[ACTION] '+msg)
def PromptAndWaitForUserAction(msg):
"""Display text in green and beep - cross-platform, then wait for user to
press enter before continuing
Args:
msg: string, the msg to prompt the user.
Returns:
string, user input string
"""
PromptUserAction(msg)
return raw_input(PurpleText('>>> '))
def Extract(dict_in, dict_out):
"""Extract all the keys and values from a nested dictionary.
Args:
dict_in: dictionary of unknown size and levels.
dict_out: dictionary to be created.
"""
if isinstance(dict_in, dict):
for key, value in dict_in.iteritems():
if isinstance(value, dict):
Extract(value, dict_out)
elif isinstance(dict_in, list):
for i in dict_in:
Extract(i, dict_out)
else:
dict_out[key] = value
else:
type(dict_in)
class Error(Exception):
"""Base class for exceptions in this module.
Args:
expr: string, expression that caused the error.
msg: string, reason for error.
"""
def __init__(self, expr, msg):
super(Error, self).__init__()
self.expr = expr
self.msg = msg
class InitError(Error):
"""Exception raised for errors in the class initialization.
Args:
msg: string, reason for error.
"""
def __init__(self, msg):
super(InitError, self).__init__('Error initializing object.', msg)