Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 """Helpers to proof charms and bundles.""" | |
2 import yaml | |
3 | |
4 from charmworld.models import ( | |
rharding
2013/10/18 19:09:24
I'm not a fan of importing models into a lib file,
gary.poster
2013/10/18 20:10:28
Similar alternative: You have to instantiate Bundl
| |
5 BundledCharmDescription, | |
6 resolve_charm_from_description, | |
7 ) | |
8 | |
9 | |
10 class ProofError(Exception): | |
11 | |
12 def __init__(self, debug_info, msg): | |
13 self.debug_info = debug_info | |
14 self.msg = msg | |
15 | |
16 | |
17 class BundleProof(object): | |
18 | |
19 @staticmethod | |
20 def check_parseable_deployer_file(data, format='yaml'): | |
gary.poster
2013/10/18 20:10:28
Ah, ye olde naming problem: a validation function
| |
21 try: | |
22 bundle_data = yaml.safe_load(data) | |
23 except yaml.YAMLError: | |
24 raise ProofError( | |
25 data, | |
26 'Could not parse the yaml provided.' | |
27 ) | |
28 return bundle_data | |
29 | |
30 @staticmethod | |
31 def check_service_exists(db, name, service_data, bundle_config): | |
32 charm_description = BundledCharmDescription( | |
33 'proof', service_data, bundle_config.get('series')) | |
34 charm_found = resolve_charm_from_description(db, charm_description) | |
35 if not charm_found: | |
36 raise ProofError( | |
37 charm_description.get_process(), | |
38 'Could not find charm: ' + name, | |
39 ) | |
40 else: | |
41 return charm_found | |
gary.poster
2013/10/18 20:10:28
Uh oh, this validation function is doing work too.
| |
42 | |
43 | |
44 class CharmProof(object): | |
45 | |
46 @staticmethod | |
47 def check_config(charm, test_key, test_value): | |
48 type_map = { | |
49 'boolean': ('bool',), | |
50 'int': ('int',), | |
51 'string': ('str', 'unicode'), | |
52 } | |
53 | |
54 if not charm.options: | |
rharding
2013/10/18 19:09:24
this is only called if it's expecting to proof opt
gary.poster
2013/10/18 20:10:28
Is this an error of the bundle author or in the ch
| |
55 raise ProofError([ | |
56 'Looking at charm id: ' + charm._id], | |
57 'The charm has no options.' | |
58 ) | |
59 | |
60 if test_key not in charm.options: | |
61 raise ProofError([ | |
62 'Looking at charm id: ' + charm._id, | |
63 'Found config keys: ' + str(charm.options.keys())], | |
64 'The charm has no option for: ' + test_key | |
65 ) | |
66 | |
67 # # The config key is a valid one for this charm. Check that the value | |
rharding
2013/10/18 19:09:24
doh, will clean up.
| |
68 # # is of the right type. | |
69 specified_type = charm.options[test_key]['type'] | |
70 valid_types = type_map[specified_type] | |
71 msg = "%s is not of type %s." | |
72 if type(test_value) not in valid_types: | |
73 raise ProofError( | |
74 ['Looking at charm id: ' + charm._id], | |
75 msg % (test_key, specified_type) | |
76 ) | |
OLD | NEW |