Merge lp:~niemeyer/pyjuju/simplify-iface-schema into lp:pyjuju

Proposed by Gustavo Niemeyer
Status: Merged
Approved by: Kapil Thangavelu
Approved revision: 333
Merged at revision: 338
Proposed branch: lp:~niemeyer/pyjuju/simplify-iface-schema
Merge into: lp:pyjuju
Diff against target: 172 lines (+42/-58)
2 files modified
ensemble/formula/metadata.py (+19/-33)
ensemble/formula/tests/test_metadata.py (+23/-25)
To merge this branch: bzr merge lp:~niemeyer/pyjuju/simplify-iface-schema
Reviewer Review Type Date Requested Status
Kapil Thangavelu (community) Approve
William Reade (community) Approve
Review via email: mp+73100@code.launchpad.net

Description of the change

The interface schema handling is doing unnecessary magic.

To post a comment you must log in.
Revision history for this message
William Reade (fwereade) wrote :

LGTM

review: Approve
Revision history for this message
Kapil Thangavelu (hazmat) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ensemble/formula/metadata.py'
2--- ensemble/formula/metadata.py 2011-08-26 17:28:45 +0000
3+++ ensemble/formula/metadata.py 2011-08-26 20:38:25 +0000
4@@ -6,7 +6,7 @@
5 EnsembleError, InvalidEnsembleHeaderValue, FileNotFound)
6 from ensemble.formula.errors import FormulaIdError
7 from ensemble.lib.schema import (
8- SchemaError, Bool, Constant, Dict, Int,
9+ SchemaError, Bool, Constant, Dict, String, Int,
10 KeyDict, OneOf, UnicodeOrString)
11
12
13@@ -50,8 +50,10 @@
14 return (namespace, name, int(revision))
15
16
17+UTF8_SCHEMA = UnicodeOrString("utf-8")
18+
19 INTERFACE_SCHEMA = KeyDict({
20- "interface": UnicodeOrString("utf-8"),
21+ "interface": UTF8_SCHEMA,
22 "limit": OneOf(Constant(None), Int()),
23 "optional": Bool()})
24
25@@ -81,8 +83,6 @@
26 representation as seen in the mysql interface description above.
27 """
28
29- value_converter = UnicodeOrString("utf-8")
30-
31 def __init__(self, limit):
32 """Create relation interface reshaper.
33
34@@ -91,18 +91,17 @@
35 """
36 self.limit = limit
37
38- def coerce_interface(self, interface_spec, path):
39- """Coerce `interface_spec` from shorthand form.
40+ def coerce(self, value, path):
41+ """Coerce `value` into an expanded interface.
42
43 Helper method to support each of the variants, either the
44 formula does not specify limit and optional, such as foobar in
45 the above example; or the interface spec is just a string,
46 such as the ``server: riak`` example.
47 """
48- if not isinstance(interface_spec, dict):
49+ if not isinstance(value, dict):
50 return {
51- "interface":
52- self.value_converter.coerce(interface_spec, path),
53+ "interface": UTF8_SCHEMA.coerce(value, path),
54 "limit": self.limit,
55 "optional": False}
56 else:
57@@ -110,35 +109,22 @@
58 # defaults, which is different than what KeyDict can
59 # readily support. So just do it here first, then
60 # coerce.
61- if "limit" not in interface_spec:
62- interface_spec["limit"] = self.limit
63- if "optional" not in interface_spec:
64- interface_spec["optional"] = False
65- return INTERFACE_SCHEMA.coerce(interface_spec, path)
66-
67- def coerce(self, value, path):
68- """Converts an interface specification in shorthand form.
69-
70- @value: the value to be coerced
71- @path: used in exception reporting"""
72- if not isinstance(value, dict):
73- raise SchemaError(path, "Invalid relation specification")
74-
75- return dict(
76- (self.value_converter.coerce(K, path),
77- self.coerce_interface(V, path))
78- for K, V in value.iteritems())
79+ if "limit" not in value:
80+ value["limit"] = self.limit
81+ if "optional" not in value:
82+ value["optional"] = False
83+ return INTERFACE_SCHEMA.coerce(value, path)
84
85
86 SCHEMA = KeyDict({
87 "ensemble": Constant("formula"),
88- "name": UnicodeOrString("utf-8"),
89+ "name": UTF8_SCHEMA,
90 "revision": Int(),
91- "summary": UnicodeOrString("utf-8"),
92- "description": UnicodeOrString("utf-8"),
93- "peers": InterfaceExpander(limit=1),
94- "provides": InterfaceExpander(limit=None),
95- "requires": InterfaceExpander(limit=1),
96+ "summary": UTF8_SCHEMA,
97+ "description": UTF8_SCHEMA,
98+ "peers": Dict(UTF8_SCHEMA, InterfaceExpander(limit=1)),
99+ "provides": Dict(UTF8_SCHEMA, InterfaceExpander(limit=None)),
100+ "requires": Dict(UTF8_SCHEMA, InterfaceExpander(limit=1)),
101 }, optional=set(["provides", "requires", "peers"]))
102
103
104
105=== modified file 'ensemble/formula/tests/test_metadata.py'
106--- ensemble/formula/tests/test_metadata.py 2011-08-26 17:28:45 +0000
107+++ ensemble/formula/tests/test_metadata.py 2011-08-26 20:38:25 +0000
108@@ -286,41 +286,39 @@
109
110 # shorthand is properly rewritten
111 self.assertEqual(
112- expander.coerce({"admin": "http"}, ["provides"]),
113- {"admin": {"interface": "http", "limit": None, "optional": False}})
114+ expander.coerce("http", ["provides"]),
115+ {"interface": "http", "limit": None, "optional": False})
116
117 # defaults are properly applied
118 self.assertEqual(
119 expander.coerce(
120- {"admin": {"interface": "http"}}, ["provides"]),
121- {"admin": {"interface": "http", "limit": None, "optional": False}})
122- self.assertEqual(
123- expander.coerce(
124- {"foobar": {"interface": "http", "limit": 2}}, ["provides"]),
125- {"foobar": {"interface": "http", "limit": 2, "optional": False}})
126- self.assertEqual(
127- expander.coerce(
128- {"foobar": {"interface": "http", "optional": True}},
129+ {"interface": "http"}, ["provides"]),
130+ {"interface": "http", "limit": None, "optional": False})
131+ self.assertEqual(
132+ expander.coerce(
133+ {"interface": "http", "limit": 2}, ["provides"]),
134+ {"interface": "http", "limit": 2, "optional": False})
135+ self.assertEqual(
136+ expander.coerce(
137+ {"interface": "http", "optional": True},
138 ["provides"]),
139- {"foobar": {"interface": "http", "limit": None, "optional": True}})
140+ {"interface": "http", "limit": None, "optional": True})
141
142 # invalid data raises SchemaError
143 self.assertRaises(
144 SchemaError,
145- expander.coerce, {"admin": 42}, ["provides"])
146- self.assertRaises(
147- SchemaError,
148- expander.coerce,
149- {"admin": {"interface": "http", "optional": None}},
150- ["provides"])
151- self.assertRaises(
152- SchemaError,
153- expander.coerce,
154- {"admin": {"interface": "http", "limit": "none, really"}},
155- ["provides"])
156+ expander.coerce, 42, ["provides"])
157+ self.assertRaises(
158+ SchemaError,
159+ expander.coerce,
160+ {"interface": "http", "optional": None}, ["provides"])
161+ self.assertRaises(
162+ SchemaError,
163+ expander.coerce,
164+ {"interface": "http", "limit": "none, really"}, ["provides"])
165
166 # can change `limit` default
167 expander = InterfaceExpander(limit=1)
168 self.assertEqual(
169- expander.coerce({"admin": "http"}, ["consumes"]),
170- {"admin": {"interface": "http", "limit": 1, "optional": False}})
171+ expander.coerce("http", ["consumes"]),
172+ {"interface": "http", "limit": 1, "optional": False})

Subscribers

People subscribed via source and target branches

to status/vote changes: