On Thu, Jul 23, 2015 at 03:57:29PM +0000, Eren Yagdiran wrote:
Any custom source provider can be added to virt-sandbox-image as a
source
---
virt-sandbox-image/sources/Source.py | 8 ++++++++
virt-sandbox-image/virt-sandbox-image.py | 30 +++++++++++++++++++++++++++++-
2 files changed, 37 insertions(+), 1 deletion(-)
create mode 100644 virt-sandbox-image/sources/Source.py
diff --git a/virt-sandbox-image/sources/Source.py b/virt-sandbox-image/sources/Source.py
new file mode 100644
index 0000000..cfc75d3
--- /dev/null
+++ b/virt-sandbox-image/sources/Source.py
@@ -0,0 +1,8 @@
+#!/usr/bin/python
+
+from abc import ABCMeta, abstractmethod
+
+class Source():
+ __metaclass__ = ABCMeta
+ def __init__(self):
+ pass
diff --git a/virt-sandbox-image/virt-sandbox-image.py
b/virt-sandbox-image/virt-sandbox-image.py
index 324e568..99ed46e 100644
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -1,5 +1,5 @@
#!/usr/bin/python -Es
-#
+# -*- coding: utf-8 -*-
# Authors: Daniel P. Berrange <berrange(a)redhat.com>
# Eren Yagdiran <erenyagdiran(a)gmail.com>
#
@@ -38,6 +38,34 @@ default_template_dir = "/var/lib/libvirt/templates"
debug = True
verbose = True
+sys.dont_write_bytecode = True
+
+
+##Hook mechanism starts##
+import __builtin__
+from sources.Source import Source
+__builtin__.hookHolder = {}
+def add_hook(driverName,clazz):
+ holder = __builtin__.hookHolder
+ if not issubclass(clazz,Source):
+ raise Exception("Loading %s failed. Make sure it is a subclass Of %s"
%(clazz,Source))
+ holder[driverName] = clazz
+
+def init_from_name(name):
+ holder = __builtin__.hookHolder
+ return holder.get(name,None)
+
+__builtin__.add_hook = add_hook
+__builtin__.init_from_name = init_from_name
+from sources import *
+
+def dynamic_source_loader(name):
+ obj = init_from_name(name)
+ if obj == None:
+ raise IOError
+ return obj()
+##Hook mechanism ends
I think this can be a hell of alot simpler if we just define
a fixed convention for module and class naming.
eg something like this:
import importlib
def dynamic_source_loader(name)
modname = "sources." + name
mod = importlib.import_module(modname)
classname = name[0].upper() + name[1:] + "Source"
classimpl = mod.getattr(classname)
return classimpl()
Regards,
Daniel
--
|:
http://berrange.com -o-
http://www.flickr.com/photos/dberrange/ :|
|:
http://libvirt.org -o-
http://virt-manager.org :|
|:
http://autobuild.org -o-
http://search.cpan.org/~danberr/ :|
|:
http://entangle-photo.org -o-
http://live.gnome.org/gtk-vnc :|