import glob import os import pickle import re import subprocess from xml.sax.saxutils import unescape import zipfile from lxml import etree class MessROM(object): ''' One Mame Software List ROM ''' def __init__(self): self.name = '' self.cloneof = None self.description = '' self.year = '' self.manufacturer = '' self.category = '' def __str__(self): return self.description # return '["%s", "%s", "%s", "%s", "%s" "%s", "%s"]' % ( # self.name, self.cloneof, self.description, self.year, # self.manufacturer, self.status, self.category) def __repr__(self): return '%s("%s", "%s", "%s", "%s", "%s", "%s")' % ( self.__class__.__name__, self.name, self.cloneof, self.description, self.year, self.manufacturer, self.category) class MessROMs(object): ''' A Collection of MAME Software List Roms ''' def __init__(self, data_dir, cfg): self.all_roms = [] self.roms = [] self.categories = [] self.all_categories = [] self.catdict = {} self.len = 0 self.data_dir = data_dir self.cfg = cfg self.emulator_dir = os.path.split(cfg['EXE'])[0] self.parse() self.filter() self.snapdir = os.path.join(self.emulator_dir, 'snap') if os.path.isfile(os.path.join(self.snapdir, 'snap.zip')): self.snapfile = zipfile.ZipFile( os.path.join(self.snapdir, "snap.zip")) else: self.snapfile = None self.snapdir = os.path.join(self.snapdir, 'snap') infofn = os.path.join(self.emulator_dir, 'messinfo.dat') if not os.path.isfile(infofn): self.info = None else: with open(infofn, 'rt', encoding='latin1') as f: self.info = f.read() historyfn = os.path.join(self.emulator_dir, 'history.dat') if not os.path.isfile(historyfn): self.history = None else: with open(historyfn, 'rt', encoding='utf-8') as f: self.history = f.read() def __len__(self): return self.len def __getitem__(self, item): return self.roms[item] def filter(self): self.roms = [] for rom in self.all_roms: if self.cfg["Category"] == "All Games" or self.cfg["Category"] == rom.category: if self.cfg["ShowClones"] or rom.cloneof is None: self.roms.append(rom) if self.cfg["Sort"] == "Name Asc": self.roms.sort(key=lambda x: x.description.lower(), reverse=False) elif self.cfg["Sort"] == "Name Dec": self.roms.sort(key=lambda x: x.description.lower(), reverse=True) elif self.cfg["Sort"] == "Year Asc": self.roms.sort(key=lambda x: x.year.lower(), reverse=False) elif self.cfg["Sort"] == "Year Dec": self.roms.sort(key=lambda x: x.year.lower(), reverse=True) self.len = len(self.roms) self.categories = self.all_categories def parse(self): """ Parse xml """ mess_version = self.get_mess_version() datfile = os.path.join(self.data_dir, 'mess.dat') if self.cfg['Version'] != mess_version: self.cfg['Version'] = mess_version if os.path.isfile(datfile): os.unlink(datfile) if os.path.isfile(datfile): with open(datfile, 'rb') as i: temp_mess_version = pickle.load(i) if temp_mess_version == mess_version: self.all_categories = pickle.load(i) self.all_roms = pickle.load(i) return True else: os.unlink(datfile) self.all_categories = [] self.catdict = {} files = glob.glob(os.path.join(self.emulator_dir, "hash", '*.xml')) for file in files: with open(file, 'rt', encoding='utf-8') as f: line = None while True: line = f.readline().strip() if line.startswith('