| OLD | NEW |
| 1 # Copyright 2013 The LUCI Authors. All rights reserved. | 1 # Copyright 2013 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 from recipe_engine import recipe_api | 5 from recipe_engine import recipe_api |
| 6 from recipe_engine.types import freeze | |
| 7 import collections | 6 import collections |
| 8 | 7 |
| 9 # Use RecipeApiPlain because collections.Mapping has its own metaclass. | 8 # Use RecipeApiPlain because collections.Mapping has its own metaclass. |
| 10 # Additionally, nothing in this class is a composite_step (nothing in this class | 9 # Additionally, nothing in this class is a composite_step (nothing in this class |
| 11 # is any sort of step :). | 10 # is any sort of step :). |
| 12 class PropertiesApi(recipe_api.RecipeApiPlain, collections.Mapping): | 11 class PropertiesApi(recipe_api.RecipeApiPlain, collections.Mapping): |
| 13 """ | 12 """ |
| 14 Provide an immutable mapping view into the 'properties' for the current run. | 13 Provide an immutable mapping view into the 'properties' for the current run. |
| 15 | 14 |
| 16 The value of this api is equivalent to this transformation of the legacy | 15 The value of this api is equivalent to this transformation of the legacy |
| 17 build values: | 16 build values: |
| 18 val = factory_properties | 17 val = factory_properties |
| 19 val.update(build_properties) | 18 val.update(build_properties) |
| 20 """ | 19 """ |
| 21 | 20 |
| 22 properties_client = recipe_api.RequireClient('properties') | 21 properties_client = recipe_api.RequireClient('properties') |
| 23 | 22 |
| 24 def __init__(self, **kwargs): | |
| 25 super(PropertiesApi, self).__init__(**kwargs) | |
| 26 self._frozen_properties = None | |
| 27 | |
| 28 @property | |
| 29 def _properties(self): | |
| 30 if self._frozen_properties is None: | |
| 31 self._frozen_properties = freeze( | |
| 32 self.properties_client.get_properties()) | |
| 33 return self._frozen_properties | |
| 34 | |
| 35 def __getitem__(self, key): | 23 def __getitem__(self, key): |
| 36 return self._properties[key] | 24 return self.properties_client.properties[key] |
| 37 | 25 |
| 38 def __len__(self): | 26 def __len__(self): |
| 39 return len(self._properties) | 27 return len(self.properties_client.properties) |
| 40 | 28 |
| 41 def __iter__(self): | 29 def __iter__(self): |
| 42 return iter(self._properties) | 30 return iter(self.properties_client.properties) |
| 43 | 31 |
| 44 def legacy(self): # pragma: no cover | 32 def legacy(self): # pragma: no cover |
| 45 """Returns a reduced set of properties, possibly used by legacy scripts.""" | 33 """Returns a reduced set of properties, possibly used by legacy scripts.""" |
| 46 | 34 |
| 47 # Add all properties to this blacklist that are required for testing, but | 35 # Add all properties to this blacklist that are required for testing, but |
| 48 # not used by any lecacy scripts, in order to avoid vast expecation | 36 # not used by any lecacy scripts, in order to avoid vast expecation |
| 49 # changes. | 37 # changes. |
| 50 blacklist = set([ | 38 blacklist = set([ |
| 51 'buildbotURL', | 39 'buildbotURL', |
| 52 ]) | 40 ]) |
| 53 return {k: v for k, v in self.iteritems() if k not in blacklist} | 41 return {k: v for k, v in self.iteritems() if k not in blacklist} |
| 54 | 42 |
| 55 def thaw(self): | 43 def thaw(self): |
| 56 """Returns a vanilla python jsonish dictionary of properties.""" | 44 """Returns a vanilla python jsonish dictionary of properties.""" |
| 57 return self.properties_client.get_properties() | 45 return self.properties_client.mutable_properties() |
| OLD | NEW |