-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jarek/5039: autotranslation [WIP] #7542
Changes from 6 commits
b0373eb
3645f31
edc77e6
5b2b5d1
6d72818
2790994
035e7c0
9da9a2a
3f05a8f
613658e
861c511
b32e623
cd6956a
b877fd7
5ed5e5f
2cd4876
573550d
0575135
530aee6
8726deb
48444b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Copyright 2018 TWO SIGMA OPEN SOURCE, LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import tornado.ioloop | ||
import tornado.web | ||
import logging | ||
import os | ||
import random | ||
import string | ||
import socket | ||
|
||
import base64 | ||
|
||
beakerx = {} | ||
|
||
logging.getLogger('tornado.access').disabled = True | ||
|
||
|
||
def basic_auth(f): | ||
def auth(username, password): | ||
return username == os.environ["BEAKERX_BASIC_AUTH_USERNAME"] \ | ||
and password == os.environ["BEAKERX_BASIC_AUTH_PASSWORD"] | ||
|
||
def _request_auth(handler): | ||
handler.set_status(401) | ||
return handler.finish() | ||
|
||
def wrap(*args): | ||
handler = args[0] | ||
try: | ||
auth_header = handler.request.headers.get('Authorization') | ||
if (auth_header is None) or (not auth_header.startswith('Basic ')): | ||
return _request_auth(handler) | ||
auth_decoded = base64.b64decode(auth_header[6:]) | ||
username, password = auth_decoded.decode('UTF-8').split(':', 2) | ||
if auth(username, password): | ||
f(*args) | ||
else: | ||
_request_auth(handler) | ||
except: | ||
_request_auth(handler) | ||
|
||
return wrap | ||
|
||
|
||
class MainSaveHandler(tornado.web.RequestHandler): | ||
|
||
@basic_auth | ||
def post(self): | ||
input_json = tornado.escape.json_decode(self.request.body) | ||
session_id = input_json["sessionId"] | ||
name = input_json["name"] | ||
json = input_json["json"] | ||
if session_id not in beakerx: | ||
beakerx[session_id] = {} | ||
|
||
beakerx[session_id][name] = json | ||
return self.finish("ok") | ||
|
||
|
||
class MainGetHandler(tornado.web.RequestHandler): | ||
|
||
@basic_auth | ||
def get(self, session_id, name): | ||
if session_id in beakerx and name in beakerx[session_id]: | ||
return self.finish(beakerx[session_id][name]) | ||
return self.finish("undefined") | ||
|
||
|
||
def make_app(): | ||
return tornado.web.Application([ | ||
(r"/autotransltion/(.*)/(.*)", MainGetHandler), | ||
(r"/autotransltion/", MainSaveHandler), | ||
]) | ||
|
||
|
||
def get_free_tcp_port(): | ||
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
tcp.bind(('', 0)) | ||
addr, port = tcp.getsockname() | ||
tcp.close() | ||
return port | ||
|
||
|
||
def random_string_generator(size=128): | ||
s = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in | ||
range(size)) | ||
return s | ||
|
||
|
||
def init_env(): | ||
os.environ["BEAKERX_BASIC_AUTH_USERNAME"] = "beakerx" | ||
os.environ["BEAKERX_BASIC_AUTH_PASSWORD"] = random_string_generator() | ||
os.environ["BEAKERX_FLASK_SERVER_PORT"] = str(get_free_tcp_port()) | ||
|
||
|
||
def start_autotranslation_server(): | ||
init_env() | ||
app = make_app() | ||
app.listen(os.environ["BEAKERX_FLASK_SERVER_PORT"]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,17 +158,35 @@ define([ | |
|
||
// assign Beaker methods to window | ||
if (window) { | ||
if (!window.beakerx) { | ||
window.beakerx = {}; | ||
} | ||
|
||
var plotApiList = plotApi.list(); | ||
var bkApp = bkCoreManager.getBkApp(); | ||
var bkObject = bkApp.getBeakerObject(); | ||
|
||
_.extend(window.beakerx, plotApiList); | ||
_.extend(window.beakerx, htmlOutput); | ||
window.beakerx.prefs = bkObject.beakerObj.prefs; | ||
var beakerxInstance = {} | ||
_.extend(beakerxInstance, plotApiList); | ||
_.extend(beakerxInstance, htmlOutput); | ||
beakerxInstance.prefs = bkObject.beakerObj.prefs; | ||
|
||
if (!window.beakerx) { | ||
var handler = { | ||
get: function (obj, prop) { | ||
return prop in obj ? obj[prop] : undefined; | ||
}, | ||
|
||
set: function (obj, prop, value) { | ||
obj[prop] = value; | ||
var comm = Jupyter.notebook.kernel.comm_manager.new_comm('beaker.autotranslation', null, null, null, utils.uuid()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create this once and reuse it with each msg. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are all our channels named "beakerx.*"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, but I will change the names from |
||
var data = { | ||
}; | ||
data.name = prop; | ||
data.value = value; | ||
comm.send(data); | ||
comm.close(); | ||
return true; | ||
} | ||
}; | ||
window.beakerx = new Proxy(beakerxInstance, handler); | ||
} | ||
} | ||
|
||
if (inNotebook) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.twosigma.beakerx; | ||
|
||
public interface AutotranslationService { | ||
|
||
String update(String name, String json); | ||
|
||
String get(String name); | ||
|
||
String close(); | ||
|
||
String getContextAsString(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for BEAKERX_BASIC_AUTH_USERNAME just make it constant on the other side
BEAKERX_BASIC_AUTH_PASSWORD → BEAKERX_AUTOTRANSLATION_PASSWORD
BEAKERX_FLASK_SERVER_PORT → BEAKERX_AUTOTRANSLATION_PORT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done