From 03b34aa750f10e9d22e26ea88c2e7b35114910f4 Mon Sep 17 00:00:00 2001 From: tza Date: Mon, 29 Apr 2024 16:11:54 +0300 Subject: [PATCH] backup progress py3 port --- pyren/mod_db_manager.py | 8 ++-- pyren/mod_ecu.py | 86 ++--------------------------------------- pyren/mod_ecu_state.py | 2 +- pyren/mod_elm.py | 38 ++++++++++-------- pyren/mod_optfile.py | 2 +- pyren/mod_scan_ecus.py | 7 ++-- pyren/mod_utils.py | 6 +-- pyren/ply/yacc.py | 4 +- pyren/pyren.py | 49 +++-------------------- 9 files changed, 44 insertions(+), 158 deletions(-) diff --git a/pyren/mod_db_manager.py b/pyren/mod_db_manager.py index 891d0d5..7926ae4 100644 --- a/pyren/mod_db_manager.py +++ b/pyren/mod_db_manager.py @@ -141,15 +141,13 @@ def get_file_from_clip( filename ): or '/sg' in filename.lower() \ or '\\sg' in filename.lower()): mode = 'rb' + enc = None else: mode = 'r' - - if (mod_globals.os == 'android' - or mod_globals.clip_arc != ''): - mode = 'r' + enc = "utf-16le" if "Vehicles" in filename else "utf-8" if mod_globals.clip_arc=='': - return open(os.path.join(mod_globals.cliproot, filename), mode) + return open(os.path.join(mod_globals.cliproot, filename), mode, encoding=enc) else: if filename.startswith('../'): filename = filename[3:] diff --git a/pyren/mod_ecu.py b/pyren/mod_ecu.py index a0de542..861f3af 100755 --- a/pyren/mod_ecu.py +++ b/pyren/mod_ecu.py @@ -16,8 +16,6 @@ from mod_elm import ELM from mod_elm import snat from mod_elm import dnat from mod_elm import AllowedList -if mod_globals.os != 'android': - from mod_ddt import DDT import mod_globals import mod_db_manager @@ -529,13 +527,13 @@ class ECU: header = 'ECU : '+self.ecudata['ecuname']+ connectionData +self.ecudata['doc']+'\n' header = header + "Screen : "+path - newScreen = newScreen + pyren_encode( header ) + '\n' + newScreen = newScreen + pyren_encode( header ).decode("utf-8") + '\n' H = 25 pages = len(strlst)/H for l in strlst[page*H:(page+1)*H]: - newScreen = newScreen + pyren_encode( l ) + ' \n' + newScreen = newScreen + pyren_encode( l ).decode("utf-8") + ' \n' if not path[:3] == 'FAV': if pages>0: newScreen = newScreen+'\n'+"[Page "+str(page+1)+" from "+str(pages+1)+"] [N-next P-prev.] H for help or any other to exit" @@ -1260,25 +1258,6 @@ def find_real_ecuid( eid ): def main(): - - try: - import androidhelper as android - mod_globals.os = 'android' - except: - try: - import android - mod_globals.os = 'android' - except: - pass - - if mod_globals.os == 'android': - ecuid = input('Enetr ECU ID:') - lanid = input('Language [RU]:') - if len(lanid)<2: lanid = 'RU' - sys.argv.append(ecuid) - sys.argv.append(lanid) - sys.argv.append('TORQ') - if len(sys.argv)<3: print("Usage: mod_ecu.py [torq] [nochk]") print("Example:") @@ -1332,13 +1311,6 @@ def main(): print("Loading mnemonics") mm_class = ecu_mnemonics ( Mnemonics, mdoc, opt_file.dict, lang.dict ) - #for p in Parameters.values(): - # print p - #for s in States.values(): - # print s - #for m in Mnemonics.values(): - # print m - if len(sys.argv)==3: print() print("Defaults") @@ -1369,11 +1341,6 @@ def main(): print() for i in sorted(Commands.keys()): print(pyren_encode( Commands[i].codeMR+"["+i+"] "+Commands[i].label )) - - sys.exit(0) - - if len(sys.argv)>3 and sys.argv[3].upper()!='TORQ': - sys.exit(0) family = sys.argv[1][:2] eindex = sys.argv[1][2:] @@ -1385,9 +1352,6 @@ def main(): sss = '82'+F2A[family]+'F1' ddd = '82'+F2A[family]+'F1' filename = "PR_"+F2A[family]+"_"+eindex+"_"+sys.argv[2]+".csv" - - if mod_globals.os=='android' and os.path.exists('/sdcard/.torque/extendedpids'): - filename = '/sdcard/.torque/extendedpids/'+filename cf = open( filename, "w") @@ -1493,50 +1457,8 @@ def main(): print("Init string for Engine K-line (SLOW INIT):") print(slow05init) print() - - # make profile for torque - profilename = str(int(time.time()))+'.tdv' - if mod_globals.os=='android' and os.path.exists('/sdcard/.torque/vehicles'): - profilename = '/sdcard/.torque/vehicles/'+str(int(time.time()))+'.tdv' - - prn = open( profilename, "w") - prn.write( '#This is an ECU profile generated by pyren\n'.encode('utf-8') ) - prn.write( 'fuelType=0\n'.encode('utf-8') ) - prn.write( 'obdAdjustNew=1.0\n'.encode('utf-8') ) - prn.write( 'lastMPG=0.0\n'.encode('utf-8') ) - prn.write( 'tankCapacity=295.5\n'.encode('utf-8') ) - prn.write( 'volumetricEfficiency=85.0\n'.encode('utf-8') ) - prn.write( 'weight=1400.0\n'.encode('utf-8') ) - prn.write( 'odoMeter=0.0\n'.encode('utf-8') ) - prn.write( 'adapterName=OBDII [00\:00\:00\:00\:00\:0]\n'.encode('utf-8') ) - prn.write( 'adapter=00\:00\:00\:00\:00\:00\n'.encode('utf-8') ) - prn.write( 'boostAdjust=0.0\n'.encode('utf-8') ) - prn.write( 'mpgAdjust=1.0\n'.encode('utf-8') ) - prn.write( 'fuelCost=0.18000000715255737\n'.encode('utf-8') ) - prn.write( 'ownProfile=false\n'.encode('utf-8') ) - prn.write( 'displacement=1.6\n'.encode('utf-8') ) - prn.write( 'tankUsed=147.75\n'.encode('utf-8') ) - prn.write( 'lastMPGCount=0\n'.encode('utf-8') ) - prn.write( 'maxRpm=7000\n'.encode('utf-8') ) - prn.write( 'fuelDistance=0.0\n'.encode('utf-8') ) - prn.write( 'fuelUsed=0.0\n'.encode('utf-8') ) - prn.write( 'alternateHeader=true\n'.encode('utf-8') ) - prn.write( ('name=PR_'+ecuid+'\n').encode('utf-8') ) - if len(candst)>1: - prn.write( ('customInit='+can500init.replace('\\','\\\\')+'\n').encode('utf-8') ) - prn.write( 'preferredProtocol=7\n'.encode('utf-8') ) - elif len(fastinit)>1: - prn.write( ('customInit='+fast10init.replace('\\','\\\\')+'\n').encode('utf-8') ) - prn.write( 'preferredProtocol=6\n'.encode('utf-8') ) - else: - prn.write( ('customInit='+slow05init.replace('\\','\\\\')+'\n').encode('utf-8') ) - prn.write( 'preferredProtocol=5\n'.encode('utf-8') ) - prn.close() - print() - print("Torque profile:", profilename, "created") - print() + self.show_screens() if __name__ == "__main__": - main() - + main() \ No newline at end of file diff --git a/pyren/mod_ecu_state.py b/pyren/mod_ecu_state.py index 3cb58e0..2762f2b 100755 --- a/pyren/mod_ecu_state.py +++ b/pyren/mod_ecu_state.py @@ -28,7 +28,7 @@ def get_state( st, mn, se, elm, calc, dataids = {} ): st.value = " "*(8-len(st.value)/2) + st.value return "%-6s %-41s %-16s"%(st.codeMR,st.label,st.value), st.helps, csv_val else: - st.value = " "*(16-len(st.value)/2) + st.value + st.value = " "*int(16-len(st.value)/2) + st.value.decode("utf-8") return "%-6s %-50s %-20s"%(st.codeMR,st.label,st.value), st.helps, csv_val class ecu_state: diff --git a/pyren/mod_elm.py b/pyren/mod_elm.py index 19e3448..5b52c94 100644 --- a/pyren/mod_elm.py +++ b/pyren/mod_elm.py @@ -210,7 +210,7 @@ class Port: print("") print("Available COM ports:") for port, desc, hwid in iterator: - print("%-30s \n\tdesc: %s \n\thwid: %s" % (port, desc.decode ("windows-1251"), hwid)) + print("%-30s \n\tdesc: %s \n\thwid: %s" % (port, desc, hwid)) print("") mod_globals.opt_demo = True exit (2) @@ -304,22 +304,25 @@ class Port: byte = self.hdr.read(inInputBuffer) else: byte = self.hdr.read(1) - except: + except Exception as e: + print(e) print('*' * 40) print('* Connection to ELM was lost') mod_globals.opt_demo = True - + if byte: + print("READINGG", byte) return byte def write(self, data): - + if type(data) is str: + data = data.encode("utf-8") # dummy sync self.rwLock = True i = 0 while self.kaLock and i < 10: time.sleep (0.02) i = i + 1 - + print("WRITINGG", data) try: if self.portType == 1: try: @@ -333,7 +336,8 @@ class Port: return self.droid.bluetoothWrite (data) else: return self.hdr.write (data) - except: + except Exception as e: + print(e) print('*' * 40) print('* Connection to ELM was lost') mod_globals.opt_demo = True @@ -381,15 +385,15 @@ class Port: # search > string tb = time.time () # start time - self.buff = "" + self.buff = b"" while True: if not mod_globals.opt_demo: - byte = self.read () + byte = self.read() or b"" else: - byte = '>' + byte = '>'.encode("ascii") self.buff += byte tc = time.time () - if '>' in self.buff: + if '>'.encode("ascii") in self.buff: mod_globals.opt_speed = s print("\nStart COM speed: ", s) self.hdr.timeout = self.portTimeout @@ -1227,12 +1231,12 @@ class ELM: if cmd_len < 8: # single frame # check L1 cache here if isCommandInCache and int('0x' + self.l1_cache[command], 16) < 16: - raw_command.append (("%0.2X" % cmd_len) + command + self.l1_cache[command]) + raw_command.append (("%0.2X" % int(cmd_len)) + command + self.l1_cache[command]) else: - raw_command.append (("%0.2X" % cmd_len) + command) + raw_command.append (("%0.2X" % int(cmd_len)) + command) else: # first frame - raw_command.append ("1" + ("%0.3X" % cmd_len)[-3:] + command[:12]) + raw_command.append ("1" + ("%0.3X" % int(cmd_len))[-3:] + command[:12]) command = command[12:] # consecutive frames frame_number = 1 @@ -1409,10 +1413,10 @@ class ELM: raw_command = [] cmd_len = len(command) / 2 if cmd_len < 8: # single frame - raw_command.append(("%0.2X" % cmd_len) + command) + raw_command.append(("%0.2X" % int(cmd_len)) + command) else: # first frame - raw_command.append("1" + ("%0.3X" % cmd_len)[-3:] + command[:12]) + raw_command.append("1" + ("%0.3X" % int(cmd_len))[-3:] + command[:12]) command = command[12:] # consecutive frames frame_number = 1 @@ -1610,10 +1614,10 @@ class ELM: raw_command = [] cmd_len = len (command) / 2 if cmd_len < 8: # single frame - raw_command.append (("%0.2X" % cmd_len) + command) + raw_command.append (("%0.2X" % int(cmd_len)) + command) else: # first frame - raw_command.append ("1" + ("%0.3X" % cmd_len)[-3:] + command[:12]) + raw_command.append ("1" + ("%0.3X" % int(cmd_len))[-3:] + command[:12]) command = command[12:] # consecutive frames frame_number = 1 diff --git a/pyren/mod_optfile.py b/pyren/mod_optfile.py index 4ef3d00..7791ec7 100755 --- a/pyren/mod_optfile.py +++ b/pyren/mod_optfile.py @@ -94,7 +94,7 @@ class optfile: if progress and (i&0xff==0): pr = (i+2-keyoff)*100/(tb-keyoff) - print('\r['+'X'*(pr/2)+' '*(50-pr/2)+'] '+str(int(pr))+'%', end=' ') + print('\r['+'X'*int(pr/2)+' '*int(50-pr/2)+'] '+str(int(pr))+'%', end=' ') sys.stdout.flush() lf.seek(i) diff --git a/pyren/mod_scan_ecus.py b/pyren/mod_scan_ecus.py index 2fcb623..23922f7 100644 --- a/pyren/mod_scan_ecus.py +++ b/pyren/mod_scan_ecus.py @@ -88,6 +88,7 @@ class ScanEcus: #if model_n<86: continue except ValueError: pass + print(file) DOMTree = xml.dom.minidom.parse(mod_db_manager.get_file_from_clip(file)) vh = DOMTree.documentElement if vh.hasAttribute("defaultText"): @@ -169,7 +170,7 @@ class ScanEcus: self.allecus[ecu]['pin1']==canH and self.allecus[ecu]['pin2']==canL): i = i+1 - print('\r'+'\t\t\t\t'+'\rScanning:'+str(i)+'/'+str(len(self.allecus))+" Detected: "+str(len(self.detectedEcus)), end=' ') + print('\r'+'\t\t\t\t'+'\rScanning CAN:'+str(i)+'/'+str(len(self.allecus))+" Detected: "+str(len(self.detectedEcus)), end=' ') sys.stdout.flush() self.elm.set_can_addr( self.allecus[ecu]['dst'], self.allecus[ecu] ) @@ -186,7 +187,7 @@ class ScanEcus: self.allecus[ecu]['pin2']=='15'): i = i+1 - print('\r'+'\t\t\t\t'+'\rScanning:'+str(i)+'/'+str(len(self.allecus))+" Detected: "+str(len(self.detectedEcus)), end=' ') + print('\r'+'\t\t\t\t'+'\rScanning KWP:'+str(i)+'/'+str(len(self.allecus))+" Detected: "+str(len(self.detectedEcus)), end=' ') sys.stdout.flush() self.elm.set_iso_addr( self.allecus[ecu]['dst'], self.allecus[ecu] ) @@ -989,7 +990,6 @@ def readECUIds( elm ): return StartSession, DiagVersion, Supplier, Version, Soft, Std, VIN - def findTCOM( addr, cmd, rsp ): ecuvhc = {} vehicle = '' @@ -1047,6 +1047,7 @@ def findTCOM( addr, cmd, rsp ): print(r, se.allecus[r]['doc'], se.allecus[r]['ids'], ecuvhc[r]) except: print() + def generateSavedEcus( eculist, fileName ): se = ScanEcus( 0 ) se.read_Uces_file( all = True ) diff --git a/pyren/mod_utils.py b/pyren/mod_utils.py index 6e39ebf..6467684 100755 --- a/pyren/mod_utils.py +++ b/pyren/mod_utils.py @@ -157,7 +157,7 @@ def Choice(list, question ): sys.exit() if ch=='q': ch = 'Q' if ch=='cmd': mod_globals.opt_cmd = True - if ch in list(d.keys()): + if ch in d.keys(): return [d[ch],ch] def ChoiceLong(list, question, header = '' ): @@ -178,7 +178,7 @@ def ChoiceLong(list, question, header = '' ): while( 1 ): - clearScreen() + # clearScreen() #os.system('cls' if os.name == 'nt' else 'clear') # clear screen #print chr(27)+"[2J"+chr(27)+"[;H", # clear ANSI screen (thanks colorama for windows) @@ -218,7 +218,7 @@ def ChoiceLong(list, question, header = '' ): break if ch=='cmd': mod_globals.opt_cmd = True - if ch in list(d.keys()): + if ch in d.keys(): return [d[ch],ch] def ChoiceFromDict(dict, question, showId = True ): diff --git a/pyren/ply/yacc.py b/pyren/ply/yacc.py index 505b049..24e088b 100755 --- a/pyren/ply/yacc.py +++ b/pyren/ply/yacc.py @@ -1330,7 +1330,7 @@ class Production(object): return len(self.prod) def __bool__(self): - return 1 + return True def __getitem__(self, index): return self.prod[index] @@ -1718,7 +1718,7 @@ class Grammar(object): def undefined_symbols(self): result = [] for p in self.Productions: - if not p: + if not p is None: continue for s in p.prod: diff --git a/pyren/pyren.py b/pyren/pyren.py index 66a9508..7e4d7ff 100755 --- a/pyren/pyren.py +++ b/pyren/pyren.py @@ -9,48 +9,6 @@ mod_globals.os = os.name os.chdir(os.path.dirname(os.path.realpath(sys.argv[0]))) import pickle - -if mod_globals.os == 'nt': - import pip - - try: - import serial - except ImportError: - pip.main(['install','pyserial']) - - try: - import colorama - except ImportError: - pip.main(['install','colorama']) - try: - import colorama - except ImportError: - print("\n\n\n\t\t\tGive me access to the Internet for download modules\n\n\n") - sys.exit() - colorama.init() -else: - # let's try android - try: - import androidhelper as android - mod_globals.os = 'android' - except: - try: - import android - mod_globals.os = 'android' - except: - pass - -if mod_globals.os != 'android': - try: - import serial - from serial.tools import list_ports - #import ply - except ImportError: - print("\n\n\n\tPleas install additional modules") - print("\t\t>sudo easy_install pyserial") - #print "\t\t>sudo easy_install ply" - sys.exit() - import mod_utils import mod_ddt_utils @@ -60,6 +18,9 @@ from mod_ecu import ECU from mod_optfile import * from mod_utils import * +from serial.tools import list_ports +from os import getenv + def optParser(): '''Parsing of command line parameters. User should define at least com port name''' @@ -221,7 +182,7 @@ def optParser(): print("") print("Available COM ports:") for port, desc, hwid in iterator: - print("%-30s \n\tdesc: %s \n\thwid: %s" % (port,desc.decode("windows-1251"),hwid)) + print("%-30s \n\tdesc: %s \n\thwid: %s" % (port,desc,hwid)) print("") exit(2) else: @@ -229,7 +190,7 @@ def optParser(): mod_globals.opt_ecuid = options.ecuid mod_globals.opt_speed = int(options.speed) mod_globals.opt_rate = int(options.rate) - mod_globals.opt_lang = options.lang + mod_globals.opt_lang = options.lang.strip() mod_globals.opt_car = options.car mod_globals.opt_log = options.logfile mod_globals.opt_demo = options.demo