Source code for bellatrix.lib.util
'''
general utility functions
'''
import logging
import os
import time
import xml.dom.minidom
[docs]def getNodeValue(dom, nodename):
"""
get 'myname' from a xml structure like this one when you call this method with 'name':
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<name>myname</name>
</root>
"""
return dom.getElementsByTagName(nodename).item(0).firstChild.nodeValue
[docs]def getDomFromXmlFile(xml_file):
"""
parse an xml file and return its dom
"""
return xml.dom.minidom.parse(xml_file)
[docs]def getValue(xml_file, value):
"""
return the value from a xml tag
this function will get 'myname' from a xml structure like this one when you call this method with 'name':
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<name>myname</name>
</root>
"""
return getNodeValue(getDomFromXmlFile(xml_file), value)
[docs]def getConfigDir(app_name):
config_dir = None
return config_dir
[docs]def getConfigFileContent(file_name, app_name):
content = None
return content
[docs]def getHome():
return os.path.expanduser('~') or os.getenv('USERPROFILE') or os.getenv('HOME')
[docs]def getCurDir():
return os.path.abspath(os.getcwd())
[docs]def getStringsFromFile(list_file):
""""Return list from file ignoring blanks and comments"""
l = []
with open(list_file) as f:
for line in f:
line = line.strip()
if len(line) > 0 and not line.startswith("#"):
l.append(line)
return l
[docs]def writeFile(file_name, content):
with open(file_name, 'w') as out:
out.write(content)
[docs]def importModule(module):
dir_name = os.path.dirname(os.path.abspath(module))
if len(dir_name) > 0 and os.path.sep in dir_name:
#we change a normal path to a python module
module = module.replace(os.path.sep, ".")
return __import__(os.path.basename(module))