Commits

Shinya Okano  committed bd5ca18

refs #6 pep8, get_skype

  • Participants
  • Parent commits 7a992c0
  • Branches t6

Comments (0)

Files changed (8)

File skypehub/admin.py

 
 from skypehub.models import Message
 
+
 class MessageModelAdmin(admin.ModelAdmin):
     list_display = ('sender', 'chat_name', 'body', 'ctime')
     list_filter = ('chat_name', 'sender', 'ctime')

File skypehub/decorators.py

 
 from skypehub.utils import get_skype, get_skype_hook_options
 
+
 def skype_required(view):
     def _inner(request, *args, **kwargs):
         hook_options = get_skype_hook_options()

File skypehub/forms.py

+# coding: utf-8
 from django import forms
 
+
 class PostMessageForm(forms.Form):
     message = forms.CharField()
     chat = forms.CharField()
 
+
 class PostUserMessageForm(forms.Form):
     message = forms.CharField()
     username = forms.CharField()

File skypehub/handlers.py

 from skypehub.models import Message
 from skypehub.utils import is_windows
 
+
 def message_logging_receiver(handler, message, status):
     Message.objects.create(
         body=message.Body,
         chat_name=message.Chat.Name,
     )
 
+
 class OnMessageHandler(object):
     """
     Skype OnMessage event handler
     default_receivers = ()
 
     def __init__(self, skype=None):
-        self.receivers = defaultdict(lambda: set()) 
+        self.receivers = defaultdict(lambda: set())
         for status in self.default_statuses:
             for receiver in self.default_receivers:
                 self.receivers[status].add(receiver)
         for receiver in self.receivers[status]:
             receiver(self, message, status)
 
+
 class OnTimeHandler(object):
     """
     Skype OnTime event handler

File skypehub/models.py

 # coding: utf-8
 from django.db import models
 
+
 class Message(models.Model):
     body = models.TextField(u'本文')
     sender = models.CharField(u'送信者', max_length=200, db_index=True)

File skypehub/urls.py

-from django.conf.urls.defaults import *
+from django.conf.urls.defaults import url, patterns
 
-urlpatterns = patterns('skypehub.views',
+
+urlpatterns = patterns(
+    'skypehub.views',
     url(r'^list_chats$', 'list_chats', name='skype_list_chats'),
     url(r'^post_message', 'post_message', name='skype_post_message'),
 )

File skypehub/utils.py

-#:coding=utf-8:
+# coding: utf-8
 
-import Skype4Py
 from Skype4Py.errors import SkypeError
 
+
 SKYPE_HOOK_OPTIONS = {
     'Transport': 'x11',
 }
 
+_skype = None
 
-def get_skype(**kwargs):
+
+def get_skype(force_create=False, **kwargs):
     """return skype object.
     """
-    options = kwargs or {}
-    return Skype4Py.Skype(**options)
+    global skype
+    from Skype4Py import Skype
+    options = get_skype_hook_options()
+    options.update(kwargs)
+    if _skype is None or force_create:
+        _skype = Skype(**options)
+    return _skype
 
 
 def get_skype_hook_options():
     """
     import platform
     return platform.system() == 'Windows'
-
-skype = get_skype(**get_skype_hook_options())

File skypehub/views.py

-#:coding=utf-8:
+# coding: utf-8
 
 from django.utils import simplejson as json
 from django.http import HttpResponse
 
-from skypehub.utils import skype, SkypeError
+from skypehub.utils import get_skype, SkypeError
 from skypehub.forms import PostMessageForm, PostUserMessageForm
 
 
 def make_json_response(content, status=200):
-    return HttpResponse(json.dumps(content, indent=2), content_type='application/javascript', status=status)
+    return HttpResponse(
+        json.dumps(content, indent=2),
+        content_type='application/javascript', status=status)
 
 
 def list_chats(request):
+    skype = get_skype()
     chats = []
     for chat in skype.Chats:
         chats.append({
         })
     return make_json_response({'chats': chats})
 
+
 def post_message(request):
     form = PostMessageForm(request.POST or None)
     try:
+        skype = get_skype()
         if form.is_valid():
             chat = skype.Chat(form.cleaned_data['chat'])
             if hasattr(chat, 'AsyncSendMessage'):
             form = PostUserMessageForm(request.POST or None)
             if form.is_valid():
                 if hasattr(skype, 'AsyncSendMessage'):
-                    skype.AsyncSendMessage(form.cleaned_data['username'], form.cleaned_data['message'])
+                    skype.AsyncSendMessage(
+                        form.cleaned_data['username'],
+                        form.cleaned_data['message'])
                 else:
-                    skype.SendMessage(form.cleaned_data['username'], form.cleaned_data['message'])
+                    skype.SendMessage(
+                        form.cleaned_data['username'],
+                        form.cleaned_data['message'])
                 return make_json_response({'result': 'ok'})
     except SkypeError, e:
         # Invalid chat name (errno=105)
             return make_json_response({'result': e.args[1]}, status=403)
         else:
             raise e
-    return  make_json_response({'result': 'validation error.'})
+    return make_json_response({'result': 'validation error.'})