-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b0373eb
#5039: add autotranslation service interface and NamespaceClient refa…
jaroslawmalekcodete 3645f31
#5039: (groovy) add sync from js to server
jaroslawmalekcodete edc77e6
#5039: (scala) add support for autotranslation
jaroslawmalekcodete 5b2b5d1
#5039: add flask
jaroslawmalekcodete 6d72818
Merge branch 'master' into jarek/5039_autotranslation_v2
jaroslawmalekcodete 2790994
#5039: change flask to tornado
jaroslawmalekcodete 035e7c0
#5039: changes after review
jaroslawmalekcodete 9da9a2a
#5039: rename to BeakerClient
jaroslawmalekcodete 3f05a8f
#5039: rename to BeakerXClient
jaroslawmalekcodete 613658e
#5039: rename to BeakerXClient
jaroslawmalekcodete 861c511
#5039: rename to BeakerClientManager
jaroslawmalekcodete b32e623
#5039: rename to BeakerXClientManager
jaroslawmalekcodete cd6956a
#5039: rename to getBeaker
jaroslawmalekcodete b877fd7
#5039: rename to getBeakerX
jaroslawmalekcodete 5ed5e5f
#5039: rename to beakerX
jaroslawmalekcodete 2cd4876
#5039: BeakerXClinetManager refactoring
jaroslawmalekcodete 573550d
Merge branch 'master' into jarek/5039_autotranslation_v2
jaroslawmalekcodete 0575135
Merge branch 'master' into jarek/5039_autotranslation_v2
jaroslawmalekcodete 530aee6
Merge branch 'master' into jarek/5039_autotranslation_v2
jaroslawmalekcodete 8726deb
Merge branch 'master' into jarek/5039_autotranslation_v2
jaroslawmalekcodete 48444b6
#7511: support structured data in scala kernel
jaroslawmalekcodete File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# 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 == "beakerx" and password == os.environ["BEAKERX_AUTOTRANSLATION_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_AUTOTRANSLATION_PASSWORD"] = random_string_generator() | ||
os.environ["BEAKERX_AUTOTRANSLATION_PORT"] = str(get_free_tcp_port()) | ||
|
||
|
||
def start_autotranslation_server(): | ||
init_env() | ||
app = make_app() | ||
app.listen(os.environ["BEAKERX_AUTOTRANSLATION_PORT"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
kernel/base/src/main/java/com/twosigma/beakerx/AutotranslationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
create this once and reuse it with each msg.
also, name the channel "beakerx.autotranslation".
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.
are all our channels named "beakerx.*"?
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.
nope, but I will change the names from
beaker.*
tobeakerx.*