Skip to content

Commit

Permalink
Run formatters based on the selected style
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Aug 10, 2022
1 parent a8c378a commit edd53a0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
21 changes: 12 additions & 9 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,38 @@ Current usage of ``pydocstringformatter``:
--strip-whitespaces, --no-strip-whitespaces
Activate or deactivate strip-whitespaces: Strip 1)
docstring start, 2) docstring end and 3) end of line.
(default: True)
Styles: default. (default: True)
--split-summary-body, --no-split-summary-body
Activate or deactivate split-summary-body: Split the
summary and body of a docstring based on a period and
max length. The maximum length of a summary can be set
with the --max-summary-lines option. (default: True)
with the --max-summary-lines option. Styles: pep257.
(default: True)
--beginning-quotes, --no-beginning-quotes
Activate or deactivate beginning-quotes: Fix the
position of the opening quotes. (default: True)
position of the opening quotes. Styles: default.
(default: True)
--closing-quotes, --no-closing-quotes
Activate or deactivate closing-quotes: Fix the
position of the closing quotes. (default: True)
position of the closing quotes. Styles: default.
(default: True)
--capitalize-first-letter, --no-capitalize-first-letter
Activate or deactivate capitalize-first-letter:
Capitalize the first letter of the docstring if
appropriate. (default: True)
appropriate. Styles: default. (default: True)
--final-period, --no-final-period
Activate or deactivate final-period: Add a period to
the end of single line docstrings and summaries.
(default: True)
Styles: default. (default: True)
--quotes-type, --no-quotes-type
Activate or deactivate quotes-type: Change all opening
and closing quotes to be triple quotes. (default:
True)
and closing quotes to be triple quotes. Styles:
default. (default: True)
optional formatters:
these formatters are turned off by default
--linewrap-full-docstring, --no-linewrap-full-docstring
Activate or deactivate linewrap-full-docstring:
Linewrap the docstring by the pre-defined line length.
(default: False)
Styles: default. (default: False)
3 changes: 2 additions & 1 deletion pydocstringformatter/_configuration/formatter_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def register_arguments_formatters(
formatter.activate_option,
action=BooleanOptionalAction,
dest=name,
help=f"Activate or deactivate {name}: {formatter.__doc__}",
help=f"Activate or deactivate {name}: {formatter.__doc__}"
f" Styles: {','.join(formatter.style)}.",
default=not formatter.optional,
)
6 changes: 6 additions & 0 deletions pydocstringformatter/_formatting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class Formatter:
config: argparse.Namespace
"""Namespace object set when set_config_namespace is called."""

style = ["default"]
"""Names of the docstring style(s) that are associated with this formatter.
Default is always ran.
"""

@property
@abc.abstractmethod
def name(self) -> str:
Expand Down
2 changes: 2 additions & 0 deletions pydocstringformatter/_formatting/formatters_pep257.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class SplitSummaryAndDocstringFormatter(SummaryFormatter):

name = "split-summary-body"

style = ["pep257"]

end_of_sentence_period = re.compile(
r"""
(?<!e.g|i.e|etc) # Not preceded by 'e.g', 'i.e', 'etc'
Expand Down
5 changes: 4 additions & 1 deletion pydocstringformatter/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def format_file(self, filename: Path) -> bool:

if _utils.is_docstring(new_tokeninfo, tokens[index - 1]):
for formatter in _formatting.FORMATTERS:
if getattr(self.config, formatter.name):
if (
"default" in formatter.style
or any(i in formatter.style for i in self.config.style)
) and getattr(self.config, formatter.name):
new_tokeninfo = formatter.treat_token(new_tokeninfo)
changed_tokens.append(new_tokeninfo)

Expand Down

0 comments on commit edd53a0

Please sign in to comment.