<commit_before>import sys
import os.path
import json

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.escape
from tornado.options import define, options
from pyld import jsonld

from .es import ESQuery
import config

src_path = os.path.split(os.path.split(os.path.abspath(__file__))[0])[0]
if src_path not in sys.path:
    sys.path.append(src_path)
print(src_path)

STATIC_PATH = os.path.join(src_path, 'src/static')

define("port", default=8000, help="run on the given port", type=int)
define("address", default="127.0.0.1", help="run on localhost")
define("debug", default=False, type=bool, help="run in debug mode")
tornado.options.parse_command_line()
if options.debug:
    import tornado.autoreload
    import logging
    logging.getLogger().setLevel(logging.DEBUG)
    options.address = '0.0.0.0'


def get_json(filename):
    with open(filename) as f:
        return json.load(f)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        # self.write('hello world!')
        # self.render(os.path.join(src_path, '../alpaca.htm'))
        self.redirect('/website/')


class BaseHandler(tornado.web.RequestHandler):
    def return_json(self, data):
        _json_data = json.dumps(data, indent=2)
        self.set_header("Content-Type", "application/json; charset=UTF-8")
        self.support_cors()
        self.write(_json_data)

    def support_cors(self, *args, **kwargs):
        '''Provide server side support for CORS request.'''
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
        self.set_header("Access-Control-Allow-Headers", "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control")
        self.set_header("Access-Control-Allow-Credentials", "false")
        self.set_header("Access-Control-Max-Age", "60")

    def options(self, *args, **kwargs):
        self.support_cors()


class JsonHandler(BaseHandler):
    def get(self, ctx_name):
        if ctx_name == 'mygene':
            obj = get_json(os.path.join(src_path, '../data/mygene.info.smartAPI.json'))
            # del obj['services'][0]['@context']
            obj = jsonld.expand(obj)[0]
            self.return_json(obj)
        elif ctx_name == '2':
            obj = get_json(os.path.join(src_path, '../data/mygene.info.smartAPI.json'))
            obj = jsonld.expand(obj['services'][0])
            self.return_json(obj)
        elif ctx_name == '3':
            obj = get_json(os.path.join(src_path, '../data/mygene.info.smartAPI.json'))
            obj = jsonld.expand(obj['services'][0])
            self.return_json(obj)
        if ctx_name == 'myvariant':
            obj = get_json(os.path.join(src_path, '../data/myvariant.info.smartAPI.json'))
            # del obj['services'][0]['@context']
            obj = jsonld.expand(obj)[0]
            self.return_json(obj)


class QueryHanlder(BaseHandler):
    def get(self):
        q = self.get_argument('q', None)
        fields = self.get_argument('fields', None)
        return_raw = self.get_argument('raw', '').lower() in ['1', 'true']

        esq = ESQuery()
        res = esq.query_api(q=q, fields=fields, return_raw=return_raw)
        self.return_json(res)


class APIHandler(BaseHandler):
    def post(self):
        # save an API metadata
        api_key = self.get_argument('api_key', None)
        if api_key != config.API_KEY:
            self.set_status(405)
            res = {'success': False, 'error': 'Invalid API key.'}
            self.return_json(res)
        else:
            data = tornado.escape.json_decode(self.request.body)
            print(json.dumps(data, indent=2))
            esq = ESQuery()
            res = esq.save_api(data)
            self.return_json(res)


class PathHanlder(BaseHandler):
    def get(self):
        _from = self.get_argument('from', None)
        _to = self.get_argument('to', None)
        if _from and _to:<commit_msg>added "overwrite" parameter.<commit_after>import sys
import os.path
import json

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.escape
from tornado.options import define, options
from pyld import jsonld

from api.es import ESQuery
import config

src_path = os.path.split(os.path.split(os.path.abspath(__file__))[0])[0]
if src_path not in sys.path:
    sys.path.append(src_path)
print(src_path)

STATIC_PATH = os.path.join(src_path, 'src/static')

define("port", default=8000, help="run on the given port", type=int)
define("address", default="127.0.0.1", help="run on localhost")
define("debug", default=False, type=bool, help="run in debug mode")
tornado.options.parse_command_line()
if options.debug:
    import tornado.autoreload
    import logging
    logging.getLogger().setLevel(logging.DEBUG)
    options.address = '0.0.0.0'


def get_json(filename):
    with open(filename) as f:
        return json.load(f)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        # self.write('hello world!')
        # self.render(os.path.join(src_path, '../alpaca.htm'))
        self.redirect('/website/')


class BaseHandler(tornado.web.RequestHandler):
    def return_json(self, data):
        _json_data = json.dumps(data, indent=2)
        self.set_header("Content-Type", "application/json; charset=UTF-8")
        self.support_cors()
        self.write(_json_data)

    def support_cors(self, *args, **kwargs):
        '''Provide server side support for CORS request.'''
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
        self.set_header("Access-Control-Allow-Headers", "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control")
        self.set_header("Access-Control-Allow-Credentials", "false")
        self.set_header("Access-Control-Max-Age", "60")

    def options(self, *args, **kwargs):
        self.support_cors()


class JsonHandler(BaseHandler):
    def get(self, ctx_name):
        if ctx_name == 'mygene':
            obj = get_json(os.path.join(src_path, '../data/mygene.info.smartAPI.json'))
            # del obj['services'][0]['@context']
            obj = jsonld.expand(obj)[0]
            self.return_json(obj)
        elif ctx_name == '2':
            obj = get_json(os.path.join(src_path, '../data/mygene.info.smartAPI.json'))
            obj = jsonld.expand(obj['services'][0])
            self.return_json(obj)
        elif ctx_name == '3':
            obj = get_json(os.path.join(src_path, '../data/mygene.info.smartAPI.json'))
            obj = jsonld.expand(obj['services'][0])
            self.return_json(obj)
        if ctx_name == 'myvariant':
            obj = get_json(os.path.join(src_path, '../data/myvariant.info.smartAPI.json'))
            # del obj['services'][0]['@context']
            obj = jsonld.expand(obj)[0]
            self.return_json(obj)


class QueryHanlder(BaseHandler):
    def get(self):
        q = self.get_argument('q', None)
        fields = self.get_argument('fields', None)
        return_raw = self.get_argument('raw', '').lower() in ['1', 'true']

        esq = ESQuery()
        res = esq.query_api(q=q, fields=fields, return_raw=return_raw)
        self.return_json(res)


class APIHandler(BaseHandler):
    def post(self):
        # save an API metadata
        api_key = self.get_argument('api_key', None)
        overwrite = self.get_argument('overwrite', '').lower()
        overwrite = overwrite in ['1', 'true']
        if api_key != config.API_KEY:
            self.set_status(405)
            res = {'success': False, 'error': 'Invalid API key.'}
            self.return_json(res)
        else:
            data = tornado.escape.json_decode(self.request.body)
            print(json.dumps(data, indent=2))
            esq = ESQuery()
            res = esq.save_api(data, overwrite=overwrite)
            self.return_json(res)


class PathHanlder(BaseHandler):
    def get(self):
        _from = self.get_argument('from', None)
        _to = self.get_argument('to', None)
        if _from and _to:
{'fraction_non_alphanumeric': 0.14587635126928217, 'fraction_numerical': 0.008137981294789263, 'mean_word_length': 2.9116389548693586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '3900079', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2632, 'n_words': 510}