Skip to content

Commit 63e6c01

Browse files
authored
fix: fix circular import (#87)
Signed-off-by: Panos Vagenas <[email protected]>
1 parent 92ba765 commit 63e6c01

File tree

3 files changed

+50
-41
lines changed

3 files changed

+50
-41
lines changed

docling_core/types/doc/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
from docling_core.types.doc import BoundingBox, Size
3838
from docling_core.types.doc.base import ImageRefMode
3939
from docling_core.types.doc.labels import DocItemLabel, GroupLabel
40+
from docling_core.types.doc.utils import relative_path
4041
from docling_core.types.legacy_doc.tokens import DocumentToken
41-
from docling_core.utils.file import relative_path
4242

4343
Uint64 = typing.Annotated[int, Field(ge=0, le=(2**64 - 1))]
4444
LevelNumber = typing.Annotated[int, Field(ge=1, le=100)]

docling_core/types/doc/utils.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# Copyright IBM Corp. 2024 - 2024
3+
# SPDX-License-Identifier: MIT
4+
#
5+
6+
"""Utils for document types."""
7+
8+
from pathlib import Path
9+
10+
11+
def relative_path(src: Path, target: Path) -> Path:
12+
"""Compute the relative path from `src` to `target`.
13+
14+
Args:
15+
src (str | Path): The source directory or file path (must be absolute).
16+
target (str | Path): The target directory or file path (must be absolute).
17+
18+
Returns:
19+
Path: The relative path from `src` to `target`.
20+
21+
Raises:
22+
ValueError: If either `src` or `target` is not an absolute path.
23+
"""
24+
src = Path(src).resolve()
25+
target = Path(target).resolve()
26+
27+
# Ensure both paths are absolute
28+
if not src.is_absolute():
29+
raise ValueError(f"The source path must be absolute: {src}")
30+
if not target.is_absolute():
31+
raise ValueError(f"The target path must be absolute: {target}")
32+
33+
# Find the common ancestor
34+
common_parts = []
35+
for src_part, target_part in zip(src.parts, target.parts):
36+
if src_part == target_part:
37+
common_parts.append(src_part)
38+
else:
39+
break
40+
41+
# Determine the path to go up from src to the common ancestor
42+
up_segments = [".."] * (len(src.parts) - len(common_parts))
43+
44+
# Add the path from the common ancestor to the target
45+
down_segments = target.parts[len(common_parts) :]
46+
47+
# Combine and return the result
48+
return Path(*up_segments, *down_segments)

docling_core/utils/file.py

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pydantic import AnyHttpUrl, TypeAdapter, ValidationError
1616
from typing_extensions import deprecated
1717

18+
from docling_core.types.doc.utils import relative_path # noqa
1819
from docling_core.types.io import DocumentStream
1920

2021

@@ -168,43 +169,3 @@ def resolve_file_source(
168169
source=source,
169170
headers=headers,
170171
)
171-
172-
173-
def relative_path(src: Path, target: Path) -> Path:
174-
"""Compute the relative path from `src` to `target`.
175-
176-
Args:
177-
src (str | Path): The source directory or file path (must be absolute).
178-
target (str | Path): The target directory or file path (must be absolute).
179-
180-
Returns:
181-
Path: The relative path from `src` to `target`.
182-
183-
Raises:
184-
ValueError: If either `src` or `target` is not an absolute path.
185-
"""
186-
src = Path(src).resolve()
187-
target = Path(target).resolve()
188-
189-
# Ensure both paths are absolute
190-
if not src.is_absolute():
191-
raise ValueError(f"The source path must be absolute: {src}")
192-
if not target.is_absolute():
193-
raise ValueError(f"The target path must be absolute: {target}")
194-
195-
# Find the common ancestor
196-
common_parts = []
197-
for src_part, target_part in zip(src.parts, target.parts):
198-
if src_part == target_part:
199-
common_parts.append(src_part)
200-
else:
201-
break
202-
203-
# Determine the path to go up from src to the common ancestor
204-
up_segments = [".."] * (len(src.parts) - len(common_parts))
205-
206-
# Add the path from the common ancestor to the target
207-
down_segments = target.parts[len(common_parts) :]
208-
209-
# Combine and return the result
210-
return Path(*up_segments, *down_segments)

0 commit comments

Comments
 (0)