Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
Endpoint support (#36)
Browse files Browse the repository at this point in the history
* Add endpoint support.
  • Loading branch information
huxuan committed May 26, 2017
1 parent 285ab78 commit b5de7cc
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ ENV/
*.swp
config.py
Subscription.txt
Endpoint.txt
35 changes: 35 additions & 0 deletions sample/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
MAX_THUMBNAIL_SIZE = 75
STYLE = wx.SIMPLE_BORDER
SUBSCRIPTION_KEY_FILENAME = 'Subscription.txt'
ENDPOINT_FILENAME = 'Endpoint.txt'
ORIENTATION_TAG = 274

LOG_FACE_LIST_REQUEST = (
Expand Down Expand Up @@ -78,6 +79,40 @@ def delete(cls):
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()
Expand Down
1 change: 0 additions & 1 deletion sample/view/panel_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ def async_detect(self, path):
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()
Expand Down
70 changes: 47 additions & 23 deletions sample/view/panel_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def __init__(self, parent):
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.'
)
Expand All @@ -38,48 +39,71 @@ def __init__(self, parent):
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)

0 comments on commit b5de7cc

Please sign in to comment.