Merge lp:~sylvain-pineau/checkbox/fix-1517312-plainbox into lp:checkbox

Proposed by Sylvain Pineau
Status: Merged
Approved by: Paul Larson
Approved revision: 4106
Merged at revision: 4111
Proposed branch: lp:~sylvain-pineau/checkbox/fix-1517312-plainbox
Merge into: lp:checkbox
Diff against target: 214 lines (+40/-32)
2 files modified
plainbox/docs/manpages/plainbox-template-units.rst (+3/-0)
plainbox/plainbox/impl/unit/template.py (+37/-32)
To merge this branch: bzr merge lp:~sylvain-pineau/checkbox/fix-1517312-plainbox
Reviewer Review Type Date Requested Status
Paul Larson Approve
Sylvain Pineau Needs Resubmitting
Review via email: mp+278085@code.launchpad.net

Description of the change

Small patch to the template unit, providing a free index parameter to be used in template definition to loop over all devices matching the template-filter.

Includes a doc update.

To post a comment you must log in.
Revision history for this message
Paul Larson (pwlars) wrote :

one minor comment

review: Needs Fixing
4106. By Sylvain Pineau

plainbox:unit:template: PEP8 fixes

Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

inline comment fixed + PEP8

review: Needs Resubmitting
Revision history for this message
Paul Larson (pwlars) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plainbox/docs/manpages/plainbox-template-units.rst'
2--- plainbox/docs/manpages/plainbox-template-units.rst 2014-09-16 12:53:41 +0000
3+++ plainbox/docs/manpages/plainbox-template-units.rst 2015-11-20 08:49:14 +0000
4@@ -77,6 +77,9 @@
5 variable named by the ``template_resource`` field. Record data is exposed as
6 attributes of that object.
7
8+The special parameter ``__index__`` can be used to iterate over the devices
9+matching the ``template-filter`` field.
10+
11 Migrating From Local Jobs
12 -------------------------
13
14
15=== modified file 'plainbox/plainbox/impl/unit/template.py'
16--- plainbox/plainbox/impl/unit/template.py 2014-09-08 16:27:47 +0000
17+++ plainbox/plainbox/impl/unit/template.py 2015-11-20 08:49:14 +0000
18@@ -52,9 +52,8 @@
19
20
21 class TemplateUnitValidator(UnitValidator, TemplateUnitValidatorLegacyAPI):
22- """
23- Validator for template unit
24- """
25+
26+ """Validator for template unit."""
27
28 def check(self, unit):
29 for issue in super().check(unit):
30@@ -80,6 +79,7 @@
31
32
33 class TemplateUnit(Unit, TemplateUnitLegacyAPI):
34+
35 """
36 Template that can instantiate zero or more additional units.
37
38@@ -161,12 +161,13 @@
39 field_offset_map)
40
41 def __str__(self):
42+ """String representation of Template unit objects."""
43 return "{} <~ {}".format(self.id, self.resource_id)
44
45 @property
46 def partial_id(self):
47 """
48- Identifier of this job, without the provider name
49+ Identifier of this job, without the provider name.
50
51 This field should not be used anymore, except for display
52 """
53@@ -174,6 +175,7 @@
54
55 @property
56 def id(self):
57+ """Identifier of this template unit."""
58 if self.provider:
59 return "{}::{}".format(self.provider.namespace, self.partial_id)
60 else:
61@@ -181,9 +183,7 @@
62
63 @property
64 def resource_partial_id(self):
65- """
66- name of the referenced resource object
67- """
68+ """name of the referenced resource object."""
69 text = self.template_resource
70 if text is not None and "::" in text:
71 return text.split("::", 1)[1]
72@@ -191,9 +191,7 @@
73
74 @property
75 def resource_namespace(self):
76- """
77- namespace of the referenced resource object
78- """
79+ """namespace of the referenced resource object."""
80 text = self.template_resource
81 if text is not None and "::" in text:
82 return text.split("::", 1)[0]
83@@ -202,9 +200,7 @@
84
85 @property
86 def resource_id(self):
87- """
88- fully qualified identifier of the resource object
89- """
90+ """fully qualified identifier of the resource object."""
91 resource_partial_id = self.resource_partial_id
92 if resource_partial_id is None:
93 return None
94@@ -221,15 +217,13 @@
95
96 @property
97 def template_resource(self):
98- """
99- value of the 'template-resource' field
100- """
101+ """value of the 'template-resource' field."""
102 return self.get_record_value('template-resource')
103
104 @property
105 def template_filter(self):
106 """
107- value of the 'template-filter' field
108+ value of the 'template-filter' field.
109
110 This attribute stores the text of a resource program (optional) that
111 select a subset of available resource objects. If you wish to access
112@@ -241,7 +235,7 @@
113 @property
114 def template_imports(self):
115 """
116- value of the 'template-imports' field
117+ value of the 'template-imports' field.
118
119 This attribute stores the text of a resource import that is specific
120 to the template itself. In other words, it allows the template
121@@ -252,7 +246,7 @@
122 @property
123 def template_unit(self):
124 """
125- value of the 'template-unit' field
126+ value of the 'template-unit' field.
127
128 This attribute stores the type of the unit that this template intends
129 to instantiate. It defaults to 'job' for backwards compatibility and
130@@ -278,7 +272,7 @@
131
132 def get_filter_program(self):
133 """
134- Get filter program compiled from the template-filter field
135+ Get filter program compiled from the template-filter field.
136
137 :returns:
138 ResourceProgram created out of the text of the template-filter
139@@ -292,7 +286,7 @@
140
141 def get_target_unit_cls(self):
142 """
143- Get the Unit subclass that implements the instantiated unit
144+ Get the Unit subclass that implements the instantiated unit.
145
146 :returns:
147 A subclass of Unit the template will try to instantiate. If there
148@@ -310,8 +304,9 @@
149
150 def instantiate_all(self, resource_list):
151 """
152- Instantiate a list of job definitions by creating one from each
153- non-filtered out resource records.
154+ Instantiate a list of job definitions.
155+
156+ By creating one from each non-filtered out resource records.
157
158 :param resource_list:
159 A list of resource objects with the correct name
160@@ -320,18 +315,26 @@
161 A list of new Unit (or subclass) objects.
162 """
163 unit_cls = self.get_target_unit_cls()
164- return [
165- self.instantiate_one(resource, unit_cls_hint=unit_cls)
166- for resource in resource_list
167- if self.should_instantiate(resource)
168- ]
169+ resources = []
170+ index = 0
171+ for resource in resource_list:
172+ if self.should_instantiate(resource):
173+ index += 1
174+ resources.append(self.instantiate_one(resource,
175+ unit_cls_hint=unit_cls,
176+ index=index))
177+ return resources
178
179- def instantiate_one(self, resource, unit_cls_hint=None):
180+ def instantiate_one(self, resource, unit_cls_hint=None, index=0):
181 """
182 Instantiate a single job out of a resource and this template.
183
184 :param resource:
185 A Resource object to provide template data
186+ :param unit_cls_hint:
187+ A unit class to instantiate
188+ :param index:
189+ An integer parameter representing the current loop index
190 :returns:
191 A new JobDefinition created out of the template and resource data.
192 :raises AttributeError:
193@@ -363,6 +366,8 @@
194 # XXX: extract raw dictionary from the resource object, there is no
195 # normal API for that due to the way resource objects work.
196 parameters = object.__getattribute__(resource, '_data')
197+ # Add the special __index__ to the resource namespace variables
198+ parameters['__index__'] = index
199 # Instantiate the class using the instantiation API
200 return unit_cls.instantiate_template(
201 data, raw_data, self.origin, self.provider, parameters,
202@@ -401,9 +406,9 @@
203 name = N_('template')
204
205 class fields(SymbolDef):
206- """
207- Symbols for each field that a TemplateUnit can have
208- """
209+
210+ """Symbols for each field that a TemplateUnit can have."""
211+
212 template_unit = 'template-unit'
213 template_resource = 'template-resource'
214 template_filter = 'template-filter'

Subscribers

People subscribed via source and target branches