The deceptively generic function name load_python_module
hid the fact that this was an API with an application
way too narrow to be in virttest.utils_test. So move it
back to the virttest.libvirt_xml module where it belongs.

Also, change the API name slightly to load_xml_module.
This is not a generic python module loader function,
it has a very specific application.

CC: Wayne Sun <[email protected]>
Signed-off-by: Lucas Meneghel Rodrigues <[email protected]>
---
 virttest/libvirt_xml/base.py                       | 34 ++++++++++++++++++++++
 virttest/libvirt_xml/devices/librarian.py          |  4 +--
 .../libvirt_xml/nwfilter_protocols/librarian.py    |  4 +--
 virttest/utils_test/__init__.py                    | 34 ----------------------
 4 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/virttest/libvirt_xml/base.py b/virttest/libvirt_xml/base.py
index a7c86e7..3e8c7bf 100644
--- a/virttest/libvirt_xml/base.py
+++ b/virttest/libvirt_xml/base.py
@@ -1,4 +1,5 @@
 import logging
+import imp
 
 from autotest.client import utils
 from virttest import propcan, xml_utils, virsh
@@ -197,3 +198,36 @@ class LibvirtXMLBase(propcan.PropCanBase):
             command += ' %s' % schema_name
         cmdresult = utils.run(command, ignore_status=True)
         return cmdresult
+
+
+def load_xml_module(path, name, type_list):
+    """
+    Returns named xml element's handler class
+
+    :param path: the xml module path
+    :param name: the xml module name
+    :param type_list: the supported type list of xml module names
+    :return: the named xml element's handler class
+    """
+    # Module names and tags are always all lower-case
+    name = str(name).lower()
+    errmsg = ("Unknown/unsupported type '%s', supported types %s"
+              % (str(name), type_list))
+    if name not in type_list:
+        raise xcepts.LibvirtXMLError(errmsg)
+    try:
+        filename, pathname, description = imp.find_module(name,
+                                                          [path])
+        mod_obj = imp.load_module(name, filename, pathname, description)
+        # Enforce capitalized class names
+        return getattr(mod_obj, name.capitalize())
+    except TypeError, detail:
+        raise xcepts.LibvirtXMLError(errmsg + ': %s' % str(detail))
+    except ImportError, detail:
+        raise xcepts.LibvirtXMLError("Can't find module %s in %s: %s"
+                                     % (name, pathname, str(detail)))
+    except AttributeError, detail:
+        raise xcepts.LibvirtXMLError("Can't find class %s in %s module in "
+                                     "%s: %s"
+                                     % (name.capitalize(), name, pathname,
+                                        str(detail)))
diff --git a/virttest/libvirt_xml/devices/librarian.py 
b/virttest/libvirt_xml/devices/librarian.py
index 1d30be6..f3e4149 100644
--- a/virttest/libvirt_xml/devices/librarian.py
+++ b/virttest/libvirt_xml/devices/librarian.py
@@ -4,6 +4,7 @@ Module to hide underlying device xml handler class 
implementation
 
 import os
 import virttest.utils_test
+from virttest.libvirt_xml import base
 
 # Avoid accidental names like __init__, librarian, and/or other support modules
 DEVICE_TYPES = ['disk', 'filesystem', 'controller', 'lease',
@@ -21,6 +22,5 @@ def get(name):
     :return: the named device xml element's handler class
     """
     mod_path = os.path.abspath(os.path.dirname(__file__))
-    handler_cl = virttest.utils_test.load_python_module(mod_path, name,
-                                                        DEVICE_TYPES)
+    handler_cl = base.load_xml_module(mod_path, name, DEVICE_TYPES)
     return handler_cl
diff --git a/virttest/libvirt_xml/nwfilter_protocols/librarian.py 
b/virttest/libvirt_xml/nwfilter_protocols/librarian.py
index 2fcb028..4495553 100644
--- a/virttest/libvirt_xml/nwfilter_protocols/librarian.py
+++ b/virttest/libvirt_xml/nwfilter_protocols/librarian.py
@@ -4,6 +4,7 @@ Module to hide underlying filter protocol xml handler class 
implementation
 
 import os
 import virttest.utils_test
+from virttest.libvirt_xml import base
 
 # Avoid accidental names like __init__, librarian, and/or other support modules
 FILTER_TYPES = ['mac', 'vlan', 'stp', 'arp', 'rarp', 'ip', 'ipv6',
@@ -20,6 +21,5 @@ def get(name):
     :return: named filter protocol xml element's handler class
     """
     mod_path = os.path.abspath(os.path.dirname(__file__))
-    handler_cl = virttest.utils_test.load_python_module(mod_path, name,
-                                                        FILTER_TYPES)
+    handler_cl = base.load_xml_module(mod_path, name, FILTER_TYPES)
     return handler_cl
diff --git a/virttest/utils_test/__init__.py b/virttest/utils_test/__init__.py
index 3bba947..cae0e65 100644
--- a/virttest/utils_test/__init__.py
+++ b/virttest/utils_test/__init__.py
@@ -33,7 +33,6 @@ from autotest.client import utils, os_dep
 from autotest.client.shared import error
 from autotest.client.tools import scan_results
 from virttest import aexpect, remote, utils_misc, virt_vm, data_dir, 
utils_net, storage
-from virttest.libvirt_xml import vm_xml, xcepts
 import virttest
 
 import libvirt
@@ -1270,36 +1269,3 @@ def get_image_info(image_file):
     except (KeyError, IndexError, ValueError, error.CmdError), detail:
         raise error.TestError("Fail to get information of %s:\n%s" %
                               (image_file, detail))
-
-
-def load_python_module(path, name, type_list):
-    """
-    Returns named xml element's handler class
-
-    :param path: the xml module path
-    :param name: the xml module name
-    :param type_list: the supported type list of xml module names
-    :return: the named xml element's handler class
-    """
-    # Module names and tags are always all lower-case
-    name = str(name).lower()
-    errmsg = ("Unknown/unsupported type '%s', supported types %s"
-              % (str(name), type_list))
-    if name not in type_list:
-        raise xcepts.LibvirtXMLError(errmsg)
-    try:
-        filename, pathname, description = imp.find_module(name,
-                                                          [path])
-        mod_obj = imp.load_module(name, filename, pathname, description)
-        # Enforce capitalized class names
-        return getattr(mod_obj, name.capitalize())
-    except TypeError, detail:
-        raise xcepts.LibvirtXMLError(errmsg + ': %s' % str(detail))
-    except ImportError, detail:
-        raise xcepts.LibvirtXMLError("Can't find module %s in %s: %s"
-                                     % (name, pathname, str(detail)))
-    except AttributeError, detail:
-        raise xcepts.LibvirtXMLError("Can't find class %s in %s module in "
-                                     "%s: %s"
-                                     % (name.capitalize(), name, pathname,
-                                        str(detail)))
-- 
1.8.5.3

_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel

Reply via email to