Skip to content

Commit

Permalink
Merge pull request #301 from trail-of-forks/ww/fix-typing
Browse files Browse the repository at this point in the history
in_toto_attestation/v1: fix type hints
  • Loading branch information
TomHennen authored Dec 11, 2023
2 parents 145b84a + 695a417 commit 325dc92
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
21 changes: 16 additions & 5 deletions python/in_toto_attestation/v1/resource_descriptor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
# Wrapper class for in-toto attestation ResourceDescriptor protos.

import json

import google.protobuf.json_format as pb_json
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, name: str='', uri: str='', digest: dict=None, content: bytes=bytes(), download_location: str='', media_type: str='', annotations: dict=None) -> 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
Expand All @@ -19,12 +30,12 @@ def __init__(self, name: str='', uri: str='', digest: dict=None, content: bytes=
self.pb.annotations.update(annotations)

@staticmethod
def copy_from_pb(proto: type[rdpb.ResourceDescriptor]) -> 'ResourceDescriptor':
def copy_from_pb(proto: rdpb.ResourceDescriptor) -> "ResourceDescriptor":
rd = ResourceDescriptor()
rd.pb.CopyFrom(proto)
return rd

def validate(self) -> None:
# at least one of name, URI or digest are required
if self.pb.name == '' and self.pb.uri == '' and len(self.pb.digest) == 0:
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")
21 changes: 11 additions & 10 deletions python/in_toto_attestation/v1/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import in_toto_attestation.v1.statement_pb2 as spb
from in_toto_attestation.v1.resource_descriptor import ResourceDescriptor

STATEMENT_TYPE_URI = 'https://in-toto.io/Statement/v1'
STATEMENT_TYPE_URI = "https://in-toto.io/Statement/v1"


class Statement:
def __init__(self, subjects: list, predicate_type: str, predicate: dict) -> None:
Expand All @@ -14,17 +15,17 @@ def __init__(self, subjects: list, predicate_type: str, predicate: dict) -> None
self.pb.predicate.update(predicate)

@staticmethod
def copy_from_pb(proto: type[spb.Statement]) -> 'Statement':
stmt = Statement([], '', {})
def copy_from_pb(proto: spb.Statement) -> "Statement":
stmt = Statement([], "", {})
stmt.pb.CopyFrom(proto)
return stmt

def validate(self) -> None:
def validate(self) -> None:
if self.pb.type != STATEMENT_TYPE_URI:
raise ValueError('Wrong statement type')
raise ValueError("Wrong statement type")

if len(self.pb.subject) == 0:
raise ValueError('At least one subject required')
raise ValueError("At least one subject required")

# check all resource descriptors in the subject
subject = self.pb.subject
Expand All @@ -36,10 +37,10 @@ def validate(self) -> None:
if len(rd.pb.digest) == 0:
# return index in the subjects list in case of failure:
# can't assume any other fields in subject are set
raise ValueError('At least one digest required (subject {0})'.format(i))
raise ValueError("At least one digest required (subject {0})".format(i))

if self.pb.predicate_type == '':
raise ValueError('Predicate type required')
if self.pb.predicate_type == "":
raise ValueError("Predicate type required")

if len(self.pb.predicate) == 0:
raise ValueError('Predicate object required')
raise ValueError("Predicate object required")

0 comments on commit 325dc92

Please sign in to comment.