Skip to content

Commit e99652e

Browse files
committed
linting: Linter fixes across the codebase.
1 parent 74f7e55 commit e99652e

File tree

6 files changed

+33
-30
lines changed

6 files changed

+33
-30
lines changed

src/jj2cli/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def render_command(argv):
101101

102102
# Renderer
103103
renderer = Jinja2TemplateRenderer(os.getcwd(), args.undefined, args.no_compact, j2_env_params=customize.j2_environment_params())
104-
customize.j2_environment(renderer._env)
104+
customize.j2_environment(renderer._env) # pylint: disable=protected-access
105105

106106
# Filters, Tests
107107
renderer.register_filters(filters.EXTRA_FILTERS)
@@ -133,6 +133,7 @@ def render():
133133
return 1
134134
outstream = getattr(sys.stdout, 'buffer', sys.stdout)
135135
outstream.write(output)
136+
return 0
136137

137138

138139
def dependencies():

src/jj2cli/customize.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11

2-
class CustomizationModule(object):
2+
class CustomizationModule:
33
""" The interface for customization functions, defined as module-level functions """
44

55
def __init__(self, module=None):
66
if module is not None:
7-
def howcall(*args):
8-
print(args)
9-
exit(1)
10-
117
# Import every module function as a method on ourselves
128
for name in self._IMPORTED_METHOD_NAMES:
139
try:

src/jj2cli/filters.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from jinja2 import is_undefined, pass_context
66

77
if sys.version_info >= (3,0):
8-
from shutil import which
8+
from shutil import which # pylint: disable=import-error
99
elif sys.version_info >= (2,5):
10-
from shutilwhich import which
10+
from shutilwhich import which # pylint: disable=import-error
1111
else:
1212
assert False, "Unsupported Python version: %s" % sys.version_info
1313

@@ -18,7 +18,7 @@
1818
else:
1919
assert False, "Unsupported Python version: %s" % sys.version_info
2020

21-
def docker_link(value, format='{addr}:{port}'):
21+
def docker_link(value, fmt='{addr}:{port}'):
2222
""" Given a Docker Link environment variable value, format it into something else.
2323
XXX: The name of the filter is not very informative. This is actually a partial URI parser.
2424
@@ -36,12 +36,12 @@ def docker_link(value, format='{addr}:{port}'):
3636
}
3737
```
3838
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}'.
4040
4141
More info here: [Docker Links](https://docs.docker.com/userguide/dockerlinks/)
4242
4343
: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}`
4545
:return: Formatted string
4646
"""
4747
# pass undefined values on down the pipeline
@@ -55,7 +55,7 @@ def docker_link(value, format='{addr}:{port}'):
5555
d = m.groupdict()
5656

5757
# Format
58-
return format.format(**d)
58+
return fmt.format(**d)
5959

6060

6161
def env(varname, default=None):
@@ -85,37 +85,41 @@ def env(varname, default=None):
8585
8686
Notice that there must be quotes around the environment variable name
8787
"""
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:
9289
# Raise KeyError when not provided
9390
return os.environ[varname]
9491

92+
# With the default, there's never an error
93+
return os.getenv(varname, default)
94+
95+
9596
def align_suffix(text, delim, column=None, spaces_after_delim=1):
9697
""" Align the suffixes of lines in text, starting from the specified delim.
98+
99+
Example: XXX
97100
"""
98101
s=''
99102

100103
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()))
102105
elif column == 'previous':
103106
column = align_suffix.column_previous
104107

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:
107110
# 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() == '':
110113
# 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
112115
else:
113116
# 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
115118

116119
align_suffix.column_previous = column
117120
return s
118121

122+
119123
align_suffix.column_previous = None
120124

121125

@@ -140,11 +144,13 @@ def sh_opt(text, name, delim=" ", quote=False):
140144
text = sh_quote(text)
141145
return '%s%s%s' % (name, delim, text)
142146

147+
143148
def sh_optq(text, name, delim=" "):
144149
""" Quote text and format as a command line option.
145150
"""
146151
return sh_opt(text, name, delim, quote=True)
147152

153+
148154
# Filters to be loaded
149155
EXTRA_FILTERS = {
150156
'sh_quote': sh_quote,
@@ -163,4 +169,3 @@ def sh_optq(text, name, delim=" "):
163169
'align_suffix': align_suffix,
164170
'ctxlookup': ctxlookup,
165171
}
166-

src/jj2cli/parsers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ def parse(self, ignore_missing=False, fallback_format='ini'):
127127
if isinstance(self._iostr, FileNotFoundError):
128128
if ignore_missing is True:
129129
return {}
130-
else:
131-
raise self._iostr
130+
raise self._iostr
132131
return getattr(self, '_parse_%s' % fmt)()
133132

134133
def _parse_ENV(self):

src/jj2cli/render.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self, cwd, encoding='utf-8'):
2222
def get_source(self, environment, template):
2323
# Path
2424
filename = os.path.join(self.cwd, template)
25+
logging.debug("TEMPLATE_PATH %s", filename)
2526

2627
# Read
2728
try:
@@ -35,7 +36,7 @@ def get_source(self, environment, template):
3536
return contents, filename, uptodate
3637

3738

38-
class Jinja2TemplateRenderer(object):
39+
class Jinja2TemplateRenderer:
3940
""" Template renderer """
4041

4142
UNDEFINED = {
@@ -44,8 +45,9 @@ class Jinja2TemplateRenderer(object):
4445
'debug': jinja2.DebugUndefined, # return the debug info when printed
4546
}
4647

47-
def __init__(self, cwd, undefined='strict', no_compact=False, j2_env_params={}):
48+
def __init__(self, cwd, undefined='strict', no_compact=False, j2_env_params=None):
4849
# Custom env params
50+
j2_env_params = j2_env_params if j2_env_params is not None else {}
4951
j2_env_params.setdefault('keep_trailing_newline', True)
5052
j2_env_params.setdefault('undefined', self.UNDEFINED[undefined])
5153
j2_env_params.setdefault('trim_blocks', not no_compact)

tests/resources/customize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def j2_environment(env):
3737
:rtype: jinja2.environment.Environment
3838
"""
3939
env.globals.update(
40-
my_function=lambda v: 'my function says "{}"'.format(v)
40+
my_function='my function says "{}"'.format
4141
)
4242
return env
4343

@@ -67,7 +67,7 @@ def extra_tests():
6767
"""
6868
return dict(
6969
# Example: {% if a|int is custom_odd %}odd{% endif %}
70-
custom_odd=lambda n: True if (n % 2) else False
70+
custom_odd=lambda n: bool(n % 2)
7171
)
7272

7373
# {% endraw %}

0 commit comments

Comments
 (0)