Merge lp:~soren/vmbuilder/0.12.plugin-priority into lp:vmbuilder

Proposed by Soren Hansen
Status: Merged
Merged at revision: not available
Proposed branch: lp:~soren/vmbuilder/0.12.plugin-priority
Merge into: lp:vmbuilder
Diff against target: 54 lines (+25/-0)
3 files modified
VMBuilder/__init__.py (+1/-0)
VMBuilder/plugins/__init__.py (+2/-0)
test/util_tests.py (+22/-0)
To merge this branch: bzr merge lp:~soren/vmbuilder/0.12.plugin-priority
Reviewer Review Type Date Requested Status
Andreas Heck Approve
Review via email: mp+17892@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Andreas Heck (aheck) wrote :

I think it might be a good idea to define a fixed set of named priority constants and make them type-safe.

Revision history for this message
Soren Hansen (soren) wrote :

On Fri, Jan 22, 2010 at 11:28:10PM -0000, Andreas Heck wrote:
> I think it might be a good idea to define a fixed set of named priority constants and make them type-safe.

I'm not sure I see the value in this?

--
Soren Hansen |
Lead virtualisation engineer | Ubuntu Server Team
Canonical Ltd. | http://www.ubuntu.com/

Revision history for this message
Andreas Heck (aheck) wrote :

Maybe I misunderstand the purpose of this change but I think the plugin writer should have an easy way to find out which is the right priority for his plugin. So if the plugin writer must read the code of all other existing plugins and find out that his plugin should be best loaded before plugin A and after plugin B than that might be bad encapsulation because you have to look into all plugins to figure out the right prio for your plugin.

It might be better to have some constants with descriptive names like PRIO_CORE, PRIO_DISTRO, PRIO_HYPERVISOR or whatever makes sense. But I don't know enough of the kind of hooks you have in mind so I'm not sure if this can be applied here.

Revision history for this message
Soren Hansen (soren) wrote :

The Distro class, for instance, will override the default value of the priority attribute, setting it to 5. It's difficult to anticipate what sort of plugins are going to be created, so it's hard to define much of this in advance. At the moment, my primary concern is to make sure there's an (albeit crude) mechanic for adjusting the order in which plugin's hook functions are run. Once we have more data, we can add more rigour to this structure.

Revision history for this message
Andreas Heck (aheck) wrote :

Right, there are some things you just can't know in advance.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'VMBuilder/__init__.py'
2--- VMBuilder/__init__.py 2009-12-08 22:27:46 +0000
3+++ VMBuilder/__init__.py 2010-01-22 15:31:14 +0000
4@@ -52,6 +52,7 @@
5 def register_plugin(cls):
6 """Register a plugin with VMBuilder"""
7 _plugins.append(cls)
8+ _plugins.sort(key=lambda x: x.priority)
9
10 def set_frontend(arg):
11 global frontend
12
13=== modified file 'VMBuilder/plugins/__init__.py'
14--- VMBuilder/plugins/__init__.py 2009-06-10 13:40:41 +0000
15+++ VMBuilder/plugins/__init__.py 2010-01-22 15:31:14 +0000
16@@ -34,6 +34,8 @@
17 return retval
18
19 class Plugin(object):
20+ priority = 10
21+
22 def __init__(self, vm):
23 self.vm = vm
24 self.register_options()
25
26=== modified file 'test/util_tests.py'
27--- test/util_tests.py 2009-09-12 19:01:53 +0000
28+++ test/util_tests.py 2010-01-22 15:31:14 +0000
29@@ -7,3 +7,25 @@
30 def test_run_cmd(self):
31 self.assertTrue("foobarbaztest" in run_cmd("env", env={'foobarbaztest' : 'bar' }))
32
33+ def test_plugin_priority(self):
34+ class Plugin(object):
35+ priority = 10
36+
37+ class PluginA(Plugin):
38+ pass
39+
40+ class PluginB(Plugin):
41+ priority = 5
42+
43+ class PluginC(Plugin):
44+ priority = 15
45+
46+ saved_plugins = VMBuilder._plugins
47+ VMBuilder._plugins = []
48+ VMBuilder.register_plugin(PluginA)
49+ VMBuilder.register_plugin(PluginB)
50+ VMBuilder.register_plugin(PluginC)
51+ self.assertEqual(VMBuilder._plugins[0], PluginB)
52+ self.assertEqual(VMBuilder._plugins[1], PluginA)
53+ self.assertEqual(VMBuilder._plugins[2], PluginC)
54+ VMBuilder._plugins = saved_plugins

Subscribers

People subscribed via source and target branches