<commit_before>"""JupyterHub authenticator."""
import requests
import os
import json
from subprocess import check_output
from flask import request, redirect, abort
from IPython.utils.traitlets import Unicode, Int, List, Bool 

from nbgrader.html.formgrade import blueprint
from .base import BaseAuth


class HubAuth(BaseAuth):
    """Jupyter hub authenticator."""

    graders = List([], config=True, help="List of JupyterHub user names allowed to grade.")

    proxy_address = Unicode(config=True, help="Address of the configurable-http-proxy server.")
    def _proxy_address_default(self):
        return self._ip
    proxy_port = Int(8001, config=True, help="Port of the configurable-http-proxy server.")

    hub_address = Unicode(config=True, help="Address of the hub server.")
    def _hub_address_default(self):
        return self._ip
    hub_port = Int(8000, config=True, help="Port of the hub server.")
    
    hubapi_address = Unicode(config=True, help="Address of the hubapi server.")
    def _hubapi_address_default(self):
        return self._ip
    hubapi_port = Int(8081, config=True, help="Port of the hubapi server.")
    hubapi_cookie = Unicode("jupyter-hub-token", config=True, help="Name of the cookie used by JupyterHub")

    notebook_url_prefix = Unicode(None, config=True, allow_none=True, help="""
        Relative path of the formgrader with respect to the hub's user base
        directory.  No trailing slash. i.e. "Documents" or "Documents/notebooks". """)
    
    hub_base_url = Unicode(config=True, help="Base URL of the hub server.")
    def _hub_base_url_default(self):
        return 'http://{}:{}'.format(self.hub_address, self.hub_port)
    
    generate_hubapi_token = Bool(False, config=True, help="""Use `jupyterhub token` as a default
        for HubAuth.hubapi_token instead of $JPY_API_TOKEN.""")

    hubapi_token = Unicode(config=True, help="""JupyterHub API auth token.  
        Generated by running `jupyterhub token`.  If not explicitly set,
        nbgrader will use $JPY_API_TOKEN as the API token.""")
    def _hubapi_token_default(self):
        if self.generate_hubapi_token:
            return check_output(['jupyterhub', 'token']).decode('utf-8').strip()
        else:
            return os.environ.get('JPY_API_TOKEN', '')

    proxy_token = Unicode(config=True, help="""JupyterHub configurable proxy 
        auth token.  If not explicitly set, nbgrader will use 
        $CONFIGPROXY_AUTH_TOKEN as the API token.""")
    def _proxy_token_default(self):<commit_msg>Allow jhub database to be specified.<commit_after>"""JupyterHub authenticator."""
import requests
import os
import json
from subprocess import check_output
from flask import request, redirect, abort
from IPython.utils.traitlets import Unicode, Int, List, Bool 

from nbgrader.html.formgrade import blueprint
from .base import BaseAuth


class HubAuth(BaseAuth):
    """Jupyter hub authenticator."""

    graders = List([], config=True, help="List of JupyterHub user names allowed to grade.")

    proxy_address = Unicode(config=True, help="Address of the configurable-http-proxy server.")
    def _proxy_address_default(self):
        return self._ip
    proxy_port = Int(8001, config=True, help="Port of the configurable-http-proxy server.")

    hub_address = Unicode(config=True, help="Address of the hub server.")
    def _hub_address_default(self):
        return self._ip
    hub_port = Int(8000, config=True, help="Port of the hub server.")
    
    hubapi_address = Unicode(config=True, help="Address of the hubapi server.")
    def _hubapi_address_default(self):
        return self._ip
    hubapi_port = Int(8081, config=True, help="Port of the hubapi server.")
    hubapi_cookie = Unicode("jupyter-hub-token", config=True, help="Name of the cookie used by JupyterHub")

    notebook_url_prefix = Unicode(None, config=True, allow_none=True, help="""
        Relative path of the formgrader with respect to the hub's user base
        directory.  No trailing slash. i.e. "Documents" or "Documents/notebooks". """)
    
    hub_base_url = Unicode(config=True, help="Base URL of the hub server.")
    def _hub_base_url_default(self):
        return 'http://{}:{}'.format(self.hub_address, self.hub_port)
    
    generate_hubapi_token = Bool(False, config=True, help="""Use `jupyterhub token` as a default
        for HubAuth.hubapi_token instead of $JPY_API_TOKEN.""")

    hub_db = Unicode(config=True, help="""Path to JupyterHub's database.  Only
        manditory if `generate_hubapi_token` is True.""")

    hubapi_token = Unicode(config=True, help="""JupyterHub API auth token.  
        Generated by running `jupyterhub token`.  If not explicitly set,
        nbgrader will use $JPY_API_TOKEN as the API token.""")
    def _hubapi_token_default(self):
        if self.generate_hubapi_token:
            return check_output([
                'jupyterhub', 'token', '--db={}'.format(self.hub_db)
                ]).decode('utf-8').strip()
        else:
            return os.environ.get('JPY_API_TOKEN', '')

    proxy_token = Unicode(config=True, help="""JupyterHub configurable proxy 
        auth token.  If not explicitly set, nbgrader will use 
        $CONFIGPROXY_AUTH_TOKEN as the API token.""")
    def _proxy_token_default(self):
{'fraction_non_alphanumeric': 0.10841546626231995, 'fraction_numerical': 0.004927975739196361, 'mean_word_length': 3.6990204808548532, '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': '3592263', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1595, 'n_words': 463}