forked from sgricci/LiveReload-sublimetext2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiveReload.py
155 lines (118 loc) · 4.31 KB
/
LiveReload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sublime
import os
import sys
import threading
import atexit
import time
try:
# Python 3
from .server.WebSocketServer import WebSocketServer
from .server.SimpleResourceServer import SimpleResourceServer
from .server.SimpleCallbackServer import SimpleCallbackServer
from .server.SimpleWSServer import SimpleWSServer
from .server.LiveReloadAPI import LiveReloadAPI
from .server.PluginAPI import PluginInterface as Plugin
from .server.Settings import Settings
except ValueError:
# Python 2
from server.WebSocketServer import WebSocketServer
from server.SimpleResourceServer import SimpleResourceServer
from server.SimpleCallbackServer import SimpleCallbackServer
from server.SimpleWSServer import SimpleWSServer
from server.LiveReloadAPI import LiveReloadAPI
from server.PluginAPI import PluginInterface as Plugin
from server.Settings import Settings
def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance
class LiveReload(threading.Thread, SimpleCallbackServer,
SimpleWSServer, SimpleResourceServer, LiveReloadAPI):
"""
Start the LiveReload, which exposes public api.
"""
def __init__(self):
threading.Thread.__init__(self)
SimpleCallbackServer.__init__(self)
SimpleWSServer.__init__(self)
SimpleResourceServer.__init__(self)
LiveReloadAPI.__init__(self)
def run(self):
"""
Start LiveReload
"""
path = os.path.join(sublime.packages_path(), 'LiveReload', 'web'
, 'dist', 'livereloadjs-sm2.js')
local = open(path, 'rU')
self.add_static_file('/livereload.js', local.read(),
'text/javascript')
settings = Settings()
self.port = settings.get('port', 35729)
self.version = settings.get('version', '2.0')
try:
self.start_server(self.port)
except Exception:
sublime.error_message('Port(' + str(self.port)
+ ') is already using, trying ('
+ str(self.port + 1) + ')')
time.sleep(3)
self.start_server(self.port + 1)
def start_server(self, port):
"""
Start the server.
"""
self.ws_server = WebSocketServer(port, self.version)
self.ws_server.start()
@atexit.register
def clean(self):
"""
Stop the server.
"""
self.ws_server.stop()
if not sublime.platform is 'build':
try:
sys.modules['LiveReload'].API
except Exception:
API = LiveReload()
API.start()
def http_callback(callback_f):
"""
Add http callback to plugin defined function. For example request to GET /callback/plugin_name/log_me
would trigger log_me function in plugin
Example:
::
@LiveReload.http_callback
def compiled(self, req):
print req # urlparse object
return "cool" #to http client
"""
callback_f.path = 'http://localhost:35729/callback/%s/%s' \
% (callback_f.__module__.lower(), callback_f.__name__)
sys.modules['LiveReload'
].API.callbacks.append({'path': callback_f.path,
'name': callback_f.__name__, 'cls': callback_f.__module__})
return callback_f
def websocket_callback(callback_f):
"""
Add websocket callback to plugin defined function. For example on function call in browser
LiveReload.SM2.plugin_name.definedfunction(data) would trigger definedfunction function in plugin or vice verse.
Shortly you can call client functions from the server and server functions from client. Everything is JSON encoded
by default.
Example:
::
@LiveReload.websocket_callback
def compiled(self, json):
print json # json object
return "cool" #to http client {msg: "cool"}
"""
callback_f.path = 'SM2.%s.%s' % (callback_f.__module__.lower(),
callback_f.__name__)
sys.modules['LiveReload'
].API.ws_callbacks.append({'path': callback_f.path,
'name': callback_f.__name__, 'cls': callback_f.__module__})
return callback_f