Mercurial > piecrust2
comparison foodtruck/configuration.py @ 610:efc1dc916e7c
admin: Configuration changes.
* Move publish targets to site configuration.
* Add direct accessor for the current site.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 28 Jan 2016 22:17:58 -0800 |
parents | effbc78b5528 |
children |
comparison
equal
deleted
inserted
replaced
609:978d8bca9fb3 | 610:efc1dc916e7c |
---|---|
8 | 8 |
9 | 9 |
10 logger = logging.getLogger(__name__) | 10 logger = logging.getLogger(__name__) |
11 | 11 |
12 | 12 |
13 def get_foodtruck_config(dirname=None): | 13 def get_foodtruck_config(dirname=None, fallback_config=None): |
14 dirname = dirname or os.getcwd() | 14 dirname = dirname or os.getcwd() |
15 cfg_path = os.path.join(dirname, 'foodtruck.yml') | 15 cfg_path = os.path.join(dirname, 'foodtruck.yml') |
16 return FoodTruckConfiguration(cfg_path) | 16 return FoodTruckConfiguration(cfg_path, fallback_config) |
17 | 17 |
18 | 18 |
19 class FoodTruckConfigNotFoundError(Exception): | 19 class FoodTruckConfigNotFoundError(Exception): |
20 pass | 20 pass |
21 | 21 |
22 | 22 |
23 class FoodTruckConfiguration(Configuration): | 23 class FoodTruckConfiguration(Configuration): |
24 def __init__(self, cfg_path): | 24 def __init__(self, cfg_path, fallback_config=None): |
25 super(FoodTruckConfiguration, self).__init__() | 25 super(FoodTruckConfiguration, self).__init__() |
26 self.cfg_path = cfg_path | 26 self.cfg_path = cfg_path |
27 self.fallback_config = fallback_config | |
27 | 28 |
28 def _load(self): | 29 def _load(self): |
29 try: | 30 try: |
30 with open(self.cfg_path, 'r', encoding='utf-8') as fp: | 31 with open(self.cfg_path, 'r', encoding='utf-8') as fp: |
31 values = yaml.load( | 32 values = yaml.load( |
32 fp.read(), | 33 fp.read(), |
33 Loader=ConfigurationLoader) | 34 Loader=ConfigurationLoader) |
34 | 35 |
35 self._values = self._validateAll(values) | 36 self._values = self._validateAll(values) |
36 except OSError: | 37 except OSError: |
37 raise FoodTruckConfigNotFoundError() | 38 if self.fallback_config is None: |
39 raise FoodTruckConfigNotFoundError() | |
40 | |
41 logger.debug("No FoodTruck configuration found, using fallback " | |
42 "configuration.") | |
43 self._values = copy.deepcopy(self.fallback_config) | |
38 except Exception as ex: | 44 except Exception as ex: |
39 raise ConfigurationError( | 45 raise ConfigurationError( |
40 "Error loading configuration from: %s" % | 46 "Error loading configuration from: %s" % |
41 self.cfg_path) from ex | 47 self.cfg_path) from ex |
42 | 48 |