<commit_before>import json
import logging

from binstar_client import errors
from tornado import web
from notebook.utils import url_path_join
from notebook.base.handlers import APIHandler
from uploader import Uploader, AccountManager

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class WhoAmIHandler(APIHandler):
    _am = None

    @web.authenticated
    def get(self, **args):
        if self.am.is_logged_in():
            self.finish(json.dumps({
                'user': self.am.user,
                'organizations': self.am.organizations
            }))
        else:
            self.set_status(401)

    @web.authenticated
    def post(self, **args):
        json_body = json.loads(self.request.body)
        try:
            self.am.login(json_body['username'], json_body['password'])
        except errors.Unauthorized:
            self.set_status(401)
        except errors.BinstarError as e:
            self.set_status(400, e)

    @property
    def am(self):
        if self._am is None:
            self._am = AccountManager()
        return self._am


class PublishHandler(APIHandler):
    @web.authenticated
    def post(self, **args):
        json_body = json.loads(self.request.body)
        uploader = Uploader(
            json_body['name'],
            json_body['content'],
            user=json_body.get('user', None),
            public=json_body.get('public', True)
        )
        try:
            self.finish(json.dumps(uploader.upload()))
        except errors.Unauthorized:
            self.set_status(401, "You must login first.")
        except errors.BinstarError as e:
            self.log.error(e)
            self.set_status(400, str(e))


def load_jupyter_server_extension(nb_app):
    """Load the nb anaconda client extension"""
    webapp = nb_app.web_app<commit_msg>Upload to a specific org<commit_after>import json
import logging

from binstar_client import errors
from tornado import web
from notebook.utils import url_path_join
from notebook.base.handlers import APIHandler
from uploader import Uploader, AccountManager

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class WhoAmIHandler(APIHandler):
    _am = None

    @web.authenticated
    def get(self, **args):
        if self.am.is_logged_in():
            self.finish(json.dumps({
                'user': self.am.user,
                'organizations': self.am.organizations
            }))
        else:
            self.set_status(401)

    @web.authenticated
    def post(self, **args):
        json_body = json.loads(self.request.body)
        try:
            self.am.login(json_body['username'], json_body['password'])
        except errors.Unauthorized:
            self.set_status(401)
        except errors.BinstarError as e:
            self.set_status(400, e)

    @property
    def am(self):
        if self._am is None:
            self._am = AccountManager()
        return self._am


class PublishHandler(APIHandler):
    @web.authenticated
    def post(self, **args):
        json_body = json.loads(self.request.body)
        uploader = Uploader(
            json_body['name'],
            json_body['content'],
            user=json_body.get('organization', None),
            public=json_body.get('public', True)
        )
        try:
            self.finish(json.dumps(uploader.upload()))
        except errors.Unauthorized:
            self.set_status(401, "You must login first.")
        except errors.BinstarError as e:
            self.log.error(e)
            self.set_status(400, str(e))


def load_jupyter_server_extension(nb_app):
    """Load the nb anaconda client extension"""
    webapp = nb_app.web_app
{'fraction_non_alphanumeric': 0.104546692077321, 'fraction_numerical': 0.008167710318540702, 'mean_word_length': 2.330915684496827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1355226', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1084, 'n_words': 235}