Created
November 19, 2022 17:54
-
-
Save sehugg/369d3c4723b1216412259691da6bd9d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.parsers.expat | |
import sys,gzip,string | |
_name = '' | |
_manf = '' | |
_year = 0 | |
_desc = '' | |
_srcfile = '' | |
_tag = None | |
_cpus = [] | |
_audio = [] | |
# 3 handler functions | |
# <machine name="3bagflnz" sourcefile="aristmk4.c" cloneof="3bagflvt" romof="3bagflvt" sampleof="3bagflvt"> | |
def start_element(name, attrs): | |
global _name, _tag, _manf, _srcfile | |
_tag = name | |
if name == 'machine' and attrs['isdevice'] != 'yes': | |
#if attrs.get('cloneof'): | |
# _name = None | |
# _tag = None | |
# return | |
_name = attrs['name'] | |
_srcfile = attrs['sourcefile'] | |
elif name == 'chip': | |
if attrs['type'] == 'cpu': | |
if attrs['tag'] == 'maincpu': | |
_cpus.append('*'+attrs['name']) | |
else: | |
_cpus.append("+"+attrs['name']) | |
elif attrs['type'] == 'audio': | |
name = attrs['name'] | |
if name != 'Speaker': | |
_audio.append(name) | |
def end_element(name): | |
_tag = None | |
if name == 'machine' and _name: | |
print('%s,"%s","%s","%s","%s","%s","%s"' % (_year,_name,_srcfile,_desc,_manf,' '.join(_cpus),' '.join(_audio))) | |
_audio[:] = [] | |
_cpus[:] = [] | |
def char_data(data): | |
global _manf, _year, _desc | |
data = data.strip() | |
if len(data): | |
if _tag == 'manufacturer': | |
_manf = data | |
elif _tag == 'year': | |
_year = data | |
elif _tag == 'description': | |
_desc = data | |
p = xml.parsers.expat.ParserCreate() | |
p.StartElementHandler = start_element | |
p.EndElementHandler = end_element | |
p.CharacterDataHandler = char_data | |
#with gzip.open('allroms.xml.gz','r') as f: | |
with open('allroms.xml','rb') as f: | |
p.ParseFile(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment