diff options
author | Tristan Maat <tristan.maat@codethink.co.uk> | 2020-01-02 15:14:09 +0000 |
---|---|---|
committer | Tristan Maat <tm@tlater.net> | 2020-01-08 17:22:54 +0000 |
commit | 0ae6205598e49e44e45d6f943a0bba9825ce51c2 (patch) | |
tree | d3f08b0b1ec959d17da2ad30ab53f9e3ab65175f | |
parent | 3c1936ab225cce954428a962effbf47c8f43728c (diff) | |
download | buildstream-0ae6205598e49e44e45d6f943a0bba9825ce51c2.tar.gz |
_pipeline.py: Refactor if/elif/else usage
-rw-r--r-- | src/buildstream/_pipeline.py | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/src/buildstream/_pipeline.py b/src/buildstream/_pipeline.py index 8f356e9b1..353a9c3a0 100644 --- a/src/buildstream/_pipeline.py +++ b/src/buildstream/_pipeline.py @@ -197,10 +197,7 @@ class Pipeline: # the selected option. # def get_selection(self, targets, mode, *, silent=True): - - if mode == _PipelineSelection.NONE: - elements = targets - elif mode == _PipelineSelection.REDIRECT: + def redirect_and_log(): # Redirect and log if permitted elements = [] for t in targets: @@ -209,19 +206,21 @@ class Pipeline: self._message(MessageType.INFO, "Element '{}' redirected to '{}'".format(t.name, new_elm.name)) if new_elm not in elements: elements.append(new_elm) - elif mode == _PipelineSelection.PLAN: - elements = self.plan(targets) - else: - if mode == _PipelineSelection.ALL: - scope = Scope.ALL - elif mode == _PipelineSelection.BUILD: - scope = Scope.BUILD - elif mode == _PipelineSelection.RUN: - scope = Scope.RUN - - elements = list(self.dependencies(targets, scope)) + return elements - return elements + # Work around python not having a switch statement; this is + # much clearer than the if/elif/else block we used to have. + # + # Note that the lambda is necessary so that we don't evaluate + # all possible values at run time; that would be slow. + return { + _PipelineSelection.NONE: lambda: targets, + _PipelineSelection.REDIRECT: redirect_and_log, + _PipelineSelection.PLAN: lambda: self.plan(targets), + _PipelineSelection.ALL: lambda: list(self.dependencies(targets, Scope.ALL)), + _PipelineSelection.BUILD: lambda: list(self.dependencies(targets, Scope.BUILD)), + _PipelineSelection.RUN: lambda: list(self.dependencies(targets, Scope.RUN)), + }[mode]() # except_elements(): # |