]> Softwares of Agnibho - ddstorm.git/blob - compile.py
Added a new function to compile module
[ddstorm.git] / compile.py
1 #! /usr/bin/python3
2
3 '''
4 This module converts text differetial diagnosis files
5 to a simplified modular file format that can be used
6 by the program.
7 '''
8 '''
9 Copyright (c) 2015 Agnibho Mondal
10 All rights reserved
11
12 This file is part of DDStorm.
13
14 DDStorm is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 DDStorm is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with DDStorm. If not, see <http://www.gnu.org/licenses/>.
26 '''
27
28 import os
29 import logging
30 import re
31 from operator import itemgetter
32 from fnmatch import fnmatch
33
34 from conf import Conf
35 from const import *
36 from alias import Alias
37
38 logging.basicConfig(filename=LOG_FILE)
39
40 class Compile:
41 '''
42 This class creates a compiler for the DDStorm
43 that compiles the text files containing list of
44 differential diagnosis to simplified modular
45 data files usable by the program.
46 '''
47
48 def __init__(self, conf=False):
49 '''
50 The constructor optionally accepts a configuration.
51 If none is provided it creates a default configuration.
52
53 Parameters:
54 conf - A dictionary containing configuration options
55 '''
56 if(conf):
57 self._conf=conf
58 else:
59 self._conf=Conf()
60
61 def compile(self):
62 ''' Compile the text files to DDStorm modules. '''
63 self.source=set()
64 self.custom=set()
65 self.alias=Alias(self._conf)
66 self.clean=True
67 for path, subdirs, files in os.walk(self._conf.get("library_path")):
68 for name in files:
69 if(fnmatch(name, "*.txt")):
70 self.source.add(os.path.join(path, name))
71 for path, subdirs, files in os.walk(self._conf.get("custom_path")):
72 for name in files:
73 if(fnmatch(name, "*.txt")):
74 self.custom.add(os.path.join(path, name))
75 if(not os.path.isdir(self._conf.get("module_path"))):
76 os.makedirs(self._conf.get("module_path"))
77 for f in os.listdir(self._conf.get("module_path")):
78 if(fnmatch(f, "*.module")):
79 os.unlink(self._conf.get("module_path")+f)
80 self.priorityRegex=re.compile("(?<=\.)\d+$")
81 for src in self._sortPriority(self.source):
82 self._makeModule(src)
83 for src in self._sortPriority(self.custom):
84 self._makeModule(src)
85
86 def _sortPriority(self, files):
87 ls=[]
88 for addr in files:
89 name=os.path.splitext(os.path.basename(addr))[0].lower().replace("_"," ").replace("-", " ")
90 m=re.search(self.priorityRegex, name)
91 if(m):
92 ls.append((name.replace("."+m.group(), ""), int(m.group()), addr))
93 else:
94 ls.append((name, 100, addr))
95 ls.sort(reverse=True)
96 if(ls):
97 return(list(zip(*ls))[2])
98 else:
99 return ls
100
101 def _makeModule(self, src):
102 module=os.path.splitext(os.path.basename(src))[0].lower().replace("_"," ").replace("-", " ")
103 m=re.search(self.priorityRegex, module)
104 if(m):
105 module=module.replace("."+m.group(), "")
106 modFile=self._conf.get("module_path")+module+".module"
107 modFlag=False
108 with open(src, "r") as sf, open(modFile, "a") as tf:
109 for line in sf:
110 line=line.strip().split("#")[0]
111 if(len(line)==0):
112 pass
113 elif(line.startswith("!")):
114 pass
115 elif(line.startswith("#")):
116 pass
117 elif(line.startswith("+")):
118 modFlag=True
119 elif(line.startswith("-")):
120 modFlag=True
121 elif(line.replace(" ","").replace("-","").replace("_","").replace("'","").isalnum()):
122 print(self.alias.get(line).capitalize(), file=tf)
123 else:
124 self.clean=False
125 logging.warning("Syntax error in file '"+src+"': "+line)
126 if(modFlag):
127 modFlag=False
128 with open(src, "r") as f:
129 for line in f:
130 line=line.strip().split("#")[0]
131 if(line[1:].replace(" ","").replace("-","").replace("_","").replace("'","").isalnum()):
132 if(line.startswith("+")):
133 with open(modFile, "r") as fn:
134 text=fn.read()
135 with open(modFile, "w") as fn:
136 print(self.alias.get(line[1:]).capitalize()+"\n"+text, file=fn)
137 elif(line.startswith("-")):
138 with open(modFile, "r") as fn:
139 text=fn.read()
140 text=text.replace(self.alias.get(line[1:]).capitalize()+"\n", "")
141 with open(modFile, "w") as fn:
142 print(text, file=fn)
143
144 def main():
145 c=Compile().compile()
146
147 if(__name__=="__main__"):
148 main()