Skip to content

Instantly share code, notes, and snippets.

@joshuagl
Last active November 12, 2020 14:34
Show Gist options
  • Save joshuagl/240e651f2745849757b4402dc3419794 to your computer and use it in GitHub Desktop.
Save joshuagl/240e651f2745849757b4402dc3419794 to your computer and use it in GitHub Desktop.
import abc
from typing import BinaryIO, TextIO, Any, Dict
JsonDict = Dict[str, Any]
#
# Simple interfaces for abstracting away the storage and network requirements
# of a TUF client.
# Interface shape shamelessly stolen from go-tuf:
# https://github.com/theupdateframework/go-tuf/blob/9d8af573a771f7069ef7a24da942ad9262704534/client/client.go#L23
#
# Pros:
# * Class-based/OO is easier for most to reason about
# * Clients with existing download logic can wrap that in a thin
# implementation of a two method interface
# * Testing of the core client logic can be simplified with lightweight
# implementations of the interfaces i.e. MemoryStorer(Storer) and
# LocalFile(Fetcher)
#
# Cons:
# * EAFP style isn't fully compatible with interfaces, we can't encode the
# expected Exceptions
# * Better to return multiple values with an error type?
class Fetcher(metaclass=abc.ABCMeta):
# Returns a file object
# This leaves deciphering the file format (i.e. JSON) to the client logic,
# whereas it might be nicer to return a more structured datatype here
# (JsonDict?) and have the Fetcher implementation do translation from the wire
# format to the format the client can operate on
@abc.abstractmethod
def getMeta(self, path: str) -> JsonDict:
pass
# Returns a file object
@abc.abstractmethod
def getTarget(self, path: str) -> TemporaryFile:
pass
class Storer(metaclass=abc.ABCMeta):
# FIXME: parameters? go-tuf just loads all metadata, returning
# Dict[str, JsonDict] mapping role filenames -> JSON metadata
@abc.abstractmethod
def loadMeta(role: str) -> JsonDict:
pass
@abc.abstractmethod
def saveMeta(name: str, meta: JsonDict):
pass
class Updater(fetch: Fetcher, store: Storer):
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment