Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/in_toto_attestation/v1/resource_descriptor.py: 73%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

26 statements  

1# Wrapper class for in-toto attestation ResourceDescriptor protos. 

2 

3from __future__ import annotations 

4 

5import json 

6 

7import google.protobuf.json_format as pb_json 

8from google.protobuf.struct_pb2 import Value 

9 

10import in_toto_attestation.v1.resource_descriptor_pb2 as rdpb 

11 

12 

13class ResourceDescriptor: 

14 def __init__( 

15 self, 

16 name: str = "", 

17 uri: str = "", 

18 digest: dict | None = None, 

19 content: bytes = bytes(), 

20 download_location: str = "", 

21 media_type: str = "", 

22 annotations: dict | None = None, 

23 ) -> None: 

24 self.pb = rdpb.ResourceDescriptor() # type: ignore[attr-defined] 

25 self.pb.name = name 

26 self.pb.uri = uri 

27 if digest: 

28 self.pb.digest.update(digest) 

29 self.pb.content = content 

30 self.pb.download_location = download_location 

31 self.pb.media_type = media_type 

32 if annotations: 

33 self.pb.annotations.update(annotations) 

34 

35 @staticmethod 

36 def copy_from_pb(proto: rdpb.ResourceDescriptor) -> "ResourceDescriptor": # type: ignore[name-defined] 

37 rd = ResourceDescriptor() 

38 rd.pb.CopyFrom(proto) 

39 return rd 

40 

41 def validate(self) -> None: 

42 # at least one of name, URI or digest are required 

43 if self.pb.name == "" and self.pb.uri == "" and len(self.pb.digest) == 0: 

44 raise ValueError("At least one of name, URI, or digest need to be set")