show error box when can't bind socket

This commit is contained in:
Alex Yatskov 2017-02-19 12:46:40 -08:00
parent f1848dfbbb
commit e69484b996

View File

@ -48,17 +48,60 @@ except ImportError:
web = request web = request
try: try:
import PyQt4 as PyQt from PyQt4.QtCore import QTimer
from PyQt4.QtGui import QMessageBox
except ImportError: except ImportError:
import PyQt5 as PyQt from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMessageBox
try: try:
unicode unicode
except: except:
unicode = str unicode = str
makeBytes = lambda data: data.encode('utf-8')
makeStr = lambda data: data.decode('utf-8') #
# Helpers
#
def makeBytes(data):
return data.encode('utf-8')
def makeStr(data):
return data.decode('utf-8')
def audioDownload(url):
try:
resp = web.urlopen(url, timeout=URL_TIMEOUT)
except web.URLError:
return None
if resp.code != 200:
return None
return resp.read()
def audioInject(note, fields, filename):
for field in fields:
if field in note:
note[field] += u'[sound:{}]'.format(filename)
def verifyString(string):
t = type(string)
return t == str or t == unicode
def verifyStringList(strings):
for s in strings:
if not verifyString(s):
return False
return True
# #
@ -224,41 +267,6 @@ class AjaxServer:
self.clients = [] self.clients = []
#
# Helpers
#
def audioDownload(url):
try:
resp = web.urlopen(url, timeout=URL_TIMEOUT)
except web.URLError:
return None
if resp.code != 200:
return None
return resp.read()
def audioInject(note, fields, filename):
for field in fields:
if field in note:
note[field] += u'[sound:{}]'.format(filename)
def verifyString(string):
t = type(string)
return t == str or t == unicode
def verifyStringList(strings):
for s in strings:
if not verifyString(s):
return False
return True
# #
# AnkiNoteParams # AnkiNoteParams
# #
@ -416,12 +424,19 @@ class AnkiConnect:
def __init__(self): def __init__(self):
self.anki = AnkiBridge() self.anki = AnkiBridge()
self.server = AjaxServer(self.handler) self.server = AjaxServer(self.handler)
self.server.listen()
self.timer = PyQt.QtCore.QTimer() try:
self.timer.timeout.connect(self.advance) self.server.listen()
self.timer.start(TICK_INTERVAL)
self.timer = QTimer()
self.timer.timeout.connect(self.advance)
self.timer.start(TICK_INTERVAL)
except:
QMessageBox.critical(
self.anki.window(),
'AnkiConnect',
'Failed to listen on port {}...\nMake sure it is available and is not in use.'.format(NET_PORT)
)
def advance(self): def advance(self):
self.server.advance() self.server.advance()