@temple.utils.set_cmd_env_var('<STR_LIT>')<EOL>def update(old_template=None, old_version=None, new_template=None, new_version=None,<EOL>enter_parameters=False):

update_branch = temple.constants.UPDATE_BRANCH_NAME<EOL>temp_update_branch = temple.constants.TEMP_UPDATE_BRANCH_NAME<EOL>temple.check.in_git_repo()<EOL>temple.check.in_clean_repo()<EOL>temple.check.is_temple_project()<EOL>temple.check.not_has_branch(update_branch)<EOL>temple.check.not_has_branch(temp_update_branch)<EOL>temple.check.has_env_vars(temple.constants.GITHUB_API_TOKEN_ENV_VAR)<EOL>temple_config = temple.utils.read_temple_config()<EOL>old_template = old_template or temple_config['<STR_LIT>']<EOL>new_template = new_template or temple_config['<STR_LIT>']<EOL>old_version = old_version or temple_config['<STR_LIT>']<EOL>new_version = new_version or _get_latest_template_version(new_template)<EOL>if new_template == old_template and new_version == old_version and not enter_parameters:<EOL><INDENT>print('<STR_LIT>')<EOL>return False<EOL><DEDENT>print('<STR_LIT>'.format(update_branch))<EOL>temple.utils.shell('<STR_LIT>'.format(update_branch),<EOL>stderr=subprocess.DEVNULL)<EOL>print('<STR_LIT>'.format(temp_update_branch))<EOL>temple.utils.shell('<STR_LIT>'.format(temp_update_branch),<EOL>stderr=subprocess.DEVNULL)<EOL>temple.utils.shell('<STR_LIT>',<EOL>stdout=subprocess.DEVNULL)<EOL>_apply_template(old_template,<EOL>'<STR_LIT:.>',<EOL>checkout=old_version,<EOL>extra_context=temple_config)<EOL>temple.utils.shell('<STR_LIT>')<EOL>temple.utils.shell(<EOL>'<STR_LIT>'.format(old_version),<EOL>stdout=subprocess.DEVNULL,<EOL>stderr=subprocess.DEVNULL)<EOL>print('<STR_LIT>')<EOL>temple.utils.shell('<STR_LIT>'.format(update_branch),<EOL>stderr=subprocess.DEVNULL)<EOL>temple.utils.shell(<EOL>'<STR_LIT>'.format(temp_update_branch),<EOL>stderr=subprocess.DEVNULL)<EOL>print('<STR_LIT>')<EOL>temple.utils.shell('<STR_LIT>'.format(temp_update_branch),<EOL>stderr=subprocess.DEVNULL)<EOL>temple.utils.shell('<STR_LIT>',<EOL>stdout=subprocess.DEVNULL)<EOL>needs_new_cc_config = _needs_new_cc_config_for_update(old_template, old_version,<EOL>new_template, new_version)<EOL>if needs_new_cc_config:<EOL><INDENT>if old_template != new_template:<EOL><INDENT>cc_config_input_msg = (<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>).format(temple.utils.get_repo_path(new_template))<EOL><DEDENT>else:<EOL><INDENT>cc_config_input_msg = (<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>input(cc_config_input_msg)<EOL><DEDENT>if needs_new_cc_config or enter_parameters:<EOL><INDENT>_, temple_config = (<EOL>temple.utils.get_cookiecutter_config(new_template,<EOL>default_config=temple_config,<EOL>version=new_version))<EOL><DEDENT>_apply_template(new_template,<EOL>'<STR_LIT:.>',<EOL>checkout=new_version,<EOL>extra_context=temple_config)<EOL>temple.utils.write_temple_config(temple_config, new_template, new_version)<EOL>temple.utils.shell('<STR_LIT>')<EOL>temple.utils.shell(<EOL>'<STR_LIT>'.format(new_version),<EOL>stdout=subprocess.DEVNULL,<EOL>stderr=subprocess.DEVNULL)<EOL>print('<STR_LIT>')<EOL>temple.utils.shell('<STR_LIT>'.format(update_branch),<EOL>stderr=subprocess.DEVNULL)<EOL>temple.utils.shell('<STR_LIT>'.format(temp_update_branch),<EOL>check=False,<EOL>stdout=subprocess.DEVNULL,<EOL>stderr=subprocess.DEVNULL)<EOL>temple.utils.shell('<STR_LIT>'.format(temple.constants.TEMPLE_CONFIG_FILE),<EOL>check=False,<EOL>stdout=subprocess.DEVNULL,<EOL>stderr=subprocess.DEVNULL)<EOL>print('<STR_LIT>'.format(temp_update_branch))<EOL>temple.utils.shell('<STR_LIT>'.format(temp_update_branch),<EOL>stdout=subprocess.DEVNULL)<EOL>print(textwrap.dedent("""<STR_LIT>""").format(update_branch))<EOL>return True<EOL>

Updates the temple project to the latest template

    Proceeeds in the following steps:

    1. Ensure we are inside the project repository
    2. Obtain the latest version of the package template
    3. If the package is up to date with the latest template, return
    4. If not, create an empty template branch with a new copy of the old template
    5. Create an update branch from HEAD and merge in the new template copy
    6. Create a new copy of the new template and merge into the empty template branch
    7. Merge the updated empty template branch into the update branch
    8. Ensure temple.yaml reflects what is in the template branch
    9. Remove the empty template branch

    Note that the `temple.constants.TEMPLE_ENV_VAR` is set to 'update' for the
    duration of this function.

    Two branches will be created during the update process, one named
    ``_temple_update`` and one named ``_temple_update_temp``. At the end of
    the process, ``_temple_update_temp`` will be removed automatically. The
    work will be left in ``_temple_update`` in an uncommitted state for
    review. The update will fail early if either of these branches exist
    before the process starts.

    Args:
        old_template (str, default=None): The old template from which to update. Defaults
            to the template in temple.yaml
        old_version (str, default=None): The old version of the template. Defaults to
            the version in temple.yaml
        new_template (str, default=None): The new template for updating. Defaults to the
            template in temple.yaml
        new_version (str, default=None): The new version of the new template to update.
            Defaults to the latest version of the new template
        enter_parameters (bool, default=False): Force entering template parameters for the project

    Raises:
        `NotInGitRepoError`: When not inside of a git repository
        `InvalidTempleProjectError`: When not inside a valid temple repository
        `InDirtyRepoError`: When an update is triggered while the repo is in a dirty state
        `ExistingBranchError`: When an update is triggered and there is an existing
            update branch

    Returns:
        boolean: True if update was performed or False if template was already up to date


@main.command()<EOL>@click.argument('<STR_LIT>', nargs=<NUM_LIT:1>, required=True)<EOL>@click.option('<STR_LIT>', '<STR_LIT>', default=None,<EOL>help='<STR_LIT>')<EOL>def setup(template, version):

temple.setup.setup(template, version=version)<EOL>

Setup new project. Takes a full git SSH path to the template as returned
by "temple ls". In order to start a project from a
particular version (instead of the latest), use the "-v" option.


@main.command()<EOL>@click.option('<STR_LIT:-c>', '<STR_LIT>', is_flag=True,<EOL>help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', '<STR_LIT>', is_flag=True,<EOL>help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', '<STR_LIT>', default=None,<EOL>help='<STR_LIT>')<EOL>def update(check, enter_parameters, version):

Update package with latest template. Must be inside of the project
folder to run.

Using "-e" will prompt for re-entering the template parameters again
even if the project is up to date.

Use "-v" to update to a particular version of a template.

Using "-c" will perform a check that the project is up to date
with the latest version of the template (or the version specified by "-v").
No updating will happen when using this option.


@main.command()<EOL>@click.argument('<STR_LIT>', nargs=<NUM_LIT:1>, required=True)<EOL>@click.argument('<STR_LIT>', nargs=<NUM_LIT:1>, required=False)<EOL>@click.option('<STR_LIT>', '<STR_LIT>', is_flag=True,<EOL>help='<STR_LIT>')<EOL>def ls(github_user, template, long_format):

github_urls = temple.ls.ls(github_user, template=template)<EOL>for ssh_path, info in github_urls.items():<EOL><INDENT>if long_format:<EOL><INDENT>print(ssh_path, '<STR_LIT:->', info['<STR_LIT:description>'] or '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>print(ssh_path)<EOL><DEDENT><DEDENT>

List packages created with temple. Enter a github user or
organization to list all templates under the user or org.
Using a template path as the second argument will list all projects
that have been started with that template.

Use "-l" to print the Github repository descriptions of templates
or projects.


temple.clean.clean()<EOL>

Cleans temporary resources created by temple, such as the temple update branch


@main.command()<EOL>@click.argument('<STR_LIT>', nargs=<NUM_LIT:1>, required=True)<EOL>@click.option('<STR_LIT>', '<STR_LIT>', default=None,<EOL>help='<STR_LIT>')<EOL>def switch(template, version):

temple.update.update(new_template=template, new_version=version)<EOL>

Switch a project's template to a different template.


if not template_path.startswith('<STR_LIT>') or not template_path.endswith('<STR_LIT>'):<EOL><INDENT>raise temple.exceptions.InvalidTemplatePathError(<EOL>'<STR_LIT>')<EOL><DEDENT>

Raises a `InvalidTemplatePathError` if ``template_path`` is not a git SSH url

    Note that the git SSH url must be in the form as provided from Github or from
    ``temple ls``. For example, ``git@github.com:user/template.git``.


ret = temple.utils.shell('<STR_LIT>', stderr=subprocess.DEVNULL, check=False)<EOL>return ret.returncode == <NUM_LIT:0><EOL>

Returns True if inside a git repo, False otherwise


if not _in_git_repo():<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise temple.exceptions.NotInGitRepoError(msg)<EOL><DEDENT>

Raises `NotInGitRepoError` if not inside a git repository


if _in_git_repo():<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise temple.exceptions.InGitRepoError(msg)<EOL><DEDENT>

Raises `InGitRepoError` if inside of a git repository


ret = temple.utils.shell('<STR_LIT>', check=False)<EOL>return ret.returncode == <NUM_LIT:0><EOL>

Returns True if the git repo is not dirty, False otherwise


if not _in_clean_repo():<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise temple.exceptions.InDirtyRepoError(msg)<EOL><DEDENT>

Raises `InDirtyRepoError` if inside a dirty repository


ret = temple.utils.shell('<STR_LIT>'.format(branch),<EOL>stderr=subprocess.DEVNULL,<EOL>stdout=subprocess.DEVNULL,<EOL>check=False)<EOL>return ret.returncode == <NUM_LIT:0><EOL>

Return True if the target branch exists.


if _has_branch(branch):<EOL><INDENT>msg = '<STR_LIT>'.format(branch)<EOL>raise temple.exceptions.ExistingBranchError(msg)<EOL><DEDENT>

Raises `ExistingBranchError` if the specified branch exists.


for env_var in env_vars:<EOL><INDENT>if not os.environ.get(env_var):<EOL><INDENT>msg = (<EOL>'<STR_LIT>'<EOL>).format(env_var, temple.constants.TEMPLE_DOCS_URL)<EOL>raise temple.exceptions.InvalidEnvironmentError(msg)<EOL><DEDENT><DEDENT>

Raises `InvalidEnvironmentError` when one isnt set


if not os.path.exists(temple.constants.TEMPLE_CONFIG_FILE):<EOL><INDENT>msg = '<STR_LIT>'.format(temple.constants.TEMPLE_CONFIG_FILE)<EOL>raise temple.exceptions.InvalidTempleProjectError(msg)<EOL><DEDENT>

Raises `InvalidTempleProjectError` if repository is not a temple project


links = {}<EOL>if '<STR_LIT>' in headers:<EOL><INDENT>link_headers = headers['<STR_LIT>'].split('<STR_LIT:U+002CU+0020>')<EOL>for link_header in link_headers:<EOL><INDENT>(url, rel) = link_header.split('<STR_LIT>')<EOL>url = url[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL>rel = rel[<NUM_LIT:5>:-<NUM_LIT:1>]<EOL>links[rel] = url<EOL><DEDENT><DEDENT>return links<EOL>

Parses Github's link header for pagination.

    TODO eventually use a github client for this


github_client = temple.utils.GithubClient()<EOL>headers = {'<STR_LIT>': '<STR_LIT>'}<EOL>resp = github_client.get('<STR_LIT>',<EOL>params={'<STR_LIT:q>': query, '<STR_LIT>': <NUM_LIT:100>},<EOL>headers=headers)<EOL>if resp.status_code == requests.codes.unprocessable_entity and github_user:<EOL><INDENT>raise temple.exceptions.InvalidGithubUserError(<EOL>'<STR_LIT>'.format(github_user))<EOL><DEDENT>resp.raise_for_status()<EOL>resp_data = resp.json()<EOL>repositories = collections.defaultdict(dict)<EOL>while True:<EOL><INDENT>repositories.update({<EOL>'<STR_LIT>'.format(repo['<STR_LIT>']['<STR_LIT>']): repo['<STR_LIT>']<EOL>for repo in resp_data['<STR_LIT>']<EOL>})<EOL>next_url = _parse_link_header(resp.headers).get('<STR_LIT>')<EOL>if next_url:<EOL><INDENT>resp = requests.get(next_url, headers=headers)<EOL>resp.raise_for_status()<EOL>resp_data = resp.json()<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>return repositories<EOL>

Performs a Github API code search

    Args:
        query (str): The query sent to Github's code search
        github_user (str, optional): The Github user being searched in the query string

    Returns:
        dict: A dictionary of repository information keyed on the git SSH url

    Raises:
        `InvalidGithubUserError`: When ``github_user`` is invalid


temple.check.has_env_vars(temple.constants.GITHUB_API_TOKEN_ENV_VAR)<EOL>if template:<EOL><INDENT>temple.check.is_git_ssh_path(template)<EOL>search_q = '<STR_LIT>'.format(<EOL>github_user,<EOL>temple.constants.TEMPLE_CONFIG_FILE,<EOL>template)<EOL><DEDENT>else:<EOL><INDENT>search_q = '<STR_LIT>'.format(github_user)<EOL><DEDENT>results = _code_search(search_q, github_user)<EOL>return collections.OrderedDict(sorted(results.items()))<EOL>

Lists all temple templates and packages associated with those templates

    If ``template`` is None, returns the available templates for the configured
    Github org.

    If ``template`` is a Github path to a template, returns all projects spun
    up with that template.

    ``ls`` uses the github search API to find results.

    Note that the `temple.constants.TEMPLE_ENV_VAR` is set to 'ls' for the duration of this
    function.

    Args:
        github_user (str): The github user or org being searched.
        template (str, optional): The template git repo path. If provided, lists
            all projects that have been created with the provided template. Note
            that the template path is the SSH path
            (e.g. git@github.com:CloverHealth/temple.git)

    Returns:
        dict: A dictionary of repository information keyed on the SSH Github url

    Raises:
        `InvalidGithubUserError`: When ``github_user`` is invalid


return template[:-<NUM_LIT:4>].split('<STR_LIT::>')[<NUM_LIT:1>]<EOL>

Given a git SSH path (e.g git@github.com:owner/repo.git), return the repo path

    The repo path is in the form of "owner/repo"


def shell(cmd, check=True, stdin=None, stdout=None, stderr=None):

return subprocess.run(cmd, shell=True, check=check, stdin=stdin, stdout=stdout, stderr=stderr)<EOL>

Runs a subprocess shell with check=True by default


old_dir = os.getcwd()<EOL>os.chdir(path)<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>os.chdir(old_dir)<EOL><DEDENT>

A context manager for changing into a directory


with open(temple.constants.TEMPLE_CONFIG_FILE) as temple_config_file:<EOL><INDENT>return yaml.load(temple_config_file, Loader=yaml.SafeLoader)<EOL><DEDENT>

Reads the temple YAML configuration file in the repository


def write_temple_config(temple_config, template, version):

with open(temple.constants.TEMPLE_CONFIG_FILE, '<STR_LIT:w>') as temple_config_file:<EOL><INDENT>versioned_config = {<EOL>**temple_config,<EOL>**{'<STR_LIT>': version, '<STR_LIT>': template},<EOL>}<EOL>yaml.dump(versioned_config, temple_config_file, Dumper=yaml.SafeDumper)<EOL><DEDENT>


def get_cookiecutter_config(template, default_config=None, version=None):

default_config = default_config or {}<EOL>config_dict = cc_config.get_user_config()<EOL>repo_dir, _ = cc_repository.determine_repo_dir(<EOL>template=template,<EOL>abbreviations=config_dict['<STR_LIT>'],<EOL>clone_to_dir=config_dict['<STR_LIT>'],<EOL>checkout=version,<EOL>no_input=True)<EOL>context_file = os.path.join(repo_dir, '<STR_LIT>')<EOL>context = cc_generate.generate_context(<EOL>context_file=context_file,<EOL>default_context={**config_dict['<STR_LIT>'], **default_config})<EOL>return repo_dir, cc_prompt.prompt_for_config(context)<EOL>

Obtains the configuration used for cookiecutter templating

    Args:
        template: Path to the template
        default_config (dict, optional): The default configuration
        version (str, optional): The git SHA or branch to use when
            checking out template. Defaults to latest version

    Returns:
        tuple: The cookiecutter repo directory and the config dict


def func_decorator(function):<EOL><INDENT>@functools.wraps(function)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>previous_cmd_env_var = os.getenv(temple.constants.TEMPLE_ENV_VAR)<EOL>os.environ[temple.constants.TEMPLE_ENV_VAR] = value<EOL>try:<EOL><INDENT>ret_val = function(*args, **kwargs)<EOL><DEDENT>finally:<EOL><INDENT>if previous_cmd_env_var is None:<EOL><INDENT>del os.environ[temple.constants.TEMPLE_ENV_VAR]<EOL><DEDENT>else:<EOL><INDENT>os.environ[temple.constants.TEMPLE_ENV_VAR] = previous_cmd_env_var<EOL><DEDENT><DEDENT>return ret_val<EOL><DEDENT>return wrapper<EOL><DEDENT>return func_decorator<EOL>

Decorator that sets the temple command env var to value


def _call_api(self, verb, url, **request_kwargs):

api = '<STR_LIT>'.format(url)<EOL>auth_headers = {'<STR_LIT>': '<STR_LIT>'.format(self.api_token)}<EOL>headers = {**auth_headers, **request_kwargs.pop('<STR_LIT>', {})}<EOL>return getattr(requests, verb)(api, headers=headers, **request_kwargs)<EOL>

Perform a github API call

        Args:
            verb (str): Can be "post", "put", or "get"
            url (str): The base URL with a leading slash for Github API (v3)
            auth (str or HTTPBasicAuth): A Github API token or a HTTPBasicAuth object


return self._call_api('<STR_LIT>', url, **request_kwargs)<EOL>


def _patched_run_hook(hook_name, project_dir, context):

if hook_name == '<STR_LIT>':<EOL><INDENT>with temple.utils.cd(project_dir):<EOL><INDENT>temple.utils.write_temple_config(context['<STR_LIT>'],<EOL>context['<STR_LIT>'],<EOL>context['<STR_LIT:version>'])<EOL><DEDENT><DEDENT>return cc_hooks.run_hook(hook_name, project_dir, context)<EOL>

Used to patch cookiecutter's ``run_hook`` function.

    This patched version ensures that the temple.yaml file is created before
    any cookiecutter hooks are executed


def _generate_files(repo_dir, config, template, version):

with unittest.mock.patch('<STR_LIT>', side_effect=_patched_run_hook):<EOL><INDENT>cc_generate.generate_files(repo_dir=repo_dir,<EOL>context={'<STR_LIT>': config,<EOL>'<STR_LIT>': template,<EOL>'<STR_LIT:version>': version},<EOL>overwrite_if_exists=False,<EOL>output_dir='<STR_LIT:.>')<EOL><DEDENT>

Uses cookiecutter to generate files for the project.

    Monkeypatches cookiecutter's "run_hook" to ensure that the temple.yaml file is
    generated before any hooks run. This is important to ensure that hooks can also
    perform any actions involving temple.yaml


temple.check.is_git_ssh_path(template)<EOL>temple.check.not_in_git_repo()<EOL>repo_path = temple.utils.get_repo_path(template)<EOL>msg = (<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>).format(repo_path)<EOL>print(msg)<EOL>cc_repo_dir, config = temple.utils.get_cookiecutter_config(template, version=version)<EOL>if not version:<EOL><INDENT>with temple.utils.cd(cc_repo_dir):<EOL><INDENT>ret = temple.utils.shell('<STR_LIT>', stdout=subprocess.PIPE)<EOL>version = ret.stdout.decode('<STR_LIT:utf-8>').strip()<EOL><DEDENT><DEDENT>_generate_files(repo_dir=cc_repo_dir, config=config, template=template, version=version)<EOL>

Sets up a new project from a template

    Note that the `temple.constants.TEMPLE_ENV_VAR` is set to 'setup' during the duration
    of this function.

    Args:
        template (str): The git SSH path to a template
        version (str, optional): The version of the template to use when updating. Defaults
            to the latest version


old_value = os.environ.get(key, None)<EOL>if value is None:<EOL><INDENT>os.environ.pop(key, None)<EOL><DEDENT>else:<EOL><INDENT>os.environ[key] = value<EOL><DEDENT>yield<EOL>if old_value is None:<EOL><INDENT>os.environ.pop(key, None)<EOL><DEDENT>else:<EOL><INDENT>os.environ[key] = value<EOL><DEDENT>

Context manager to set an environment variable temporarily.


utf8_encoded = string.encode('<STR_LIT:utf-8>')<EOL>return base64.urlsafe_b64encode(utf8_encoded)<EOL>


@decorator<EOL>def x_runtime(f, *args, **kwargs):

_t0 = now()<EOL>r = f(*args, **kwargs)<EOL>_t1 = now()<EOL>r.headers['<STR_LIT>'] = '<STR_LIT>'.format(Decimal(str(_t1 - _t0)))<EOL>return r<EOL>


@decorator<EOL>def gzip(f, *args, **kwargs):

data = f(*args, **kwargs)<EOL>if isinstance(data, Response):<EOL><INDENT>content = data.data<EOL><DEDENT>else:<EOL><INDENT>content = data<EOL><DEDENT>gzip_buffer = BytesIO()<EOL>gzip_file = gzip2.GzipFile(<EOL>mode='<STR_LIT:wb>',<EOL>compresslevel=<NUM_LIT:4>,<EOL>fileobj=gzip_buffer<EOL>)<EOL>gzip_file.write(content)<EOL>gzip_file.close()<EOL>gzip_data = gzip_buffer.getvalue()<EOL>if isinstance(data, Response):<EOL><INDENT>data.data = gzip_data<EOL>data.headers['<STR_LIT>'] = '<STR_LIT>'<EOL>data.headers['<STR_LIT>'] = str(len(data.data))<EOL>return data<EOL><DEDENT>return gzip_data<EOL>


@decorator<EOL>def deflate(f, *args, **kwargs):

data = f(*args, **kwargs)<EOL>if isinstance(data, Response):<EOL><INDENT>content = data.data<EOL><DEDENT>else:<EOL><INDENT>content = data<EOL><DEDENT>deflater = zlib.compressobj()<EOL>deflated_data = deflater.compress(content)<EOL>deflated_data += deflater.flush()<EOL>if isinstance(data, Response):<EOL><INDENT>data.data = deflated_data<EOL>data.headers['<STR_LIT>'] = '<STR_LIT>'<EOL>data.headers['<STR_LIT>'] = str(len(data.data))<EOL>return data<EOL><DEDENT>return deflated_data<EOL>


try:<EOL><INDENT>string = string.decode('<STR_LIT:utf-8>')<EOL>json.dumps(string)<EOL>return string<EOL><DEDENT>except (ValueError, TypeError):<EOL><INDENT>return b'<STR_LIT>'.join([<EOL>b'<STR_LIT>',<EOL>content_type.encode('<STR_LIT:utf-8>'),<EOL>b'<STR_LIT>',<EOL>base64.b64encode(string)<EOL>]).decode('<STR_LIT:utf-8>')<EOL><DEDENT>

Returns JSON-safe version of `string`.

    If `string` is a Unicode string or a valid UTF-8,
    it is returned unmodified,
    as it can safely be encoded to JSON string.

    If `string` contains raw/binary data, it is Base64-encoded, formatted and
    returned according to "data" URL scheme (RFC2397). Since JSON is not
    suitable for binary data, some additional encoding was necessary; "data"
    URL scheme was chosen for its simplicity.


files = dict()<EOL>for k, v in request.files.items():<EOL><INDENT>content_type = (<EOL>request.files[k].content_type or '<STR_LIT>'<EOL>)<EOL>val = json_safe(v.read(), content_type)<EOL>if files.get(k):<EOL><INDENT>if not isinstance(files[k], list):<EOL><INDENT>files[k] = [files[k]]<EOL><DEDENT>files[k].append(val)<EOL><DEDENT>else:<EOL><INDENT>files[k] = val<EOL><DEDENT><DEDENT>return files<EOL>

Returns files dict from request context.


headers = dict(request.headers.items())<EOL>if hide_env and ('<STR_LIT>' not in request.args):<EOL><INDENT>for key in ENV_HEADERS:<EOL><INDENT>try:<EOL><INDENT>del headers[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>return CaseInsensitiveDict(headers.items())<EOL>

Returns headers dict from request context.


if multi:<EOL><INDENT>result = multi.to_dict()<EOL>for k, v in result.items():<EOL><INDENT>if len(v) == <NUM_LIT:1>:<EOL><INDENT>result[k] = v[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>return result<EOL><DEDENT>else:<EOL><INDENT>return multi<EOL><DEDENT>

Convert a MutiDict into a regular dict. If there are more than one value
    for a key, the result will have a list of values for the key. Otherwise it
    will have the plain value.


protocol = (<EOL>request.headers.get('<STR_LIT>') or<EOL>request.headers.get('<STR_LIT>')<EOL>)<EOL>if protocol is None and request.headers.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>protocol = '<STR_LIT>'<EOL><DEDENT>if protocol is None:<EOL><INDENT>return request.url<EOL><DEDENT>url = list(urlparse(request.url))<EOL>url[<NUM_LIT:0>] = protocol<EOL>return urlunparse(url)<EOL>

Since we might be hosted behind a proxy, we need to check the
X-Forwarded-Proto, X-Forwarded-Protocol, or X-Forwarded-SSL headers
to find out what protocol was used to access us.


_keys = ('<STR_LIT:url>', '<STR_LIT:args>', '<STR_LIT>', '<STR_LIT:data>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>')<EOL>assert all(map(_keys.__contains__, keys))<EOL>data = request.data<EOL>form = request.form<EOL>form = semiflatten(request.form)<EOL>try:<EOL><INDENT>_json = json.loads(data.decode('<STR_LIT:utf-8>'))<EOL><DEDENT>except (ValueError, TypeError):<EOL><INDENT>_json = None<EOL><DEDENT>d = dict(<EOL>url=get_url(request),<EOL>args=semiflatten(request.args),<EOL>form=form,<EOL>data=json_safe(data),<EOL>origin=request.headers.get('<STR_LIT>', request.remote_addr),<EOL>headers=get_headers(request),<EOL>files=get_files(request),<EOL>json=_json<EOL>)<EOL>out_d = dict()<EOL>for key in keys:<EOL><INDENT>out_d[key] = d.get(key)<EOL><DEDENT>out_d.update(extras)<EOL>return out_d<EOL>


redirect = dict(headers=dict(location=REDIRECT_LOCATION))<EOL>code_map = {<EOL><NUM_LIT>: redirect,<EOL><NUM_LIT>: redirect,<EOL><NUM_LIT>: redirect,<EOL><NUM_LIT>: dict(data='<STR_LIT>'),<EOL><NUM_LIT>: redirect,<EOL><NUM_LIT>: redirect,<EOL><NUM_LIT>: dict(headers={'<STR_LIT>': '<STR_LIT>'}),<EOL><NUM_LIT>: dict(<EOL>data='<STR_LIT>',<EOL>headers={<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>}<EOL>),<EOL><NUM_LIT>: dict(data=json.dumps({<EOL>'<STR_LIT:message>': '<STR_LIT>',<EOL>'<STR_LIT>': ACCEPTED_MEDIA_TYPES<EOL>}),<EOL>headers={<EOL>'<STR_LIT:Content-Type>': '<STR_LIT:application/json>'<EOL>}),<EOL><NUM_LIT>: dict(headers={'<STR_LIT>': '<STR_LIT>'}),<EOL><NUM_LIT>: dict(  <EOL>data=ASCII_ART,<EOL>headers={<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>}<EOL>),<EOL>}<EOL>r = Response()<EOL>r.status_code = code<EOL>if code in code_map:<EOL><INDENT>m = code_map[code]<EOL>if '<STR_LIT:data>' in m:<EOL><INDENT>r.data = m['<STR_LIT:data>']<EOL><DEDENT>if '<STR_LIT>' in m:<EOL><INDENT>r.headers = m['<STR_LIT>']<EOL><DEDENT><DEDENT>return r<EOL>

Returns response object of given status code.


def check_basic_auth(request, user, passwd):

auth = request.authorization<EOL>return auth and auth.username == user and auth.password == passwd<EOL>

Checks user authentication using HTTP Basic Auth.


if not realm:<EOL><INDENT>realm = u'<STR_LIT>'<EOL><DEDENT>return H(b'<STR_LIT::>'.join([username.encode('<STR_LIT:utf-8>'),<EOL>realm.encode('<STR_LIT:utf-8>'),<EOL>password.encode('<STR_LIT:utf-8>')]))<EOL>

Create HA1 hash by realm, username, password

    HA1 = md5(A1) = MD5(username:realm:password)


if credentails.get('<STR_LIT>') == '<STR_LIT>' or credentails.get('<STR_LIT>') is None:<EOL><INDENT>return H(b'<STR_LIT::>'.join([request['<STR_LIT>'].encode('<STR_LIT:utf-8>'),<EOL>request['<STR_LIT>'].encode('<STR_LIT:utf-8>')]))<EOL><DEDENT>elif credentails.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>for k in '<STR_LIT>', '<STR_LIT>', '<STR_LIT:body>':<EOL><INDENT>if k not in request:<EOL><INDENT>raise ValueError('<STR_LIT>' % k)<EOL><DEDENT><DEDENT>return H('<STR_LIT>' % (request['<STR_LIT>'],<EOL>request['<STR_LIT>'],<EOL>H(request['<STR_LIT:body>'])))<EOL><DEDENT>raise ValueError<EOL>

Create HA2 md5 hash

    If the qop directive's value is "auth" or is unspecified, then HA2:
        HA2 = md5(A2) = MD5(method:digestURI)
    If the qop directive's value is "auth-int" , then HA2 is
        HA2 = md5(A2) = MD5(method:digestURI:MD5(entityBody))


def response(credentails, password, request):

response = None<EOL>HA1_value = HA1(<EOL>credentails.get('<STR_LIT>'),<EOL>credentails.get('<STR_LIT:username>'),<EOL>password<EOL>)<EOL>HA2_value = HA2(credentails, request)<EOL>if credentails.get('<STR_LIT>') is None:<EOL><INDENT>response = H(b'<STR_LIT::>'.join([<EOL>HA1_value.encode('<STR_LIT:utf-8>'),<EOL>credentails.get('<STR_LIT>', '<STR_LIT>').encode('<STR_LIT:utf-8>'),<EOL>HA2_value.encode('<STR_LIT:utf-8>')<EOL>]))<EOL><DEDENT>elif (credentails.get('<STR_LIT>') == '<STR_LIT>' or<EOL>credentails.get('<STR_LIT>') == '<STR_LIT>'):<EOL><INDENT>for k in '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>':<EOL><INDENT>if k not in credentails:<EOL><INDENT>raise ValueError('<STR_LIT>' % k)<EOL><DEDENT><DEDENT>response = H(b'<STR_LIT::>'.join([HA1_value.encode('<STR_LIT:utf-8>'),<EOL>credentails.get('<STR_LIT>').encode('<STR_LIT:utf-8>'),<EOL>credentails.get('<STR_LIT>').encode('<STR_LIT:utf-8>'),<EOL>credentails.get('<STR_LIT>').encode('<STR_LIT:utf-8>'),<EOL>credentails.get('<STR_LIT>').encode('<STR_LIT:utf-8>'),<EOL>HA2_value.encode('<STR_LIT:utf-8>')]))<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return response<EOL>

Compile digest auth response

    If the qop directive's value is "auth" or "auth-int" ,
    then compute the response as follows:
       RESPONSE = MD5(HA1:nonce:nonceCount:clienNonce:qop:HA2)
    Else if the qop directive is unspecified,
    then compute the response as follows:
       RESPONSE = MD5(HA1:nonce:HA2)

    Arguments:
    - `credentails`: credentails dict
    - `password`: request user password
    - `request`: request dict


def check_digest_auth(request, user, passwd):

if request.headers.get('<STR_LIT>'):<EOL><INDENT>credentails = parse_authorization_header(<EOL>request.headers.get('<STR_LIT>')<EOL>)<EOL>if not credentails:<EOL><INDENT>return<EOL><DEDENT>response_hash = response(credentails, passwd,<EOL>dict(uri=request.script_root + request.path,<EOL>body=request.data,<EOL>method=request.method))<EOL>if credentails.get('<STR_LIT>') == response_hash:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>

Check user authentication using HTTP Digest auth


return request.environ['<STR_LIT>'] == '<STR_LIT>'<EOL>

Return true if cookie should have secure attribute


left = None<EOL>right = None<EOL>if not range_header_text:<EOL><INDENT>return left, right<EOL><DEDENT>range_header_text = range_header_text.strip()<EOL>if not range_header_text.startswith('<STR_LIT>'):<EOL><INDENT>return left, right<EOL><DEDENT>components = range_header_text.split('<STR_LIT:=>')<EOL>if len(components) != <NUM_LIT:2>:<EOL><INDENT>return left, right<EOL><DEDENT>components = components[<NUM_LIT:1>].split('<STR_LIT:->')<EOL>try:<EOL><INDENT>right = int(components[<NUM_LIT:1>])<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>left = int(components[<NUM_LIT:0>])<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>return left, right<EOL>

Return a tuple describing the byte range requested in a GET request
    If the range is open ended on the left or right side, then a value of None
    will be set.
    RFC7233:
      http://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7233.html#header.range
    Examples:
      Range : bytes=1024-
      Range : bytes=10-20
      Range : bytes=-999


tracking_enabled = '<STR_LIT>' in os.environ<EOL>return render_template('<STR_LIT>', request=request,<EOL>tracking_enabled=tracking_enabled)<EOL>


return render_template('<STR_LIT>')<EOL>


response = Response()<EOL>response.content = ROBOT_TXT<EOL>response.content_type = '<STR_LIT>'<EOL>return response<EOL>


response = Response()<EOL>response.content = ANGRY_ASCII<EOL>response.content_type = '<STR_LIT>'<EOL>return response<EOL>


return jsonify(origin=request.headers.get('<STR_LIT>',<EOL>request.remote_addr))<EOL>


return jsonify(get_dict(request, '<STR_LIT>'))<EOL>


headers = get_headers(request)<EOL>return jsonify({'<STR_LIT>': headers['<STR_LIT>']})<EOL>


@app.route('<STR_LIT>', methods=('<STR_LIT:GET>', '<STR_LIT>'))<EOL>def view_get(request):

return jsonify(get_dict(request, '<STR_LIT:url>', '<STR_LIT:args>', '<STR_LIT>', '<STR_LIT>'))<EOL>


return jsonify(get_dict(request, '<STR_LIT:url>', '<STR_LIT:args>', '<STR_LIT>', '<STR_LIT:data>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>


return jsonify(get_dict(request, '<STR_LIT>', '<STR_LIT>',<EOL>method=request.method, gzipped=True))<EOL>


return jsonify(get_dict(request, '<STR_LIT>', '<STR_LIT>',<EOL>method=request.method, deflated=True))<EOL>


n = int(n)<EOL>assert n > <NUM_LIT:0><EOL>absolute = request.args.get('<STR_LIT>', '<STR_LIT:false>').lower() == '<STR_LIT:true>'<EOL>if n == <NUM_LIT:1>:<EOL><INDENT>return redirect(app.url_for('<STR_LIT>', _request=request,<EOL>_external=absolute))<EOL><DEDENT>if absolute:<EOL><INDENT>return _redirect(request, '<STR_LIT>', n, True)<EOL><DEDENT>else:<EOL><INDENT>return _redirect(request, '<STR_LIT>', n, False)<EOL><DEDENT>


args = CaseInsensitiveDict(request.args.items())<EOL>response = Response('<STR_LIT>')<EOL>response.status_code = <NUM_LIT><EOL>response.headers['<STR_LIT>'] = args['<STR_LIT:url>'].encode('<STR_LIT:utf-8>')<EOL>return response<EOL>


n = int(n)<EOL>assert n > <NUM_LIT:0><EOL>response = Response('<STR_LIT>')<EOL>response.status_code = <NUM_LIT><EOL>if n == <NUM_LIT:1>:<EOL><INDENT>response.headers['<STR_LIT>'] = url_for('<STR_LIT>')<EOL>return response<EOL><DEDENT>response.headers['<STR_LIT>'] = app.url_for(<EOL>'<STR_LIT>', n=n - <NUM_LIT:1><EOL>)<EOL>return response<EOL>


n = int(n)<EOL>assert n > <NUM_LIT:0><EOL>if n == <NUM_LIT:1>:<EOL><INDENT>return redirect(app.url_for('<STR_LIT>', _request=request,<EOL>_external=True))<EOL><DEDENT>return _redirect(request, '<STR_LIT>', n, True)<EOL>


n = int(n)<EOL>response = get_dict(request, '<STR_LIT:url>', '<STR_LIT:args>', '<STR_LIT>', '<STR_LIT>')<EOL>n = min(n, <NUM_LIT:100>)<EOL>def generate_stream():<EOL><INDENT>for i in range(n):<EOL><INDENT>response['<STR_LIT:id>'] = i<EOL>yield json.dumps(response, default=json_dumps_default) + '<STR_LIT:\n>'<EOL><DEDENT><DEDENT>return Response(generate_stream(), headers={<EOL>'<STR_LIT:Content-Type>': '<STR_LIT:application/json>',<EOL>})<EOL>


@app.route('<STR_LIT>',<EOL>methods=['<STR_LIT:GET>', '<STR_LIT:POST>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>def view_status_code(request, codes):

if '<STR_LIT:U+002C>' not in codes:<EOL><INDENT>code = int(codes)<EOL>return status_code(code)<EOL><DEDENT>choices = []<EOL>for choice in codes.split('<STR_LIT:U+002C>'):<EOL><INDENT>if '<STR_LIT::>' not in choice:<EOL><INDENT>code = choice<EOL>weight = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>code, weight = choice.split('<STR_LIT::>')<EOL><DEDENT>choices.append((int(code), float(weight)))<EOL><DEDENT>code = weighted_choice(choices)<EOL>return status_code(code)<EOL>

Return status code or random status code if more than one are given


headers = Headers(request.args.to_dict())<EOL>response = jsonify(headers)<EOL>while True:<EOL><INDENT>content_len_shown = response.headers['<STR_LIT>']<EOL>d = {}<EOL>for key in response.headers.keys():<EOL><INDENT>value = response.headers.get_all(key)<EOL>if len(value) == <NUM_LIT:1>:<EOL><INDENT>value = value[<NUM_LIT:0>]<EOL><DEDENT>d[key] = value<EOL><DEDENT>response = jsonify(d)<EOL>for key, value in headers.to_list():<EOL><INDENT>response.headers.add(key, value)<EOL><DEDENT>if response.headers['<STR_LIT>'] == content_len_shown:<EOL><INDENT>break<EOL><DEDENT><DEDENT>return response<EOL>

Returns a set of response headers from the query string


cookies = dict(request.cookies.items())<EOL>if hide_env and ('<STR_LIT>' not in request.args):<EOL><INDENT>for key in ENV_COOKIES:<EOL><INDENT>try:<EOL><INDENT>del cookies[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>return jsonify(cookies=cookies)<EOL>


return render_template('<STR_LIT>')<EOL>


@app.route('<STR_LIT>')<EOL>def set_cookie(request, name, value):

r = app.make_response(redirect(url_for('<STR_LIT>')))<EOL>r.set_cookie(key=name, value=value, secure=secure_cookie(request))<EOL>return r<EOL>

Sets a cookie and redirects to cookie list.


cookies = dict(request.args.items())<EOL>r = app.make_response(redirect(url_for('<STR_LIT>')))<EOL>for key, value in cookies.items():<EOL><INDENT>r.set_cookie(key=key, value=value, secure=secure_cookie(request))<EOL><DEDENT>return r<EOL>

Sets cookie(s) as provided by the query string
    and redirects to cookie list.


cookies = dict(request.args.items())<EOL>r = app.make_response(redirect(url_for('<STR_LIT>')))<EOL>for key, value in cookies.items():<EOL><INDENT>r.delete_cookie(key=key)<EOL><DEDENT>return r<EOL>

Deletes cookie(s) as provided by the query string
    and redirects to cookie list.


@app.route('<STR_LIT>')<EOL>def basic_auth(request, user='<STR_LIT:user>', passwd='<STR_LIT>'):

if not check_basic_auth(request, user, passwd):<EOL><INDENT>return status_code(<NUM_LIT>)<EOL><DEDENT>return jsonify(authenticated=True, user=user)<EOL>

Prompts the user for authorization using HTTP Basic Auth.


@app.route('<STR_LIT>')<EOL>def hidden_basic_auth(request, user='<STR_LIT:user>', passwd='<STR_LIT>'):


@app.route('<STR_LIT>')<EOL>def digest_auth(request, qop=None, user='<STR_LIT:user>', passwd='<STR_LIT>'):

if qop not in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>qop = None<EOL><DEDENT>if '<STR_LIT>' not in request.headers ornot check_digest_auth(user, passwd) or'<STR_LIT>' not in request.headers:<EOL><INDENT>response = app.make_response('<STR_LIT>')<EOL>response.status_code = <NUM_LIT><EOL>nonce = H(b'<STR_LIT>'.join([<EOL>getattr(request, '<STR_LIT>', u'<STR_LIT>').encode('<STR_LIT:ascii>'),<EOL>b'<STR_LIT::>',<EOL>str(time.time()).encode('<STR_LIT:ascii>'),<EOL>b'<STR_LIT::>',<EOL>os.urandom(<NUM_LIT:10>)<EOL>]))<EOL>opaque = H(os.urandom(<NUM_LIT:10>))<EOL>auth = WWWAuthenticate('<STR_LIT>')<EOL>auth.set_digest('<STR_LIT>', nonce, opaque=opaque,<EOL>qop=('<STR_LIT>', '<STR_LIT>') if qop is None else (qop, ))<EOL>response.headers['<STR_LIT>'] = auth.to_header()<EOL>response.headers['<STR_LIT>'] = '<STR_LIT>'<EOL>return response<EOL><DEDENT>return jsonify(authenticated=True, user=user)<EOL>

Prompts the user for authorization using HTTP Digest auth


delay = min(float(delay), <NUM_LIT:10>)<EOL>time.sleep(delay)<EOL>return jsonify(get_dict(request, '<STR_LIT:url>', '<STR_LIT:args>', '<STR_LIT>', '<STR_LIT:data>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>


args = CaseInsensitiveDict(request.args.items())<EOL>duration = float(args.get('<STR_LIT>', <NUM_LIT:2>))<EOL>numbytes = int(args.get('<STR_LIT>', <NUM_LIT:10>))<EOL>code = int(args.get('<STR_LIT:code>', <NUM_LIT:200>))<EOL>pause = duration / numbytes<EOL>delay = float(args.get('<STR_LIT>', <NUM_LIT:0>))<EOL>if delay > <NUM_LIT:0>:<EOL><INDENT>time.sleep(delay)<EOL><DEDENT>def generate_bytes():<EOL><INDENT>for i in xrange(numbytes):<EOL><INDENT>yield u'<STR_LIT:*>'.encode('<STR_LIT:utf-8>')<EOL>time.sleep(pause)<EOL><DEDENT><DEDENT>response = Response(generate_bytes(), headers={<EOL>'<STR_LIT:Content-Type>': '<STR_LIT>',<EOL>'<STR_LIT>': str(numbytes),<EOL>})<EOL>response.status_code = code<EOL>return response<EOL>

Drips data over a duration after an optional initial delay.


encoded = value.encode('<STR_LIT:utf-8>')  <EOL>return base64.urlsafe_b64decode(encoded).decode('<STR_LIT:utf-8>')<EOL>


is_conditional = (<EOL>request.headers.get('<STR_LIT>') or<EOL>request.headers.get('<STR_LIT>')<EOL>)<EOL>if is_conditional is None:<EOL><INDENT>response = view_get(request)<EOL>response.headers['<STR_LIT>'] = http_date()<EOL>response.headers['<STR_LIT>'] = uuid.uuid4().hex<EOL>return response<EOL><DEDENT>else:<EOL><INDENT>return status_code(<NUM_LIT>)<EOL><DEDENT>

Returns a 304 if an If-Modified-Since header or
    If-None-Match is present. Returns the same as a GET otherwise.


value = int(value)<EOL>response = view_get(request)<EOL>response.headers['<STR_LIT>'] = '<STR_LIT>'.format(value)<EOL>return response<EOL>


n = int(n)<EOL>n = min(n, <NUM_LIT:100> * <NUM_LIT>)  <EOL>params = CaseInsensitiveDict(request.args.items())<EOL>if '<STR_LIT>' in params:<EOL><INDENT>random.seed(int(params['<STR_LIT>']))<EOL><DEDENT>response = Response()<EOL>response.data = bytearray(random.randint(<NUM_LIT:0>, <NUM_LIT:255>) for i in range(n))<EOL>response.content_type = '<STR_LIT>'<EOL>return response<EOL>

Returns n random bytes generated with given seed.


n = int(n)<EOL>n = min(n, <NUM_LIT:100> * <NUM_LIT>)  <EOL>params = CaseInsensitiveDict(request.args.items())<EOL>if '<STR_LIT>' in params:<EOL><INDENT>random.seed(int(params['<STR_LIT>']))<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>chunk_size = max(<NUM_LIT:1>, int(params['<STR_LIT>']))<EOL><DEDENT>else:<EOL><INDENT>chunk_size = <NUM_LIT:10> * <NUM_LIT><EOL><DEDENT>def generate_bytes():<EOL><INDENT>chunks = bytearray()<EOL>for i in xrange(n):<EOL><INDENT>chunks.append(random.randint(<NUM_LIT:0>, <NUM_LIT:255>))<EOL>if len(chunks) == chunk_size:<EOL><INDENT>yield(bytes(chunks))<EOL>chunks = bytearray()<EOL><DEDENT><DEDENT>if chunks:<EOL><INDENT>yield(bytes(chunks))<EOL><DEDENT><DEDENT>headers = {'<STR_LIT:Content-Type>': '<STR_LIT>'}<EOL>return Response(generate_bytes(), headers=headers)<EOL>

Streams n random bytes generated with given seed,
    at given chunk size per packet.


numbytes = int(numbytes)<EOL>if numbytes <= <NUM_LIT:0> or numbytes > (<NUM_LIT:100> * <NUM_LIT>):<EOL><INDENT>response = Response(headers={<EOL>'<STR_LIT>': '<STR_LIT>' % numbytes,<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>})<EOL>response.status_code = <NUM_LIT><EOL>response.content = '<STR_LIT>'<EOL>return response<EOL><DEDENT>params = CaseInsensitiveDict(request.args.items())<EOL>if '<STR_LIT>' in params:<EOL><INDENT>chunk_size = max(<NUM_LIT:1>, int(params['<STR_LIT>']))<EOL><DEDENT>else:<EOL><INDENT>chunk_size = <NUM_LIT:10> * <NUM_LIT><EOL><DEDENT>duration = float(params.get('<STR_LIT>', <NUM_LIT:0>))<EOL>pause_per_byte = duration / numbytes<EOL>request_headers = get_headers(request)<EOL>first_byte_pos, last_byte_pos = get_request_range(request_headers,<EOL>numbytes)<EOL>if (<EOL>first_byte_pos > last_byte_pos or<EOL>first_byte_pos not in xrange(<NUM_LIT:0>, numbytes) or<EOL>last_byte_pos not in xrange(<NUM_LIT:0>, numbytes)<EOL>):<EOL><INDENT>response = Response(headers={<EOL>'<STR_LIT>': '<STR_LIT>' % numbytes,<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>' % numbytes<EOL>})<EOL>response.status_code = <NUM_LIT><EOL>return response<EOL><DEDENT>def generate_bytes():<EOL><INDENT>chunks = bytearray()<EOL>for i in xrange(first_byte_pos, last_byte_pos + <NUM_LIT:1>):<EOL><INDENT>chunks.append(ord('<STR_LIT:a>') + (i % <NUM_LIT>))<EOL>if len(chunks) == chunk_size:<EOL><INDENT>yield(bytes(chunks))<EOL>time.sleep(pause_per_byte * chunk_size)<EOL>chunks = bytearray()<EOL><DEDENT><DEDENT>if chunks:<EOL><INDENT>time.sleep(pause_per_byte * len(chunks))<EOL>yield(bytes(chunks))<EOL><DEDENT><DEDENT>content_range = '<STR_LIT>' % (first_byte_pos, last_byte_pos,<EOL>numbytes)<EOL>response_headers = {<EOL>'<STR_LIT:Content-Type>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>' % numbytes,<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': content_range}<EOL>response = Response(generate_bytes(), headers=response_headers)<EOL>if (first_byte_pos == <NUM_LIT:0>) and (last_byte_pos == (numbytes - <NUM_LIT:1>)):<EOL><INDENT>response.status_code = <NUM_LIT:200><EOL><DEDENT>else:<EOL><INDENT>response.status_code = <NUM_LIT><EOL><DEDENT>return response<EOL>


@app.route('<STR_LIT>')<EOL>def link_page(request, n, offset):

n = int(n)<EOL>offset = int(offset)<EOL>n = min(max(<NUM_LIT:1>, n), <NUM_LIT:200>)  <EOL>link = "<STR_LIT>"<EOL>html = ['<STR_LIT>']<EOL>for i in xrange(n):<EOL><INDENT>if i == offset:<EOL><INDENT>html.append('<STR_LIT>'.format(i))<EOL><DEDENT>else:<EOL><INDENT>html.append(link.format(url_for('<STR_LIT>', n=n, offset=i), i))<EOL><DEDENT><DEDENT>html.append('<STR_LIT>')<EOL>return '<STR_LIT>'.join(html)<EOL>

Generate a page containing n links to other pages which do the same.


n = int(n)<EOL>return redirect(url_for('<STR_LIT>', n=n, offset=<NUM_LIT:0>))<EOL>


headers = get_headers(request)<EOL>if '<STR_LIT>' not in headers:<EOL><INDENT>return image_png(request)  <EOL><DEDENT>accept = headers['<STR_LIT>'].lower()<EOL>if '<STR_LIT>' in accept:<EOL><INDENT>return image_webp(request)<EOL><DEDENT>elif '<STR_LIT>' in accept:<EOL><INDENT>return image_svg(request)<EOL><DEDENT>elif '<STR_LIT>' in accept:<EOL><INDENT>return image_jpeg(request)<EOL><DEDENT>elif '<STR_LIT>' in accept or '<STR_LIT>' in accept:<EOL><INDENT>return image_png(request)<EOL><DEDENT>else:<EOL><INDENT>return status_code(<NUM_LIT>)<EOL><DEDENT>

Returns a simple image of the type suggest by the Accept header.


values, weights = zip(*choices)<EOL>total = <NUM_LIT:0><EOL>cum_weights = []<EOL>for w in weights:<EOL><INDENT>total += w<EOL>cum_weights.append(total)<EOL><DEDENT>x = random.uniform(<NUM_LIT:0>, total)<EOL>i = bisect.bisect(cum_weights, x)<EOL>return values[i]<EOL>

Returns a value from choices chosen by weighted random selection

    choices should be a list of (value, weight) tuples.

    eg. weighted_choice([('val1', 5), ('val2', 0.3), ('val3', 1)])


<EOL>env = self.base_environ = {}<EOL>env['<STR_LIT>'] = self.server_name<EOL>env['<STR_LIT>'] = '<STR_LIT>'<EOL>env['<STR_LIT>'] = str(self.server_port)<EOL>env['<STR_LIT>'] = '<STR_LIT>'<EOL>env['<STR_LIT>'] = '<STR_LIT>'<EOL>env['<STR_LIT>'] = '<STR_LIT>'<EOL>


env = self.base_environ.copy()<EOL>env['<STR_LIT>'] = self.request_method<EOL>if '<STR_LIT:?>' in self.path:<EOL><INDENT>path, query = self.path.split('<STR_LIT:?>', <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>path, query = self.path, '<STR_LIT>'<EOL><DEDENT>env['<STR_LIT>'] = urllib.parse.unquote(path)<EOL>env['<STR_LIT>'] = query<EOL>env['<STR_LIT>'] = self.headers.get('<STR_LIT:Content-Type>', '<STR_LIT>')<EOL>env['<STR_LIT>'] = self.headers.get('<STR_LIT>', '<STR_LIT:0>')<EOL>env['<STR_LIT>'] = self.request_version<EOL>env['<STR_LIT>'] = self.client_address[<NUM_LIT:0>]<EOL>env['<STR_LIT>'] = self.client_address[<NUM_LIT:1>]<EOL>env['<STR_LIT>'] = (<NUM_LIT:1>, <NUM_LIT:0>)<EOL>env['<STR_LIT>'] = '<STR_LIT:http>'<EOL>env['<STR_LIT>'] = io.BytesIO(self.raw_request)<EOL>env['<STR_LIT>'] = sys.stderr<EOL>env['<STR_LIT>'] = False<EOL>env['<STR_LIT>'] = True<EOL>env['<STR_LIT>'] = False<EOL>for k, v in self.headers.items():<EOL><INDENT>k = k.replace('<STR_LIT:->', '<STR_LIT:_>').upper()<EOL>if k in env:<EOL><INDENT>continue<EOL><DEDENT>env['<STR_LIT>' + k] = v<EOL><DEDENT>return env<EOL>


for url_match, func_pair in self._urls_regex_map.items():<EOL><INDENT>m = url_match.match(path)<EOL>if m is not None:<EOL><INDENT>return func_pair.func, func_pair.methods, m.groupdict()<EOL><DEDENT><DEDENT>return None, None, None<EOL>


groupdict = match.groupdict()<EOL>_type = groupdict.get('<STR_LIT:type>')<EOL>type_regex = cls.TYPE_REGEX_MAP.get(_type, '<STR_LIT>')<EOL>name = groupdict.get('<STR_LIT:name>')<EOL>return r'<STR_LIT>'.format(<EOL>name=name, type_regex=type_regex<EOL>)<EOL>


def cookie_dump(key, value='<STR_LIT>', max_age=None, expires=None, path='<STR_LIT:/>',<EOL>domain=None, secure=False, httponly=False):

cookie = SimpleCookie()<EOL>cookie[key] = value<EOL>for attr in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT:path>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>attr_key = attr.replace('<STR_LIT:_>', '<STR_LIT:->')<EOL>attr_value = locals()[attr]<EOL>if attr_value:<EOL><INDENT>cookie[key][attr_key] = attr_value<EOL><DEDENT><DEDENT>return cookie<EOL>


mean = HTTP_STATUS_CODES.get(code, '<STR_LIT>').upper()<EOL>return '<STR_LIT>'.format(code=code, mean=mean)<EOL>


return self.environ['<STR_LIT>']<EOL>


http_cookie = self.environ.get('<STR_LIT>', '<STR_LIT>')<EOL>_cookies = {<EOL>k: v.value<EOL>for (k, v) in SimpleCookie(http_cookie).items()<EOL>}<EOL>return _cookies<EOL>


requested_with = self.headers.get('<STR_LIT>', '<STR_LIT>').lower()<EOL>return requested_with == '<STR_LIT>'<EOL>

The ``X-Requested-With`` header equal to ``HttpRequest``


globals_dict = globals_dict or {}<EOL>globals_dict.setdefault('<STR_LIT>', {})<EOL>exec(self._code, globals_dict)<EOL>return globals_dict<EOL>


match = self.re_extends.match(text)<EOL>if match:<EOL><INDENT>extra_text = self.re_extends.sub('<STR_LIT>', text, count=<NUM_LIT:1>)<EOL>blocks = self.get_blocks(extra_text)<EOL>path = os.path.join(self.base_dir, match.group('<STR_LIT:path>'))<EOL>with open(path, encoding='<STR_LIT:utf-8>') as fp:<EOL><INDENT>return self.replace_blocks_in_extends(fp.read(), blocks)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

replace all blocks in extends with current blocks


self.code_builder.add_line('<STR_LIT>',<EOL>self.result_var, '<STR_LIT:U+002C>'.join(self.buffered))<EOL>self.buffered = []<EOL>


def strip_token(self, text, start, end):

text = text.replace(start, '<STR_LIT>', <NUM_LIT:1>)<EOL>text = text.replace(end, '<STR_LIT>', <NUM_LIT:1>)<EOL>return text<EOL>


return re.sub(r'<STR_LIT>', r'<STR_LIT>', text)<EOL>

cleanup extra whitespaces let numbers of whitespaces <=1


return '<STR_LIT>'.format(self), value<EOL>


@classmethod<EOL><INDENT>def __prepare__(cls, name, bases):<DEDENT>

return collections.OrderedDict()<EOL>


return self._router.get_func(path)<EOL>

url -> view

        :return: (func, methods, func_kwargs)


def __call__(self, environ, start_response):

self.start_response = start_response<EOL>path = environ['<STR_LIT>']<EOL>method = environ['<STR_LIT>']<EOL>func, methods, func_kwargs = self.url_resolve(path)<EOL>try:<EOL><INDENT>if func is None:<EOL><INDENT>self.notfound()<EOL><DEDENT>if method not in methods:<EOL><INDENT>self.abort(<NUM_LIT>)<EOL><DEDENT>request = Request(environ)<EOL>result = self.handle_before_request_hooks(request, view_func=func)<EOL>if isinstance(result, Response):<EOL><INDENT>response = result<EOL><DEDENT>else:<EOL><INDENT>response = self.handle_view(request, func, func_kwargs)<EOL><DEDENT>self.handle_after_request_hooks(request, response, view_func=func)<EOL><DEDENT>except HTTPException as ex:<EOL><INDENT>response = ex.response<EOL><DEDENT>return self._start_response(response)<EOL>


if '<STR_LIT>' not in metadata:<EOL><INDENT>metadata['<STR_LIT>'] = []<EOL><DEDENT>self.metadata = metadata<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>self.uri_generator = kwargs.get('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>self.uri_generator = DefaultUrnGenerator(self.metadata.get('<STR_LIT:id>'))<EOL><DEDENT>if '<STR_LIT>' in kwargs:<EOL><INDENT>self.concept_scheme = kwargs.get('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>self.concept_scheme = ConceptScheme(<EOL>uri=DefaultConceptSchemeUrnGenerator().generate(<EOL>id=self.metadata.get('<STR_LIT:id>')<EOL>)<EOL>)<EOL><DEDENT>

Create a new provider and register some metadata.

        :param uri_generator: An object that implements the
            :class:`skosprovider.uri.UriGenerator` interface.
        :param concept_scheme: A :class:`~skosprovider.skos.ConceptScheme`. If
            not present, a default :class:`~skosprovider.skos.ConceptScheme`
            will be created with a uri generated by the
            :class:`~skosprovider.uri.DefaultConceptSchemeUrnGenerator` in
            combination with the provider `id`.
        :param dict metadata: Metadata essential to this provider. Possible
            metadata:

                * `id`: A unique identifier for the vocabulary. Required.
                * `default_language`: Used to determine what language to use when \
                    returning labels if no language is specified. Will default \
                    to `en` if not specified.
                * `subject`: A list of subjects or tags that define what the \
                    provider is about or what the provider can handle. This \
                    information can then be used when querying a \
                    :class:`~skosprovider.registry.Registry` for providers.
                * `dataset`: A :class:`dict` detailing the dataset the \
                    conceptscheme and all concepts and collections are part of. \
                    Currently the contents of the dictionary are undefined \
                    except for a :term:`uri` attribute that must be present.


return kwargs.get(<EOL>'<STR_LIT>',<EOL>self.metadata.get('<STR_LIT>', '<STR_LIT>')<EOL>)<EOL>

Determine what language to render labels in.

        Will first check if there's a language keyword specified in **kwargs.
        If not, will check the default language of the provider. If there's no
        default language, will fall back to 'en'.

        :rtype: str


return kwargs.get('<STR_LIT>', None)<EOL>

Determine on what attribute to sort.

        :rtype: str


return kwargs.get('<STR_LIT>', '<STR_LIT>')<EOL>

Determine the sort order.

        :rtype: str
        :returns: 'asc' or 'desc'


def _sort(self, concepts, sort=None, language='<STR_LIT>', reverse=False):

sorted = copy.copy(concepts)<EOL>if sort:<EOL><INDENT>sorted.sort(key=methodcaller('<STR_LIT>', sort, language), reverse=reverse)<EOL><DEDENT>return sorted<EOL>

Returns a sorted version of a list of concepts. Will leave the original
list unsorted.

:param list concepts: A list of concepts and collections.
:param string sort: What to sort on: `id`, `label` or `sortlabel`
:param string language: Language to use when sorting on `label` or
    `sortlabel`.
:param boolean reverse: Reverse the sort order?
:rtype: list


return self.metadata.get('<STR_LIT:id>')<EOL>

Get an identifier for the vocabulary.

        :rtype: String or number.


return self.metadata<EOL>

Get some metadata on the provider or the vocab it represents.

        :rtype: Dict.


Get all information on a concept or collection, based on id.

        Providers should assume that all id's passed are strings. If a provider
        knows that internally it uses numeric identifiers, it's up to the
        provider to do the typecasting. Generally, this should not be done by
        changing the id's themselves (eg. from int to str), but by doing the
        id comparisons in a type agnostic way.

        Since this method could be used to find both concepts and collections,
        it's assumed that there are no id collisions between concepts and
        collections.

        :rtype: :class:`skosprovider.skos.Concept` or
            :class:`skosprovider.skos.Collection` or `False` if the concept or
            collection is unknown to the provider.


Get all information on a concept or collection, based on a
        :term:`URI`.

        :rtype: :class:`skosprovider.skos.Concept` or
            :class:`skosprovider.skos.Collection` or `False` if the concept or
            collection is unknown to the provider.


Returns all concepts and collections in this provider.

        :param string language: Optional. If present, it should be a
            :term:`language-tag`. This language-tag is passed on to the
            underlying providers and used when selecting the label to display
            for each concept.
        :param string sort: Optional. If present, it should either be `id`,
            `label` or `sortlabel`. The `sortlabel` option means the providers should
            take into account any `sortLabel` if present, if not it will
            fallback to a regular label to sort on.
        :param string sort_order: Optional. What order to sort in: `asc` or
            `desc`. Defaults to `asc`

        :returns: A :class:`lst` of concepts and collections. Each of these is a dict
            with the following keys:

            * id: id within the conceptscheme
            * uri: :term:`uri` of the concept or collection
            * type: concept or collection
            * label: A label to represent the concept or collection. It is \
                determined by looking at the `language` parameter, the default \
                language of the provider and finally falls back to `en`.


Returns all top-level concepts in this provider.

Top-level concepts are concepts that have no broader concepts
themselves. They might have narrower concepts, but this is not
mandatory.

:param string language: Optional. If present, it should be a
    :term:`language-tag`. This language-tag is passed on to the
    underlying providers and used when selecting the label to display
    for each concept.
:param string sort: Optional. If present, it should either be `id`,
    `label` or `sortlabel`. The `sortlabel` option means the providers should
    take into account any `sortLabel` if present, if not it will
    fallback to a regular label to sort on.
:param string sort_order: Optional. What order to sort in: `asc` or
    `desc`. Defaults to `asc`

:returns: A :class:`lst` of concepts, NOT collections. Each of these
    is a dict with the following keys:

    * id: id within the conceptscheme
    * uri: :term:`uri` of the concept or collection
    * type: concept or collection
    * label: A label to represent the concept or collection. It is \
        determined by looking at the `language` parameter, the default \
        language of the provider and finally falls back to `en`.


@abc.abstractmethod<EOL><INDENT>def find(self, query, **kwargs):<DEDENT>

Find concepts that match a certain query.

        Currently query is expected to be a dict, so that complex queries can
        be passed. You can use this dict to search for concepts or collections
        with a certain label, with a certain type and for concepts that belong
        to a certain collection.

        .. code-block:: python

            # Find anything that has a label of church.
            provider.find({'label': 'church'})

            # Find all concepts that are a part of collection 5.
            provider.find({'type': 'concept', 'collection': {'id': 5})

            # Find all concepts, collections or children of these
            # that belong to collection 5.
            provider.find({'collection': {'id': 5, 'depth': 'all'})

            # Find anything that has a label of church.
            # Preferentially display a label in Dutch.
            provider.find({'label': 'church'}, language='nl')

        :param query: A dict that can be used to express a query. The following
            keys are permitted:

            * `label`: Search for something with this label value. An empty \
                label is equal to searching for all concepts.
            * `type`: Limit the search to certain SKOS elements. If not \
                present or `None`, `all` is assumed:

                * `concept`: Only return :class:`skosprovider.skos.Concept` \
                    instances.
                * `collection`: Only return \
                    :class:`skosprovider.skos.Collection` instances.
                * `all`: Return both :class:`skosprovider.skos.Concept` and \
                    :class:`skosprovider.skos.Collection` instances.
            * `collection`: Search only for concepts belonging to a certain \
                collection. This argument should be a dict with two keys:

                * `id`: The id of a collection. Required.
                * `depth`: Can be `members` or `all`. Optional. If not \
                    present, `members` is assumed, meaning only concepts or \
                    collections that are a direct member of the collection \
                    should be considered. When set to `all`, this method \
                    should return concepts and collections that are a member \
                    of the collection or are a narrower concept of a member \
                    of the collection.

        :param string language: Optional. If present, it should be a
            :term:`language-tag`. This language-tag is passed on to the
            underlying providers and used when selecting the label to display
            for each concept.
        :param string sort: Optional. If present, it should either be `id`,
            `label` or `sortlabel`. The `sortlabel` option means the providers should
            take into account any `sortLabel` if present, if not it will
            fallback to a regular label to sort on.
        :param string sort_order: Optional. What order to sort in: `asc` or
            `desc`. Defaults to `asc`

        :returns: A :class:`lst` of concepts and collections. Each of these
            is a dict with the following keys:

            * id: id within the conceptscheme
            * uri: :term:`uri` of the concept or collection
            * type: concept or collection
            * label: A label to represent the concept or collection. It is \
                determined by looking at the `language` parameter, the default \
                language of the provider and finally falls back to `en`.


Expand a concept or collection to all it's narrower
        concepts.

        This method should recurse and also return narrower concepts
        of narrower concepts.

        If the id passed belongs to a :class:`skosprovider.skos.Concept`,
        the id of the concept itself should be include in the return value.

        If the id passed belongs to a :class:`skosprovider.skos.Collection`,
        the id of the collection itself must not be present in the return value
        In this case the return value includes all the member concepts and
        their narrower concepts.

        :param id: A concept or collection id.
        :rtype: A list of id's or `False` if the concept or collection doesn't
            exist.


Returns all concepts or collections that form the top-level of a
display hierarchy.

As opposed to the :meth:`get_top_concepts`, this method can possibly
return both concepts and collections.

:param string language: Optional. If present, it should be a
    :term:`language-tag`. This language-tag is passed on to the
    underlying providers and used when selecting the label to display
    for each concept.
:param string sort: Optional. If present, it should either be `id`,
    `label` or `sortlabel`. The `sortlabel` option means the providers should
    take into account any `sortLabel` if present, if not it will
    fallback to a regular label to sort on.
:param string sort_order: Optional. What order to sort in: `asc` or
    `desc`. Defaults to `asc`

:returns: A :class:`lst` of concepts and collections. Each of these
    is a dict with the following keys:

    * id: id within the conceptscheme
    * uri: :term:`uri` of the concept or collection
    * type: concept or collection
    * label: A label to represent the concept or collection. It is\
        determined by looking at the `language` parameter, the default\
        language of the provider and finally falls back to `en`.


def get_children_display(self, id, **kwargs):

Return a list of concepts or collections that should be displayed
under this concept or collection.

:param string language: Optional. If present, it should be a
    :term:`language-tag`. This language-tag is passed on to the
    underlying providers and used when selecting the label to display
    for each concept.
:param string sort: Optional. If present, it should either be `id`,
    `label` or `sortlabel`. The `sortlabel` option means the providers should
    take into account any `sortLabel` if present, if not it will
    fallback to a regular label to sort on.
:param string sort_order: Optional. What order to sort in: `asc` or
    `desc`. Defaults to `asc`

:param str id: A concept or collection id.
:returns: A :class:`lst` of concepts and collections. Each of these
    is a dict with the following keys:

    * id: id within the conceptscheme
    * uri: :term:`uri` of the concept or collection
    * type: concept or collection
    * label: A label to represent the concept or collection. It is \
        determined by looking at the `language` parameter, the default \
        language of the provider and finally falls back to `en`.


def __init__(self, metadata, list, **kwargs):

super(MemoryProvider, self).__init__(metadata, **kwargs)<EOL>self.list = list<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>self.case_insensitive = kwargs['<STR_LIT>']<EOL><DEDENT>

:param dict metadata: A dictionary with keywords like language.
:param list list: A list of :class:`skosprovider.skos.Concept` and
    :class:`skosprovider.skos.Collection` instances.
:param Boolean case_insensitive: Should searching for labels be done
    case-insensitive?


if '<STR_LIT:type>' in query and query['<STR_LIT:type>'] not in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>del query['<STR_LIT:type>']<EOL><DEDENT>return query<EOL>

:param query: A dict that can be used to express a query.
:rtype: dict


include = True<EOL>if include and '<STR_LIT:type>' in query:<EOL><INDENT>include = query['<STR_LIT:type>'] == c.type<EOL><DEDENT>if include and '<STR_LIT:label>' in query:<EOL><INDENT>def finder(l, query):<EOL><INDENT>if not self.case_insensitive:<EOL><INDENT>return l.label.find(query['<STR_LIT:label>'])<EOL><DEDENT>else:<EOL><INDENT>return l.label.upper().find(query['<STR_LIT:label>'].upper())<EOL><DEDENT><DEDENT>include = any([finder(l, query) >= <NUM_LIT:0> for l in c.labels])<EOL><DEDENT>if include and '<STR_LIT>' in query:<EOL><INDENT>coll = self.get_by_id(query['<STR_LIT>']['<STR_LIT:id>'])<EOL>if not coll or not isinstance(coll, Collection):<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>if '<STR_LIT>' in query['<STR_LIT>'] and query['<STR_LIT>']['<STR_LIT>'] == '<STR_LIT:all>':<EOL><INDENT>members = self.expand(coll.id)<EOL><DEDENT>else:<EOL><INDENT>members = coll.members<EOL><DEDENT>include = any([True for id in members if str(id) == str(c.id)]) <EOL><DEDENT>return include<EOL>

:param c: A :class:`skosprovider.skos.Concept` or
    :class:`skosprovider.skos.Collection`.
:param query: A dict that can be used to express a query.
:rtype: boolean


language = self._get_language(**kwargs)<EOL>return {<EOL>'<STR_LIT:id>': c.id,<EOL>'<STR_LIT>': c.uri,<EOL>'<STR_LIT:type>': c.type,<EOL>'<STR_LIT:label>': None if c.label() is None else c.label(language).label<EOL>}<EOL>

Return a dict that can be used in the return list of the :meth:`find`
method.

:param c: A :class:`skosprovider.skos.Concept` or
    :class:`skosprovider.skos.Collection`.
:rtype: dict


def __init__(self, metadata, reader, **kwargs):

super(SimpleCsvProvider, self).__init__(metadata, [], **kwargs)<EOL>self.list = [self._from_row(row) for row in reader]<EOL>

:param metadata: A metadata dictionary.
:param reader: A csv reader.


def label(labels=[], language='<STR_LIT>', sortLabel=False):

if not labels:<EOL><INDENT>return None<EOL><DEDENT>if not language:<EOL><INDENT>language = '<STR_LIT>'<EOL><DEDENT>labels = [dict_to_label(l) for l in labels]<EOL>l = False<EOL>if sortLabel:<EOL><INDENT>l = find_best_label_for_type(labels, language, '<STR_LIT>')<EOL><DEDENT>if not l:<EOL><INDENT>l = find_best_label_for_type(labels, language, '<STR_LIT>')<EOL><DEDENT>if not l:<EOL><INDENT>l = find_best_label_for_type(labels, language, '<STR_LIT>')<EOL><DEDENT>if l:<EOL><INDENT>return l<EOL><DEDENT>else:<EOL><INDENT>return label(labels, '<STR_LIT>', sortLabel) if language != '<STR_LIT>' else None<EOL><DEDENT>

Provide a label for a list of labels.

The items in the list of labels are assumed to be either instances of
:class:`Label`, or dicts with at least the key `label` in them. These will
be passed to the :func:`dict_to_label` function.

This method tries to find a label by looking if there's
a pref label for the specified language. If there's no pref label,
it looks for an alt label. It disregards hidden labels.

While matching languages, preference will be given to exact matches. But,
if no exact match is present, an inexact match will be attempted. This might
be because a label in language `nl-BE` is being requested, but only `nl` or
even `nl-NL` is present. Similarly, when requesting `nl`, a label with
language `nl-NL` or even `nl-Latn-NL` will also be considered,
providing no label is present that has an exact match with the
requested language.

If language 'any' was specified, all labels will be considered,
regardless of language.

To find a label without a specified language, pass `None` as language.

If a language or None was specified, and no label could be found, this
method will automatically try to find a label in some other language.

Finally, if no label could be found, None is returned.

:param string language: The preferred language to receive the label in. This
    should be a valid IANA language tag.
:param boolean sortLabel: Should sortLabels be considered or not? If True,
    sortLabels will be preferred over prefLabels. Bear in mind that these
    are still language dependent. So, it's possible to have a different
    sortLabel per language.
:rtype: A :class:`Label` or `None` if no label could be found.


def find_best_label_for_type(labels, language, labeltype):

typelabels = [l for l in labels if l.type == labeltype]<EOL>if not typelabels:<EOL><INDENT>return False<EOL><DEDENT>if language == '<STR_LIT>':<EOL><INDENT>return typelabels[<NUM_LIT:0>]<EOL><DEDENT>exact = filter_labels_by_language(typelabels, language)<EOL>if exact:<EOL><INDENT>return exact[<NUM_LIT:0>]<EOL><DEDENT>inexact = filter_labels_by_language(typelabels, language, True)<EOL>if inexact:<EOL><INDENT>return inexact[<NUM_LIT:0>]<EOL><DEDENT>return False<EOL>

Find the best label for a certain labeltype.

:param list labels: A list of :class:`Label`.
:param str language: An IANA language string, eg. `nl` or `nl-BE`.
:param str labeltype: Type of label to look for, eg. `prefLabel`.


def filter_labels_by_language(labels, language, broader=False):

if language == '<STR_LIT>':<EOL><INDENT>return labels<EOL><DEDENT>if broader:<EOL><INDENT>language = tags.tag(language).language.format<EOL>return [l for l in labels if tags.tag(l.language).language.format == language]<EOL><DEDENT>else:<EOL><INDENT>language = tags.tag(language).format<EOL>return [l for l in labels if tags.tag(l.language).format == language]<EOL><DEDENT>

Filter a list of labels, leaving only labels of a certain language.

:param list labels: A list of :class:`Label`.
:param str language: An IANA language string, eg. `nl` or `nl-BE`.
:param boolean broader: When true, will also match `nl-BE` when filtering
    on `nl`. When false, only exact matches are considered.


try:<EOL><INDENT>return Label(<EOL>dict['<STR_LIT:label>'],<EOL>dict.get('<STR_LIT:type>', '<STR_LIT>'),<EOL>dict.get('<STR_LIT>', '<STR_LIT>')<EOL>)<EOL><DEDENT>except (KeyError, AttributeError, TypeError):<EOL><INDENT>return dict<EOL><DEDENT>

Transform a dict with keys `label`, `type` and `language` into a
:class:`Label`.

Only the `label` key is mandatory. If `type` is not present, it will
default to `prefLabel`. If `language` is not present, it will default
to `und`.

If the argument passed is not a dict, this method just
returns the argument.


if isinstance(dict, Note):<EOL><INDENT>return dict<EOL><DEDENT>return Note(<EOL>dict['<STR_LIT>'],<EOL>dict.get('<STR_LIT:type>', '<STR_LIT>'),<EOL>dict.get('<STR_LIT>', '<STR_LIT>'),<EOL>dict.get('<STR_LIT>')<EOL>)<EOL>

Transform a dict with keys `note`, `type` and `language` into a
:class:`Note`.

Only the `note` key is mandatory. If `type` is not present, it will
default to `note`. If `language` is not present, it will default to `und`.
If `markup` is not present it will default to `None`.

If the argument passed is already a :class:`Note`, this method just returns
the argument.


if isinstance(dict, Source):<EOL><INDENT>return dict<EOL><DEDENT>return Source(<EOL>dict['<STR_LIT>'],<EOL>dict.get('<STR_LIT>')<EOL>)<EOL>

Transform a dict with key 'citation' into a :class:`Source`.

If the argument passed is already a :class:`Source`, this method just
returns the argument.


return type in Label.valid_types<EOL>

Check if the argument is a valid SKOS label type.

:param string type: The type to be checked.


return type in Note.valid_types<EOL>

Check if the argument is a valid SKOS note type.

:param string type: The type to be checked.


return markup in valid_markup<EOL>

Check the argument is a valid type of markup.

:param string markup: The type to be checked.


return markup in valid_markup<EOL>


return label(self.labels, language)<EOL>

Provide a single label for this conceptscheme.

This uses the :func:`label` function to determine which label to
return.

:param string language: The preferred language to receive the label in.
    This should be a valid IANA language tag.
:rtype: :class:`skosprovider.skos.Label` or False if no labels were found.


def _sortkey(self, key='<STR_LIT>', language='<STR_LIT>'):

if key == '<STR_LIT>':<EOL><INDENT>return self.uri<EOL><DEDENT>else:<EOL><INDENT>l = label(self.labels, language, key == '<STR_LIT>')<EOL>return l.label.lower() if l else '<STR_LIT>'<EOL><DEDENT>

Provide a single sortkey for this conceptscheme.

:param string key: Either `uri`, `label` or `sortlabel`.
:param string language: The preferred language to receive the label in
    if key is `label` or `sortlabel`. This should be a valid IANA language tag.
:rtype: :class:`str`


Provide a single label for this concept.

This uses the :func:`label` function to determine which label to return.

:param string language: The preferred language to receive the label in.
    This should be a valid IANA language tag.
:rtype: :class:`skosprovider.skos.Label` or False if no labels were found.


def _sortkey(self, key='<STR_LIT:id>', language='<STR_LIT>'):

if key == '<STR_LIT:id>':<EOL><INDENT>return str(self.id)<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>return self.uri if self.uri else '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>l = label(self.labels, language, key == '<STR_LIT>')<EOL>return l.label.lower() if l else '<STR_LIT>'<EOL><DEDENT>

Provide a single sortkey for this collection.

:param string key: Either `id`, `uri`, `label` or `sortlabel`.
:param string language: The preferred language to receive the label in
    if key is `label` or `sortlabel`. This should be a valid IANA language tag.
:rtype: :class:`str`


return label(self.labels, language, False)<EOL>

Provide a single label for this collection.

This uses the :func:`label` function to determine which label to return.

:param string language: The preferred language to receive the label in.
    This should be a valid IANA language tag.
:rtype: :class:`skosprovider.skos.Label` or False if no labels were found.


if uri is None:<EOL><INDENT>return False<EOL><DEDENT>return rfc3987.match(uri, rule='<STR_LIT>')<EOL>

Check if a string is a valid URI according to rfc3987

:param string uri:
:rtype: boolean


Generate a :term:`URI` based on parameters passed.


return self.pattern % kwargs['<STR_LIT:id>']<EOL>

Generate a :term:`URI` based on parameters passed.

:param id: The id of the concept or collection.
:rtype: string


return (self.pattern % (self.vocabulary_id, kwargs['<STR_LIT:id>'])).lower()<EOL>


return (self.pattern % (kwargs['<STR_LIT:id>'])).lower()<EOL>

Generate a :term:`URI` based on parameters passed.

:param id: The id of the conceptscheme.
:rtype: string


if kwargs['<STR_LIT:type>'] not in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>raise ValueError('<STR_LIT>' % kwargs['<STR_LIT:type>'])<EOL><DEDENT>return (<EOL>self.pattern % (self.vocabulary_id, kwargs['<STR_LIT:type>'], kwargs['<STR_LIT:id>'])<EOL>).lower()<EOL>

Generate a :term:`URI` based on parameters passed.

:param id: The id of the concept or collection.
:param type: What we're generating a :term:`URI` for: `concept`
    or `collection`.
:rtype: string


if provider.get_vocabulary_id() in self.providers:<EOL><INDENT>raise RegistryException(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>self.providers[provider.get_vocabulary_id()] = provider<EOL>if provider.concept_scheme.uri in self.concept_scheme_uri_map:<EOL><INDENT>raise RegistryException(<EOL>'<STR_LIT>' % provider.concept_scheme.uri<EOL>)<EOL><DEDENT>self.concept_scheme_uri_map[provider.concept_scheme.uri] = provider.get_vocabulary_id()<EOL>

Register a :class:`skosprovider.providers.VocabularyProvider`.

:param skosprovider.providers.VocabularyProvider provider: The provider
    to register.
:raises RegistryException: A provider with this id or uri has already
    been registered.


if id in self.providers:<EOL><INDENT>p = self.providers.get(id, False)<EOL>del self.providers[id]<EOL>del self.concept_scheme_uri_map[p.concept_scheme.uri]<EOL>return p<EOL><DEDENT>elif id in self.concept_scheme_uri_map:<EOL><INDENT>id = self.concept_scheme_uri_map[id]<EOL>return self.remove_provider(id)<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>

Remove the provider with the given id or :term:`URI`.

:param str id: The identifier for the provider.
:returns: A :class:`skosprovider.providers.VocabularyProvider` or
    `False` if the id is unknown.


if id in self.providers:<EOL><INDENT>return self.providers.get(id, False)<EOL><DEDENT>elif is_uri(id) and id in self.concept_scheme_uri_map:<EOL><INDENT>return self.providers.get(self.concept_scheme_uri_map[id], False)<EOL><DEDENT>return False<EOL>

Get a provider by id or :term:`uri`.

:param str id: The identifier for the provider. This can either be the
    id with which it was registered or the :term:`uri` of the conceptscheme
    that the provider services.
:returns: A :class:`skosprovider.providers.VocabularyProvider`
    or `False` if the id or uri is unknown.


if '<STR_LIT>' in kwargs:<EOL><INDENT>ids = [self.concept_scheme_uri_map.get(id, id) for id in kwargs['<STR_LIT>']]<EOL>providers = [<EOL>self.providers[k] for k in self.providers.keys() if k in ids<EOL>]<EOL><DEDENT>else:<EOL><INDENT>providers = list(self.providers.values())<EOL><DEDENT>if '<STR_LIT>' in kwargs:<EOL><INDENT>providers = [p for p in providers if kwargs['<STR_LIT>'] in p.metadata['<STR_LIT>']]<EOL><DEDENT>return providers<EOL>

Get all providers registered.

        If keyword `ids` is present, get only the providers with these ids.

        If keys `subject` is present, get only the providers that have this subject.

        .. code-block:: python

           # Get all providers with subject 'biology'
           registry.get_providers(subject='biology')

           # Get all providers with id 1 or 2
           registry.get_providers(ids=[1,2])

           # Get all providers with id 1 or 2 and subject 'biology'
           registry.get_providers(ids=[1,2], subject='biology']

        :param list ids: Only return providers with one of the Ids or :term:`URIs <uri>`.
        :param str subject: Only return providers with this subject.
        :returns: A list of :class:`providers <skosprovider.providers.VocabularyProvider>`


if '<STR_LIT>' not in kwargs:<EOL><INDENT>providers = self.get_providers()<EOL><DEDENT>else:<EOL><INDENT>pargs = kwargs['<STR_LIT>']<EOL>if isinstance(pargs, list):<EOL><INDENT>providers = self.get_providers(ids=pargs)<EOL><DEDENT>else:<EOL><INDENT>providers = self.get_providers(**pargs)<EOL><DEDENT><DEDENT>kwarguments = {}<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>kwarguments['<STR_LIT>'] = kwargs['<STR_LIT>']<EOL><DEDENT>return [{'<STR_LIT:id>': p.get_vocabulary_id(), '<STR_LIT>': p.find(query, **kwarguments)}<EOL>for p in providers]<EOL>

Launch a query across all or a selection of providers.

        .. code-block:: python

            # Find anything that has a label of church in any provider.
            registry.find({'label': 'church'})

            # Find anything that has a label of church with the BUILDINGS provider.
            # Attention, this syntax was deprecated in version 0.3.0
            registry.find({'label': 'church'}, providers=['BUILDINGS'])

            # Find anything that has a label of church with the BUILDINGS provider.
            registry.find({'label': 'church'}, providers={'ids': ['BUILDINGS']})

            # Find anything that has a label of church with a provider
            # marked with the subject 'architecture'.
            registry.find({'label': 'church'}, providers={'subject': 'architecture'})

            # Find anything that has a label of church in any provider.
            # If possible, display the results with a Dutch label.
            registry.find({'label': 'church'}, language='nl')

        :param dict query: The query parameters that will be passed on to each
            :meth:`~skosprovider.providers.VocabularyProvider.find` method of
            the selected.
            :class:`providers <skosprovider.providers.VocabularyProvider>`.
        :param dict providers: Optional. If present, it should be a dictionary.
            This dictionary can contain any of the keyword arguments available
            to the :meth:`get_providers` method. The query will then only
            be passed to the providers confirming to these arguments.
        :param string language: Optional. If present, it should be a
            :term:`language-tag`. This language-tag is passed on to the
            underlying providers and used when selecting the label to display
            for each concept.
        :returns: a list of :class:`dict`.
            Each dict has two keys: id and concepts.


kwarguments = {}<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>kwarguments['<STR_LIT>'] = kwargs['<STR_LIT>']<EOL><DEDENT>return [{'<STR_LIT:id>': p.get_vocabulary_id(), '<STR_LIT>': p.get_all(**kwarguments)}<EOL>for p in self.providers.values()]<EOL>

Get all concepts from all providers.

        .. code-block:: python

            # get all concepts in all providers.
            registry.get_all()

            # get all concepts in all providers.
            # If possible, display the results with a Dutch label.
            registry.get_all(language='nl')

        :param string language: Optional. If present, it should be a
            :term:`language-tag`. This language-tag is passed on to the
            underlying providers and used when selecting the label to display
            for each concept.

        :returns: a list of :class:`dict`.
            Each dict has two keys: id and concepts.


if not is_uri(uri):<EOL><INDENT>raise ValueError('<STR_LIT>' % uri)<EOL><DEDENT>csuris = [csuri for csuri in self.concept_scheme_uri_map.keys() if uri.startswith(csuri)]<EOL>for csuri in csuris:<EOL><INDENT>c = self.get_provider(csuri).get_by_uri(uri)<EOL>if c:<EOL><INDENT>return c<EOL><DEDENT><DEDENT>for p in self.providers.values():<EOL><INDENT>c = p.get_by_uri(uri)<EOL>if c:<EOL><INDENT>return c<EOL><DEDENT><DEDENT>return False<EOL>

Get a concept or collection by its uri.

        Returns a single concept or collection if one exists with this uri.
        Returns False otherwise.

        :param string uri: The uri to find a concept or collection for.
        :raises ValueError: The uri is invalid.
        :rtype: :class:`skosprovider.skos.Concept` or
            :class:`skosprovider.skos.Collection`


ret = []<EOL>for stuff in provider.get_all():<EOL><INDENT>c = provider.get_by_id(stuff['<STR_LIT:id>'])<EOL>labels = [l.__dict__ for l in c.labels]<EOL>notes = [n.__dict__ for n in c.notes]<EOL>sources = [s.__dict__ for s in c.sources]<EOL>if isinstance(c, Concept):<EOL><INDENT>ret.append({<EOL>'<STR_LIT:id>': c.id,<EOL>'<STR_LIT>': c.uri,<EOL>'<STR_LIT:type>': c.type,<EOL>'<STR_LIT>': labels,<EOL>'<STR_LIT>': notes,<EOL>'<STR_LIT>': sources,<EOL>'<STR_LIT>': c.narrower,<EOL>'<STR_LIT>': c.broader,<EOL>'<STR_LIT>': c.related,<EOL>'<STR_LIT>': c.member_of,<EOL>'<STR_LIT>': c.subordinate_arrays,<EOL>'<STR_LIT>': c.matches<EOL>})<EOL><DEDENT>elif isinstance(c, Collection):<EOL><INDENT>ret.append({<EOL>'<STR_LIT:id>': c.id,<EOL>'<STR_LIT>': c.uri,<EOL>'<STR_LIT:type>': c.type,<EOL>'<STR_LIT>': labels,<EOL>'<STR_LIT>': notes,<EOL>'<STR_LIT>': sources,<EOL>'<STR_LIT>': c.members,<EOL>'<STR_LIT>': c.member_of,<EOL>'<STR_LIT>': c.superordinates<EOL>})<EOL><DEDENT><DEDENT>return ret<EOL>

Dump a provider to a format that can be passed to a
:class:`skosprovider.providers.DictionaryProvider`.

:param skosprovider.providers.VocabularyProvider provider: The provider
    that wil be turned into a `dict`.
:rtype: A list of dicts.

.. versionadded:: 0.2.0


self.message = message<EOL>

:param message: More information about the exception.


settings = config.registry.settings<EOL>if asbool(settings.get('<STR_LIT>', True)):<EOL><INDENT>LOGGER.debug('<STR_LIT>')<EOL>config.include('<STR_LIT>')<EOL>config.include('<STR_LIT>')<EOL>config.include('<STR_LIT>')<EOL>config.include('<STR_LIT>')<EOL>config.add_xmlrpc_endpoint('<STR_LIT>', '<STR_LIT>')<EOL>config.add_xmlrpc_method(RPCInterface, attr='<STR_LIT>', endpoint='<STR_LIT>', method='<STR_LIT>')<EOL>config.add_xmlrpc_method(RPCInterface, attr='<STR_LIT>', endpoint='<STR_LIT>', method='<STR_LIT>')<EOL>config.add_xmlrpc_method(RPCInterface, attr='<STR_LIT>', endpoint='<STR_LIT>', method='<STR_LIT>')<EOL>config.add_xmlrpc_method(RPCInterface, attr='<STR_LIT>', endpoint='<STR_LIT>', method='<STR_LIT>')<EOL>config.add_xmlrpc_method(RPCInterface, attr='<STR_LIT>', endpoint='<STR_LIT>', method='<STR_LIT>')<EOL>config.add_xmlrpc_method(RPCInterface, attr='<STR_LIT>', endpoint='<STR_LIT>', method='<STR_LIT>')<EOL>config.add_xmlrpc_method(RPCInterface, attr='<STR_LIT>', endpoint='<STR_LIT>', method='<STR_LIT>')<EOL>config.add_xmlrpc_method(RPCInterface, attr='<STR_LIT>', endpoint='<STR_LIT>', method='<STR_LIT>')<EOL>config.add_xmlrpc_method(RPCInterface, attr='<STR_LIT>', endpoint='<STR_LIT>', method='<STR_LIT>')<EOL><DEDENT>

The callable makes it possible to include rpcinterface
    in a Pyramid application.

    Calling ``config.include(twitcher.rpcinterface)`` will result in this
    callable being called.

    Arguments:

    * ``config``: the ``pyramid.config.Configurator`` object.


def generate_token(self, valid_in_hours=<NUM_LIT:1>, environ=None):

return self.tokenmgr.generate_token(valid_in_hours, environ)<EOL>


return self.tokenmgr.revoke_token(token)<EOL>


return self.tokenmgr.revoke_all_tokens()<EOL>


def register_service(self, url, data=None, overwrite=True):

return self.srvreg.register_service(url, data, overwrite)<EOL>


return self.srvreg.unregister_service(name)<EOL>


return self.srvreg.get_service_by_name(name)<EOL>


return self.srvreg.get_service_by_url(url)<EOL>


return self.srvreg.list_services()<EOL>


return self.srvreg.clear_services()<EOL>


try:<EOL><INDENT>service_name = request.matchdict.get('<STR_LIT>')<EOL>extra_path = request.matchdict.get('<STR_LIT>')<EOL>store = servicestore_factory(request.registry)<EOL>service = store.fetch_by_name(service_name)<EOL><DEDENT>except Exception as err:<EOL><INDENT>return OWSAccessFailed("<STR_LIT>".format(service_name, err.message))<EOL><DEDENT>else:<EOL><INDENT>return _send_request(request, service, extra_path, request_params=request.query_string)<EOL><DEDENT>


twitcher_url = request.registry.settings.get('<STR_LIT>')<EOL>protected_path = request.registry.settings.get('<STR_LIT>', '<STR_LIT>')<EOL>url = twitcher_url + protected_path + '<STR_LIT>'<EOL>if request.matchdict.get('<STR_LIT>'):<EOL><INDENT>url += '<STR_LIT:/>' + request.matchdict.get('<STR_LIT>')<EOL>if request.matchdict.get('<STR_LIT>'):<EOL><INDENT>url += '<STR_LIT:/>' + request.matchdict.get('<STR_LIT>')<EOL><DEDENT><DEDENT>url += '<STR_LIT:?>' + urlparse.urlencode(request.params)<EOL>LOGGER.debug("<STR_LIT>", url)<EOL>resp = requests.request(method=request.method.upper(), url=url, data=request.body,<EOL>headers=request.headers, verify=False)<EOL>return Response(resp.content, status=resp.status_code, headers=resp.headers)<EOL>

Delegates owsproxy request to external twitcher service.


def create_access_token(self, valid_in_hours=<NUM_LIT:1>, data=None):

data = data or {}<EOL>token = AccessToken(<EOL>token=self.generate(),<EOL>expires_at=expires_at(hours=valid_in_hours),<EOL>data=data)<EOL>return token<EOL>

Creates an access token.

TODO: check valid in hours
TODO: maybe specify how often a token can be used


return uuid.uuid4().get_hex()<EOL>


def _retrieve_certificate(self, access_token, timeout=<NUM_LIT:3>):

logger.debug("<STR_LIT>")<EOL>key_pair = crypto.PKey()<EOL>key_pair.generate_key(crypto.TYPE_RSA, <NUM_LIT>)<EOL>private_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, key_pair).decode("<STR_LIT:utf-8>")<EOL>cert_request = crypto.X509Req()<EOL>cert_request.set_pubkey(key_pair)<EOL>cert_request.sign(key_pair, '<STR_LIT>')<EOL>der_cert_req = crypto.dump_certificate_request(crypto.FILETYPE_ASN1, cert_request)<EOL>encoded_cert_req = base64.b64encode(der_cert_req)<EOL>token = {'<STR_LIT>': access_token, '<STR_LIT>': '<STR_LIT>'}<EOL>client = OAuth2Session(token=token)<EOL>response = client.post(<EOL>self.certificate_url,<EOL>data={'<STR_LIT>': encoded_cert_req},<EOL>verify=False,<EOL>timeout=timeout,<EOL>)<EOL>if response.ok:<EOL><INDENT>content = "<STR_LIT>".format(response.text, private_key)<EOL>with open(self.esgf_credentials, '<STR_LIT:w>') as fh:<EOL><INDENT>fh.write(content)<EOL><DEDENT>logger.debug('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>".format(response.status_code, response.reason)<EOL>raise Exception(msg)<EOL><DEDENT>return True<EOL>

Generates a new private key and certificate request, submits the request to be
signed by the SLCS CA and returns the certificate.


from pyramid.config import Configurator<EOL>config = Configurator(settings=settings)<EOL>config.include('<STR_LIT>')<EOL>config.include('<STR_LIT>')<EOL>config.include('<STR_LIT>')<EOL>config.include('<STR_LIT>')<EOL>config.include('<STR_LIT>')<EOL>config.scan()<EOL>return config.make_wsgi_app()<EOL>

This function returns a Pyramid WSGI application.


security = owssecurity_factory(registry)<EOL>def ows_security_tween(request):<EOL><INDENT>try:<EOL><INDENT>security.check_request(request)<EOL>return handler(request)<EOL><DEDENT>except OWSException as err:<EOL><INDENT>logger.exception("<STR_LIT>")<EOL>return err<EOL><DEDENT>except Exception as err:<EOL><INDENT>logger.exception("<STR_LIT>")<EOL>return OWSNoApplicableCode("<STR_LIT:{}>".format(err))<EOL><DEDENT><DEDENT>return ows_security_tween<EOL>

A tween factory which produces a tween which raises an exception
    if access to OWS service is not allowed.


return int(time.time())<EOL>

Return the current time in seconds since the Epoch.


tz_aware_dt = dt<EOL>if dt.tzinfo is None:<EOL><INDENT>utc = pytz.timezone('<STR_LIT>')<EOL>aware = utc.localize(dt)<EOL>timezone = pytz.timezone(tz_name)<EOL>tz_aware_dt = aware.astimezone(timezone)<EOL><DEDENT>else:<EOL><INDENT>logger.warn('<STR_LIT>')<EOL><DEDENT>return tz_aware_dt<EOL>

Provide a timzeone-aware object for a given datetime and timezone name


parsed_url = urlparse.urlparse(url)<EOL>if not parsed_url.netloc or parsed_url.scheme not in ("<STR_LIT:http>", "<STR_LIT>"):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>service_url = "<STR_LIT>" % (parsed_url.scheme, parsed_url.netloc, parsed_url.path.strip())<EOL>return service_url<EOL>


return self['<STR_LIT:url>']<EOL>


return self.get('<STR_LIT:name>', '<STR_LIT>')<EOL>


return self.get('<STR_LIT:type>', '<STR_LIT>')<EOL>


return self.get('<STR_LIT>', '<STR_LIT>')<EOL>


return is_valid_url(self.purl)<EOL>

Return true if we have a valid public URL (purl).


<EOL>return self.get('<STR_LIT>', False)<EOL>


Authentication method: public, token, cert.


value = self.get('<STR_LIT>', '<STR_LIT:true>')<EOL>if isinstance(value, bool):<EOL><INDENT>verify = value<EOL><DEDENT>elif value.lower() == '<STR_LIT:true>':<EOL><INDENT>verify = True<EOL><DEDENT>elif value.lower() == '<STR_LIT:false>':<EOL><INDENT>verify = False<EOL><DEDENT>else:<EOL><INDENT>verify = value<EOL><DEDENT>return verify<EOL>


return self['<STR_LIT>']<EOL>


time_left = self.expires_at - now_secs()<EOL>if time_left > <NUM_LIT:0>:<EOL><INDENT>return time_left<EOL><DEDENT>return <NUM_LIT:0><EOL>

Returns the time until the token expires.
:return: The remaining time until expiration in seconds or 0 if the
         token has expired.


if self.expires_at is None:<EOL><INDENT>return True<EOL><DEDENT>if self.expires_in > <NUM_LIT:0>:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Determines if the token has expired.
:return: `True` if the token has expired. Otherwise `False`.


raise NotImplementedError<EOL>

Stores an access token with additional data.


raise NotImplementedError<EOL>

Deletes an access token from the store using its token string to identify it.
This invalidates both the access token and the token.

:param token: A string containing the token.
:return: None.


raise NotImplementedError<EOL>

Fetches an access token from the store using its token string to
identify it.

:param token: A string containing the token.
:return: An instance of :class:`twitcher.datatype.AccessToken`.


raise NotImplementedError<EOL>


def save_service(self, service, overwrite=True):

raise NotImplementedError<EOL>

Stores an OWS service in storage.

:param service: An instance of :class:`twitcher.datatype.Service`.


raise NotImplementedError<EOL>

Get service for given ``name`` from storage.

:param token: A string containing the service name.
:return: An instance of :class:`twitcher.datatype.Service`.


raise NotImplementedError<EOL>

Get service for given ``url`` from storage.

:param token: A string containing the service url.
:return: An instance of :class:`twitcher.datatype.Service`.


name = namesgenerator.get_sane_name(service.name)<EOL>if not name:<EOL><INDENT>name = namesgenerator.get_random_name()<EOL>if self.collection.count_documents({'<STR_LIT:name>': name}) > <NUM_LIT:0>:<EOL><INDENT>name = namesgenerator.get_random_name(retry=True)<EOL><DEDENT><DEDENT>if self.collection.count_documents({'<STR_LIT:name>': name}) > <NUM_LIT:0>:<EOL><INDENT>if overwrite:<EOL><INDENT>self.collection.delete_one({'<STR_LIT:name>': name})<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT><DEDENT>self.collection.insert_one(Service(<EOL>name=name,<EOL>url=baseurl(service.url),<EOL>type=service.type,<EOL>purl=service.purl,<EOL>public=service.public,<EOL>auth=service.auth,<EOL>verify=service.verify))<EOL>return self.fetch_by_name(name=name)<EOL>


self.collection.delete_one({'<STR_LIT:name>': name})<EOL>return True<EOL>


my_services = []<EOL>for service in self.collection.find().sort('<STR_LIT:name>', pymongo.ASCENDING):<EOL><INDENT>my_services.append(Service(service))<EOL><DEDENT>return my_services<EOL>


service = self.collection.find_one({'<STR_LIT:name>': name})<EOL>if not service:<EOL><INDENT>raise ServiceNotFound<EOL><DEDENT>return Service(service)<EOL>

Gets service for given ``name`` from mongodb storage.


service = self.collection.find_one({'<STR_LIT:url>': url})<EOL>if not service:<EOL><INDENT>raise ServiceNotFound<EOL><DEDENT>return Service(service)<EOL>

Gets service for given ``url`` from mongodb storage.


self.collection.drop()<EOL>return True<EOL>

Removes all OWS services from mongodb storage.


name = namesgenerator.get_sane_name(service.name)<EOL>if not name:<EOL><INDENT>name = namesgenerator.get_random_name()<EOL>if name in self.name_index:<EOL><INDENT>name = namesgenerator.get_random_name(retry=True)<EOL><DEDENT><DEDENT>if name in self.name_index:<EOL><INDENT>if overwrite:<EOL><INDENT>self._delete(name=name)<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT><DEDENT>self._insert(Service(<EOL>name=name,<EOL>url=baseurl(service.url),<EOL>type=service.type,<EOL>purl=service.purl,<EOL>public=service.public,<EOL>auth=service.auth,<EOL>verify=service.verify))<EOL>return self.fetch_by_name(name=name)<EOL>


self._delete(name=name)<EOL>return True<EOL>


my_services = []<EOL>for service in self.name_index.values():<EOL><INDENT>my_services.append(Service(service))<EOL><DEDENT>return my_services<EOL>


service = self.name_index.get(name)<EOL>if not service:<EOL><INDENT>raise ServiceNotFound<EOL><DEDENT>return Service(service)<EOL>

Get service for given ``name`` from memory storage.


self.name_index = {}<EOL>return True<EOL>

Removes all OWS services from memory storage.


database = database or '<STR_LIT>'<EOL>if database == '<STR_LIT>':<EOL><INDENT>db = _mongodb(registry)<EOL>store = MongodbTokenStore(db.tokens)<EOL><DEDENT>else:<EOL><INDENT>store = MemoryTokenStore()<EOL><DEDENT>return store<EOL>

Creates a token store with the interface of :class:`twitcher.store.AccessTokenStore`.
By default the mongodb implementation will be used.

:param database: A string with the store implementation name: "mongodb" or "memory".
:return: An instance of :class:`twitcher.store.AccessTokenStore`.


database = database or '<STR_LIT>'<EOL>if database == '<STR_LIT>':<EOL><INDENT>db = _mongodb(registry)<EOL>store = MongodbServiceStore(collection=db.services)<EOL><DEDENT>else:<EOL><INDENT>store = MemoryServiceStore()<EOL><DEDENT>return store<EOL>

Creates a service store with the interface of :class:`twitcher.store.ServiceStore`.
By default the mongodb implementation will be used.

:return: An instance of :class:`twitcher.store.ServiceStore`.


def generate_token(self, valid_in_hours=<NUM_LIT:1>, data=None):

raise NotImplementedError<EOL>

Generates an access token which is valid for ``valid_in_hours``.

Arguments:

* :param valid_in_hours: an int with number of hours the token is valid.
* :param data: a dict with extra data used with this token.

Possible keys: ``esgf_access_token``, ``esgf_slcs_service_url`` or ``esgf_credentials``.


raise NotImplementedError<EOL>


def register_service(self, url, data, overwrite):

raise NotImplementedError<EOL>

Adds an OWS service with the given ``url`` to the service store.

:param data: a dict with additional information like ``name``.


raise NotImplementedError<EOL>

Removes OWS service with the given ``name`` from the service store.


raise NotImplementedError<EOL>

Gets service with given ``name`` from service store.


raise NotImplementedError<EOL>

Gets service with given ``url`` from service store.


raise NotImplementedError<EOL>

Removes all services from the service store.


data = data or {}<EOL>access_token = self.tokengenerator.create_access_token(<EOL>valid_in_hours=valid_in_hours,<EOL>data=data,<EOL>)<EOL>self.store.save_token(access_token)<EOL>return access_token.params<EOL>


try:<EOL><INDENT>self.store.delete_token(token)<EOL><DEDENT>except Exception:<EOL><INDENT>LOGGER.exception('<STR_LIT>')<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>


try:<EOL><INDENT>self.store.clear_tokens()<EOL><DEDENT>except Exception:<EOL><INDENT>LOGGER.exception('<STR_LIT>')<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>


data = data or {}<EOL>args = dict(data)<EOL>args['<STR_LIT:url>'] = url<EOL>service = Service(**args)<EOL>service = self.store.save_service(service, overwrite=overwrite)<EOL>return service.params<EOL>


try:<EOL><INDENT>self.store.delete_service(name=name)<EOL><DEDENT>except Exception:<EOL><INDENT>LOGGER.exception('<STR_LIT>')<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>


try:<EOL><INDENT>service = self.store.fetch_by_name(name=name)<EOL><DEDENT>except Exception:<EOL><INDENT>LOGGER.error('<STR_LIT>', name)<EOL>return {}<EOL><DEDENT>else:<EOL><INDENT>return service.params<EOL><DEDENT>


try:<EOL><INDENT>service = self.store.fetch_by_url(url=url)<EOL><DEDENT>except Exception:<EOL><INDENT>LOGGER.error('<STR_LIT>', url)<EOL>return {}<EOL><DEDENT>else:<EOL><INDENT>return service.params<EOL><DEDENT>


try:<EOL><INDENT>services = [service.params for service in self.store.list_services()]<EOL><DEDENT>except Exception:<EOL><INDENT>LOGGER.error('<STR_LIT>')<EOL>return []<EOL><DEDENT>else:<EOL><INDENT>return services<EOL><DEDENT>


try:<EOL><INDENT>self.store.clear_services()<EOL><DEDENT>except Exception:<EOL><INDENT>LOGGER.error('<STR_LIT>')<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>


def _get_param(self, param, allowed_values=None, optional=False):

request_params = self._request_params()<EOL>if param in request_params:<EOL><INDENT>value = request_params[param].lower()<EOL>if allowed_values is not None:<EOL><INDENT>if value in allowed_values:<EOL><INDENT>self.params[param] = value<EOL><DEDENT>else:<EOL><INDENT>raise OWSInvalidParameterValue("<STR_LIT>" % (param, value), value=param)<EOL><DEDENT><DEDENT><DEDENT>elif optional:<EOL><INDENT>self.params[param] = None<EOL><DEDENT>else:<EOL><INDENT>raise OWSMissingParameterValue('<STR_LIT>' % param, value=param)<EOL><DEDENT>return self.params[param]<EOL>


return self._get_param(param="<STR_LIT>", allowed_values=allowed_service_types)<EOL>

Check mandatory service name parameter in GET request.