style: format source code using Ruff

This commit is contained in:
jakob.scheid committed 2026-09-21 20:10:04 +02:00
1 parent d9887f34c9
commit aa4a139878
41 files changed
+6061 -5670

No files matched your search

+40 -37
View File
@@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
'''
"""
The command line interface
'''
"""
import argparse
import pathlib
@@ -31,80 +31,81 @@ __all__ = [
_LANG_PYTHON = 'python'
_PROGRAM_NAME = 'jcloud-docsgen'
def existing_directory(path: str) -> pathlib.Path:
_path = path
path = pathlib.Path(_path)
if not path.exists():
raise argparse.ArgumentTypeError(
f'{_path}: no such file or directory'
)
raise argparse.ArgumentTypeError(f'{_path}: no such file or directory')
if not path.is_dir():
raise argparse.ArgumentTypeError(f'{_path}: not a directory')
return path
def error(message: str, exit_code: int = 1):
'''
"""
Shows an error message and returns the exit code.
:param message: The error message to show.
:type message: str
:param exit_code: The exit code.
:type exit_code: int
'''
"""
print(f'{_PROGRAM_NAME}: error: {message}', file = sys.stderr)
print(f'{_PROGRAM_NAME}: error: {message}', file=sys.stderr)
return exit_code
def create_argument_parser() -> argparse.ArgumentParser:
'''
"""
Prepares a parser to parse the command-line arguments using
argparse.
:return: The parser.
:rtype: argparse.ArgumentParser
'''
"""
common_parser = argparse.ArgumentParser(add_help = False)
common_parser = argparse.ArgumentParser(add_help=False)
common_parser.add_argument(
'project_directory',
type = existing_directory,
nargs = '?',
default = pathlib.Path('.'),
help = 'The directory of the project',
type=existing_directory,
nargs='?',
default=pathlib.Path('.'),
help='The directory of the project',
)
common_parser.add_argument(
'-o',
'--output-dir',
type = pathlib.Path,
help = 'the output directory the documentation will be written to',
type=pathlib.Path,
help='the output directory the documentation will be written to',
)
argument_parser = argparse.ArgumentParser()
subparsers = argument_parser.add_subparsers(
dest = 'language',
required = True,
dest='language',
required=True,
)
python_parser = subparsers.add_parser(
_LANG_PYTHON,
parents = [common_parser],
help = 'generate documentation for Python project',
parents=[common_parser],
help='generate documentation for Python project',
)
python_parser.add_argument(
'-s',
'--sections',
nargs = '+',
choices = VALID_PYTHON_SECTIONS,
help = 'the sections to be included in the documentation',
nargs='+',
choices=VALID_PYTHON_SECTIONS,
help='the sections to be included in the documentation',
)
python_parser.add_argument(
'-S',
'--sections-exclude',
nargs = '+',
default = [],
choices = VALID_PYTHON_SECTIONS,
help = (
nargs='+',
default=[],
choices=VALID_PYTHON_SECTIONS,
help=(
'the sections to be excluded in the documentation. '
'If a section shall be included AND excluded, it is excluded.'
),
@@ -112,8 +113,8 @@ def create_argument_parser() -> argparse.ArgumentParser:
python_parser.add_argument(
'-H',
'--allow-html',
action = 'store_true',
help = (
action='store_true',
help=(
'indicates whether HTML is allowed in the documentation. '
'By default, HTML is disabled.'
),
@@ -121,8 +122,8 @@ def create_argument_parser() -> argparse.ArgumentParser:
python_parser.add_argument(
'-T',
'--allow-tables',
action = 'store_true',
help = (
action='store_true',
help=(
'indicates whether Markdown tables are allowed in the '
'documentation. By default, Markdown tables are disabled. '
'Warning: Markdown tables are not CommonMark.'
@@ -131,7 +132,8 @@ def create_argument_parser() -> argparse.ArgumentParser:
return argument_parser
def main(argv = None) -> int:
def main(argv=None) -> int:
argument_parser = create_argument_parser()
args = argument_parser.parse_args(argv)
@@ -141,13 +143,14 @@ def main(argv = None) -> int:
try:
if args.language == _LANG_PYTHON:
from .languages.python import generate
generate(
args.project_directory,
args.output_dir,
include_sections = args.sections,
exclude_sections = args.sections_exclude,
allow_html = args.allow_html,
allow_tables = args.allow_tables,
include_sections=args.sections,
exclude_sections=args.sections_exclude,
allow_html=args.allow_html,
allow_tables=args.allow_tables,
)
except FileNotFoundError as exc:
return error(f'{exc.filename}: no such file or directory')
+24 -21
View File
@@ -29,11 +29,12 @@ VALID_SECTIONS = list(
PythonDefinitionDocumentationIncludeSections()._args.keys()
)
def parse_sections_list(
include_sections: Optional[list[str]] = None,
exclude_sections: list[str] = [],
) -> PythonDefinitionDocumentationIncludeSections:
'''
"""
Returns a Python definition documentation sections list from the
sections list to be included and the sections list to be excluded.
These lists both consist of the sections as strings which contain
@@ -46,7 +47,7 @@ def parse_sections_list(
:return: The Python definition documentation sections object.
:rtype: PythonDefinitionDocumentationIncludeSections
'''
"""
if include_sections is None:
include_sections = VALID_SECTIONS[:]
@@ -56,17 +57,18 @@ def parse_sections_list(
del include_sections[include_sections.index(section)]
return PythonDefinitionDocumentationIncludeSections(
signature = 'signature' in include_sections,
returns = 'returns' in include_sections,
params = 'params' in include_sections,
exceptions = 'exceptions' in include_sections,
examples = 'examples' in include_sections,
docstring = 'docstring' in include_sections,
bases = 'bases' in include_sections,
decorators = 'decorators' in include_sections,
class_body = 'class_body' in include_sections,
signature='signature' in include_sections,
returns='returns' in include_sections,
params='params' in include_sections,
exceptions='exceptions' in include_sections,
examples='examples' in include_sections,
docstring='docstring' in include_sections,
bases='bases' in include_sections,
decorators='decorators' in include_sections,
class_body='class_body' in include_sections,
)
def _generate(
project_directory: pathlib.Path,
output_directory: pathlib.Path,
@@ -77,7 +79,7 @@ def _generate(
allow_html: bool = False,
allow_tables: bool = False,
) -> None:
'''
"""
Generates the documentation for a Python project.
:param project_directory: The project directory.
@@ -92,17 +94,18 @@ def _generate(
:type allow_html: bool
:param allow_tables: Indicates whether Markdown tables are allowed.
Warning: Markdown tables are not CommonMark.
'''
"""
docs_generator = PythonDocumentationGenerator(
project_directory,
output_directory,
include_sections = include_sections,
allow_html = allow_html,
allow_tables = allow_tables,
include_sections=include_sections,
allow_html=allow_html,
allow_tables=allow_tables,
)
docs_generator.generate_documentation()
def generate(
project_directory: pathlib.Path,
output_directory: pathlib.Path,
@@ -112,7 +115,7 @@ def generate(
allow_html: bool = False,
allow_tables: bool = False,
) -> None:
'''
"""
Generates the documentation for a Python project.
:param project_directory: The project directory.
@@ -130,14 +133,14 @@ def generate(
:type allow_html: bool
:param allow_tables: Indicates whether Markdown tables are allowed.
Warning: Markdown tables are not CommonMark.
'''
"""
sections = parse_sections_list(include_sections, exclude_sections)
_generate(
project_directory,
output_directory,
include_sections = sections,
allow_html = allow_html,
allow_tables = allow_tables,
include_sections=sections,
allow_html=allow_html,
allow_tables=allow_tables,
)