-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make parameters explicit in constructors; use in tests * Add type annotations in functions * Update constructors based on #257 and #263 Signed-off-by: Marcela Melara <marcela.melara@intel.com>
- Loading branch information
1 parent
62c1e9c
commit e5f2403
Showing
5 changed files
with
88 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,30 @@ | ||
# Wrapper class for in-toto attestation ResourceDescriptor protos. | ||
|
||
import in_toto_attestation.v1.resource_descriptor_pb2 as rdpb | ||
from google.protobuf.struct_pb2 import Value | ||
import google.protobuf.json_format as pb_json | ||
import json | ||
|
||
class ResourceDescriptor: | ||
def __init__(self, rd=None): | ||
def __init__(self, name: str='', uri: str='', digest: dict=None, content: bytes=bytes(), download_location: str='', media_type: str='', annotations: dict=None) -> None: | ||
self.pb = rdpb.ResourceDescriptor() | ||
self.pb.name = name | ||
self.pb.uri = uri | ||
if digest: | ||
self.pb.digest.update(digest) | ||
self.pb.content = content | ||
self.pb.download_location = download_location | ||
self.pb.media_type = media_type | ||
if annotations: | ||
self.pb.annotations.update(annotations) | ||
|
||
if rd: | ||
self.pb.CopyFrom(rd) | ||
@staticmethod | ||
def copy_from_pb(proto: type[rdpb.ResourceDescriptor]) -> 'ResourceDescriptor': | ||
rd = ResourceDescriptor() | ||
rd.pb.CopyFrom(proto) | ||
return rd | ||
|
||
def validate(self): | ||
def validate(self) -> None: | ||
# at least one of name, URI or digest are required | ||
if (not self.pb.name and not self.pb.uri and not self.pb.digest) or len(self.pb.digest) == 0: | ||
raise ValueError("At least one of name, URI, or digest are required") | ||
if self.pb.name == '' and self.pb.uri == '' and len(self.pb.digest) == 0: | ||
raise ValueError("At least one of name, URI, or digest need to be set") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters