Mercurial > piecrust2
comparison piecrust/admin/configuration.py @ 778:5e91bc0e3b4d
internal: Move admin panel code into the piecrust package.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 16 Jul 2016 15:02:24 +0200 |
parents | foodtruck/configuration.py@efc1dc916e7c |
children | 82509bce94ca |
comparison
equal
deleted
inserted
replaced
777:8d633ca59bc5 | 778:5e91bc0e3b4d |
---|---|
1 import os.path | |
2 import copy | |
3 import logging | |
4 import yaml | |
5 from piecrust.configuration import ( | |
6 Configuration, ConfigurationError, ConfigurationLoader, | |
7 merge_dicts) | |
8 | |
9 | |
10 logger = logging.getLogger(__name__) | |
11 | |
12 | |
13 def get_foodtruck_config(dirname=None, fallback_config=None): | |
14 dirname = dirname or os.getcwd() | |
15 cfg_path = os.path.join(dirname, 'foodtruck.yml') | |
16 return FoodTruckConfiguration(cfg_path, fallback_config) | |
17 | |
18 | |
19 class FoodTruckConfigNotFoundError(Exception): | |
20 pass | |
21 | |
22 | |
23 class FoodTruckConfiguration(Configuration): | |
24 def __init__(self, cfg_path, fallback_config=None): | |
25 super(FoodTruckConfiguration, self).__init__() | |
26 self.cfg_path = cfg_path | |
27 self.fallback_config = fallback_config | |
28 | |
29 def _load(self): | |
30 try: | |
31 with open(self.cfg_path, 'r', encoding='utf-8') as fp: | |
32 values = yaml.load( | |
33 fp.read(), | |
34 Loader=ConfigurationLoader) | |
35 | |
36 self._values = self._validateAll(values) | |
37 except OSError: | |
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) | |
44 except Exception as ex: | |
45 raise ConfigurationError( | |
46 "Error loading configuration from: %s" % | |
47 self.cfg_path) from ex | |
48 | |
49 def _validateAll(self, values): | |
50 if values is None: | |
51 values = {} | |
52 | |
53 values = merge_dicts(copy.deepcopy(default_configuration), values) | |
54 | |
55 return values | |
56 | |
57 def save(self): | |
58 with open(self.cfg_path, 'w', encoding='utf8') as fp: | |
59 self.cfg.write(fp) | |
60 | |
61 | |
62 default_configuration = { | |
63 'triggers': { | |
64 'bake': 'chef bake' | |
65 }, | |
66 'scm': { | |
67 'type': 'hg' | |
68 }, | |
69 'security': { | |
70 'username': '', | |
71 'password': '' | |
72 } | |
73 } | |
74 |