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