Files
docling-core/docling_core/utils/generate_docs.py
NintoracandCesar Berrospi Ramis be42fcea2e feat: add fsspec support (#444)
* feat: add universal-pathlib support for document to_*/from_* methods

Enable cloud storage backends (S3, GCS, Azure, etc.) for document
serialization methods via fsspec/universal-pathlib integration.

Signed-off-by: Nintorac <Nintorac@users.noreply.github.com>
Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>
Co-authored-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* test(fsspec): remove universal-pathlib check

Remove the import check for universal-pathlib library and the decorators
'@pytest.mark.skipif' since it is unnecessary and adds complexity.

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* chore: remove potentially misleading comment

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* fix(types): convert image URI path to AnyUrl for type safety

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* test(upath): add error handling for remote paths in 'relative_path()'

Catch error in case 'relative_path()' is used with remote paths and 'resolve' fails.
Consolidate the tests in test_upath_support.py module.

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* fix: misplaced block, annotations, tests

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* refactor: deduplicate code in '_with_pictures_refs'

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

---------

Signed-off-by: Nintorac <Nintorac@users.noreply.github.com>
Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>
Co-authored-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>
2026-07-02 16:36:19 +02:00

75 lines
2.1 KiB
Python

"""Generate documentation of Docling types as JSON schema.
Example:
python docling_core/utils/generate_docs.py /tmp/docling_core_files
"""
import argparse
import json
from argparse import BooleanOptionalAction
from pathlib import Path
from shutil import rmtree
from typing import Final
from docling_core.utils.generate_jsonschema import generate_json_schema
MODELS: Final = ["DoclingDocument"]
def _prepare_directory(folder: str, clean: bool = False) -> None:
"""Create a directory or empty its content if it already exists.
Args:
folder: The name of the directory.
clean: Whether any existing content in the directory should be removed.
"""
folder_path = Path(folder)
if folder_path.is_dir():
if clean:
for path in folder_path.glob("**/*"):
if path.is_file():
path.unlink()
elif path.is_dir():
rmtree(path)
else:
folder_path.mkdir(parents=True, exist_ok=True)
def generate_collection_jsonschema(folder: str):
"""Generate the JSON schema of Docling collections and export them to a folder.
Args:
folder: The name of the directory.
"""
folder_path = Path(folder)
for item in MODELS:
json_schema = generate_json_schema(item)
output_file = folder_path / f"{item}.json"
output_file.write_text(json.dumps(json_schema, ensure_ascii=False, indent=2), encoding="utf-8")
def main() -> None:
"""Generate the JSON Schema of Docling collections and export documentation."""
argparser = argparse.ArgumentParser()
argparser.add_argument(
"directory",
help=("Directory to generate files. If it exists, any existing content will be removed."),
)
argparser.add_argument(
"--clean",
help="Whether any existing content in directory should be removed.",
action=BooleanOptionalAction,
dest="clean",
default=False,
required=False,
)
args = argparser.parse_args()
_prepare_directory(args.directory, args.clean)
generate_collection_jsonschema(args.directory)
if __name__ == "__main__":
main()