-
Notifications
You must be signed in to change notification settings - Fork 0
/
romset.py
334 lines (319 loc) · 15.7 KB
/
romset.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
330
331
332
333
334
from typing import IO, Optional, Literal, Any, Union
from logging import Logger
from lxml import etree
import sys
thisLogger: Logger | None
class Romset():
"""
This class object is a simple timer implementation...
"""
def __init__(self, descriptor: IO[str], logger: Optional['Logger']
= None) -> None:
"""
Constructor of the class.
Arguments:
- seconds (IO[str]): Romset sidecar file descriptor.
"""
global thisLogger
thisLogger = logger
self.descriptor = descriptor
if not self.__extractData__():
_tryLogger_(log='Cannot extract data, syntax error parsing xml file',
level='critical')
sys.exit(0)
def __getDocInfo__(self, data: etree.DocInfo) -> bool:
"""
Extract DOCTYPE data from the XML descriptor using lxml
and store it in a dictionary named docInfo
"""
docInfo: dict = {}
# for prop_name in dir(data):
for prop_name in data.__dir__():
if not prop_name.startswith("__"):
prop_value = getattr(data, prop_name)
docInfo.update({prop_name: prop_value})
if len(docInfo) > 0:
self.docInfo = docInfo
_tryLogger_(log='Doc info as been retrieved', level='debug')
return True
return False
def __getHeader__(self, data: etree._Element | None) -> bool:
"""
Extract header data from the XML descriptor using lxml
and store it in a dictionary named header
"""
if data is not None:
if len(data) > 0:
header_data: dict = {}
for i in data:
header_data.update({i.tag: {}})
if i.text is not None:
header_data[i.tag]['text'] = i.text
if len(i.attrib) != 0:
header_data[i.tag]['attributes'] = i.attrib
if header_data:
self.header = header_data
_tryLogger_(log='Header as been retrieved', level='debug')
return True
return False
def __extractElements__(self, elements) -> bool:
"""
Extract games and roms data from the XML descriptor using lxml
keeping it in etree._Elements
"""
bioses: list[etree._Element] = []
parents: list[etree._Element] = []
clones: list[etree._Element] = []
try:
if isinstance(elements, list):
if len(elements) > 0:
for element in elements:
if isinstance(element, etree._Element):
if element.get(key='isbios') == 'yes':
bioses.append(element)
else:
if 'id' in self.header.keys():
# Working with No-Intro standard dat
if element.get(key='cloneofid') is None:
parents.append(element)
else:
clones.append(element)
else:
# Working with Logiqx standard dat
if element.get(key='cloneof') is None:
parents.append(element)
else:
clones.append(element)
if len(bioses) > 0:
self.bioses = tuple(bioses)
_tryLogger_(log=f'Bioses: {len(self.bioses)}',
level='info')
if len(parents) > 0:
self.parents = tuple(parents)
_tryLogger_(log=f'Parent ROMs: {len(self.parents)}',
level='info')
if len(clones) > 0:
self.clones = tuple(clones)
_tryLogger_(log=f'Clone ROMs: {len(self.clones)}',
level='info')
if ('bioses' or 'parents' or 'clones') in locals().keys():
return True
return False
except Exception as e:
_tryLogger_(log='An error occurred retrieving parents, clones and '
'bioses')
return False
def __extractData__(self) -> bool:
"""
Data extraction manager.
"""
# Defining game nodeTags, keys are used to scan for some notehead in header and
# values are used to scan for game elements
nodeTags = {
'no-intro':['game'],
'finalburn':['game'],
'mame':['game', 'machine']
}
issuers: tuple[str, ...] = ('logiqx', 'no-intro') # Words to search for in doc info
issuer: str | None = None # If found in the doc info
hasId: bool = False # If header has ID field
noteHead: str | None = None # If found in the header
parser = etree.XMLParser(remove_blank_text=True) # some parser options here
try:
tree = etree.parse(source=self.descriptor, parser=parser)
if self.__getDocInfo__(data=tree.docinfo): # retrieving xml DOCInfo
for info in self.docInfo:
if type(self.docInfo[info]) == str:
for i in issuers:
if i in self.docInfo[info]:
issuer = i
_tryLogger_(log=f'DOC Type seems like {issuer}'
f' - found in "{info}" DOC Info '
'tag', level='debug')
break
if issuer != None:
break
if issuer is None:
_tryLogger_(log='Unknow xml DOC Type', level='debug')
else:
_tryLogger_(log='No xml DOC info found', level='warning')
# retrieving xml Header
if not self.__getHeader__(data=tree.find(path='//header')):
_tryLogger_(log='No header found', level='error')
return False
if 'id' in self.header.keys():
hasId = True # Descriptor has ID
for element in self.header: # retrieving some "unique" notehead
for n in nodeTags.keys():
if n in self.header[element]['text'].lower():
noteHead = n
_tryLogger_(log=f'Header seems like {noteHead} - '
f'found in "{element}" header tag')
break
if noteHead != None:
break
if noteHead is None:
_tryLogger_(log=f'Descriptor is of unknow type', level='error')
return False
schema = getWeight(issuer, hasId, noteHead):
pass
# nodeTag = nodeTags.get(schema)
# if not self.__extractElements__(elements=tree.xpath(_path='//'+f'{nodeTag}')):
# _tryLogger_(log=f'No game elements found with tag "{nodeTag}"', level='error')
# return False
except Exception as e:
if isinstance(e, etree.XMLSyntaxError):
return False
else:
raise
return True
def getWeight(self, issuer, hasId, noteHead)
def getGames(self, rSet: Literal['bioses', 'parents', 'clones', 'all']
= 'all') -> dict | None:
"""
Retrieve specific data elements based on the provided set.
Args:
rSet (str): The data set to retrieve. Valid values are 'bioses', 'parents' or
'clones'; note: nothing = all
Raises:
ValueError: If the set or data parameter has an invalid value.
Return a dict or None:
{
"name": {
"attributes": {
"name": str,
...
},
"subelements": {
"comment": str,
"description": str,
"year": int | str,
"manufacturer": str,
"rom": {
"name1": {
"name": str,
"size": int | str,
"crc": str
...
},
"name2": {
"name": str,
"size": int | str,
"crc": str,
"sha": str
...
},
...
},
"video": {
...
},
"driver": {
"status": bool
...
}
...
}
}
}
Where possible datas are converted to int and bool
"""
if rSet not in ('bioses', 'parents', 'clones', 'all'):
raise ValueError(f"Invalid set value: {rSet}")
if rSet == 'all':
if hasattr(self, 'parents'):
temp = self.parents
if hasattr(self, 'clones'):
temp = self.parents + self.clones
if hasattr(self, 'bioses'):
temp = self.parents + self.clones + self.bioses
else:
if hasattr(self, rSet):
temp = getattr(self, rSet)
if 'temp' in locals().keys():
if isinstance(locals()['temp'], tuple) and len(locals()['temp']) > 0:
reponse: dict = {}
for element in temp: # type: ignore
if element.tag == 'game':
game = element.attrib.get('name')
reponse.update({game: {'attributes': {}, 'subelements': {}}})
for key, value in element.attrib.items():
if value is not None:
if key == 'isbios':
if value == 'yes':
reponse[game]['attributes'].update({key: True})
else:
reponse[game]['attributes'].update({key: value})
else:
reponse[game]['attributes'].update({key: value})
for subelement in element:
if subelement.tag == 'comment':
reponse[game]['subelements'].update({subelement.tag: subelement.text})
elif subelement.tag == 'description':
reponse[game]['subelements'].update({subelement.tag: subelement.text})
elif subelement.tag == 'year':
try:
newValue = int(getattr(subelement, 'text'))
reponse[game]['subelements'].update({subelement.tag: newValue})
except Exception as e:
_tryLogger_(log=f'Error converting year into integer for {str(object=game)}: {e}')
reponse[game]['subelements'].update({subelement.tag: subelement.text})
elif subelement.tag == 'manufacturer':
reponse[game]['subelements'].update({subelement.tag: subelement.text})
elif subelement.tag == 'rom':
rom = subelement.attrib.get('name')
if not subelement.tag in reponse[game]['subelements'].keys():
reponse[game]['subelements'].update({subelement.tag: {}})
reponse[game]['subelements'][subelement.tag].update({rom: {}})
for key, value in subelement.attrib.items():
if value is not None:
if key == 'size':
try:
newValue = int(value)
reponse[game]['subelements'][subelement.tag][rom].update({key: newValue})
except Exception as e:
_tryLogger_(log=f'Error converting year into integer for {str(object=game)}: {e}')
reponse[game]['subelements'][subelement.tag][rom].update({key: value})
else:
reponse[game]['subelements'][subelement.tag][rom].update({key: value})
elif subelement.tag == 'video':
reponse[game]['subelements'].update({subelement.tag: {}})
for key, value in subelement.items():
if value is not None:
if key in {'width', 'height', 'aspectx', 'aspecty'}:
try:
newValue = int(value)
reponse[game]['subelements'][subelement.tag].update({key: newValue})
except Exception as e:
_tryLogger_(log=f'Error converting video data into integer for {str(object=game)}: {e}')
reponse[game]['subelements'][subelement.tag].update({key: value})
else:
reponse[game]['subelements'][subelement.tag].update({key: value})
elif subelement.tag == 'driver':
reponse[game]['subelements'].update({subelement.tag: {}})
for key, value in subelement.items():
if value is not None:
if key == 'status':
if value == 'good':
reponse[game]['subelements'][subelement.tag].update({key: True})
elif value == 'bad':
reponse[game]['subelements'][subelement.tag].update({key: False})
else:
reponse[game]['subelements'][subelement.tag].update({key: value})
else:
reponse[game]['subelements'][subelement.tag].update({key: value})
else:
reponse[game]['subelements'].update({subelement.tag: subelement.text})
else:
raise ValueError(f'Non-game element found scanning {rSet}')
return reponse
return None
return None
def _tryLogger_(log: Any, level: Literal['debug', 'info', 'warning', 'error',
'critical'] = 'debug') -> None:
if thisLogger is not None:
thisLogger.name = __name__
log_method = getattr(thisLogger, level)
log_method(log)
else:
print(f'Error writing log, continuing with simple print\n{log}')