<commit_before># -*- coding: utf-8 -*-
"""Getting and putting simulation db files

:copyright: Copyright (c) 2020 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern import pkio
from pykern.pkcollections import PKDict
import functools
import sirepo.job
import sirepo.tornado
import tornado.web

_KEY_TO_UID = PKDict()


# Must be defined before use
def _sim_db_file_req_validated(func):
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        t = self.request.headers.get(sirepo.job.AUTH_HEADER)
        if not t:
            sirepo.tornado.raise_unauthorized()
        p = t.split(' ')
        if len(p) != 2:
            sirepo.tornado.raise_unauthorized()
        if p[0] != sirepo.job.AUTH_HEADER_SCHEME_BEARER \
           or not sirepo.job.UNIQUE_KEY_RE.search(p[1]):
            sirepo.tornado.raise_unauthorized()
        if p[1] not in _KEY_TO_UID or \
           self.request.path.split('/')[3] != _KEY_TO_UID[p[1]]:
            sirepo.tornado.raise_forbidden()
        # TODO(e-carlin): discuss with rn what validation needs to happen
        p = self.request.path
        if p.count('.') > 1:
            sirepo.tornado.raise_not_found()
        return func(self, *args, **kwargs)
    return wrapper


class FileReq(tornado.web.RequestHandler):
    SUPPORTED_METHODS = ['GET','PUT']

    @_sim_db_file_req_validated
    def get(self, path):
        p = sirepo.srdb.root().join(path)
        if not p.exists():
            sirepo.tornado.raise_not_found()
        self.write(pkio.read_binary(p))

    @_sim_db_file_req_validated
    def put(self, path):
        sirepo.srdb.root().join(path).write_binary(self.request.body)


def get_key(uid):
    assert uid not in _KEY_TO_UID.values(), \
        f'existing key for uid={uid}'
    k = sirepo.job.unique_key()
    _KEY_TO_UID[k] = uid
    return k<commit_msg>there can be multiple drivers for one uid (ex 1 parallel and 1 sequential)<commit_after># -*- coding: utf-8 -*-
"""Getting and putting simulation db files

:copyright: Copyright (c) 2020 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern import pkio
from pykern.pkdebug import pkdp
from pykern.pkcollections import PKDict
import functools
import sirepo.job
import sirepo.tornado
import tornado.web

_KEY_TO_UID = PKDict()


# Must be defined before use
def _sim_db_file_req_validated(func):
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        t = self.request.headers.get(sirepo.job.AUTH_HEADER)
        if not t:
            sirepo.tornado.raise_unauthorized()
        p = t.split(' ')
        if len(p) != 2:
            sirepo.tornado.raise_unauthorized()
        if p[0] != sirepo.job.AUTH_HEADER_SCHEME_BEARER \
           or not sirepo.job.UNIQUE_KEY_RE.search(p[1]):
            sirepo.tornado.raise_unauthorized()
        if p[1] not in _KEY_TO_UID or \
           self.request.path.split('/')[3] != _KEY_TO_UID[p[1]]:
            sirepo.tornado.raise_forbidden()
        # TODO(e-carlin): discuss with rn what validation needs to happen
        p = self.request.path
        if p.count('.') > 1:
            sirepo.tornado.raise_not_found()
        return func(self, *args, **kwargs)
    return wrapper


class FileReq(tornado.web.RequestHandler):
    SUPPORTED_METHODS = ['GET','PUT']

    @_sim_db_file_req_validated
    def get(self, path):
        p = sirepo.srdb.root().join(path)
        if not p.exists():
            sirepo.tornado.raise_not_found()
        self.write(pkio.read_binary(p))

    @_sim_db_file_req_validated
    def put(self, path):
        sirepo.srdb.root().join(path).write_binary(self.request.body)


def get_key(uid):
    for u, k in _KEY_TO_UID.items():
        if u == uid:
            return k
    k = sirepo.job.unique_key()
    _KEY_TO_UID[k] = uid
    return k
{'fraction_non_alphanumeric': 0.11833582461385152, 'fraction_numerical': 0.007473841554559043, 'mean_word_length': 2.83476599808978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7620851', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1397, 'n_words': 301}