Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

"""Common exceptions, classes, and functions.""" 

 

import os 

import sys 

import argparse 

import logging 

 

from . import settings 

 

 

_log = logging.getLogger(__name__) 

 

 

class WideHelpFormatter(argparse.HelpFormatter): 

"""Command-line help text formatter with wider help text.""" 

 

def __init__(self, *args, **kwargs): 

super().__init__(*args, max_help_position=40, **kwargs) 

 

 

class WarningFormatter(logging.Formatter): 

"""Logging formatter that displays verbose formatting for WARNING+.""" 

 

def __init__(self, default_format, verbose_format, *args, **kwargs): 

super().__init__(*args, **kwargs) 

self.default_format = default_format 

self.verbose_format = verbose_format 

 

def format(self, record): 

"""A hack to change the formatting style dynamically.""" 

# pylint: disable=protected-access 

if record.levelno > logging.INFO: 

self._style._fmt = self.verbose_format 

else: 

self._style._fmt = self.default_format 

return super().format(record) 

 

 

def positive_int(value): 

"""Custom `int` that must be positive.""" 

value = int(value) 

if value < 1: 

raise TypeError 

return value 

 

 

class _Config: 

"""Share configuration options.""" 

 

MAX_VERBOSITY = 4 

 

verbosity = 0 

indent_level = 0 

 

 

def configure_logging(count=0): 

"""Configure logging using the provided verbosity count.""" 

if count == -1: 

level = settings.QUIET_LOGGING_LEVEL 

default_format = settings.DEFAULT_LOGGING_FORMAT 

verbose_format = settings.LEVELED_LOGGING_FORMAT 

elif count == 0: 

level = settings.DEFAULT_LOGGING_LEVEL 

default_format = settings.DEFAULT_LOGGING_FORMAT 

verbose_format = settings.LEVELED_LOGGING_FORMAT 

elif count == 1: 

level = settings.VERBOSE_LOGGING_LEVEL 

default_format = settings.VERBOSE_LOGGING_FORMAT 

verbose_format = settings.VERBOSE_LOGGING_FORMAT 

elif count == 2: 

level = settings.VERBOSE2_LOGGING_LEVEL 

default_format = settings.VERBOSE_LOGGING_FORMAT 

verbose_format = settings.VERBOSE_LOGGING_FORMAT 

elif count == 3: 

level = settings.VERBOSE2_LOGGING_LEVEL 

default_format = settings.VERBOSE2_LOGGING_FORMAT 

verbose_format = settings.VERBOSE2_LOGGING_FORMAT 

else: 

level = settings.VERBOSE2_LOGGING_LEVEL - 1 

default_format = settings.VERBOSE2_LOGGING_FORMAT 

verbose_format = settings.VERBOSE2_LOGGING_FORMAT 

 

# Set a custom formatter 

logging.basicConfig(level=level) 

logging.captureWarnings(True) 

formatter = WarningFormatter(default_format, verbose_format, 

datefmt=settings.LOGGING_DATEFMT) 

logging.root.handlers[0].setFormatter(formatter) 

logging.getLogger('yorm').setLevel(max(level, settings.YORM_LOGGING_LEVEL)) 

 

# Warn about excessive verbosity 

if count > _Config.MAX_VERBOSITY: 

msg = "Maximum verbosity level is {}".format(_Config.MAX_VERBOSITY) 

logging.warning(msg) 

_Config.verbosity = _Config.MAX_VERBOSITY 

else: 

_Config.verbosity = count 

 

 

def indent(): 

"""Increase the indent of future output lines.""" 

_Config.indent_level += 1 

 

 

def dedent(level=None): 

"""Decrease (or reset) the indent of future output lines.""" 

if level is None: 

_Config.indent_level = max(0, _Config.indent_level - 1) 

else: 

_Config.indent_level = level 

 

 

def newline(): 

"""Write a new line to standard output.""" 

show("") 

 

 

def show(*messages, file=sys.stdout, log=_log, **kwargs): 

"""Write to standard output or error if enabled.""" 

if any(messages): 

assert 'color' in kwargs, "Color is required" 

 

color = kwargs.pop('color', None) 

 

for message in messages: 

if _Config.verbosity == 0: 

text = ' ' * 2 * _Config.indent_level + style(message, color) 

print(text, file=file) 

elif _Config.verbosity >= 1: 

message = message.strip() 

131 ↛ 125line 131 didn't jump to line 125, because the condition on line 131 was never false if message and log: 

if color == 'error': 

log.error(message) 

else: 

log.info(message) 

 

 

BOLD = '\033[1m' 

RED = '\033[31m' 

GREEN = '\033[32m' 

YELLOW = '\033[33m' 

BLUE = '\033[34m' 

MAGENTA = '\033[35m' 

CYAN = '\033[36m' 

WHITE = '\033[37m' 

RESET = '\033[0m' 

 

COLORS = dict( 

path='', 

git_rev=BOLD + BLUE, 

git_dirty=BOLD + MAGENTA, 

git_changes=YELLOW, 

shell=BOLD + GREEN, 

shell_info=BOLD + MAGENTA, 

shell_output=CYAN, 

shell_error=YELLOW, 

message=BOLD + WHITE, 

success=BOLD + GREEN, 

error=BOLD + RED, 

) 

 

 

def style(msg, name=None, *, _color_support=False): 

is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() 

supports_ansi = sys.platform != 'win32' or 'ANSICON' in os.environ 

if not (is_tty and supports_ansi) and not _color_support: 

return msg 

 

if name == 'shell': 

return msg.replace("$ ", COLORS.get(name) + "$ " + RESET) 

 

color = COLORS.get(name) 

if color: 

return color + msg + RESET 

 

if msg: 

assert color is not None, \ 

"Unknown style name requested: {!r}".format(name) 

 

return msg