An option value, together with a rank inferred from its source. Allows us to control which source wins: e.g., a command-line flag overrides an environment variable which overrides a config, etc. For example: Consider this config: [compile.java] foo: 11 And this envi
| 36 | |
| 37 | @dataclass(frozen=True) |
| 38 | class RankedValue: |
| 39 | """An option value, together with a rank inferred from its source. |
| 40 | |
| 41 | Allows us to control which source wins: e.g., a command-line flag overrides an environment |
| 42 | variable which overrides a config, etc. For example: |
| 43 | |
| 44 | Consider this config: |
| 45 | |
| 46 | [compile.java] |
| 47 | foo: 11 |
| 48 | |
| 49 | And this environment variable: |
| 50 | |
| 51 | PANTS_COMPILE_FOO: 22 |
| 52 | |
| 53 | If the command-line is |
| 54 | |
| 55 | ./pants compile target |
| 56 | |
| 57 | we expect the value of foo in the compile.java scope to be 22, because it was explicitly |
| 58 | set by the user in the enclosing compile scope. I.e., the outer scope's environment value |
| 59 | overrides the inner scope's config value. |
| 60 | |
| 61 | However if the command-line is |
| 62 | |
| 63 | ./pants compile.java --foo=33 target |
| 64 | |
| 65 | we now expect the value of foo in the compile.java to be 33. I.e., the inner scope's flag |
| 66 | overrides the outer scope's environment value. |
| 67 | |
| 68 | To tell these cases apart we need to know the "ranking" of the value. |
| 69 | """ |
| 70 | |
| 71 | @classmethod |
| 72 | def prioritized_iter( |
| 73 | cls, |
| 74 | flag_val: Value, |
| 75 | env_val: Value, |
| 76 | config_val: Value, |
| 77 | config_default_val: Value, |
| 78 | hardcoded_val: Value, |
| 79 | default: Value, |
| 80 | ) -> Iterator["RankedValue"]: |
| 81 | """Yield the non-None values from highest-ranked to lowest, wrapped in RankedValue |
| 82 | instances.""" |
| 83 | if flag_val is not None: |
| 84 | yield RankedValue(Rank.FLAG, flag_val) |
| 85 | if env_val is not None: |
| 86 | yield RankedValue(Rank.ENVIRONMENT, env_val) |
| 87 | if config_val is not None: |
| 88 | yield RankedValue(Rank.CONFIG, config_val) |
| 89 | if config_default_val is not None: |
| 90 | yield RankedValue(Rank.CONFIG_DEFAULT, config_default_val) |
| 91 | if hardcoded_val is not None: |
| 92 | yield RankedValue(Rank.HARDCODED, hardcoded_val) |
| 93 | yield RankedValue(Rank.NONE, default) |
| 94 | |
| 95 | rank: Rank |
no outgoing calls