-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdat.py
52 lines (44 loc) · 2.12 KB
/
dat.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
import collections
import os
import xml.etree.ElementTree as etree
Dat = collections.namedtuple('Dat', 'name description manufacturer year cloneof isbios node')
def get(i, e):
ll = i.find(e)
return ll.text if ll is not None else None
def parseDat(file, logger):
dats = dict()
# Old format
parser = etree.XMLParser(encoding="utf-8")
games = etree.parse(file, parser=parser).findall(".//game")
if len(games) > 0: # remove systems with no games
for g in games:
isbios = True if 'isbios' in g.attrib and g.attrib['isbios'] == 'yes' else False
cloneof = g.attrib['cloneof'] if 'cloneof' in g.attrib else None
datEntry = Dat(g.attrib['name'], get(g, 'description'), get(g, 'manufacturer'), get(g, 'year'),
cloneof, isbios, g)
dats[g.attrib['name']] = datEntry
else:
# New format
newparser = etree.XMLParser(encoding="utf-8")
machines = etree.parse(file, parser=newparser).findall(".//machine")
if len(machines) > 0: # remove systems with no machines
for g in machines:
isbios = True if 'isbios' in g.attrib and g.attrib['isbios'] == 'yes' else False
cloneof = g.attrib['cloneof'] if 'cloneof' in g.attrib else None
datEntry = Dat(g.attrib['name'], get(g, 'description'), get(g, 'manufacturer'), get(g, 'year'),
cloneof, isbios, g)
dats[g.attrib['name']] = datEntry
logger.log('Dat ' + str(file) + ' : ' + str(len(dats)) + ' entries')
return dats
def parseDats(scriptDir, dataDir, setDats, usingSystems, logger):
dats = dict()
for setKey in usingSystems:
header = etree.parse(os.path.join(scriptDir, dataDir, setDats[setKey]),
etree.XMLParser(encoding="utf-8")).findall(".//header")
if os.path.exists(setDats[setKey]):
datPath = setDats[setKey]
else:
datPath = os.path.join(scriptDir, dataDir, setDats[setKey])
dats[setKey] = parseDat(datPath, logger)
dats[setKey + "Header"] = header[0]
return dats