MCPcopy Create free account
hub / github.com/Paper2Poster/Paper2Poster / create_file_hash

Function create_file_hash

docling/utils/utils.py:19–37  ·  view source on GitHub ↗

Create a stable page_hash of the path_or_stream of a file

(path_or_stream: Union[BytesIO, Path])

Source from the content-addressed store, hash-verified

17
18
19def create_file_hash(path_or_stream: Union[BytesIO, Path]) -> str:
20 """Create a stable page_hash of the path_or_stream of a file"""
21
22 block_size = 65536
23 hasher = hashlib.sha256()
24
25 def _hash_buf(binary_stream):
26 buf = binary_stream.read(block_size) # read and page_hash in chunks
27 while len(buf) > 0:
28 hasher.update(buf)
29 buf = binary_stream.read(block_size)
30
31 if isinstance(path_or_stream, Path):
32 with path_or_stream.open("rb") as afile:
33 _hash_buf(afile)
34 elif isinstance(path_or_stream, BytesIO):
35 _hash_buf(path_or_stream)
36
37 return hasher.hexdigest()
38
39
40def create_hash(string: str):

Callers 1

__init__Method · 0.90

Calls 1

_hash_bufFunction · 0.70

Tested by

no test coverage detected