5
5
from jinja2 import is_undefined , pass_context
6
6
7
7
if sys .version_info >= (3 ,0 ):
8
- from shutil import which
8
+ from shutil import which # pylint: disable=import-error
9
9
elif sys .version_info >= (2 ,5 ):
10
- from shutilwhich import which
10
+ from shutilwhich import which # pylint: disable=import-error
11
11
else :
12
12
assert False , "Unsupported Python version: %s" % sys .version_info
13
13
18
18
else :
19
19
assert False , "Unsupported Python version: %s" % sys .version_info
20
20
21
- def docker_link (value , format = '{addr}:{port}' ):
21
+ def docker_link (value , fmt = '{addr}:{port}' ):
22
22
""" Given a Docker Link environment variable value, format it into something else.
23
23
XXX: The name of the filter is not very informative. This is actually a partial URI parser.
24
24
@@ -36,12 +36,12 @@ def docker_link(value, format='{addr}:{port}'):
36
36
}
37
37
```
38
38
39
- And then uses `format ` to format it, where the default format is '{addr}:{port}'.
39
+ And then uses `fmt ` to format it, where the default format is '{addr}:{port}'.
40
40
41
41
More info here: [Docker Links](https://docs.docker.com/userguide/dockerlinks/)
42
42
43
43
:param value: Docker link (from an environment variable)
44
- :param format : The format to apply. Supported placeholders: `{proto}`, `{addr}`, `{port}`
44
+ :param fmt : The format to apply. Supported placeholders: `{proto}`, `{addr}`, `{port}`
45
45
:return: Formatted string
46
46
"""
47
47
# pass undefined values on down the pipeline
@@ -55,7 +55,7 @@ def docker_link(value, format='{addr}:{port}'):
55
55
d = m .groupdict ()
56
56
57
57
# Format
58
- return format .format (** d )
58
+ return fmt .format (** d )
59
59
60
60
61
61
def env (varname , default = None ):
@@ -85,37 +85,41 @@ def env(varname, default=None):
85
85
86
86
Notice that there must be quotes around the environment variable name
87
87
"""
88
- if default is not None :
89
- # With the default, there's never an error
90
- return os .getenv (varname , default )
91
- else :
88
+ if default is None :
92
89
# Raise KeyError when not provided
93
90
return os .environ [varname ]
94
91
92
+ # With the default, there's never an error
93
+ return os .getenv (varname , default )
94
+
95
+
95
96
def align_suffix (text , delim , column = None , spaces_after_delim = 1 ):
96
97
""" Align the suffixes of lines in text, starting from the specified delim.
98
+
99
+ Example: XXX
97
100
"""
98
101
s = ''
99
102
100
103
if column is None or column == 'auto' :
101
- column = max (map (lambda l : l .find (delim ), text .splitlines ()))
104
+ column = max (map (lambda ln : ln .find (delim ), text .splitlines ()))
102
105
elif column == 'previous' :
103
106
column = align_suffix .column_previous
104
107
105
- for l in map (lambda s : s .split (delim , 1 ), text .splitlines ()):
106
- if len (l ) < 2 :
108
+ for ln in map (lambda s : s .split (delim , 1 ), text .splitlines ()):
109
+ if len (ln ) < 2 :
107
110
# no delimiter occurs
108
- s += l [0 ].rstrip () + os .linesep
109
- elif l [0 ].strip () == '' :
111
+ s += ln [0 ].rstrip () + os .linesep
112
+ elif ln [0 ].strip () == '' :
110
113
# no content before delimiter - leave as-is
111
- s += l [0 ] + delim + l [1 ] + os .linesep
114
+ s += ln [0 ] + delim + ln [1 ] + os .linesep
112
115
else :
113
116
# align
114
- s += l [0 ].rstrip ().ljust (column ) + delim + spaces_after_delim * ' ' + l [1 ].strip () + os .linesep
117
+ s += ln [0 ].rstrip ().ljust (column ) + delim + spaces_after_delim * ' ' + ln [1 ].strip () + os .linesep
115
118
116
119
align_suffix .column_previous = column
117
120
return s
118
121
122
+
119
123
align_suffix .column_previous = None
120
124
121
125
@@ -140,11 +144,13 @@ def sh_opt(text, name, delim=" ", quote=False):
140
144
text = sh_quote (text )
141
145
return '%s%s%s' % (name , delim , text )
142
146
147
+
143
148
def sh_optq (text , name , delim = " " ):
144
149
""" Quote text and format as a command line option.
145
150
"""
146
151
return sh_opt (text , name , delim , quote = True )
147
152
153
+
148
154
# Filters to be loaded
149
155
EXTRA_FILTERS = {
150
156
'sh_quote' : sh_quote ,
@@ -163,4 +169,3 @@ def sh_optq(text, name, delim=" "):
163
169
'align_suffix' : align_suffix ,
164
170
'ctxlookup' : ctxlookup ,
165
171
}
166
-
0 commit comments