forked from mriscoc/Special_Configurations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateConfigs.py
206 lines (174 loc) · 5.72 KB
/
CreateConfigs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
# ------------------------------------------------------------------------------
# Configurations generator script for the Professional Firmware
# Author: Miguel A. Risco Castillo
# URL: https://github.com/mriscoc/Marlin_Configurations
# version: 5.1
# date: 2022/07/28
# ------------------------------------------------------------------------------
import re
import os
import json
verbose = False
error = False
loglines = ""
SourceDir = 'Original Configs/'
def log(*text):
global loglines
txt = ' '.join(text)
loglines += txt + "\n"
print(txt)
class Customize:
op = ''
searchfor = ''
newline = ''
mask = ''
value = ''
comment = ''
def Do(self):
return getattr(self, self.op, self.Invalid)()
def InsertAfter(self):
global error
match = re.search(r''+self.searchfor+'(.*)', lines)
if match :
if verbose: log('>>>> found ',self.searchfor)
return lines.replace(match[0], match[0]+'\n'+self.newline)
else :
log('>>>> Not found '+self.searchfor)
error = True
return lines
def Replace(self) :
global lines
global error
lines, n = re.subn(self.searchfor, self.value, lines)
if n :
if verbose: log('>>>> found',n,self.searchfor)
else:
log('>>>> Not found '+self.searchfor)
error = True
return lines
def Custom(self) :
global lines
global error
self.mask = '('+self.mask+')'
if self.comment : self.comment = ' // '+self.comment
lines, n = re.subn(r'(\n *)(//)?( *)(?P<searchfor>#define +'+self.searchfor+r'\b *)'+self.mask+r'( *.*)', r'\1\3\g<searchfor>'+self.value+r'\6'+self.comment, lines)
if n :
if verbose: log('>>>> found',n,self.searchfor)
else:
log('>>>> Not found '+self.searchfor+' mask:'+self.mask)
error = True
return lines
def CustomVal(self) :
self.mask='[-,0-9,.,a-z,A-Z,_]+'
return self.Custom()
def Enable(self) :
self.mask = ''
self.value = ''
return self.Custom();
def Disable(self) :
global lines
global error
if self.comment : self.comment = ' // '+self.comment
lines, n = re.subn(r'(\n *)(//)?( *)(?P<searchfor>#define +'+self.searchfor+r'\b *.*)', r'\1//\3\g<searchfor>'+self.comment, lines)
if n :
if verbose: log('>>>> found',n,self.searchfor)
else:
log('>>>> Not found '+self.searchfor)
error = True
return lines
def Invalid(self) :
global error
log('>>>> Invalid operation:',self.op)
error = True
return lines
def ProcessLines(jsonfile, config):
global lines
global error
C = Customize()
try:
j = open(jsonfile, 'r')
data = json.load(j)
if not data.get(config) :
if verbose: log('>>>>',jsonfile,'section',config,'not in use')
return lines
for l in data[config] :
C.op = l.get('op')
if not C.op : continue
C.searchfor = l.get('searchfor')
C.newline = l.get('newline')
C.mask = l.get('mask')
C.value = l.get('value')
C.comment = l.get('comment')
if not C.comment : C.comment = ''
lines = C.Do()
if error : break
except Exception as e:
log(str(e))
error = True
j.close()
if error: log(">>>> While processing file:",jsonfile)
return lines
def CustomizeFile(Machine_Name, SourceDir, TargetDir, Mode, config) :
global lines
global error
Source = SourceDir+config
Target = TargetDir+config
if os.path.isfile(Source) :
log('-Process', Target)
os.makedirs(TargetDir, exist_ok=True)
f = open(Source, 'r', encoding="utf8")
lines = f.read()
if verbose: log(">>>> using: _printers/Common.json")
lines = ProcessLines("_printers/Common.json", config)
PathConfig = ['_printers/','_boards/','_leveling/','_thermistor/','_features/']
for val in Mode:
for path in PathConfig:
JsonFile = path + val + '.json'
if os.path.isfile(JsonFile) :
if verbose: log(">>>> using:", JsonFile)
lines = ProcessLines(JsonFile, config)
break
if not os.path.isfile(JsonFile):
log(">>>>",val+".json","was not found")
error = True
if error: break
if not error:
if Machine_Name :
lines = lines.replace('//#define CUSTOM_MACHINE_NAME "3D Printer"','#define CUSTOM_MACHINE_NAME "'+Machine_Name+'"')
lines = lines.replace('//#define DETAILED_BUILD_VERSION SHORT_BUILD_VERSION','#define DETAILED_BUILD_VERSION SHORT_BUILD_VERSION " '+Machine_Name+', based on bugfix-2.1.x"')
else :
lines = lines.replace('//#define CUSTOM_MACHINE_NAME "3D Printer"','#define CUSTOM_MACHINE_NAME "'+' '.join(Mode)+'"')
lines = lines.replace('//#define DETAILED_BUILD_VERSION SHORT_BUILD_VERSION','#define DETAILED_BUILD_VERSION SHORT_BUILD_VERSION " '+' '.join(Mode)+', based on bugfix-2.1.x"')
with open(Target, "w", encoding="utf8") as of:
of.write(lines)
of.close()
f.close()
if verbose: log('-----')
else :
log('Source file:', Source,'not found')
error = True
def Generate(Machine_Name, Mode) :
global error
error = False
global loglines
loglines = ""
print()
log('Configurations generator script for the Professional Firmware')
log('Author: Miguel A. Risco Castillo (c) 2022\n')
if Machine_Name:
TargetDir = Machine_Name+'/'
else:
TargetDir = '-'.join(Mode)+'/'
SourceList = ['Configuration.h','Configuration_adv.h','Version.h','platformio.ini']
for SourceFile in SourceList:
CustomizeFile(Machine_Name, SourceDir, TargetDir, Mode, SourceFile)
if error: break
if error:
log("\nAn error was found while processing your request")
else:
log("\nConfiguration files correctly generated")
with open(TargetDir+"/log.txt", "w", encoding="utf8") as logf:
logf.write(loglines)
logf.close()
return error