X-Git-Url: https://code.agnibho.com/repo?p=ddstorm.git;a=blobdiff_plain;f=compile.py;h=80bb4d1456f07621b9e71890592f309c14b69768;hp=acb5ec7b39a61bf978b81888350c9f6a43cbdf1b;hb=HEAD;hpb=ad3c926d180f5d843b77db44c2b2449cdffeec34 diff --git a/compile.py b/compile.py index acb5ec7..80bb4d1 100644 --- a/compile.py +++ b/compile.py @@ -57,41 +57,57 @@ class Compile: self._conf=conf else: self._conf=Conf() + self.clean=True def compile(self): ''' Compile the text files to DDStorm modules. ''' self.source=set() self.custom=set() self.alias=Alias(self._conf) - self.clean=True + + # Loop over library files and add *.txt files to source for path, subdirs, files in os.walk(self._conf.get("library_path")): for name in files: if(fnmatch(name, "*.txt")): self.source.add(os.path.join(path, name)) + + # Loop over custom files and add *.txt files to custom for path, subdirs, files in os.walk(self._conf.get("custom_path")): for name in files: if(fnmatch(name, "*.txt")): self.custom.add(os.path.join(path, name)) + + # Create module directory if not already present and delete all module files if(not os.path.isdir(self._conf.get("module_path"))): os.makedirs(self._conf.get("module_path")) for f in os.listdir(self._conf.get("module_path")): if(fnmatch(f, "*.module")): os.unlink(self._conf.get("module_path")+f) + + # Create a regex for calculating priority from filename self.priorityRegex=re.compile("(?<=\.)\d+$") + + # First sort files by priority then compile them to module for src in self._sortPriority(self.source): self._makeModule(src) for src in self._sortPriority(self.custom): self._makeModule(src) def _sortPriority(self, files): + ''' Sort data files based on their priority settings. ''' ls=[] + # Loop over the files for addr in files: + # Format the file name name=os.path.splitext(os.path.basename(addr))[0].lower().replace("_"," ").replace("-", " ") + # Search for priority tag on file name m=re.search(self.priorityRegex, name) + # Add to ls as (symptom name, priority number, file name) with default priority of 100 if(m): ls.append((name.replace("."+m.group(), ""), int(m.group()), addr)) else: ls.append((name, 100, addr)) + # Sort the file list, first by the symptom name, then by the priority number ls.sort(reverse=True) if(ls): return(list(zip(*ls))[2]) @@ -99,13 +115,19 @@ class Compile: return ls def _makeModule(self, src): + ''' Create application usable modules from data files. ''' + # Format the file name module=os.path.splitext(os.path.basename(src))[0].lower().replace("_"," ").replace("-", " ") + # Remove the priority tag from file name m=re.search(self.priorityRegex, module) if(m): module=module.replace("."+m.group(), "") + # Create the module file name modFile=self._conf.get("module_path")+module+".module" modFlag=False + # Loop over both files, the source data file and the target module file with open(src, "r") as sf, open(modFile, "a") as tf: + # Ignore lines starting with ! or #, + and - has special meaning, write other lines to module. Log the errors. for line in sf: line=line.strip().split("#")[0] if(len(line)==0): @@ -123,17 +145,20 @@ class Compile: else: self.clean=False logging.warning("Syntax error in file '"+src+"': "+line) + # Deal with special lines if(modFlag): modFlag=False with open(src, "r") as f: for line in f: line=line.strip().split("#")[0] if(line[1:].replace(" ","").replace("-","").replace("_","").replace("'","").isalnum()): + # If line starts with + add it to the module file if(line.startswith("+")): with open(modFile, "r") as fn: text=fn.read() with open(modFile, "w") as fn: print(self.alias.get(line[1:]).capitalize()+"\n"+text, file=fn) + # If line starts with - remove corresponding item from the module file elif(line.startswith("-")): with open(modFile, "r") as fn: text=fn.read() @@ -141,7 +166,12 @@ class Compile: with open(modFile, "w") as fn: print(text, file=fn) + def is_clean(self): + '''Report if compilation ended successfully''' + return self.clean + def main(): + ''' Compile the data files into formatted module files ''' c=Compile().compile() if(__name__=="__main__"):