<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright 2013 Kitware Inc.
#
#  Licensed under the Apache License, Version 2.0 ( the "License" );
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
###############################################################################

import cgi
import cherrypy
import collections
import datetime
import inspect
import json
import posixpath
import six
import sys
import traceback
import unicodedata

from dogpile.cache.util import kwarg_function_key_generator
from . import docs
from girder import auditLogger, events, logger, logprint
from girder.constants import SettingKey, TokenScope, SortDir
from girder.exceptions import AccessException, GirderException, ValidationException, RestException
from girder.models.setting import Setting
from girder.models.token import Token
from girder.models.user import User
from girder.utility import toBool, config, JsonEncoder, optionalArgumentDecorator
from girder.utility._cache import requestCache
from girder.utility.model_importer import ModelImporter
from six.moves import range, urllib

# Arbitrary buffer length for stream-reading request bodies
READ_BUFFER_LEN = 65536


def getUrlParts(url=None):
    """
    Calls `urllib.parse.urlparse`_ on a URL.

    :param url: A URL, or None to use the current request's URL.
    :type url: str or None
    :return: The URL's separate components.
    :rtype: `urllib.parse.ParseResult`_

    .. note:: This is compatible with both Python 2 and 3.

    .. _urllib.parse.urlparse: https://docs.python.org/3/library/
       urllib.parse.html#urllib.parse.urlparse

    .. _urllib.parse.ParseResult: https://docs.python.org/3/library/
       urllib.parse.html#urllib.parse.ParseResult
    """
    url = url or cherrypy.url()
    return urllib.parse.urlparse(url)


def getApiUrl(url=None, preferReferer=False):
    """
    In a request thread, call this to get the path to the root of the REST API.
    The returned path does *not* end in a forward slash.

    :param url: URL from which to extract the base URL. If not specified, uses
        the server root system setting. If that is not specified, uses `cherrypy.url()`
    :param preferReferer: if no url is specified, this is true, and this is in
        a cherrypy request that has a referer header that contains the api
        string, use that referer as the url.
    """
    apiStr = '/api/v1'

    if not url:
        if preferReferer and apiStr in cherrypy.request.headers.get('referer', ''):
            url = cherrypy.request.headers['referer']
        else:
            root = Setting().get(SettingKey.SERVER_ROOT)
            if root:
                return posixpath.join(root, config.getConfig()['server']['api_root'].lstrip('/'))

    url = url or cherrypy.url()
    idx = url.find(apiStr)

    if idx < 0:
        raise GirderException('Could not determine API root in %s.' % url)

    return url[:idx + len(apiStr)]

<commit_msg>Respect config api_root setting in getApiUrl()<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright 2013 Kitware Inc.
#
#  Licensed under the Apache License, Version 2.0 ( the "License" );
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
###############################################################################

import cgi
import cherrypy
import collections
import datetime
import inspect
import json
import posixpath
import six
import sys
import traceback
import unicodedata

from dogpile.cache.util import kwarg_function_key_generator
from . import docs
from girder import auditLogger, events, logger, logprint
from girder.constants import SettingKey, TokenScope, SortDir
from girder.exceptions import AccessException, GirderException, ValidationException, RestException
from girder.models.setting import Setting
from girder.models.token import Token
from girder.models.user import User
from girder.utility import toBool, config, JsonEncoder, optionalArgumentDecorator
from girder.utility._cache import requestCache
from girder.utility.model_importer import ModelImporter
from six.moves import range, urllib

# Arbitrary buffer length for stream-reading request bodies
READ_BUFFER_LEN = 65536


def getUrlParts(url=None):
    """
    Calls `urllib.parse.urlparse`_ on a URL.

    :param url: A URL, or None to use the current request's URL.
    :type url: str or None
    :return: The URL's separate components.
    :rtype: `urllib.parse.ParseResult`_

    .. note:: This is compatible with both Python 2 and 3.

    .. _urllib.parse.urlparse: https://docs.python.org/3/library/
       urllib.parse.html#urllib.parse.urlparse

    .. _urllib.parse.ParseResult: https://docs.python.org/3/library/
       urllib.parse.html#urllib.parse.ParseResult
    """
    url = url or cherrypy.url()
    return urllib.parse.urlparse(url)


def getApiUrl(url=None, preferReferer=False):
    """
    In a request thread, call this to get the path to the root of the REST API.
    The returned path does *not* end in a forward slash.

    :param url: URL from which to extract the base URL. If not specified, uses
        the server root system setting. If that is not specified, uses `cherrypy.url()`
    :param preferReferer: if no url is specified, this is true, and this is in
        a cherrypy request that has a referer header that contains the api
        string, use that referer as the url.
    """
    apiStr = config.getConfig()['server']['api_root']

    if not url:
        if preferReferer and apiStr in cherrypy.request.headers.get('referer', ''):
            url = cherrypy.request.headers['referer']
        else:
            root = Setting().get(SettingKey.SERVER_ROOT)
            if root:
                return posixpath.join(root, apiStr.lstrip('/'))

    url = url or cherrypy.url()
    idx = url.find(apiStr)

    if idx < 0:
        raise GirderException('Could not determine API root in %s.' % url)

    return url[:idx + len(apiStr)]


{'fraction_non_alphanumeric': 0.12546762589928057, 'fraction_numerical': 0.005611510791366906, 'mean_word_length': 3.658847184986595, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '5678533', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1927, 'n_words': 725}