Skip to content
Snippets Groups Projects
Commit 860f436e authored by Nicola Vigano's avatar Nicola Vigano
Browse files

Python: reformatted to be more PEP compliant

parent 0bec5d30
No related branches found
No related tags found
No related merge requests found
...@@ -27,11 +27,8 @@ class DCTLauncher(object): ...@@ -27,11 +27,8 @@ class DCTLauncher(object):
cmd = args[1] cmd = args[1]
if cmd in ("-h", "--help"): if cmd in ("-h", "--help"):
self.command = "help" self.command = "help"
elif cmd in ("matlab", "update", "compile_mex", "compile_matlab", \
"update_conf", "make", "publish"):
self.command = cmd
else: else:
raise ValueError("Command '%s' not recognized" % cmd) self.command = cmd
self.args = args[2:] self.args = args[2:]
self.initialized = True self.initialized = True
...@@ -56,7 +53,7 @@ class DCTLauncher(object): ...@@ -56,7 +53,7 @@ class DCTLauncher(object):
elif self.command == "publish": elif self.command == "publish":
self._launchPublish() self._launchPublish()
else: else:
raise ValueError("Not recognized command: %s" % self.command) raise ValueError("Command not recognized: '%s'" % self.command)
def _launchMatlab(self): def _launchMatlab(self):
from zUtil_Python.dct_compile_matlab_functions import FunctionsBuilder from zUtil_Python.dct_compile_matlab_functions import FunctionsBuilder
......
...@@ -7,6 +7,7 @@ Created on May 2, 2013 ...@@ -7,6 +7,7 @@ Created on May 2, 2013
import re import re
import fnmatch import fnmatch
import os import os
import sys
import dct_io_xml import dct_io_xml
...@@ -18,20 +19,20 @@ class DCTBatchConf(dct_io_xml.DCTXMLBase): ...@@ -18,20 +19,20 @@ class DCTBatchConf(dct_io_xml.DCTXMLBase):
etree = dct_io_xml.ET.ElementTree(tree) etree = dct_io_xml.ET.ElementTree(tree)
etree.write(xml_file, pretty_print = True) etree.write(xml_file, pretty_print = True)
conf = DCTBatchConf(xml_file, read_only = False) conf = DCTBatchConf(xml_file, read_only = False)
conf.fixStructure() conf.fix_structure()
return conf return conf
def __init__(self, xml_file = os.path.join("zUtil_Conf", "batch-dev-conf.xml"), read_only = True): def __init__(self, xml_file = os.path.join("zUtil_Conf", "batch-dev-conf.xml"), read_only = True):
dct_io_xml.DCTXMLBase.__init__(self, xml_file) dct_io_xml.DCTXMLBase.__init__(self, xml_file)
self.read_only = read_only self.read_only = read_only
def fixStructure(self): def fix_structure(self):
if self.tree.find("options") is None: if self.tree.find("options") is None:
dct_io_xml.ET.SubElement(self.tree.getroot(), "options") dct_io_xml.ET.SubElement(self.tree.getroot(), "options")
if self.tree.find("functions") is None: if self.tree.find("functions") is None:
dct_io_xml.ET.SubElement(self.tree.getroot(), "functions") dct_io_xml.ET.SubElement(self.tree.getroot(), "functions")
def getPath(self, xml_path, base_dir, base_node = None): def get_path(self, xml_path, base_dir, base_node = None):
if base_node is None: if base_node is None:
node = self.tree.find(xml_path) node = self.tree.find(xml_path)
else: else:
...@@ -44,10 +45,10 @@ class DCTBatchConf(dct_io_xml.DCTXMLBase): ...@@ -44,10 +45,10 @@ class DCTBatchConf(dct_io_xml.DCTXMLBase):
else: else:
raise ValueError("No path defined for '%s" % xml_path) raise ValueError("No path defined for '%s" % xml_path)
def getBinDir(self, base_dir): def get_bin_dir(self, base_dir):
return self.getPath("options/binaries/path", base_dir) return self.get_path("options/binaries/path", base_dir)
def addFunction(self, name, path, is_absolute = False): def add_function(self, name, path, is_absolute = False):
if self.read_only is True: if self.read_only is True:
raise ValueError("File %s is open in read only mode" % self.xmlFile) raise ValueError("File %s is open in read only mode" % self.xmlFile)
...@@ -56,30 +57,32 @@ class DCTBatchConf(dct_io_xml.DCTXMLBase): ...@@ -56,30 +57,32 @@ class DCTBatchConf(dct_io_xml.DCTXMLBase):
funcs = self.tree.find("functions") funcs = self.tree.find("functions")
func = dct_io_xml.ET.SubElement(funcs, "function", {"name" : name}) func = dct_io_xml.ET.SubElement(funcs, "function", {"name" : name})
pathNode = func.find("path") path_node = func.find("path")
if pathNode is None: if path_node is None:
pathNode = dct_io_xml.ET.SubElement(func, "path") path_node = dct_io_xml.ET.SubElement(func, "path")
pathNode.text = path path_node.text = path
if is_absolute is True: if is_absolute is True:
pathNode.attrib["absolute"] = "true" path_node.attrib["absolute"] = "true"
else: else:
pathNode.attrib["absolute"] = "false" path_node.attrib["absolute"] = "false"
self.save() self.save()
def getFunctions(self, base_dir): def get_functions(self, base_dir):
functions = {} functions = {}
nodes = self.tree.findall("functions/function") nodes = self.tree.findall("functions/function")
for node in nodes: for node in nodes:
functions[node.attrib.get("name")] = self.getPath("path", base_dir, base_node = node) functions[node.attrib.get("name")] = self.get_path("path", base_dir, base_node = node)
return functions return functions
class DCTBatch(object): class DCTBatch(object):
def __init__(self, dct_dir = "", dev_mode = False): def __init__(self, dct_dir = "", dev_mode = False):
if dct_dir is "": if dct_dir is "":
dct_dir = os.getcwd() dct_dir = os.path.dirname(sys.argv[0])
if os.path.basename(dct_dir) == "zUtil_Python":
dct_dir = os.path.basename(dct_dir)
self.dct_dir = dct_dir self.dct_dir = dct_dir
devel_conf_path = os.path.join(self.dct_dir, "zUtil_Conf", "batch-dev-conf.xml"); devel_conf_path = os.path.join(self.dct_dir, "zUtil_Conf", "batch-dev-conf.xml");
...@@ -93,41 +96,41 @@ class DCTBatch(object): ...@@ -93,41 +96,41 @@ class DCTBatch(object):
self.user_conf = DCTBatchConf(xml_file = user_conf_path, read_only = False) self.user_conf = DCTBatchConf(xml_file = user_conf_path, read_only = False)
# We assume system configuration is always consistent, so we only # We assume system configuration is always consistent, so we only
# check user configuration # check user configuration
self.user_conf.fixStructure() self.user_conf.fix_structure()
except: except:
self.user_conf = DCTBatchConf.getNewFile(user_conf_path) self.user_conf = DCTBatchConf.getNewFile(user_conf_path)
def autoDetectFunctions(self, user = True): def auto_detect_functions(self, user = True):
simpleMatch = 'OAR_make' simple_match = 'OAR_make'
searchPattern = re.compile('[^%]*\s*' + simpleMatch + "\s*\(\s*'.*'") search_pattern = re.compile('[^%]*\s*' + simple_match + "\s*\(\s*'.*'")
filesList = [] files_list = []
for root, dir_name, file_names in os.walk(self.dct_dir): for root, dir_name, file_names in os.walk(self.dct_dir):
for file_name in file_names: for file_name in file_names:
if fnmatch.fnmatch(file_name, '*.m'): if fnmatch.fnmatch(file_name, '*.m'):
filesList.append((root, file_name)) files_list.append((root, file_name))
# First creating list of file matching methods name # First creating list of file matching methods name
mFiles = {} m_files = {}
for dir_path, file_name in filesList: for dir_path, file_name in files_list:
filepath = os.path.join(dir_path, file_name) filepath = os.path.join(dir_path, file_name)
mFiles[file_name[:-2]] = filepath m_files[file_name[:-2]] = filepath
# Then looking for complex regexp inside matching files # Then looking for complex regexp inside matching files
for dir_path, file_name in mFiles.iteritems(): for dir_path, file_name in m_files.iteritems():
filepath = os.path.join(dir_path, file_name) filepath = os.path.join(dir_path, file_name)
fid = open(filepath, 'r') fid = open(filepath, 'r')
for line in fid: for line in fid:
if simpleMatch in line: if simple_match in line:
matched = searchPattern.search(line) matched = search_pattern.search(line)
if matched: if matched:
function_name = matched.group().split("'")[1] function_name = matched.group().split("'")[1]
self.addFunction(mFiles[function_name], user) self.add_function(m_files[function_name], user)
fid.close() fid.close()
def addFunction(self, path, user = True): def add_function(self, path, user = True):
name = os.path.basename(path) name = os.path.basename(path)
name = name[0:-2] name = name[0:-2]
...@@ -137,20 +140,20 @@ class DCTBatch(object): ...@@ -137,20 +140,20 @@ class DCTBatch(object):
rel_path = abs_path[len(abs_dct_path)+1:] rel_path = abs_path[len(abs_dct_path)+1:]
if user is True: if user is True:
self.user_conf.addFunction(name, rel_path) self.user_conf.add_function(name, rel_path)
else: else:
self.dev_conf.addFunction(name, rel_path) self.dev_conf.add_function(name, rel_path)
def getBinDir(self): def get_bin_dir(self):
try: try:
bin_path = self.user_conf.getBinDir(self.dct_dir) bin_path = self.user_conf.get_bin_dir(self.dct_dir)
except ValueError: except ValueError:
bin_path = self.dev_conf.getBinDir(self.dct_dir) bin_path = self.dev_conf.get_bin_dir(self.dct_dir)
return bin_path return bin_path
def getFunctions(self): def get_functions(self):
functions = self.dev_conf.getFunctions(self.dct_dir) functions = self.dev_conf.get_functions(self.dct_dir)
functions.update(self.user_conf.getFunctions(self.dct_dir)) functions.update(self.user_conf.get_functions(self.dct_dir))
return functions return functions
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -118,12 +118,12 @@ class FunctionsBuilder(object): ...@@ -118,12 +118,12 @@ class FunctionsBuilder(object):
batch = dct_batch.DCTBatch(self.dct_dir) batch = dct_batch.DCTBatch(self.dct_dir)
if self.scan_sources is True: if self.scan_sources is True:
self.out.printSubJob("Finding functions that may be compiled, from: '%s'" % self.dct_dir) self.out.printSubJob("Finding functions that may be compiled, from: '%s'" % self.dct_dir)
batch.autoDetectFunctions() batch.auto_detect_functions()
self.out.printSubJob("Functions available for compilation:") self.out.printSubJob("Functions available for compilation:")
funcs = batch.getFunctions() funcs = batch.get_functions()
for function in funcs: for function in funcs:
self.out.printSubSubJob(function, funcs[function]) self.out.printSubSubJob(function, funcs[function])
bin_dir = batch.getBinDir() bin_dir = batch.get_bin_dir()
self.out.printSubJob("Adding information about functions..") self.out.printSubJob("Adding information about functions..")
for function in funcs: for function in funcs:
outpath = os.path.join(bin_dir, function) outpath = os.path.join(bin_dir, function)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment