build/generic_plaform: add loose parameter to return None when not available/existing.

Similar to loose parameter already present on Platform.lookup_request.
This commit is contained in:
Florent Kermarrec 2020-08-06 19:32:02 +02:00
parent 81df7b7036
commit 48d63f2362
1 changed files with 9 additions and 4 deletions

View File

@ -82,11 +82,14 @@ class PlatformInfo:
return "{}({})".format(self.__class__.__name__, repr(self.info)) return "{}({})".format(self.__class__.__name__, repr(self.info))
def _lookup(description, name, number): def _lookup(description, name, number, loose=True):
for resource in description: for resource in description:
if resource[0] == name and (number is None or resource[1] == number): if resource[0] == name and (number is None or resource[1] == number):
return resource return resource
raise ConstraintError("Resource not found: {}:{}".format(name, number)) if loose:
return None
else:
raise ConstraintError("Resource not found: {}:{}".format(name, number))
def _resource_type(resource): def _resource_type(resource):
@ -178,8 +181,10 @@ class ConstraintManager:
def add_extension(self, io): def add_extension(self, io):
self.available.extend(io) self.available.extend(io)
def request(self, name, number=None): def request(self, name, number=None, loose=False):
resource = _lookup(self.available, name, number) resource = _lookup(self.available, name, number, loose)
if resource is None:
return None
rt, ri = _resource_type(resource) rt, ri = _resource_type(resource)
if number is None: if number is None:
resource_name = name resource_name = name