Skip to content

Commit

Permalink
Switch to black
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Finucane <[email protected]>
  • Loading branch information
stephenfin committed Jul 22, 2020
1 parent 7a7240b commit 8bfb76a
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 84 deletions.
33 changes: 28 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
---
default_language_version:
# force all unspecified python hooks to run python3
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
- id: flake8
- id: trailing-whitespace
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
hooks:
- id: trailing-whitespace
- id: mixed-line-ending
args: ['--fix', 'lf']
- id: check-byte-order-marker
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: debug-statements
- id: end-of-file-fixer
- id: check-yaml
files: .*\.(yaml|yml)$
- id: check-added-large-files
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.3
hooks:
- id: flake8
16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.black]
line-length = 88
target-version = ['py36']
skip-string-normalization = true
exclude = '''
(
/(
\.eggs
| \.git
| \.tox
| \.venv
| build
| dist
)
)
'''
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ packages =

[wheel]
universal = 1

[flake8]
max-line-length = 88
ignore = E203,E501,E741,W503
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
from setuptools import setup

setup(
setup_requires=['pbr>=2.0'],
pbr=True,
setup_requires=['pbr>=2.0'], pbr=True,
)
83 changes: 46 additions & 37 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def _get_help_record(opt):
[1] http://www.sphinx-doc.org/en/stable/domains.html#directive-option
"""

def _write_opts(opts):
rv, _ = click.formatting.join_options(opts)
if not opt.is_flag and not opt.count:
Expand All @@ -57,9 +58,14 @@ def _write_opts(opts):
# documentation thus needs a manually written string.
extra.append('default: %s' % opt.show_default)
else:
extra.append('default: %s' %
(', '.join('%s' % d for d in opt.default) if isinstance(
opt.default, (list, tuple)) else opt.default, ))
extra.append(
'default: %s'
% (
', '.join('%s' % d for d in opt.default)
if isinstance(opt.default, (list, tuple))
else opt.default,
)
)
if opt.required:
extra.append('required')
if extra:
Expand All @@ -84,9 +90,9 @@ def _format_description(ctx):
return

bar_enabled = False
for line in statemachine.string2lines(help_string,
tab_width=4,
convert_whitespace=True):
for line in statemachine.string2lines(
help_string, tab_width=4, convert_whitespace=True
):
if line == '\b':
bar_enabled = True
continue
Expand All @@ -113,18 +119,19 @@ def _format_option(opt):
yield '.. option:: {}'.format(opt[0])
if opt[1]:
yield ''
for line in statemachine.string2lines(opt[1],
tab_width=4,
convert_whitespace=True):
for line in statemachine.string2lines(
opt[1], tab_width=4, convert_whitespace=True
):
yield _indent(line)


def _format_options(ctx):
"""Format all `click.Option` for a `click.Command`."""
# the hidden attribute is part of click 7.x only hence use of getattr
params = [
x for x in ctx.command.params
if isinstance(x, click.Option) and not getattr(x, 'hidden', False)
param
for param in ctx.command.params
if isinstance(param, click.Option) and not getattr(param, 'hidden', False)
]

for param in params:
Expand All @@ -137,9 +144,11 @@ def _format_argument(arg):
"""Format the output of a `click.Argument`."""
yield '.. option:: {}'.format(arg.human_readable_name)
yield ''
yield _indent('{} argument{}'.format(
'Required' if arg.required else 'Optional',
'(s)' if arg.nargs != 1 else ''))
yield _indent(
'{} argument{}'.format(
'Required' if arg.required else 'Optional', '(s)' if arg.nargs != 1 else ''
)
)


def _format_arguments(ctx):
Expand Down Expand Up @@ -195,9 +204,9 @@ def _format_subcommand(command):

if short_help:
yield ''
for line in statemachine.string2lines(short_help,
tab_width=4,
convert_whitespace=True):
for line in statemachine.string2lines(
short_help, tab_width=4, convert_whitespace=True
):
yield _indent(line)


Expand Down Expand Up @@ -311,39 +320,39 @@ def _load_module(self, module_path):
module_name, attr_name = module_path.split(':', 1)
except ValueError: # noqa
raise self.error(
'"{}" is not of format "module:parser"'.format(module_path))
'"{}" is not of format "module:parser"'.format(module_path)
)

try:
mod = __import__(module_name, globals(), locals(), [attr_name])
except (Exception, SystemExit) as exc: # noqa
err_msg = 'Failed to import "{}" from "{}". '.format(
attr_name, module_name)
err_msg = 'Failed to import "{}" from "{}". '.format(attr_name, module_name)
if isinstance(exc, SystemExit):
err_msg += 'The module appeared to call sys.exit()'
else:
err_msg += 'The following exception was raised:\n{}'.format(
traceback.format_exc())
traceback.format_exc()
)

raise self.error(err_msg)

if not hasattr(mod, attr_name):
raise self.error('Module "{}" has no attribute "{}"'.format(
module_name, attr_name))
raise self.error(
'Module "{}" has no attribute "{}"'.format(module_name, attr_name)
)

parser = getattr(mod, attr_name)

if not isinstance(parser, click.BaseCommand):
raise self.error('"{}" of type "{}" is not derived from '
'"click.BaseCommand"'.format(
type(parser), module_path))
raise self.error(
'"{}" of type "{}" is not derived from '
'"click.BaseCommand"'.format(type(parser), module_path)
)
return parser

def _generate_nodes(self,
name,
command,
parent=None,
show_nested=False,
commands=None):
def _generate_nodes(
self, name, command, parent=None, show_nested=False, commands=None
):
"""Generate the relevant Sphinx nodes.
Format a `click.Group` or `click.Command`.
Expand All @@ -367,7 +376,8 @@ def _generate_nodes(self,
'',
nodes.title(text=name),
ids=[nodes.make_id(ctx.command_path)],
names=[nodes.fully_normalize_name(ctx.command_path)])
names=[nodes.fully_normalize_name(ctx.command_path)],
)

# Summary

Expand All @@ -387,8 +397,8 @@ def _generate_nodes(self,
commands = _filter_commands(ctx, commands)
for command in commands:
section.extend(
self._generate_nodes(command.name, command, ctx,
show_nested))
self._generate_nodes(command.name, command, ctx, show_nested)
)

return [section]

Expand All @@ -404,8 +414,7 @@ def run(self):
show_nested = 'show-nested' in self.options
commands = self.options.get('commands')

return self._generate_nodes(prog_name, command, None, show_nested,
commands)
return self._generate_nodes(prog_name, command, None, show_nested, commands)


def setup(app):
Expand Down
Loading

0 comments on commit 8bfb76a

Please sign in to comment.