forked from petrockblog/ESConfigEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesconfedit.py
executable file
·329 lines (271 loc) · 11.1 KB
/
esconfedit.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python
"""esconfig.py: Sets or removes system information from system configuration \
files of EmulationStation. """
__author__ = "Florian Mueller"
__copyright__ = "Copyright 2014, Florian Mueller"
__credits__ = ["Florian Mueller"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Florian Mueller"
__email__ = "[email protected]"
from lxml import etree
from xml.dom import minidom
import argparse
import os.path
import sys
class Systemlist(object):
REPLENT_TOXML = 0
REPLENT_FROMXML = 1
def __init__(self):
""" Initializes the system as empty list """
super(Systemlist, self).__init__()
self.__systemlist = []
def __iter__(self):
self.__currentIndex = 0
if len(self.__systemlist) > 0:
return self
else:
raise StopIteration
def next(self):
if self.__currentIndex < len(self.__systemlist):
self.__currentIndex += 1
return self.__systemlist[self.__currentIndex - 1]
else:
raise StopIteration
def __elementText(self, system, tag):
temptext = system.findtext(tag)
if temptext is None:
temptext = ""
return temptext
def loadSystems(self, sourcefile, dontstop):
self.__sourcefile = sourcefile
self.__assure_path_exists(os.path.dirname(sourcefile))
if not os.path.isfile(sourcefile):
if not dontstop:
print "[" + str(os.path.basename(__file__)) + \
"] Cannot find input file", sourcefile
sys.exit(1)
else:
emptyfile = open(sourcefile, "w")
emptyfile.writelines(["<?xml version=\"1.0\" ?>\n",
"<systemList>\n", "</systemList>"])
emptyfile.close()
inputfile = open(sourcefile, 'r')
xmlstring = inputfile.read()
inputfile.close()
xmlstring = self.__transformEntities(
xmlstring, Systemlist.REPLENT_TOXML)
xmlparser = etree.XMLParser(encoding='utf-8', recover=True)
self.__root = etree.XML(xmlstring, parser=xmlparser)
for system in self.__root.findall('system'):
self.__systemlist.append(Systementry(self.__elementText(system,'fullname'),
self.__elementText(system,'name'),
self.__elementText(system,'path'),
self.__elementText(system,'extension'),
self.__elementText(system,'command'),
self.__elementText(system,'platform'),
self.__elementText(system,'theme')))
def saveSystems(self, targetfile):
self.__assure_path_exists(os.path.dirname(targetfile))
if os.path.isfile(targetfile):
fileName, fileExtension = os.path.splitext(targetfile)
os.renames(targetfile, str(fileName) + "BAK" +
fileExtension)
newroot = etree.Element("systemList")
for system in self.__systemlist:
system.addToTree(newroot)
prettyxml = self.__prettify(newroot)
prettyxml = self.__transformEntities(
prettyxml, Systemlist.REPLENT_FROMXML)
outputfile = open(targetfile, 'w')
outputfile.write(prettyxml)
outputfile.close()
def existsSystem(self, searchname):
entry = self.__findSystem(searchname)
if entry is None:
return None
else:
return entry
def setSystem(self, systementry):
existingSystem = self.__findSystem(systementry.getName())
if existingSystem is None:
self.__systemlist.append(systementry)
else:
index = self.__systemlist.index(existingSystem)
self.__systemlist[index] = systementry
def removeSystem(self, systemname):
existingSystem = self.__findSystem(systemname)
if existingSystem is not None:
self.__systemlist.remove(existingSystem)
def __findSystem(self, searchname):
for system in self.__systemlist:
if system.getName() == searchname:
return system
return None
def __prettify(self, elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = etree.tostring(elem)
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def __transformEntities(self, inputstring, direction):
if direction == Systemlist.REPLENT_TOXML:
replacements = {'&&': '&&'}
elif direction == Systemlist.REPLENT_FROMXML:
replacements = {
'&&': '&&', '&&': '&&', '"': '"'}
for src, target in replacements.iteritems():
inputstring = inputstring.replace(src, target)
return inputstring
def __assure_path_exists(self, path):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
class Systementry(object):
"""Systementry represents a single entry in the EmulationStation file for \
the system configurations."""
ENABLEDTRUE = "True"
ENABLEDFALSE = "False"
def __init__(self, fullname, name, path, extension, command, platform,
theme):
super(Systementry, self).__init__()
self.__fullname = fullname
self.__name = name
self.__path = path
self.__extension = extension
self.__command = command
self.__platform = platform
self.__theme = theme
def __str__(self):
return str(self.__name)
def __eq__(self, other):
return self.__name == other.getName()
def addToTree(self, root):
newnode = etree.SubElement(root, 'system')
newsubnode = etree.SubElement(newnode, "fullname")
newsubnode.text = self.__fullname
newsubnode = etree.SubElement(newnode, "name")
newsubnode.text = self.__name
newsubnode = etree.SubElement(newnode, "path")
newsubnode.text = self.__path
newsubnode = etree.SubElement(newnode, "extension")
newsubnode.text = self.__extension
newsubnode = etree.SubElement(newnode, "command")
newsubnode.text = self.__command
newsubnode = etree.SubElement(newnode, "platform")
newsubnode.text = self.__platform
newsubnode = etree.SubElement(newnode, "theme")
newsubnode.text = self.__theme
def getFullname(self):
return self.__fullname
def getName(self):
return self.__name
def getPath(self):
return self.__path
def getExtension(self):
return self.__extension
def getCommand(self):
return self.__command
def getPlatform(self):
return self.__platform
def getTheme(self):
return self.__theme
def setFullname(self, fullname):
self.__fullname = fullname
return True
def setName(self, name):
self.__name = name
return True
def setPath(self, path):
self.__path = path
return True
def setExtension(self, extension):
self.__extension = extension
return True
def setCommand(self, command):
self.__command = command
return True
def setPlatform(self, platform):
self.__platform = platform
return True
def setTheme(self, theme):
self.__theme = theme
return True
def checkArguments(args):
result = True
if args.mode == "set":
if args.fullname is None or args.name is None or args.directory is None or args.extension is None or args.command is None or args.platform is None or args.theme is None:
result = False
elif args.mode == "remove":
if args.name is None:
result = False
return result
class Toolparser(object):
def __init__(self):
super(Toolparser, self).__init__()
self.__parser = argparse.ArgumentParser(
description='Adds/sets or removes system information from system \
configuration files of EmulationStation \
(see emulationstation.org).')
self.__parser.add_argument("mode", help="Sets the mode",
choices=["add", "remove"])
self.__parser.add_argument(
"inputfile", help="path and name of input XML file")
self.__parser.add_argument(
"outputfile", help="path and name of output XML file")
self.__parser.add_argument(
"--dontstop", help="Do not stop, if inputfile does not exist.\
Continue with empty systems list then.", action='store_true')
group = self.__parser.add_argument_group(
'optional arguments, system-specific')
group.add_argument("-f", "--fullname", help="full name of the system")
group.add_argument("-n", "--name", help="name of the system")
group.add_argument(
"-d", "--directory", help="path of the ROMs of the system")
group.add_argument("-e", "--extension", help="Extensions of the ROMs")
group.add_argument("-c", "--command",
help="Command to start the emulator with a ROM")
group.add_argument("-p", "--platform",
help="Platform name for EmulationStation")
group.add_argument("-t", "--theme",
help="Theme name for EmulationStation")
def parse_args(self):
return self.__parser.parse_args()
if __name__ == "__main__":
returnvalue = 0
parser = Toolparser()
args = parser.parse_args()
if args.mode == "add":
if checkArguments(args):
systemlist = Systemlist()
systemlist.loadSystems(args.inputfile, args.dontstop)
entry = Systementry(args.fullname,
args.name,
args.directory,
args.extension,
args.command,
args.platform,
args.theme)
systemlist.setSystem(entry)
systemlist.saveSystems(args.outputfile)
print "[" + str(os.path.basename(__file__)) + \
"] Successfully saved to file " + str(args.outputfile)
returnvalue = 0
else:
print "[" + str(os.path.basename(__file__)) + \
"] System arguments are incomplete."
returnvalue = 1
elif args.mode == "remove":
if checkArguments(args):
systemlist = Systemlist()
systemlist.loadSystems(args.inputfile, args.dontstop)
systemlist.removeSystem(args.name)
systemlist.saveSystems(args.outputfile)
print "[" + str(os.path.basename(__file__)) + \
"] Removed system " + str(args.name) + " from " +\
str(args.inputfile)
else:
print "[" + str(os.path.basename(__file__)) + \
"] System name is missing as argument."
returnvalue = 1
sys.exit(returnvalue)