Coverage for src/bioimageio/core/_description_serializer.py: 0%

61 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 09:46 +0000

1import base64 

2import hashlib 

3from io import BytesIO 

4from zipfile import ZipFile 

5 

6from bioimageio.spec import ( 

7 InvalidDescr, 

8 ResourceDescr, 

9 load_description, 

10 save_bioimageio_package_to_stream, 

11) 

12from bioimageio.spec.common import FileDescr, RelativeFilePath, Sha256 

13from bioimageio.spec.model import v0_4 

14 

15 

16class DescriptionSerializer: 

17 """Description serializer intended for client/server communication, NOT for sharing resource descriptions. 

18 

19 This serializer only includes local files and required v0.4 Python architectures 

20 to keep the serialized package small. 

21 """ 

22 

23 STRING_ENCODING = "ascii" 

24 

25 @staticmethod 

26 def serialize(rd: ResourceDescr) -> bytes: 

27 stream = save_bioimageio_package_to_stream(rd, local_files_only=True) 

28 if isinstance(rd, v0_4.ModelDescr): 

29 weights = rd.weights.pytorch_state_dict 

30 if weights is not None and isinstance( 

31 weights.architecture, v0_4.CallableFromFile 

32 ): 

33 source = weights.architecture.source_file 

34 if isinstance(source, RelativeFilePath): 

35 name = str(source) 

36 if "\\" in name or ":" in name or ".." in source.path.parts: 

37 raise ValueError(f"Unsafe architecture path: {name}") 

38 

39 contents = ( 

40 FileDescr(source=source, sha256=weights.architecture_sha256) 

41 .get_reader() 

42 .read() 

43 ) 

44 with ZipFile(stream, "a") as package: 

45 if name in package.namelist(): 

46 if package.read(name) != contents: 

47 raise ValueError(f"Conflicting package file: {name}") 

48 else: 

49 package.writestr(name, contents) 

50 _ = stream.seek(0) 

51 return stream.read() 

52 

53 @classmethod 

54 def serialize_to_string(cls, rd: ResourceDescr) -> str: 

55 package_bytes = cls.serialize(rd) 

56 

57 safe_bytes = cls._get_safe_bytes(package_bytes) 

58 serialized_str = safe_bytes.decode(cls.STRING_ENCODING) 

59 serialized_str = cls._pad_to_min_length(serialized_str) 

60 return serialized_str 

61 

62 MIN_LENGTH = 2084 

63 """Serialized strings of at most 2083 characters would be treated as a URL on the server side.""" 

64 

65 @classmethod 

66 def _pad_to_min_length(cls, serialized_str: str) -> str: 

67 # `base64.b64decode` discards non-alphabet characters, so newlines are safe padding 

68 return serialized_str + "\n" * (cls.MIN_LENGTH - len(serialized_str)) 

69 

70 @staticmethod 

71 def _get_safe_bytes(raw_bytes: bytes) -> bytes: 

72 return base64.b64encode(raw_bytes) 

73 

74 @classmethod 

75 def deserialize_from_string(cls, serialized: str) -> ResourceDescr: 

76 package_bytes = base64.b64decode(serialized.encode(cls.STRING_ENCODING)) 

77 return cls.deserialize(package_bytes) 

78 

79 @staticmethod 

80 def deserialize(serialized: bytes) -> ResourceDescr: 

81 descr = load_description(ZipFile(BytesIO(serialized)), perform_io_checks=False) 

82 if isinstance(descr, InvalidDescr): 

83 raise TypeError(f"invalid serialized model package: {descr.get_reason()}") 

84 

85 return descr 

86 

87 @classmethod 

88 def serialize_to_string_and_hash(cls, rd: ResourceDescr) -> tuple[str, Sha256]: 

89 package_bytes = cls.serialize(rd) 

90 safe_bytes = cls._get_safe_bytes(package_bytes) 

91 serialized_str = safe_bytes.decode(cls.STRING_ENCODING) 

92 serialized_str = cls._pad_to_min_length(serialized_str) 

93 sha256 = Sha256(hashlib.sha256(package_bytes).hexdigest()) 

94 return serialized_str, sha256