manifest: add get method to lookup pipelines

Add a new helper helper, `Manfifest.get` that will return a
pipeline give a name or an id or `None` if no pipeline could
be found with either. The implementation is taken from the
existing `__getitem__` method and the latter as now based on
the new `get` method.
This commit is contained in:
Christian Kellner 2021-01-21 15:50:28 +00:00
parent 569345cc72
commit 18671686ee

View file

@ -3,7 +3,7 @@ import contextlib
import hashlib
import json
import os
from typing import Dict, Iterator
from typing import Dict, Iterator, Optional
from .api import API
from . import buildroot
@ -339,14 +339,20 @@ class Manifest:
return points
def __getitem__(self, name_or_id: str) -> Pipeline:
def get(self, name_or_id: str) -> Optional[Pipeline]:
pl = self.pipelines.get(name_or_id)
if pl:
return pl
for pl in self.pipelines.values():
if pl.id == name_or_id:
return pl
raise KeyError("{name_or_id} not found")
return None
def __getitem__(self, name_or_id: str) -> Pipeline:
pl = self.get(name_or_id)
if pl:
return pl
raise KeyError(f"'{name_or_id}' not found")
def __iter__(self) -> Iterator[Pipeline]:
return iter(self.pipelines.values())