Merge lp:~paulliu/qr-tools/python3 into lp:qr-tools

Proposed by Ying-Chun Liu
Status: Merged
Approved by: Ramiro Algozino
Approved revision: 33
Merged at revision: 33
Proposed branch: lp:~paulliu/qr-tools/python3
Merge into: lp:qr-tools
Diff against target: 404 lines (+92/-91)
2 files modified
qrtools.py (+12/-12)
qtqr.py (+80/-79)
To merge this branch: bzr merge lp:~paulliu/qr-tools/python3
Reviewer Review Type Date Requested Status
Ramiro Algozino Approve
Review via email: mp+372711@code.launchpad.net

Commit message

Porting to python3

Description of the change

Python2 is end of life. We have to port this project to python3 so that we can continue using it.

To post a comment you must log in.
Ramiro Algozino (algozino) wrote :

Lgtm. The packaging is pending yet though.

Thank you very much for your collaboration!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'qrtools.py'
2--- qrtools.py 2018-03-01 17:24:59 +0000
3+++ qrtools.py 2019-09-12 18:29:16 +0000
4@@ -1,4 +1,4 @@
5-#!/usr/bin/env python
6+#!/usr/bin/env python3
7
8 # Authors:
9 # David Green <david4dev@gmail.com>
10@@ -35,7 +35,7 @@
11 import Image
12 import re
13 from codecs import BOM_UTF8
14-from urlparse import urlparse, parse_qs
15+from urllib.parse import urlparse, parse_qs
16
17 def eprint(*args, **kwargs):
18 print(*args, file=sys.stderr, **kwargs)
19@@ -170,7 +170,7 @@
20 utf8_bytedata = None
21 try:
22 utf8_bytedata = self.data_to_string()
23- except QR.EncodeError, e:
24+ except QR.EncodeError as e:
25 eprint(repr(e))
26 return 1
27
28@@ -178,8 +178,8 @@
29 command = [
30 'qrencode',
31 '-o', self.filename,
32- '-s', unicode(self.pixel_size),
33- '-m', unicode(self.margin_size),
34+ '-s', str(self.pixel_size),
35+ '-m', str(self.margin_size),
36 '-l', self.level,
37 '-t', ext,
38 self.data_to_string()
39@@ -188,8 +188,8 @@
40 command = [
41 'qrencode',
42 '-o', self.filename,
43- '-s', unicode(self.pixel_size),
44- '-m', unicode(self.margin_size),
45+ '-s', str(self.pixel_size),
46+ '-m', str(self.margin_size),
47 '-l', self.level,
48 #'-t', ext,
49 self.data_to_string()
50@@ -222,7 +222,7 @@
51 # clean up
52 del(image)
53 #Assuming data is encoded in utf8
54- self.data = symbol.data.decode(u'utf-8')
55+ self.data = symbol.data
56 self.data_type = self.data_recognise()
57 return True
58 else:
59@@ -267,19 +267,19 @@
60 #FIXME: Probably a future bug in newer versions.
61 #We should at least check if the binary is available.
62 p = subprocess.Popen(['qrencode','-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
63- version_text = p.communicate()[1]
64- version = re.search('version\s([\d.]*)',version_text)
65+ version_text = p.communicate()[0]
66+ version = re.search('version\s([\d.]*)',str(version_text))
67 if version:
68 version_number = version.group(1)
69 else:
70 version_number = -1
71- #print "Using qrencode version:", version_number
72+ #print ("Using qrencode version:", version_number)
73 return version_number
74
75 def get_qrencode_types(self):
76 p = subprocess.Popen(['qrencode','-h'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
77 help_text = p.communicate()[1]
78- types_text = re.search('-t {([\w,]*)}', help_text)
79+ types_text = re.search('-t {([\w,]*)}', str(help_text))
80 if types_text:
81 types = types_text.group(1).split(',')
82 #print "The following format types have been found!:", types
83
84=== modified file 'qtqr.py'
85--- qtqr.py 2018-06-04 01:50:55 +0000
86+++ qtqr.py 2019-09-12 18:29:16 +0000
87@@ -1,4 +1,4 @@
88-#!/usr/bin/env python
89+#!/usr/bin/env python3
90 #-*- encoding: utf-8 -*-
91
92 """
93@@ -16,7 +16,7 @@
94 try:
95 # import pynotify
96 if not pynotify.init("QtQR"):
97- print "DEBUG: There was a problem initializing the pynotify module"
98+ print ("DEBUG: There was a problem initializing the pynotify module")
99 NOTIFY = True
100 except:
101 NOTIFY = False
102@@ -43,17 +43,17 @@
103
104 # Templates for creating QRCodes supported by qrtools
105 self.templates = {
106- "text": unicode(self.tr("Text")),
107- "url": unicode(self.tr("URL")),
108- "bookmark": unicode(self.tr("Bookmark")),
109- "emailmessage": unicode(self.tr("E-Mail")),
110- "telephone": unicode(self.tr("Telephone Number")),
111- "phonebook": unicode(self.tr("Contact Information (PhoneBook)")),
112- "sms": unicode(self.tr("SMS")),
113- "mms": unicode(self.tr("MMS")),
114- "geo": unicode(self.tr("Geolocalization")),
115- "wifi": unicode(self.tr("WiFi Network")),
116- "sepa": unicode(self.tr("SEPA Single Payment")),
117+ "text": str(self.tr("Text")),
118+ "url": str(self.tr("URL")),
119+ "bookmark": str(self.tr("Bookmark")),
120+ "emailmessage": str(self.tr("E-Mail")),
121+ "telephone": str(self.tr("Telephone Number")),
122+ "phonebook": str(self.tr("Contact Information (PhoneBook)")),
123+ "sms": str(self.tr("SMS")),
124+ "mms": str(self.tr("MMS")),
125+ "geo": str(self.tr("Geolocalization")),
126+ "wifi": str(self.tr("WiFi Network")),
127+ "sepa": str(self.tr("SEPA Single Payment")),
128 }
129 # With this we make the dict bidirectional
130 self.templates.update( dict((self.templates[k], k) for k in self.templates))
131@@ -440,7 +440,7 @@
132 self.smsBodyEdit.textChanged.connect(self.qrencode)
133 self.smsBodyEdit.textChanged.connect(
134 lambda: self.smsCharCount.setText(
135- unicode(self.tr("characters count: %s - %d message(s)")) % (
136+ str(self.tr("characters count: %s - %d message(s)")) % (
137 len(self.smsBodyEdit.toPlainText()),
138 ceil(len(self.smsBodyEdit.toPlainText()) / 160.0)
139 )
140@@ -508,35 +508,35 @@
141 fileName = kargs.get('fileName')
142 #Functions to get the correct data
143 data_fields = {
144- "text": unicode(self.textEdit.toPlainText()),
145- "url": unicode(self.urlEdit.text()),
146- "bookmark": ( unicode(self.bookmarkTitleEdit.text()), unicode(self.bookmarkUrlEdit.text()) ),
147- "email": unicode(self.emailEdit.text()),
148- "emailmessage": ( unicode(self.emailEdit.text()), unicode(self.emailSubjectEdit.text()), unicode(self.emailBodyEdit.toPlainText()) ),
149- "telephone": unicode(self.telephoneEdit.text()),
150- "phonebook": (('N',unicode(self.phonebookNameEdit.text())),
151- ('TEL', unicode(self.phonebookTelEdit.text())),
152- ('EMAIL',unicode(self.phonebookEMailEdit.text())),
153- ('NOTE', unicode(self.phonebookNoteEdit.text())),
154- ('BDAY', unicode(self.phonebookBirthdayEdit.date().toString("yyyyMMdd")) if self.phonebookBirthdayLabel.isChecked() else ""), #YYYYMMDD
155- ('ADR', unicode(self.phonebookAddressEdit.text())), #The fields divided by commas (,) denote PO box, room number, house number, city, prefecture, zip code and country, in order.
156- ('URL', unicode(self.phonebookUrlEdit.text())),
157+ "text": str(self.textEdit.toPlainText()),
158+ "url": str(self.urlEdit.text()),
159+ "bookmark": ( str(self.bookmarkTitleEdit.text()), str(self.bookmarkUrlEdit.text()) ),
160+ "email": str(self.emailEdit.text()),
161+ "emailmessage": ( str(self.emailEdit.text()), str(self.emailSubjectEdit.text()), str(self.emailBodyEdit.toPlainText()) ),
162+ "telephone": str(self.telephoneEdit.text()),
163+ "phonebook": (('N',str(self.phonebookNameEdit.text())),
164+ ('TEL', str(self.phonebookTelEdit.text())),
165+ ('EMAIL',str(self.phonebookEMailEdit.text())),
166+ ('NOTE', str(self.phonebookNoteEdit.text())),
167+ ('BDAY', str(self.phonebookBirthdayEdit.date().toString("yyyyMMdd")) if self.phonebookBirthdayLabel.isChecked() else ""), #YYYYMMDD
168+ ('ADR', str(self.phonebookAddressEdit.text())), #The fields divided by commas (,) denote PO box, room number, house number, city, prefecture, zip code and country, in order.
169+ ('URL', str(self.phonebookUrlEdit.text())),
170 # ('NICKNAME', ''),
171 ),
172- "sms": ( unicode(self.smsNumberEdit.text()), unicode(self.smsBodyEdit.toPlainText()) ),
173- "mms": ( unicode(self.mmsNumberEdit.text()), unicode(self.mmsBodyEdit.toPlainText()) ),
174- "geo": ( unicode(self.geoLatEdit.text()), unicode(self.geoLongEdit.text()) ),
175- "wifi": ( unicode(self.wifiSSIDEdit.text()), (u"WEP",u"WPA",u"nopass")[self.wifiEncryptionType.currentIndex()], unicode(self.wifiPasswordEdit.text())),
176- "sepa": (unicode(self.sepaNameEdit.text()),
177- unicode(self.sepaAccountEdit.text()),
178- unicode(self.sepaBNCEdit.text()),
179- unicode(self.sepaAmountEdit.text()),
180- unicode(self.sepaReasonEdit.text()),
181- unicode(self.sepaCurrencyEdit.text())
182+ "sms": ( str(self.smsNumberEdit.text()), str(self.smsBodyEdit.toPlainText()) ),
183+ "mms": ( str(self.mmsNumberEdit.text()), str(self.mmsBodyEdit.toPlainText()) ),
184+ "geo": ( str(self.geoLatEdit.text()), str(self.geoLongEdit.text()) ),
185+ "wifi": ( str(self.wifiSSIDEdit.text()), (u"WEP",u"WPA",u"nopass")[self.wifiEncryptionType.currentIndex()], str(self.wifiPasswordEdit.text())),
186+ "sepa": (str(self.sepaNameEdit.text()),
187+ str(self.sepaAccountEdit.text()),
188+ str(self.sepaBNCEdit.text()),
189+ str(self.sepaAmountEdit.text()),
190+ str(self.sepaReasonEdit.text()),
191+ str(self.sepaCurrencyEdit.text())
192 ),
193 }
194
195- data_type = unicode(self.templates[unicode(self.selector.currentText())])
196+ data_type = str(self.templates[str(self.selector.currentText())])
197 data = data_fields[data_type]
198
199 level = (u'L',u'M',u'Q',u'H')
200@@ -545,14 +545,14 @@
201 if data_type == 'emailmessage' and data[1] == '' and data[2] == '':
202 data_type = 'email'
203 data = data_fields[data_type]
204- qr = QR(pixel_size = unicode(self.pixelSize.value()),
205+ qr = QR(pixel_size = str(self.pixelSize.value()),
206 data = data,
207- level = unicode(level[self.ecLevel.currentIndex()]),
208- margin_size = unicode(self.marginSize.value()),
209+ level = str(level[self.ecLevel.currentIndex()]),
210+ margin_size = str(self.marginSize.value()),
211 data_type = data_type,
212 )
213 error = 1
214- if type(fileName) is not unicode:
215+ if type(fileName) is not str:
216 error = qr.encode()
217 else:
218 error = qr.encode(fileName)
219@@ -563,12 +563,12 @@
220 if NOTIFY:
221 n = pynotify.Notification(
222 "QtQR",
223- unicode(self.tr("ERROR: Something went wrong while trying to generate the QR Code.")),
224+ str(self.tr("ERROR: Something went wrong while trying to generate the QR Code.")),
225 "qtqr"
226 )
227 n.show()
228 else:
229- print "Something went wrong while trying to generate the QR Code"
230+ print ("Something went wrong while trying to generate the QR Code")
231 qr.destroy()
232 else:
233 self.saveButton.setEnabled(False)
234@@ -596,19 +596,19 @@
235 and not fn.lower().endswith(u".utf8") \
236 and not fn.lower().endswith(u".ansiutf8"):
237 fn += u".png"
238- self.qrencode(fileName=unicode(fn))
239+ self.qrencode(fileName=str(fn))
240 if NOTIFY:
241 n = pynotify.Notification(
242- unicode(self.tr("Save QR Code")),
243- unicode(self.tr("QR Code succesfully saved to %s")) % fn,
244+ str(self.tr("Save QR Code")),
245+ str(self.tr("QR Code succesfully saved to %s")) % fn,
246 "qtqr"
247 )
248 n.show()
249 else:
250 QtWidgets.QMessageBox.information(
251 self,
252- unicode(self.tr('Save QRCode')),
253- unicode(self.tr('QRCode succesfully saved to <b>%s</b>.')) % fn
254+ str(self.tr('Save QRCode')),
255+ str(self.tr('QRCode succesfully saved to <b>%s</b>.')) % fn
256 )
257
258 def decodeFile(self, fn=None):
259@@ -619,7 +619,7 @@
260 filter=self.tr('Images (*.png *.jpg);; All Files (*.*)')
261 )
262 if fn:
263- fn = unicode(fn)
264+ fn = str(fn)
265 else:
266 return
267 if os.path.isfile(fn):
268@@ -638,7 +638,7 @@
269 QtWidgets.QMessageBox.information(
270 self,
271 self.tr('Decode File'),
272- unicode(self.tr('No QRCode could be found in file: <b>%s</b>.')) % fn
273+ str(self.tr('No QRCode could be found in file: <b>%s</b>.')) % fn
274 )
275 qr.destroy()
276 else:
277@@ -652,26 +652,26 @@
278
279 def showInfo(self, qr):
280 dt = qr.data_type
281- print dt.encode(u"utf-8") + ':',
282+ print (str(dt) + ':', end='')
283 data = qr.data_decode[dt](qr.data)
284 if type(data) == tuple:
285 for d in data:
286- print d.encode(u"utf-8")
287+ print (d.encode(u"utf-8"))
288 elif type(data) == dict:
289 # FIX-ME: Print the decoded symbols
290- print "Dict"
291- print data.keys()
292- print data.values()
293+ print ("Dict")
294+ print (data.keys())
295+ print (data.values())
296 else:
297- print data.encode(u"utf-8")
298+ print (data.encode(u"utf-8"))
299 msg = {
300- 'text': lambda : unicode(self.tr("QRCode contains the following text:\n\n%s")) % (data),
301- 'url': lambda : unicode(self.tr("QRCode contains the following url address:\n\n%s")) % (data),
302- 'bookmark': lambda: unicode(self.tr("QRCode contains a bookmark:\n\nTitle: %s\nURL: %s")) % (data),
303- 'email': lambda : unicode(self.tr("QRCode contains the following e-mail address:\n\n%s")) % (data),
304- 'emailmessage': lambda : unicode(self.tr("QRCode contains an e-mail message:\n\nTo: %s\nSubject: %s\nMessage: %s")) % (data),
305- 'telephone': lambda : unicode(self.tr("QRCode contains a telephone number: ")) + (data),
306- 'phonebook': lambda : unicode(self.tr("QRCode contains a phonebook entry:\n\nName: %s\nTel: %s\nE-Mail: %s\nNote: %s\nBirthday: %s\nAddress: %s\nURL: %s")) %
307+ 'text': lambda : str(self.tr("QRCode contains the following text:\n\n%s")) % (data),
308+ 'url': lambda : str(self.tr("QRCode contains the following url address:\n\n%s")) % (data),
309+ 'bookmark': lambda: str(self.tr("QRCode contains a bookmark:\n\nTitle: %s\nURL: %s")) % (data),
310+ 'email': lambda : str(self.tr("QRCode contains the following e-mail address:\n\n%s")) % (data),
311+ 'emailmessage': lambda : str(self.tr("QRCode contains an e-mail message:\n\nTo: %s\nSubject: %s\nMessage: %s")) % (data),
312+ 'telephone': lambda : str(self.tr("QRCode contains a telephone number: ")) + (data),
313+ 'phonebook': lambda : str(self.tr("QRCode contains a phonebook entry:\n\nName: %s\nTel: %s\nE-Mail: %s\nNote: %s\nBirthday: %s\nAddress: %s\nURL: %s")) %
314 (data.get('N') or "",
315 data.get('TEL') or "",
316 data.get('EMAIL') or "",
317@@ -679,11 +679,11 @@
318 QtCore.QDate.fromString(data.get('BDAY') or "",'yyyyMMdd').toString(),
319 data.get('ADR') or "",
320 data.get('URL') or ""),
321- 'sms': lambda : unicode(self.tr("QRCode contains the following SMS message:\n\nTo: %s\nMessage: %s")) % (data),
322- 'mms': lambda : unicode(self.tr("QRCode contains the following MMS message:\n\nTo: %s\nMessage: %s")) % (data),
323- 'geo': lambda : unicode(self.tr("QRCode contains the following coordinates:\n\nLatitude: %s\nLongitude:%s")) % (data),
324- 'wifi': lambda : unicode(self.tr("QRCode contains the following WiFi Network Configuration:\n\nSSID: %s\nEncryption Type: %s\nPassword: %s")) % (data),
325- 'sepa': lambda : unicode(self.tr("QRCode contains the following Single Payment Information:\n\nName: %s\nAccount: %s\nBNC: %s\nAmmount: %s\nReason: %s\nCurrency: %s\n")) % (
326+ 'sms': lambda : str(self.tr("QRCode contains the following SMS message:\n\nTo: %s\nMessage: %s")) % (data),
327+ 'mms': lambda : str(self.tr("QRCode contains the following MMS message:\n\nTo: %s\nMessage: %s")) % (data),
328+ 'geo': lambda : str(self.tr("QRCode contains the following coordinates:\n\nLatitude: %s\nLongitude:%s")) % (data),
329+ 'wifi': lambda : str(self.tr("QRCode contains the following WiFi Network Configuration:\n\nSSID: %s\nEncryption Type: %s\nPassword: %s")) % (data),
330+ 'sepa': lambda : str(self.tr("QRCode contains the following Single Payment Information:\n\nName: %s\nAccount: %s\nBNC: %s\nAmmount: %s\nReason: %s\nCurrency: %s\n")) % (
331 data.get('name')[0] or '',
332 data.get('account')[0] or '',
333 data.get('bnc')[0] or '',
334@@ -743,7 +743,7 @@
335 link = data[1]
336 else:
337 link = qr.data_decode[qr.data_type](qr.data)
338- print u"Opening " + link
339+ print (u"Opening " + link)
340 QtGui.QDesktopServices.openUrl(QtCore.QUrl(link))
341 elif rsp == 0:
342 #Edit the code
343@@ -813,6 +813,7 @@
344 self.tabs.setCurrentIndex(tabIndex)
345
346 def decodeWebcam(self):
347+ device = None
348 vdDialog = VideoDevices()
349 if len(vdDialog.videoDevices) == 1:
350 device = vdDialog.videoDevices[0][1]
351@@ -837,7 +838,7 @@
352 QtWidgets.QMessageBox.about(
353 self,
354 self.tr("About QtQR"),
355- unicode(self.tr('<h1>QtQR %s</h1><p>A simple software for creating and decoding QR Codes that uses python-qrtools as backend. Both are part of the <a href="https://launchpad.net/qr-tools">QR Tools</a> project.</p><p></p><p>This is Free Software: GNU-GPLv3</p><p></p><p>Please visit our website for more information and to check out the code:<br /><a href="https://code.launchpad.net/qr-tools/">https://code.launchpad.net/qr-tools/</p><p>copyright &copy; Ramiro Algozino &lt;<a href="mailto:algozino@gmail.com">algozino@gmail.com</a>&gt;</p>')) % __version__,
356+ str(self.tr('<h1>QtQR %s</h1><p>A simple software for creating and decoding QR Codes that uses python-qrtools as backend. Both are part of the <a href="https://launchpad.net/qr-tools">QR Tools</a> project.</p><p></p><p>This is Free Software: GNU-GPLv3</p><p></p><p>Please visit our website for more information and to check out the code:<br /><a href="https://code.launchpad.net/qr-tools/">https://code.launchpad.net/qr-tools/</p><p>copyright &copy; Ramiro Algozino &lt;<a href="mailto:algozino@gmail.com">algozino@gmail.com</a>&gt;</p>')) % __version__,
357 )
358
359 def dragEnterEvent(self, event):
360@@ -848,9 +849,9 @@
361 for url in event.mimeData().urls():
362 fn = url.toLocalFile()
363 if fn:
364- self.decodeFile(unicode(fn))
365+ self.decodeFile(str(fn))
366 else:
367- print "DEBUG: Downloading dropped file from %s" % url.toString()
368+ print ("DEBUG: Downloading dropped file from %s" % url.toString() )
369 self.NetAccessMgr.get(QtNetwork.QNetworkRequest(url))
370 # FIX-ME: We should check if the download gets timeout.
371 # and notify that we are downloading, the download could
372@@ -861,7 +862,7 @@
373
374
375 def handleNetworkData(self, QNetReply):
376- print "DEBUG: Finished downloading file."
377+ print ("DEBUG: Finished downloading file.")
378 tmpfn = '/tmp/qrtemp.png'
379 fn = open(tmpfn,"w")
380 fn.write(QNetReply.readAll())
381@@ -869,10 +870,10 @@
382 self.decodeFile(tmpfn)
383
384 def toggleShowPassword(self, status):
385- if status == 0:
386- self.wifiPasswordEdit.setEchoMode(QtWidgets.QLineEdit.Password)
387- elif status == 2:
388- self.wifiPasswordEdit.setEchoMode(QtWidgets.QLineEdit.Normal)
389+ if status == 0:
390+ self.wifiPasswordEdit.setEchoMode(QtWidgets.QLineEdit.Password)
391+ elif status == 2:
392+ self.wifiPasswordEdit.setEchoMode(QtWidgets.QLineEdit.Normal)
393
394 class VideoDevices(QtWidgets.QDialog):
395 def __init__(self):
396@@ -924,7 +925,7 @@
397 app = QtWidgets.QApplication(sys.argv)
398 # This is to make Qt use locale configuration; i.e. Standard Buttons
399 # in your system's language.
400- locale = unicode(QtCore.QLocale.system().name())
401+ locale = str(QtCore.QLocale.system().name())
402 translator=QtCore.QTranslator()
403 # translator.load(os.path.join(os.path.abspath(
404 # os.path.dirname(__file__)),

Subscribers

People subscribed via source and target branches