mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
apidoc: Split `main()` into funtions
This commit is contained in:
@@ -262,90 +262,100 @@ def main(argv: Sequence[str] = (), /) -> int:
|
|||||||
locale.setlocale(locale.LC_ALL, '')
|
locale.setlocale(locale.LC_ALL, '')
|
||||||
sphinx.locale.init_console()
|
sphinx.locale.init_console()
|
||||||
|
|
||||||
|
opts = _parse_args(argv)
|
||||||
|
rootpath = opts.module_path
|
||||||
|
excludes = tuple(
|
||||||
|
re.compile(fnmatch.translate(os.path.abspath(exclude)))
|
||||||
|
for exclude in dict.fromkeys(opts.exclude_pattern)
|
||||||
|
)
|
||||||
|
|
||||||
|
written_files, modules = recurse_tree(rootpath, excludes, opts, opts.templatedir)
|
||||||
|
|
||||||
|
if opts.full:
|
||||||
|
_full_quickstart(opts, modules=modules)
|
||||||
|
elif opts.tocfile:
|
||||||
|
written_files.append(
|
||||||
|
create_modules_toc_file(modules, opts, opts.tocfile, opts.templatedir)
|
||||||
|
)
|
||||||
|
|
||||||
|
if opts.remove_old and not opts.dryrun:
|
||||||
|
_remove_old_files(written_files, opts.destdir, opts.suffix)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_args(argv: Sequence[str], /) -> CliOptions:
|
||||||
parser = get_parser()
|
parser = get_parser()
|
||||||
args = parser.parse_args(argv or sys.argv[1:])
|
args = parser.parse_args(argv or sys.argv[1:])
|
||||||
|
|
||||||
args.module_path = rootpath = Path(args.module_path).resolve()
|
# normalise options
|
||||||
|
|
||||||
# normalize opts
|
args.module_path = root_path = Path(args.module_path).resolve()
|
||||||
|
args.destdir = Path(args.destdir)
|
||||||
|
if not root_path.is_dir():
|
||||||
|
logger.error(__('%s is not a directory.'), root_path)
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
if args.header is None:
|
if args.header is None:
|
||||||
args.header = str(rootpath).split(os.path.sep)[-1]
|
args.header = root_path.name
|
||||||
args.suffix = args.suffix.removeprefix('.')
|
args.suffix = args.suffix.removeprefix('.')
|
||||||
if not Path(rootpath).is_dir():
|
|
||||||
logger.error(__('%s is not a directory.'), rootpath)
|
|
||||||
raise SystemExit(1)
|
|
||||||
args.destdir = destdir = Path(args.destdir)
|
|
||||||
if not args.dryrun:
|
if not args.dryrun:
|
||||||
ensuredir(args.destdir)
|
ensuredir(args.destdir)
|
||||||
excludes = tuple(
|
|
||||||
re.compile(fnmatch.translate(os.path.abspath(exclude)))
|
|
||||||
for exclude in dict.fromkeys(args.exclude_pattern)
|
|
||||||
)
|
|
||||||
if not args.automodule_options:
|
if not args.automodule_options:
|
||||||
args.automodule_options = set()
|
args.automodule_options = set()
|
||||||
elif isinstance(args.automodule_options, str):
|
elif isinstance(args.automodule_options, str):
|
||||||
args.automodule_options = set(args.automodule_options.split(','))
|
args.automodule_options = set(args.automodule_options.split(','))
|
||||||
|
|
||||||
opts = CliOptions(**args.__dict__)
|
return CliOptions(**args.__dict__)
|
||||||
written_files, modules = recurse_tree(rootpath, excludes, opts, args.templatedir)
|
|
||||||
|
|
||||||
if args.full:
|
|
||||||
from sphinx.cmd import quickstart as qs
|
|
||||||
|
|
||||||
modules.sort()
|
def _full_quickstart(opts: CliOptions, /, *, modules: list[str]) -> None:
|
||||||
prev_module = ''
|
from sphinx.cmd import quickstart as qs
|
||||||
text = ''
|
|
||||||
for module in modules:
|
|
||||||
if module.startswith(prev_module + '.'):
|
|
||||||
continue
|
|
||||||
prev_module = module
|
|
||||||
text += f' {module}\n'
|
|
||||||
d: dict[str, Any] = {
|
|
||||||
'path': str(destdir),
|
|
||||||
'sep': False,
|
|
||||||
'dot': '_',
|
|
||||||
'project': args.header,
|
|
||||||
'author': args.author or 'Author',
|
|
||||||
'version': args.version or '',
|
|
||||||
'release': args.release or args.version or '',
|
|
||||||
'suffix': '.' + args.suffix,
|
|
||||||
'master': 'index',
|
|
||||||
'epub': True,
|
|
||||||
'extensions': [
|
|
||||||
'sphinx.ext.autodoc',
|
|
||||||
'sphinx.ext.viewcode',
|
|
||||||
'sphinx.ext.todo',
|
|
||||||
],
|
|
||||||
'makefile': True,
|
|
||||||
'batchfile': True,
|
|
||||||
'make_mode': True,
|
|
||||||
'mastertocmaxdepth': args.maxdepth,
|
|
||||||
'mastertoctree': text,
|
|
||||||
'language': 'en',
|
|
||||||
'module_path': str(rootpath),
|
|
||||||
'append_syspath': args.append_syspath,
|
|
||||||
}
|
|
||||||
if args.extensions:
|
|
||||||
d['extensions'].extend(args.extensions)
|
|
||||||
if args.quiet:
|
|
||||||
d['quiet'] = True
|
|
||||||
|
|
||||||
for ext in d['extensions'][:]:
|
modules.sort()
|
||||||
if ',' in ext:
|
prev_module = ''
|
||||||
d['extensions'].remove(ext)
|
text = ''
|
||||||
d['extensions'].extend(ext.split(','))
|
for module in modules:
|
||||||
|
if module.startswith(prev_module + '.'):
|
||||||
|
continue
|
||||||
|
prev_module = module
|
||||||
|
text += f' {module}\n'
|
||||||
|
d: dict[str, Any] = {
|
||||||
|
'path': str(opts.destdir),
|
||||||
|
'sep': False,
|
||||||
|
'dot': '_',
|
||||||
|
'project': opts.header,
|
||||||
|
'author': opts.author or 'Author',
|
||||||
|
'version': opts.version or '',
|
||||||
|
'release': opts.release or opts.version or '',
|
||||||
|
'suffix': '.' + opts.suffix,
|
||||||
|
'master': 'index',
|
||||||
|
'epub': True,
|
||||||
|
'extensions': [
|
||||||
|
'sphinx.ext.autodoc',
|
||||||
|
'sphinx.ext.viewcode',
|
||||||
|
'sphinx.ext.todo',
|
||||||
|
],
|
||||||
|
'makefile': True,
|
||||||
|
'batchfile': True,
|
||||||
|
'make_mode': True,
|
||||||
|
'mastertocmaxdepth': opts.maxdepth,
|
||||||
|
'mastertoctree': text,
|
||||||
|
'language': 'en',
|
||||||
|
'module_path': str(opts.module_path),
|
||||||
|
'append_syspath': opts.append_syspath,
|
||||||
|
}
|
||||||
|
if opts.extensions:
|
||||||
|
d['extensions'].extend(opts.extensions)
|
||||||
|
if opts.quiet:
|
||||||
|
d['quiet'] = True
|
||||||
|
|
||||||
if not args.dryrun:
|
for ext in d['extensions'][:]:
|
||||||
qs.generate(
|
if ',' in ext:
|
||||||
d, silent=True, overwrite=args.force, templatedir=args.templatedir
|
d['extensions'].remove(ext)
|
||||||
)
|
d['extensions'].extend(ext.split(','))
|
||||||
elif args.tocfile:
|
|
||||||
written_files.append(
|
|
||||||
create_modules_toc_file(modules, opts, args.tocfile, args.templatedir)
|
|
||||||
)
|
|
||||||
|
|
||||||
if args.remove_old and not args.dryrun:
|
if not opts.dryrun:
|
||||||
_remove_old_files(written_files, destdir, args.suffix)
|
qs.generate(d, silent=True, overwrite=opts.force, templatedir=opts.templatedir)
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|||||||
Reference in New Issue
Block a user