1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-15 12:25:12 +03:00

Use Python 2/3 compatible scripts

Almost entirely fixing print statements, but also a couple importing
changes, wrapped in try/excepts, and one case where dict.keys() not
being a list would be a problem.

Also reverts e41139a397

The real fix for #1907
This commit is contained in:
Sean Vig 2015-10-04 13:24:59 -05:00
parent b3d52e3525
commit 5efa5d47d3
14 changed files with 103 additions and 72 deletions

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import os
@ -43,7 +44,7 @@ while True:
links = "(" + ", ".join(issue_links) + ")"
line = "<li>" + line + " " + links + "</li>"
print line
print(line)
inp.close()

View file

@ -1,9 +1,11 @@
#!/bin/env python
from __future__ import print_function
import sys, glob
def addLine(filename, newline, after):
print filename, newline
print(filename, newline)
lines = file(filename, 'r').readlines()
for i, line in enumerate(lines):
if after in line:

View file

@ -1,5 +1,7 @@
#!/bin/env python
from __future__ import print_function
SIGN_BIT = (0x80) # Sign bit for a A-law byte.
QUANT_MASK = (0xf) # Quantization field mask.
SEG_SHIFT = (4) # Left shift for segment number.
@ -47,5 +49,5 @@ def tableToString(name, table):
result += ' };'
return result
print tableToString('alawTable', pcmTable(alaw2linear))
print tableToString('ulawTable', pcmTable(ulaw2linear))
print(tableToString('alawTable', pcmTable(alaw2linear)))
print(tableToString('ulawTable', pcmTable(ulaw2linear)))

View file

@ -1,5 +1,7 @@
#!/usr/bin/env python
from __future__ import print_function
import os
import re
import sys
@ -38,9 +40,9 @@ def writeheader(filename,header):
f = open(filename,'w')
f.writelines(output)
f.close()
print "added header to %s" %filename
except IOError,err:
print "something went wrong trying to add header to %s: %s" % (filename,err)
print("added header to %s" %filename)
except IOError as err:
print("something went wrong trying to add header to %s: %s" % (filename,err))
def main(args=sys.argv):
headerfile = open(args[1])

View file

@ -1,7 +1,9 @@
from __future__ import print_function
BITLEN_DSM2 = 16
def sendByteDsm2(b):
print "%02x:" % b,
print("%02x:" % b, end=' ')
lev = 0
len = BITLEN_DSM2
for i in range(9):
@ -9,13 +11,13 @@ def sendByteDsm2(b):
if (lev == nlev):
len += BITLEN_DSM2
else:
print len,
print(len, end=' ')
# _send_1(nlev ? len-5 : len+3);
len = BITLEN_DSM2
lev = nlev
b = (b>>1) | 0x80
# _send_1(len+BITLEN_DSM2+3); // 2 stop bits
print len+BITLEN_DSM2
print(len+BITLEN_DSM2)
sendByteDsm2(24)
sendByteDsm2(17)

View file

@ -1,5 +1,7 @@
#!/bin/env python
from __future__ import division, print_function
curr = 0
idx = 0
byte = 0
@ -10,12 +12,12 @@ def push4bits(val):
curr += val << idx
idx += 4
if idx == 8:
print "0x%02X," % curr,
print("0x%02X," % curr, end=' ')
idx = 0
curr = 0
byte += 1
if byte % 16 == 0:
print
print()
cluster = 0
@ -27,8 +29,8 @@ def pushCluster(val):
cluster += 1
def pushFile(size):
sectors = size / 512
count = sectors / 8
sectors = size // 512
count = sectors // 8
for i in range(count-1):
pushCluster(cluster+1)
pushCluster(0xFFF)
@ -36,14 +38,14 @@ def pushFile(size):
def pushDisk(eeprom, flash):
global curr, idx, byte, cluster
curr = idx = byte = cluster = 0
print "Disk with %dk EEPROM and %dk FLASH:" % (eeprom, flash)
print("Disk with %dk EEPROM and %dk FLASH:" % (eeprom, flash))
pushCluster(0xFF8)
pushCluster(0xFFF)
pushFile(eeprom*1024)
pushFile(flash*1024)
while byte < 512:
push4bits(0)
print
print()
pushDisk(32, 512)
pushDisk(64, 512)

View file

