#!/usr/bin/env python3 import struct import sys import os import mod_globals import mod_db_manager try: import pickle as pickle except: import pickle from xml.dom.minidom import parseString class optfile: dict = {} obf = True def __init__(self,filename,progress=False,cache=True): self.dict = {} # check in cache folder cachename = mod_globals.cache_dir+os.path.basename(filename)[:-4]+'.p' if os.path.isfile(cachename): self.dict = pickle.load( open( cachename, "rb" ) ) return if mod_globals.clip_arc!='' and mod_db_manager.file_in_clip(filename[:-4]+'.p'): mod_db_manager.extract_from_clip_to_cache(filename[:-4]+'.p') self.dict = pickle.load( open( cachename, "rb" ) ) return lf = mod_db_manager.get_file_from_clip( filename ) if lf: self.get_dict( lf, progress ) if cache: pickle.dump( self.dict, open( cachename, "wb" ) ) def get_string(self, lf, len): i = lf.tell() bytes = lf.read(2 * len) st = '' j = 0 len = len * 2 while j < len: x = struct.unpack(' [key]") print(" mod_optfile.py ALLSG") print(" mod_optfile.py HEX ") print("Example:") print(" mod_optfile.py ../Location/DiagOnCan_RU.bqm") print(" mod_optfile.py ../EcuRenault/Sessions/SG0110016.XML P001") sys.exit(0) if sys.argv[1]=='ALLSG': for subdir, dirs, files in os.walk('../EcuRenault/Sessions'): for file in files: if file.startswith('SG'): sgFileName = '../EcuRenault/Sessions/S'+file[1:] ugFileName = '../EcuRenault/Sessions/U'+file[1:] if os.path.isfile(ugFileName): continue print(sgFileName) try: of = optfile(sgFileName,False,False) except: print('bad file') continue rf = '' for k in sorted(of.dict.keys()): if of.dict[k][:1]=='<' and of.dict[k][-1:]=='>': rf = rf + of.dict[k] rf = rf + '' rf = parseString(rf).toprettyxml(indent=' ') print(ugFileName) f = open(ugFileName,'wt') f.write( rf ) f.close() exit(0) if sys.argv[1]=='HEX': lf = open(sys.argv[2], "rb") of = open(sys.argv[3], "wb") while(1): i = lf.tell() bytes = lf.read(2) if len(bytes)<2: exit() x = 0 x = struct.unpack('': print(parseString(of.dict[k]).toprettyxml(indent=' ')) else: print(of.dict[k]) if len(sys.argv)==3: k = sys.argv[2] if k in list(of.dict.keys()): if of.dict[k][:1]=='<' and of.dict[k][-1:]=='>': print(parseString(of.dict[k]).toprettyxml(indent=' ')) else: print(of.dict[k]) else: for i in sorted(of.dict.keys()): if k in i: print('#'*60) print('Key:',i) print('-'*60) if of.dict[i][:1]=='<' and of.dict[i][-1:]=='>': print(parseString(of.dict[i]).toprettyxml(indent=' ')) else: print(of.dict[i])