]>
Softwares of Agnibho - ddstorm.git/blob - compile.py
5 # Copyright (c) 2015 Agnibho Mondal
8 # This file is part of DDStorm.
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.
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.
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/>.
23 import os
, logging
, re
24 from operator
import itemgetter
25 from fnmatch
import fnmatch
28 from alias
import Alias
29 logging
.basicConfig(filename
=LOG_FILE
)
32 def __init__(self
, conf
=False):
39 self
.alias
=Alias(conf
)
41 for path
, subdirs
, files
in os
.walk(self
.conf
.get("library_path")):
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")):
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
):
57 for src
in self
._sortPriority
(self
.custom
):
60 def _sortPriority(self
, files
):
63 name
=os
.path
.splitext(os
.path
.basename(addr
))[0].lower().replace("_"," ").replace("-", " ")
64 m
=re
.search(self
.priorityRegex
, name
)
66 ls
.append((name
.replace("."+m
.group(), ""), int(m
.group()), addr
))
68 ls
.append((name
, 100, addr
))
71 return(list(zip(*ls
))[2])
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
)
79 module
=module
.replace("."+m
.group(), "")
80 modFile
=self
.conf
.get("module_path")+module
+".module"
82 with
open(src
, "r") as sf
, open(modFile
, "a") as tf
:
84 line
=line
.strip().split("#")[0]
87 elif(line
.startswith("!")):
89 elif(line
.startswith("#")):
91 elif(line
.startswith("+")):
93 elif(line
.startswith("-")):
95 elif(line
.replace(" ","").replace("-","").replace("_","").replace("'","").isalnum()):
96 print(self
.alias
.get(line
).capitalize(), file=tf
)
99 logging
.warning("Syntax error in file '"+src
+"': "+line
)
102 with
open(src
, "r") as 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
:
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
:
114 text
=text
.replace(self
.alias
.get(line
[1:]).capitalize()+"\n", "")
115 with
open(modFile
, "w") as fn
:
121 if(__name__
=="__main__"):