]>
Softwares of Agnibho - ddstorm.git/blob - compile.py
4 This module converts text differetial diagnosis files
5 to a simplified modular file format that can be used
9 Copyright (c) 2015 Agnibho Mondal
12 This file is part of DDStorm.
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.
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.
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/>.
31 from operator
import itemgetter
32 from fnmatch
import fnmatch
36 from alias
import Alias
38 logging
.basicConfig(filename
=LOG_FILE
)
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.
48 def __init__(self
, conf
=False):
50 The constructor optionally accepts a configuration.
51 If none is provided it creates a default configuration.
54 conf - A dictionary containing configuration options
62 ''' Compile the text files to DDStorm modules. '''
65 self
.alias
=Alias(self
._conf
)
67 for path
, subdirs
, files
in os
.walk(self
._conf
.get("library_path")):
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")):
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
):
83 for src
in self
._sortPriority
(self
.custom
):
86 def _sortPriority(self
, files
):
89 name
=os
.path
.splitext(os
.path
.basename(addr
))[0].lower().replace("_"," ").replace("-", " ")
90 m
=re
.search(self
.priorityRegex
, name
)
92 ls
.append((name
.replace("."+m
.group(), ""), int(m
.group()), addr
))
94 ls
.append((name
, 100, addr
))
97 return(list(zip(*ls
))[2])
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
)
105 module
=module
.replace("."+m
.group(), "")
106 modFile
=self
._conf
.get("module_path")+module
+".module"
108 with
open(src
, "r") as sf
, open(modFile
, "a") as tf
:
110 line
=line
.strip().split("#")[0]
113 elif(line
.startswith("!")):
115 elif(line
.startswith("#")):
117 elif(line
.startswith("+")):
119 elif(line
.startswith("-")):
121 elif(line
.replace(" ","").replace("-","").replace("_","").replace("'","").isalnum()):
122 print(self
.alias
.get(line
).capitalize(), file=tf
)
125 logging
.warning("Syntax error in file '"+src
+"': "+line
)
128 with
open(src
, "r") as 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
:
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
:
140 text
=text
.replace(self
.alias
.get(line
[1:]).capitalize()+"\n", "")
141 with
open(modFile
, "w") as fn
:
145 c
=Compile().compile()
147 if(__name__
=="__main__"):