@ -1,5 +1,7 @@
#!/usr/bin/env python
from __future__ import division, print_function
import sys
from PyQt4 import Qt, QtGui
@ -19,7 +21,7 @@ else:
def writeSize(f, width, height):
if lcdwidth > 255:
f.write("%d,%d,%d,%d,\n" % (width%256, width/256, height%256, height/256))
f.write("%d,%d,%d,%d,\n" % (width%256, width//256, height%256, height//256))
else:
f.write("%d,%d,\n" % (width, height))
@ -27,7 +29,7 @@ if what == "1bit":
rows = 1
if len(sys.argv) > 5:
rows = int(sys.argv[5])
writeSize(f, width, height/rows)
writeSize(f, width, height//rows)
for y in range(0, height, 8):
for x in range(width):
value = 0
@ -42,7 +44,7 @@ elif what == "4/4/4/4":
for y in range(height):
for x in range(width):
pixel = image.pixel(x, y)
f.write("0x%1x%1x%1x%1x," % (Qt.qAlpha(pixel)/16, Qt.qRed(pixel)/16, Qt.qGreen(pixel)/16, Qt.qBlue(pixel)/16))
f.write("0x%1x%1x%1x%1x," % (Qt.qAlpha(pixel)//16, Qt.qRed(pixel)//16, Qt.qGreen(pixel)//16, Qt.qBlue(pixel)//16))
f.write("\n")
elif what == "5/6/5/8":
colors = []
@ -148,4 +150,3 @@ elif what == "22x38":
f.write("\n")
else:
print("wrong argument", sys.argv[4])

View file

@ -1,5 +1,7 @@
#!/usr/bin/env python
from __future__ import print_function
import sys, os
for filename in sys.argv[1:]:
@ -14,7 +16,7 @@ for filename in sys.argv[1:]:
if line.startswith("#ifndef "):
guard = line[8:]
if lines[i+1].strip() == "#define %s" % guard:
print filename, ":", guard, "=>", newguard
print(filename, ":", guard, "=>", newguard)
lines[i] = "#ifndef %s\n" % newguard
lines[i+1] = "#define %s\n" % newguard
end = -1

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import os
@ -24,11 +25,11 @@ error = None
def checkName(name):
global warning
if name in dups_name:
print "WARNING: Duplicate name %s found for constant %s" % (name, CONSTANT_VALUE)
print("WARNING: Duplicate name %s found for constant %s" % (name, CONSTANT_VALUE))
warning = True
dups_name.append(name)
if name != name.lower():
print "WARNING: Name not in lower case %s found for constant %s" % (name, CONSTANT_VALUE)
print("WARNING: Name not in lower case %s found for constant %s" % (name, CONSTANT_VALUE))
warning = True
@ -46,9 +47,9 @@ def LEXP_MULTIPLE(nameFormat, descriptionFormat, valuesCount):
exports_multiple.append( (CONSTANT_VALUE, nameFormat, descriptionFormat, valuesCount) )
if len(sys.argv) < 3:
print "Error: not enough arguments!"
print "Usage:"
print " luaexport.py <version> <input txt> <output cpp> [<doc output>]"
print("Error: not enough arguments!")
print("Usage:")
print(" luaexport.py <version> <input txt> <output cpp> [<doc output>]")
version = sys.argv[1]
inputFile = sys.argv[2]
@ -56,11 +57,11 @@ outputFile = sys.argv[3]
docFile = None
if len(sys.argv) >= 4:
docFile = sys.argv[4]
print "Version %s" % version
print "Input file %s" % inputFile
print "Output file %s" % outputFile
print("Version %s" % version)
print("Input file %s" % inputFile)
print("Output file %s" % outputFile)
if docFile:
print "Documentation file %s" % docFile
print("Documentation file %s" % docFile)
inp = open(inputFile, "r")
@ -75,7 +76,7 @@ while True:
parts = line.split('LEXP')
#print parts
if len(parts) != 2:
print "Wrong line: %s" % line
print("Wrong line: %s" % line)
continue
cmd = 'LEXP' + parts[1]
cnst = parts[0].rstrip(', ')
@ -93,7 +94,7 @@ while True:
# print cmd
eval(cmd)
except:
print "ERROR: problem with the definition: %s" % line
print("ERROR: problem with the definition: %s" % line)
traceback.print_exc()
error = True
@ -130,7 +131,7 @@ exports.sort(key = lambda x: x[1]) #sort by name
data = [" {%s, \"%s\", \"%s\"}" % export for export in exports]
out.write(",\n".join(data))
out.write("\n};\n\n")
print "Generated %d items in luaFields[]" % len(exports)
print("Generated %d items in luaFields[]" % len(exports))
out.write("""
// The list of Lua fields that have a range of values
@ -139,7 +140,7 @@ const LuaMultipleField luaMultipleFields[] = {
data = [" {%s, \"%s\", \"%s\", %d}" % export for export in exports_multiple]
out.write(",\n".join(data))
out.write("\n};\n\n")
print "Generated %d items in luaMultipleFields[]" % len(exports_multiple)
print("Generated %d items in luaMultipleFields[]" % len(exports_multiple))
out.close()
if docFile:

View file

@ -1,9 +1,11 @@
#!/usr/bin/env python
from __future__ import print_function
import sys
filename = sys.argv[1]
print filename
print(filename)
fr = open(filename)
fw = open(filename+".new", "w")
ew = open(filename+".en", "w")
@ -46,10 +48,10 @@ for line in fr.readlines():
if str_rep in replacements.keys():
if replacements[str_rep] != str:
print "!!!!! NON !!!!!"
print("!!!!! NON !!!!!")
else:
replacements[str_rep] = str
print glob_str, "=>", str, str_rep
print(glob_str, "=>", str, str_rep)
ew.write("#define " + str_rep[1:] + " "*(17-len(str_rep)) + '"%s"\n' % str)
hw.write("extern const PROGMEM char %s[];\n" % str_rep)
cw.write("const prog_char APM %s[] = %s;\n" % (str_rep, str_rep[1:]))

View file

@ -1,5 +1,7 @@
#!/bin/env python
from __future__ import print_function
import math
samples = 1024
@ -17,13 +19,13 @@ for i in range(samples):
max = sample
elif sample < min:
min = sample
print "%d," % sample,
print("%d," % sample, end=' ')
if i % 10 == 9:
print
print()
print
print 'Range is:', min, max
print()
print('Range is:', min, max)
if max > 32767 or min < -32768:
print "Invalid range!"
print("Invalid range!")

View file

@ -3,6 +3,7 @@
# This program parses sport.log files
from __future__ import division, print_function
import os
import sys
@ -28,28 +29,28 @@ quiet = False
def ParseFlVSS(packet, dataId, prim, appId, data, crc):
# if dataId != 0xa1:
# return
print "packet: %s (%4d)" % (dump(packet), lineNumber) ,
print("packet: %s (%4d)" % (dump(packet), lineNumber), end=' ')
cells = (data & 0xF0) >> 4
battnumber = data & 0xF;
voltage1 = ((data & 0x000FFF00) >> 8) / 5
voltage2 = ((data & 0xFFF00000) >> 20) / 5
print " FLVSS: no cells: %d, cell: %d: voltages: %0.2f %0.2f" % (cells, battnumber, voltage1/100.0, voltage2/100.0)
voltage1 = ((data & 0x000FFF00) >> 8) // 5
voltage2 = ((data & 0xFFF00000) >> 20) // 5
print(" FLVSS: no cells: %d, cell: %d: voltages: %0.2f %0.2f" % (cells, battnumber, voltage1/100., voltage2/100.))
def ParseRSSI(packet, dataId, prim, appId, data, crc):
print "packet: %s (%4d)" % (dump(packet), lineNumber) ,
print " RSSI: %d" % (data & 0xFF)
print("packet: %s (%4d)" % (dump(packet), lineNumber), end=' ')
print(" RSSI: %d" % (data & 0xFF))
def ParseAdc(packet, dataId, prim, appId, data, crc):
print "packet: %s (%4d)" % (dump(packet), lineNumber) ,
print " A%d: %d" % (dataId - 0xf102, data & 0xFF)
print("packet: %s (%4d)" % (dump(packet), lineNumber), end=' ')
print(" A%d: %d" % (dataId - 0xf102, data & 0xFF))
def ParseBatt(packet, dataId, prim, appId, data, crc):
print "packet: %s (%4d)" % (dump(packet), lineNumber) ,
print " Batt: %d" % (data & 0xFF)
print("packet: %s (%4d)" % (dump(packet), lineNumber), end=' ')
print(" Batt: %d" % (data & 0xFF))
def ParseSWR(packet, dataId, prim, appId, data, crc):
print "packet: %s (%4d)" % (dump(packet), lineNumber) ,
print " SWR: %d" % (data & 0xFF)
print("packet: %s (%4d)" % (dump(packet), lineNumber), end=' ')
print(" SWR: %d" % (data & 0xFF))
def ParseAirSpeed(packet, dataId, prim, appId, data, crc):
print "packet: %s (%4d)" % (dump(packet), lineNumber) ,
@ -77,7 +78,7 @@ def ParseSportPacket(packet):
(dataId, prim, appId, data, crc) = struct.unpack('<BBHIB', packet)
#print "dataId:%02x, prim:%02x, appId:%04x, data:%08x, crc:%02x)\n" % (dataId, prim, appId, data, crc)
if prim != DATA_FRAME:
print "unknown prim: %02x for packet %s in line %s" % (prim, dump(packet), lineNumber)
print("unknown prim: %02x for packet %s in line %s" % (prim, dump(packet), lineNumber))
#proces according to appId
for (firstId, lastId, parser) in appIdParsers:
if appId >= firstId and appId <= lastId:
@ -85,8 +86,8 @@ def ParseSportPacket(packet):
return
#no parser found
if not quiet:
print "\tdataId:%02x, prim:%02x, appId:%04x, data:%08x, crc:%02x)" % (dataId, prim, appId, data, crc)
print "\tparser for appId %02x not implemented" % appId
print("\tdataId:%02x, prim:%02x, appId:%04x, data:%08x, crc:%02x)" % (dataId, prim, appId, data, crc))
print("\tparser for appId %02x not implemented" % appId)
def ParsePacket(packet):
global lineNumber
@ -104,7 +105,7 @@ def ParsePacket(packet):
#print "buff after unstuff %s" % dump(packet, 20)
else:
#missin data, wait for more data
print "unstuff missing data"
print("unstuff missing data")
return
#print "packet: %s" % dump(packet, 10)
@ -113,11 +114,11 @@ def ParsePacket(packet):
#print "\npacket: %s @%d" % (dump(packet), lineNumber)
#check crc
if not CheckSportCrc(packet):
print "error: wrong CRC for packet %s at line %d" % (dump(packet), lineNumber)
print("error: wrong CRC for packet %s at line %d" % (dump(packet), lineNumber))
ParseSportPacket(packet)
else:
if len(packet) > 1:
print "warning: wrong length %s for packet %s at line %d" % (len(packet), dump(packet, 10), lineNumber)
print("warning: wrong length %s for packet %s at line %d" % (len(packet), dump(packet, 10), lineNumber))
def ParseSportData(data):
@ -169,7 +170,7 @@ while True:
#print line
parts = line.split(': ')
if len(parts) < 2:
print "weird data: \"%s\" at line %d" % (line, lineNumber)
print("weird data: \"%s\" at line %d" % (line, lineNumber))
continue
sportData = parts[1].strip()
# print "sd: %s" % sportData

View file

@ -127,7 +127,7 @@ parser.add_argument("--reverse", help="Reversed char conversion (from number to
args = parser.parse_args()
if args.language not in translations:
parser.error(args.language + ' is not a supported language. Try one of the supported ones: ' + str(translations.keys()))
parser.error(args.language + ' is not a supported language. Try one of the supported ones: ' + str(list(translations.keys())))
system.exit()
if args.reverse:

View file

@ -26,7 +26,18 @@
# Portuguese : Romolo Manfredini
# Spanish : Romolo Manfredini (With the help of Jose Moreno)
import os, sys, shutil, platform, subprocess, wave, zipfile, httplib, urllib
from __future__ import print_function
import os, sys, shutil, platform, subprocess, wave, zipfile
try:
# Python 3
from http.client import HTTPConnection
from urllib.parse import urlencode
except ImportError:
# Python 2
from httplib import HTTPConnection
from urllib import urlencode
NO_ALTERNATE = 1024
@ -53,7 +64,7 @@ def wavstrip(filename):
def generate(str, filename):
print filename, str
print(filename, str)
if not str:
str = " !" #this is so blank wav files never exist!
@ -67,7 +78,7 @@ def generate(str, filename):
"speak not implemented with google tts engine"
exit()
else:
print "which speach engine?"
print("which speach engine?")
exit()
else:
if "sapi" in sys.argv:
@ -82,8 +93,8 @@ def generate(str, filename):
elif "google" in sys.argv:
ttsmp3 = "ttsfile.mp3"
ttsfilename = "ttsfile.wav"
conn = httplib.HTTPConnection("translate.google.com")
params = urllib.urlencode({'ie': "UTF-8", 'tl': directory, 'q': str.encode("utf-8")})
conn = HTTPConnection("translate.google.com")
params = urlencode({'ie': "UTF-8", 'tl': directory, 'q': str.encode("utf-8")})
headers = {"User-Agent": "Mozilla"}
conn.request("GET", u"/translate_tts?%s" % params, headers=headers)
# conn.request("GET", "/translate_tts?ie=UTF-8&tl=%s&q=%s" % (directory, urllib.urlencode(str)), headers={"User-Agent": "Mozilla"})
@ -93,7 +104,7 @@ def generate(str, filename):
conn.close()
else:
print "which speach engine?"
print("which speach engine?")
exit()
wavstrip(ttsfilename)
@ -771,7 +782,7 @@ if __name__ == "__main__":
# tts.Volume = 40
# tts.SetRate(1)
if "list" in sys.argv:
print tts.GetVoiceNames()
print(tts.GetVoiceNames())
exit()
if "mulaw" in sys.argv:
@ -887,7 +898,7 @@ if __name__ == "__main__":
systemSounds, sounds = ttsCz()
else:
print "which language?"
print("which language?")
exit()
if "csv" in sys.argv: