* Add endpoint support.
This commit is contained in:
Xuan Hu (Sean) 2017-05-26 19:11:17 +08:00 коммит произвёл GitHub
Родитель 285ab787da
Коммит b5de7ccb18
4 изменённых файлов: 83 добавлений и 24 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -92,3 +92,4 @@ ENV/
*.swp
config.py
Subscription.txt
Endpoint.txt

Просмотреть файл

@ -27,6 +27,7 @@ MAX_IMAGE_SIZE = 300
MAX_THUMBNAIL_SIZE = 75
STYLE = wx.SIMPLE_BORDER
SUBSCRIPTION_KEY_FILENAME = 'Subscription.txt'
ENDPOINT_FILENAME = 'Endpoint.txt'
ORIENTATION_TAG = 274
LOG_FACE_LIST_REQUEST = (
@ -78,6 +79,40 @@ class SubscriptionKey(object):
CF.Key.set(cls.key)
class Endpoint(object):
"""Endpoint."""
@classmethod
def get(cls):
"""Get the endpoint."""
if not hasattr(cls, 'endpoint'):
cls.endpoint = ''
if not cls.endpoint:
if os.path.isfile(ENDPOINT_FILENAME):
with file(ENDPOINT_FILENAME) as fin:
cls.endpoint = fin.read().strip()
else:
cls.endpoint = CF.BaseUrl.get()
CF.BaseUrl.set(cls.endpoint)
return cls.endpoint
@classmethod
def set(cls, endpoint):
"""Set the endpoint."""
cls.endpoint = endpoint
with file(ENDPOINT_FILENAME, 'w') as fout:
print >>fout, endpoint
CF.BaseUrl.set(cls.endpoint)
@classmethod
def delete(cls):
"""Delete the endpoint."""
cls.endpoint = ''
if os.path.isfile(ENDPOINT_FILENAME):
os.remove(ENDPOINT_FILENAME)
CF.BaseUrl.set(CF.util.DEFAULT_BASE_URL)
def scale_image(img, size=MAX_IMAGE_SIZE):
"""Scale the wx.Image."""
width = img.GetWidth()

Просмотреть файл

@ -116,7 +116,6 @@ class DetectionPanel(base.MyPanel):
self.result.SetLabelText(text)
except util.CF.CognitiveFaceException as exp:
self.log.log('Response: {}. {}'.format(exp.code, exp.msg))
return
self.btn.Enable()
self.rsizer.Layout()

Просмотреть файл

@ -22,7 +22,8 @@ class SubscriptionPanel(MyPanel):
label = (
'To use the service, make sure you have a valid '
'subscription key.\nNote that each service (Face, Emotion, '
'Speech, etc.) has its own subscription keys.\nYou can use '
'Speech, etc.) has its own subscription keys.\nAnd each '
'subscription key belongs to one specific endpoint.\nYou can use '
'the link below to get a key.\nWhen ready, paste your key '
'into the textbox below.'
)
@ -38,48 +39,71 @@ class SubscriptionPanel(MyPanel):
self.link.SetBackgroundColour(colour_window)
self.sizer.Add(self.link, flag=flag, border=5)
subsizer = wx.BoxSizer()
subgridsizer = wx.GridSizer(rows=2, cols=2)
flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL
self.label = wx.StaticText(self, label='Subscription Key : ')
subsizer.Add(self.label, flag=flag, border=5)
flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.FIXED_MINSIZE
label = 'Subscription Key : '
self.subscription_key_label = wx.StaticText(self, label=label)
subgridsizer.Add(self.subscription_key_label, flag=flag, border=5)
flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL
self.text = wx.TextCtrl(self, size=(-1, -1))
self.text.SetValue(util.SubscriptionKey.get().decode('utf-8'))
subsizer.Add(self.text, 1, flag=flag, border=5)
subscription_key = util.SubscriptionKey.get().decode('utf-8')
self.subscription_key_text = wx.TextCtrl(self, size=(-1, -1))
self.subscription_key_text.SetValue(subscription_key)
subgridsizer.Add(self.subscription_key_text, 1, flag=flag, border=5)
flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL
self.btn_save = wx.Button(self, label='Save Key')
flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.FIXED_MINSIZE
label = 'Endpoint : '
self.endpoint_label = wx.StaticText(self, label=label)
subgridsizer.Add(self.endpoint_label, flag=flag, border=5)
flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL
endpoint = util.Endpoint.get().decode('utf-8')
self.endpoint_text = wx.TextCtrl(self, size=(-1, -1))
self.endpoint_text.SetValue(endpoint)
subgridsizer.Add(self.endpoint_text, 1, flag=flag, border=5)
flag = wx.EXPAND | wx.TOP | wx.BOTTOM
self.sizer.Add(subgridsizer, flag=flag, border=5)
subsizer = wx.BoxSizer()
flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.FIXED_MINSIZE
self.btn_save = wx.Button(self, label='Save')
subsizer.Add(self.btn_save, flag=flag, border=5)
self.Bind(wx.EVT_BUTTON, self.OnSaveKey, self.btn_save)
self.Bind(wx.EVT_BUTTON, self.OnSave, self.btn_save)
flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL
self.btn_del = wx.Button(self, label='Delete Key')
self.btn_del = wx.Button(self, label='Delete')
subsizer.Add(self.btn_del, flag=flag, border=5)
self.Bind(wx.EVT_BUTTON, self.OnDeleteKey, self.btn_del)
self.Bind(wx.EVT_BUTTON, self.OnDelete, self.btn_del)
flag = wx.EXPAND | wx.TOP | wx.BOTTOM
self.sizer.Add(subsizer, flag=flag, border=5)
self.SetSizer(self.sizer)
def OnSaveKey(self, evt):
"""Save the key."""
util.SubscriptionKey.set(self.text.GetValue().encode('utf-8'))
def OnSave(self, evt):
"""Save the key and endpoint."""
util.SubscriptionKey.set(
self.subscription_key_text.GetValue().encode('utf-8'))
util.Endpoint.set(
self.endpoint_text.GetValue().encode('utf-8'))
text = (
'Subscription key successfully saved on your disk.\n'
'Settings successfully saved on your disk.\n'
'You do not need to paste the key next time.'
)
title = 'Subscription Key'
title = 'Settings'
style = wx.OK | wx.ICON_INFORMATION
wx.MessageBox(text, title, style)
def OnDeleteKey(self, evt):
"""Delete the key."""
self.text.Clear()
def OnDelete(self, evt):
"""Delete the key and endpoint."""
util.SubscriptionKey.delete()
text = 'Subscription key successfully deleted from your disk.'
title = 'Subscription Key'
util.Endpoint.delete()
self.subscription_key_text.Clear()
self.endpoint_text.SetValue(util.Endpoint.get())
text = 'Settings successfully deleted from your disk.'
title = 'Settings'
style = wx.OK | wx.ICON_INFORMATION
wx.MessageBox(text, title, style)