<commit_before>from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.security import unauthenticated_userid
import logging

log = logging.getLogger(__name__)

import json
from urllib.parse import urlparse
from pymongo import MongoClient
from pyramid.config import Configurator


def main(global_config, **settings):
    import os
    if 'mongo_uri' in settings:
        db_url = urlparse(settings['mongo_uri'])
    else:
        db_url = urlparse(os.getenv("WEBCAN_MONGO_URI"))
        settings['ldap_server'] = os.getenv('LDAP_SERVER')
        settings['ldap_suffix'] = os.getenv('LDAP_USERNAME_SUFFIX')
        settings['auth_ticket_key'] = os.getenv('AUTH_TICKET_KEY')
        settings['smtp_domain'] = os.getenv('SMTP_DOMAIN')
        settings['smtp_from'] = os.getenv('SMTP_FROM')
        settings['gmaps_api_key'] = os.getenv('GMAPS_API_KEY')

    config = Configurator(settings=settings)
    config.include('pyramid_mako')
    log.debug('Settings are: \n{}'.format(json.dumps(settings, indent=4)))

    def add_db(request):
        conn = MongoClient(db_url.geturl(),
                           serverSelectionTimeoutMS=10000,
                           connectTimeoutMS=10000,
                           socketTimeoutMS=10000,
                           maxPoolSize=200,
                           maxIdleTimeMs=30000,
                           appname='htm-site')
        db = conn[db_url.path[1:]]

        def conn_close(request):
            conn.close()

        request.add_finished_callback(conn_close)
        return db

    config.add_request_method(add_db, 'db', reify=True)

    def get_user(request):
        userid = unauthenticated_userid(request)
        if userid is not None:
            return request.db['scats_users'].find_one({'username': request.authenticated_userid})

    def auth_callback(uid, request):

        user = request.db['scats_users'].find_one({'username': uid})
        if user is not None:
            return ['auth']

    auth_policy = AuthTktAuthenticationPolicy(settings['auth_ticket_key'], callback=auth_callback, hashalg='sha512')
    config.set_authentication_policy(auth_policy)
    config.set_authorization_policy(ACLAuthorizationPolicy())
    config.add_request_method(get_user, 'user', reify=True)
    config.add_renderer('bson', 'htmsite.renderers.BSONRenderer')
    config.add_renderer('zip', 'htmsite.renderers.ZipRenderer')
    config.add_renderer('pymongo_cursor', 'htmsite.renderers.PymongoCursorRenderer')

    config.add_route('map', '/')<commit_msg>update unauthenticated_userid to use it from request<commit_after>from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.request import Request
import logging

log = logging.getLogger(__name__)

import json
from urllib.parse import urlparse
from pymongo import MongoClient
from pyramid.config import Configurator


def main(global_config, **settings):
    import os
    if 'mongo_uri' in settings:
        db_url = urlparse(settings['mongo_uri'])
    else:
        db_url = urlparse(os.getenv("WEBCAN_MONGO_URI"))
        settings['ldap_server'] = os.getenv('LDAP_SERVER')
        settings['ldap_suffix'] = os.getenv('LDAP_USERNAME_SUFFIX')
        settings['auth_ticket_key'] = os.getenv('AUTH_TICKET_KEY')
        settings['smtp_domain'] = os.getenv('SMTP_DOMAIN')
        settings['smtp_from'] = os.getenv('SMTP_FROM')
        settings['gmaps_api_key'] = os.getenv('GMAPS_API_KEY')

    config = Configurator(settings=settings)
    config.include('pyramid_mako')
    log.debug('Settings are: \n{}'.format(json.dumps(settings, indent=4)))

    def add_db(request):
        conn = MongoClient(db_url.geturl(),
                           serverSelectionTimeoutMS=10000,
                           connectTimeoutMS=10000,
                           socketTimeoutMS=10000,
                           maxPoolSize=200,
                           maxIdleTimeMs=30000,
                           appname='htm-site')
        db = conn[db_url.path[1:]]

        def conn_close(request):
            conn.close()

        request.add_finished_callback(conn_close)
        return db

    config.add_request_method(add_db, 'db', reify=True)

    def get_user(request: Request):
        userid = request.unauthenticated_userid
        if userid is not None:
            return request.db['scats_users'].find_one({'username': request.authenticated_userid})

    def auth_callback(uid, request):

        user = request.db['scats_users'].find_one({'username': uid})
        if user is not None:
            return ['auth']

    auth_policy = AuthTktAuthenticationPolicy(settings['auth_ticket_key'], callback=auth_callback, hashalg='sha512')
    config.set_authentication_policy(auth_policy)
    config.set_authorization_policy(ACLAuthorizationPolicy())
    config.add_request_method(get_user, 'user', reify=True)
    config.add_renderer('bson', 'htmsite.renderers.BSONRenderer')
    config.add_renderer('zip', 'htmsite.renderers.ZipRenderer')
    config.add_renderer('pymongo_cursor', 'htmsite.renderers.PymongoCursorRenderer')

    config.add_route('map', '/')
{'fraction_non_alphanumeric': 0.10844989461582678, 'fraction_numerical': 0.010730024908986396, 'mean_word_length': 3.2962962962962963, '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': '4638121', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1561, 'n_words': 257}