Package amplee :: Package loader :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module amplee.loader.utils

 1  # -*- coding: utf-8 -*- 
 2  __docformat__ = 'epytext' 
 3   
 4  import os.path 
 5  import imp 
 6  from xml.dom import XMLNS_NAMESPACE, XML_NAMESPACE 
 7   
 8  __all__ = ['get_value', 'get_attr_value', 'get_xml_attrs', 
 9             'is_present', 'load_callable'] 
10   
11 -def get_value(node, name, default=None):
12 if name in node.xml_child_elements: 13 return unicode(node.xml_child_elements[name]) 14 15 return default
16
17 -def get_attr_value(node, name, default=None):
18 if node.is_attribute(name): 19 return unicode(getattr(node, name)) 20 21 return default
22
23 -def get_xml_attrs(node):
24 xml_attrs = {} 25 for (ns, (prefix, local_name)) in node.attributes: 26 if ns == XML_NAMESPACE: 27 attr = node.attributes[(ns, (prefix, local_name))] 28 xml_attrs[local_name] = attr.value 29 30 return xml_attrs
31
32 -def is_present(node, name):
33 return name in node.xml_child_elements
34
35 -def load_callable(modulepath, cb, base_path=None):
36 directory, name = os.path.split(modulepath) 37 if base_path: 38 directory = os.path.join(base_path, directory) 39 file, filename, description = imp.find_module(name, [directory]) 40 mod = imp.load_module(name, file, filename, description) 41 if hasattr(mod, cb): 42 return getattr(mod, cb) 43 else: 44 raise ImportError("cannot import name %s from %s" % (cb, name))
45