temple.check.in_git_repo()<EOL>temple.check.is_temple_project()<EOL>temple_config = temple.utils.read_temple_config()<EOL>old_template_version = temple_config['<STR_LIT>']<EOL>new_template_version = version or _get_latest_template_version(temple_config['<STR_LIT>'])<EOL>return new_template_version == old_template_version<EOL>

Checks if a temple project is up to date with the repo

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

    Args:
        version (str, optional): Update against this git SHA or branch of the template

    Returns:
        boolean: True if up to date with ``version`` (or latest version), False otherwise

    Raises:
        `NotInGitRepoError`: When running outside of a git repo
        `InvalidTempleProjectError`: When not inside a valid temple repository


def _needs_new_cc_config_for_update(old_template, old_version, new_template, new_version):

if old_template != new_template:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return _cookiecutter_configs_have_changed(new_template,<EOL>old_version,<EOL>new_version)<EOL><DEDENT>

Given two templates and their respective versions, return True if a new cookiecutter
config needs to be obtained from the user


def _cookiecutter_configs_have_changed(template, old_version, new_version):

temple.check.is_git_ssh_path(template)<EOL>repo_path = temple.utils.get_repo_path(template)<EOL>github_client = temple.utils.GithubClient()<EOL>api = '<STR_LIT>'.format(repo_path)<EOL>old_config_resp = github_client.get(api, params={'<STR_LIT>': old_version})<EOL>old_config_resp.raise_for_status()<EOL>new_config_resp = github_client.get(api, params={'<STR_LIT>': new_version})<EOL>new_config_resp.raise_for_status()<EOL>return old_config_resp.json()['<STR_LIT:content>'] != new_config_resp.json()['<STR_LIT:content>']<EOL>

Given an old version and new version, check if the cookiecutter.json files have changed

    When the cookiecutter.json files change, it means the user will need to be prompted for
    new context

    Args:
        template (str): The git SSH path to the template
        old_version (str): The git SHA of the old version
        new_version (str): The git SHA of the new version

    Returns:
        bool: True if the cookiecutter.json files have been changed in the old and new versions


@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.


@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.


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 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.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.


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``.


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


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.


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 not _in_clean_repo():<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise temple.exceptions.InDirtyRepoError(msg)<EOL><DEDENT>

Raises `InDirtyRepoError` if inside a dirty repository


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


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


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


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


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>


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


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 _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


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


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.


@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>


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.


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.


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


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.


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 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))


_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>


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.


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>


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>


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>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.


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.


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>


@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


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


headers = get_headers(request)<EOL>return jsonify({'<STR_LIT>': headers['<STR_LIT>']})<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.


@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>


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


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


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


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>


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>


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>


mean = HTTP_STATUS_CODES.get(code, '<STR_LIT>').upper()<EOL>return '<STR_LIT>'.format(code=code, mean=mean)<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>


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>


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>


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>


return self.metadata<EOL>

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

        :rtype: Dict.


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


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`.


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


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

Determine on what attribute to sort.

        :rtype: str


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


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.


@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`.


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.


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.


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.


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.


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 type in Label.valid_types<EOL>

Check if the argument is a valid SKOS label type.

:param string type: The type to be checked.


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`.


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

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

:param id: The id of the concept or collection.
: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


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


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 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.


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.


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.tokenmgr.revoke_all_tokens()<EOL>


return self.srvreg.list_services()<EOL>


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


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.


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


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


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

Authentication method: public, token, cert.


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`.


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


return is_valid_url(self.purl)<EOL>

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


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>


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`.


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._delete(name=name)<EOL>return True<EOL>


raise NotImplementedError<EOL>

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


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>


raise NotImplementedError<EOL>

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


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>


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``.


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

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>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>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>


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>


return self._get_param(param="<STR_LIT>", allowed_values=allowed_request_types[self.params['<STR_LIT>']])<EOL>

Find requested request type in GET request.


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>


if "<STR_LIT>" in self.document.attrib:<EOL><INDENT>value = self.document.attrib["<STR_LIT>"].lower()<EOL>if value in allowed_service_types:<EOL><INDENT>self.params["<STR_LIT>"] = value<EOL><DEDENT>else:<EOL><INDENT>raise OWSInvalidParameterValue("<STR_LIT>" % value, value="<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise OWSMissingParameterValue('<STR_LIT>', value="<STR_LIT>")<EOL><DEDENT>return self.params["<STR_LIT>"]<EOL>

Check mandatory service name parameter in POST request.


if "<STR_LIT:version>" in self.document.attrib:<EOL><INDENT>value = self.document.attrib["<STR_LIT:version>"].lower()<EOL>if value in allowed_versions[self.params['<STR_LIT>']]:<EOL><INDENT>self.params["<STR_LIT:version>"] = value<EOL><DEDENT>else:<EOL><INDENT>raise OWSInvalidParameterValue("<STR_LIT>" % value, value="<STR_LIT:version>")<EOL><DEDENT><DEDENT>elif self._get_request_type() == "<STR_LIT>":<EOL><INDENT>self.params["<STR_LIT:version>"] = None<EOL><DEDENT>else:<EOL><INDENT>raise OWSMissingParameterValue('<STR_LIT>', value="<STR_LIT:version>")<EOL><DEDENT>return self.params["<STR_LIT:version>"]<EOL>


def wrap(cls):<EOL><INDENT>warnings.warn(UserWarning('<STR_LIT>'))<EOL>these = {<EOL>name: attr.ib()<EOL>for name in inspect(cls).columns.keys()<EOL>}<EOL>return attr.s(cls, these=these, init=False)<EOL><DEDENT>if maybe_cls is None:<EOL><INDENT>return wrap<EOL><DEDENT>else:<EOL><INDENT>return wrap(maybe_cls)<EOL><DEDENT>

A class decorator that adds ``__repr__``, ``__eq__``, ``__cmp__``, and
``__hash__`` methods according to the fields defined on the SQLAlchemy
model class.


with codecs.open(os.path.join(HERE, *parts), '<STR_LIT:rb>', '<STR_LIT:utf-8>') as f:<EOL><INDENT>return f.read()<EOL><DEDENT>

Build an absolute path from *parts* and and return the contents of the
resulting file.  Assume UTF-8 encoding.


meta_match = re.search(<EOL>r"<STR_LIT>".format(meta=meta),<EOL>META_FILE, re.M<EOL>)<EOL>if meta_match:<EOL><INDENT>return meta_match.group(<NUM_LIT:1>)<EOL><DEDENT>raise RuntimeError('<STR_LIT>'.format(meta=meta))<EOL>


return requests.head(url).status_code == <NUM_LIT:200><EOL>

Check whether the remote file exists on Storage


<EOL>ml_graph, cl_graph = {}, {}<EOL>for i in range(n):<EOL><INDENT>ml_graph[i] = set()<EOL>cl_graph[i] = set()<EOL><DEDENT>def add_both(d, i, j):<EOL><INDENT>d[i].add(j)<EOL>d[j].add(i)<EOL><DEDENT>for (i, j) in ml:<EOL><INDENT>ml_graph[i].add(j)<EOL>ml_graph[j].add(i)<EOL><DEDENT>for (i, j) in cl:<EOL><INDENT>cl_graph[i].add(j)<EOL>cl_graph[j].add(i)<EOL><DEDENT>def dfs(i, graph, visited, component):<EOL><INDENT>visited[i] = True<EOL>for j in graph[i]:<EOL><INDENT>if not visited[j]:<EOL><INDENT>dfs(j, graph, visited, component)<EOL><DEDENT><DEDENT>component.append(i)<EOL><DEDENT>visited = [False] * n<EOL>neighborhoods = []<EOL>for i in range(n):<EOL><INDENT>if not visited[i] and ml_graph[i]:<EOL><INDENT>component = []<EOL>dfs(i, ml_graph, visited, component)<EOL>for x1 in component:<EOL><INDENT>for x2 in component:<EOL><INDENT>if x1 != x2:<EOL><INDENT>ml_graph[x1].add(x2)<EOL><DEDENT><DEDENT><DEDENT>neighborhoods.append(component)<EOL><DEDENT><DEDENT>for (i, j) in cl:<EOL><INDENT>for x in ml_graph[i]:<EOL><INDENT>add_both(cl_graph, x, j)<EOL><DEDENT>for y in ml_graph[j]:<EOL><INDENT>add_both(cl_graph, i, y)<EOL><DEDENT>for x in ml_graph[i]:<EOL><INDENT>for y in ml_graph[j]:<EOL><INDENT>add_both(cl_graph, x, y)<EOL><DEDENT><DEDENT><DEDENT>for i in ml_graph:<EOL><INDENT>for j in ml_graph[i]:<EOL><INDENT>if j != i and j in cl_graph[i]:<EOL><INDENT>raise InconsistentConstraintsException('<STR_LIT>'.format(i, j))<EOL><DEDENT><DEDENT><DEDENT>return ml_graph, cl_graph, neighborhoods<EOL>

Create a graph of constraints for both must- and cannot-links


return scipy.spatial.distance.mahalanobis(x, y, A) ** <NUM_LIT:2><EOL>


if self.queries_cnt < self.max_queries_cnt:<EOL><INDENT>self.queries_cnt += <NUM_LIT:1><EOL>return self.labels[i] == self.labels[j]<EOL><DEDENT>else:<EOL><INDENT>raise MaximumQueriesExceeded<EOL><DEDENT>

Query the oracle to find out whether i and j should be must-linked


local_match = LOCAL_RE.match(db_spec)<EOL>remote_match = REMOTE_RE.match(db_spec)<EOL>plain_match = PLAIN_RE.match(db_spec)<EOL>if local_match:<EOL><INDENT>return local_match.groupdict()['<STR_LIT>']<EOL><DEDENT>elif remote_match:<EOL><INDENT>hostname, portnum, database = map(remote_match.groupdict().get,<EOL>('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>local_url = settings._('<STR_LIT>', '<STR_LIT>')<EOL>localhost, localport = urlparse.urlparse(local_url)[<NUM_LIT:1>].split('<STR_LIT::>')<EOL>if (localhost == hostname) and (localport == portnum):<EOL><INDENT>return database<EOL><DEDENT>return '<STR_LIT>' % (hostname, portnum, database)<EOL><DEDENT>elif plain_match:<EOL><INDENT>return plain_match.groupdict()['<STR_LIT>']<EOL><DEDENT>raise ValueError('<STR_LIT>' % (db_spec,))<EOL>

Return the database string for a database specifier.

The database specifier takes a custom format for specifying local and remote
databases. A local database is specified by the following format:

    local:<db_name>

For example, a database called 'sessions' would be specified by the string
``'local:sessions'``. Remote databases are specified like this:

    remote:<host>:<port_num>:<db_name>

For example, a database called 'log' on the server 'dev.example.com' at port
number 5984 would be specified by ``'remote:dev.example.com:5984:log'``.

These specifiers are translated into strings acceptable to CouchDB; local
specs are turned into the database name alone, and remote specs are turned
into ``'http://host:port/db_name'`` URLs.


<EOL>server = shortcuts.get_server()<EOL>logger = logging.getLogger('<STR_LIT>')<EOL>logger.debug('<STR_LIT>' + urllib.parse.urljoin(server.resource.uri, '<STR_LIT>'))<EOL>source, target = specifier_to_db(source_db), specifier_to_db(target_db)<EOL>logger.debug('<STR_LIT>' % (source,))<EOL>logger.debug('<STR_LIT>' % (target,))<EOL>try:<EOL><INDENT>resp_headers, resp_body = server.resource.post(path='<STR_LIT>',<EOL>content=json.dumps({'<STR_LIT:source>': source, '<STR_LIT:target>': target}))<EOL><DEDENT>except couchdb.client.ServerError as exc:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>raise ReplicationError(exc.args)<EOL><DEDENT>result = resp_body['<STR_LIT>'][<NUM_LIT:0>]<EOL>if resp_body['<STR_LIT>']:<EOL><INDENT>logger.info('<STR_LIT>' % (<EOL>resp_body['<STR_LIT>'][:<NUM_LIT:6>],))<EOL>logger.info('<STR_LIT>' + result['<STR_LIT>'])<EOL>logger.info('<STR_LIT>' + result['<STR_LIT>'])<EOL>result['<STR_LIT>'] = datetime.datetime.strptime(result['<STR_LIT>'],<EOL>'<STR_LIT>')<EOL>result['<STR_LIT>'] = datetime.datetime.strptime(result['<STR_LIT>'],<EOL>'<STR_LIT>')<EOL>timedelta = result['<STR_LIT>'] - result['<STR_LIT>']<EOL>if timedelta.days:<EOL><INDENT>logger.info('<STR_LIT>' % (<EOL>timedelta.days,<EOL>timedelta.seconds + (timedelta.microseconds * (<NUM_LIT>))))<EOL><DEDENT>else:<EOL><INDENT>logger.info('<STR_LIT>' % (<EOL>timedelta.seconds + (timedelta.microseconds * (<NUM_LIT>))))<EOL><DEDENT>result['<STR_LIT>'] = resp_body['<STR_LIT>']<EOL>result['<STR_LIT>'] = resp_body['<STR_LIT>']<EOL>result['<STR_LIT>'] = resp_body['<STR_LIT>']<EOL>if result['<STR_LIT>'] == <NUM_LIT:1>:<EOL><INDENT>docs_read = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>docs_read = '<STR_LIT>' % (result['<STR_LIT>'],)<EOL><DEDENT>if result['<STR_LIT>'] == <NUM_LIT:1>:<EOL><INDENT>docs_written = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>docs_written = '<STR_LIT>' % (result['<STR_LIT>'],)<EOL><DEDENT>if result['<STR_LIT>'] == <NUM_LIT:1>:<EOL><INDENT>missing_checked = '<STR_LIT>' % (<EOL>result['<STR_LIT>'],)<EOL><DEDENT>else:<EOL><INDENT>missing_checked = '<STR_LIT>' % (<EOL>result['<STR_LIT>'], result['<STR_LIT>'],)<EOL><DEDENT>logging.info('<STR_LIT>' % (docs_read, docs_written))<EOL>logging.info(missing_checked)<EOL>return result<EOL><DEDENT>else:<EOL><INDENT>logger.error('<STR_LIT>' % (<EOL>resp_body['<STR_LIT>'][:<NUM_LIT:6>],))<EOL>result['<STR_LIT>'] = resp_body['<STR_LIT>']<EOL>result['<STR_LIT>'] = resp_body['<STR_LIT>']<EOL>result['<STR_LIT>'] = resp_body['<STR_LIT>']<EOL>raise ReplicationFailure(resp_headers, result)<EOL><DEDENT>

Replicate an existing database to another existing database.


def get_doc(doc_id, db_name, server_url='<STR_LIT>', rev=None):

db = get_server(server_url)[db_name]<EOL>if rev:<EOL><INDENT>headers, response = db.resource.get(doc_id, rev=rev)<EOL>return couchdb.client.Document(response)<EOL><DEDENT>return db[doc_id]<EOL>

Return a CouchDB document, given its ID, revision and database name.


return get_server(server_url)[db_name]<EOL>

Return a CouchDB database instance, given its name.


return couchdb.client.Server(<EOL>server_url if server_url else settings._('<STR_LIT>'))<EOL>

Return a CouchDB server instance based on Django project settings.


<EOL>for function in sorted(list(self.functions.values()), key=lambda x: x[<NUM_LIT:0>]):<EOL><INDENT>try:<EOL><INDENT>yield [list(function(document))]<EOL><DEDENT>except Exception as exc:<EOL><INDENT>yield []<EOL>self.log(repr(exc))<EOL><DEDENT><DEDENT>

Return the mapping of a document according to the function list.


module, basename = str(function_name).rsplit('<STR_LIT:.>', <NUM_LIT:1>)<EOL>try:<EOL><INDENT>return getattr(__import__(module, fromlist=[basename]), basename)<EOL><DEDENT>except (ImportError, AttributeError):<EOL><INDENT>raise FunctionNotFound(function_name)<EOL><DEDENT>

Given a Python function name, return the function it refers to.


self.wfile.write(json.dumps({'<STR_LIT>': string}) + NEWLINE)<EOL>


self.functions.clear()<EOL>self.function_counter = <NUM_LIT:0><EOL>


<EOL>return json.dumps({<EOL>'<STR_LIT:error>': type(exc).__name__,<EOL>'<STR_LIT>': str(exc)})<EOL>

Transform a Python exception into a CouchDB JSON error.


def handle_reduce(self, reduce_function_names, mapped_docs):

reduce_functions = []<EOL>for reduce_function_name in reduce_function_names:<EOL><INDENT>try:<EOL><INDENT>reduce_function = get_function(reduce_function_name)<EOL>if getattr(reduce_function, '<STR_LIT>', None):<EOL><INDENT>reduce_function = reduce_function(self.log)<EOL><DEDENT>reduce_functions.append(reduce_function)<EOL><DEDENT>except Exception as exc:<EOL><INDENT>self.log(repr(exc))<EOL>reduce_functions.append(lambda *args, **kwargs: None)<EOL><DEDENT><DEDENT>keys, values = list(zip(<EOL>(key, value) for ((key, doc_id), value) in mapped_docs))<EOL>results = []<EOL>for reduce_function in reduce_functions:<EOL><INDENT>try:<EOL><INDENT>results.append(reduce_function(keys, values, rereduce=False))<EOL><DEDENT>except Exception as exc:<EOL><INDENT>self.log(repr(exc))<EOL>results.append(None)<EOL><DEDENT><DEDENT>return [True, results]<EOL>

Reduce several mapped documents by several reduction functions.


def handle_validate(self, function_name, new_doc, old_doc, user_ctx):

try:<EOL><INDENT>function = get_function(function_name)<EOL><DEDENT>except Exception as exc:<EOL><INDENT>self.log(repr(exc))<EOL>return False<EOL><DEDENT>try:<EOL><INDENT>return function(new_doc, old_doc, user_ctx)<EOL><DEDENT>except Exception as exc:<EOL><INDENT>self.log(repr(exc))<EOL>return repr(exc)<EOL><DEDENT>

Validate...this function is undocumented, but still in CouchDB.


@classmethod<EOL><INDENT>def from_uniform(<EOL>cls, z, origin=(<NUM_LIT:0>, <NUM_LIT:0>), step=(<NUM_LIT:1>, <NUM_LIT:1>), formatter=numpy_formatter):<DEDENT>

z = np.ma.asarray(z, dtype=np.float64)<EOL>if z.ndim != <NUM_LIT:2>:<EOL><INDENT>raise TypeError(<EOL>"<STR_LIT>".format(z.ndim))<EOL><DEDENT>if len(origin) != <NUM_LIT:2>:<EOL><INDENT>raise TypeError(<EOL>"<STR_LIT>".format(<EOL>len(origin)))<EOL><DEDENT>if len(step) != <NUM_LIT:2>:<EOL><INDENT>raise TypeError(<EOL>"<STR_LIT>".format(<EOL>len(step)))<EOL><DEDENT>if any(s == <NUM_LIT:0> for s in step):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>".format(<EOL>str(step)))<EOL><DEDENT>y, x = np.mgrid[<EOL>origin[<NUM_LIT:0>]:(origin[<NUM_LIT:0>]+step[<NUM_LIT:0>]*z.shape[<NUM_LIT:0>]):step[<NUM_LIT:0>],<EOL>origin[<NUM_LIT:1>]:(origin[<NUM_LIT:1>]+step[<NUM_LIT:1>]*z.shape[<NUM_LIT:1>]):step[<NUM_LIT:1>]]<EOL>return cls(x, y, z, formatter)<EOL>

Construct a contour generator from a uniform grid.

        NOTE
        ----
        The default `origin` and `step` values is equivalent to calling
        :meth:`matplotlib.axes.Axes.contour` with only the `z` argument.

        Parameters
        ----------
        z : array_like
            The 2-dimensional uniform grid of data to compute contours for.
            Masked arrays are supported.
        origin : (number.Number, number.Number)
            The (x, y) coordinate of data point `z[0,0]`.
        step :  (number.Number, number.Number)
            The (x, y) distance between data points in `z`.
        formatter : callable
            A conversion function to convert from the internal `Matplotlib`_
            contour format to an external format.  See :ref:`formatters` for
            more information.

        Returns
        -------
        : :class:`QuadContourGenerator`
            Initialized contour generator.


@classmethod<EOL><INDENT>def from_curvilinear(cls, x, y, z, formatter=numpy_formatter):<DEDENT>

return cls(x, y, z, formatter)<EOL>

Construct a contour generator from a curvilinear grid.

        Note
        ----
        This is an alias for the default constructor.

        Parameters
        ----------
        x : array_like
            x coordinates of each point in `z`.  Must be the same size as `z`.
        y : array_like
            y coordinates of each point in `z`.  Must be the same size as `z`.
        z : array_like
            The 2-dimensional curvilinear grid of data to compute
            contours for.  Masked arrays are supported.
        formatter : callable
            A conversion function to convert from the internal `Matplotlib`_
            contour format to an external format.  See :ref:`formatters` for
            more information.

        Returns
        -------
        : :class:`QuadContourGenerator`
            Initialized contour generator.


def __init__(self, formatter=numpy_formatter, *args, **kwargs):

<EOL>self.formatter = formatter<EOL>

Initialize a :class:`ContourMixin`, see class docstring.


if not isinstance(level, numbers.Number):<EOL><INDENT>raise TypeError(<EOL>("<STR_LIT>"<EOL>"<STR_LIT>").format(type(level)))<EOL><DEDENT>vertices = self._contour_generator.create_contour(level)<EOL>return self.formatter(level, vertices)<EOL>

Get contour lines at the given level.

        Parameters
        ----------
        level : numbers.Number
            The data level to calculate the contour lines for.

        Returns
        -------
        :
            The result of the :attr:`formatter` called on the contour at the
            given `level`.


def filled_contour(self, min=None, max=None):

<EOL>if min is None:<EOL><INDENT>min = np.finfo(np.float64).min<EOL><DEDENT>if max is None:<EOL><INDENT>max = np.finfo(np.float64).max<EOL><DEDENT>vertices, codes = (<EOL>self._contour_generator.create_filled_contour(min, max))<EOL>return self.formatter((min, max), vertices, codes)<EOL>

Get contour polygons between the given levels.

        Parameters
        ----------
        min : numbers.Number or None
            The minimum data level of the contour polygon.  If :obj:`None`,
            ``numpy.finfo(numpy.float64).min`` will be used.
        max : numbers.Number or None
            The maximum data level of the contour polygon.  If :obj:`None`,
            ``numpy.finfo(numpy.float64).max`` will be used.

        Returns
        -------
        :
            The result of the :attr:`formatter` called on the filled contour
            between `min` and `max`.


def null_formatter(level, vertices, codes=None):

return level, vertices, codes<EOL>

Null formatter that passes through the raw vertices and codes.


def create_repository(self, path, info=None, description=None, replace=True, allowNoneEmpty=True, raiseError=True):

assert isinstance(raiseError, bool), "<STR_LIT>"<EOL>assert isinstance(allowNoneEmpty, bool), "<STR_LIT>"<EOL>assert isinstance(replace, bool), "<STR_LIT>"<EOL>assert isinstance(path, basestring), "<STR_LIT>"<EOL>if info is None:<EOL><INDENT>info = '<STR_LIT>'<EOL><DEDENT>try:<EOL><INDENT>pickle.dumps(info)<EOL><DEDENT>except Exception as err:<EOL><INDENT>raise Exception("<STR_LIT>"%str(err))<EOL><DEDENT>if description is None:<EOL><INDENT>description = '<STR_LIT>'<EOL><DEDENT>assert isinstance(description, basestring), "<STR_LIT>"<EOL>if path.strip() in ('<STR_LIT>','<STR_LIT:.>'):<EOL><INDENT>path = os.getcwd()<EOL><DEDENT>realPath = os.path.realpath( os.path.expanduser(path) )<EOL>message = []<EOL>if self.is_repository(realPath):<EOL><INDENT>if not replace:<EOL><INDENT>message.append("<STR_LIT>"%path)<EOL>return False, message<EOL><DEDENT>else:<EOL><INDENT>message.append("<STR_LIT>"%path)<EOL>try:<EOL><INDENT>for _df in os.listdir(realPath):<EOL><INDENT>_p = os.path.join(realPath, _df)<EOL>if os.path.isdir(_p):<EOL><INDENT>shutil.rmtree( _p )<EOL><DEDENT>else:<EOL><INDENT>os.remove(_p)<EOL><DEDENT><DEDENT><DEDENT>except Exception as err:<EOL><INDENT>message.append("<STR_LIT>"%(str(err)))<EOL>return False, '<STR_LIT:\n>'.join(message)<EOL><DEDENT><DEDENT><DEDENT>if not os.path.isdir(realPath):<EOL><INDENT>os.makedirs(realPath)<EOL><DEDENT>elif len(os.listdir(realPath)) and not allowNoneEmpty:<EOL><INDENT>return False, "<STR_LIT>"<EOL><DEDENT>oldRepo = self.__repo<EOL>self.reset()<EOL>self.__path = realPath.rstrip(os.sep)<EOL>self.__repo['<STR_LIT>'] = info<EOL>saved = self.save(description=description)<EOL>if not saved:<EOL><INDENT>self.__repo = oldRepo<EOL>message.append("<STR_LIT>")<EOL>return False, '<STR_LIT:\n>'.join(message)<EOL><DEDENT>return True, '<STR_LIT:\n>'.join(message)<EOL>

create a repository in a directory. This method insures the creation of
the directory in the system if it is missing.\n

**N.B. If replace is True and existing repository is found in path, create_repository erases all existing files and directories in path.**

:Parameters:
    #. path (string): The real absolute path where to create the Repository.
       If '.' or an empty string is passed, the current working directory will be used.
    #. description (None, str): Repository main directory information.
    #. info (None, object): Repository information. It can
       be None or any pickle writable type of data.
    #. replace (boolean): Whether to replace existing repository.
    #. allowNoneEmpty (boolean): Allow creating repository in none-empty
       directory.
    #. raiseError (boolean): Whether to raise encountered error instead
       of returning failure.

:Returns:
    #. success (boolean): Whether creating repository was successful
    #. message (None, str): Any returned message.


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


state = []<EOL>def _walk_dir(relaPath, dirList):<EOL><INDENT>dirDict = {'<STR_LIT:type>':'<STR_LIT>',<EOL>'<STR_LIT>':os.path.isdir(os.path.join(self.__path,relaPath)),<EOL>'<STR_LIT>':os.path.isfile(os.path.join(self.__path,relaPath,self.__dirInfo)),<EOL>}<EOL>state.append({relaPath:dirDict})<EOL>for fname in sorted([f for f in dirList if isinstance(f, basestring)]):<EOL><INDENT>relaFilePath = os.path.join(relaPath,fname)<EOL>realFilePath = os.path.join(self.__path,relaFilePath)<EOL>fileDict = {'<STR_LIT:type>':'<STR_LIT:file>',<EOL>'<STR_LIT>':os.path.isfile(realFilePath),<EOL>'<STR_LIT>':os.path.isfile(os.path.join(self.__path,relaPath,self.__fileInfo%fname)),<EOL>}<EOL>state.append({relaFilePath:fileDict})<EOL><DEDENT>for ddict in sorted([d for d in dirList if isinstance(d, dict)], key=lambda k: list(k)[<NUM_LIT:0>]):<EOL><INDENT>dirname = list(ddict)[<NUM_LIT:0>]<EOL>_walk_dir(relaPath=os.path.join(relaPath,dirname), dirList=ddict[dirname])<EOL><DEDENT><DEDENT>if relaPath is None:<EOL><INDENT>_walk_dir(relaPath='<STR_LIT>', dirList=self.__repo['<STR_LIT>'])<EOL><DEDENT>else:<EOL><INDENT>assert isinstance(relaPath, basestring), "<STR_LIT>"<EOL>relaPath = self.to_repo_relative_path(path=relaPath, split=False)<EOL>spath    = relaPath.split(os.sep)<EOL>dirList  = self.__repo['<STR_LIT>']<EOL>while len(spath):<EOL><INDENT>dirname = spath.pop(<NUM_LIT:0>)<EOL>dList   = [d for d in dirList if isinstance(d, dict)]<EOL>if not len(dList):<EOL><INDENT>dirList = None<EOL>break<EOL><DEDENT>cDict = [d for d in dList if dirname in d]<EOL>if not len(cDict):<EOL><INDENT>dirList = None<EOL>break<EOL><DEDENT>dirList = cDict[<NUM_LIT:0>][dirname]<EOL><DEDENT>if dirList is not None:<EOL><INDENT>_walk_dir(relaPath=relaPath, dirList=dirList)<EOL><DEDENT><DEDENT>return state<EOL>

Get a list representation of repository state along with useful
information. List state is ordered relativeley to directories level

:Parameters:
    #. relaPath (None, str): relative directory path from where to
       start. If None all repository representation is returned.

:Returns:
    #. state (list): List representation of the repository.
       List items are all dictionaries. Every dictionary has a single
       key which is the file or the directory name and the value is a
       dictionary of information including:

           * 'type': the type of the tracked whether it's file, dir, or objectdir
           * 'exists': whether file or directory actually exists on disk
           * 'pyrepfileinfo': In case of a file or an objectdir whether .%s_pyrepfileinfo exists
           * 'pyrepdirinfo': In case of a directory whether .pyrepdirinfo exists


def walk_directories_info(self, relativePath="<STR_LIT>", fullPath=False, recursive=False):

assert isinstance(fullPath, bool), "<STR_LIT>"<EOL>assert isinstance(recursive, bool), "<STR_LIT>"<EOL>relativePath = self.to_repo_relative_path(path=relativePath, split=False)<EOL>for dpath in self.walk_directories_path(relativePath=relativePath, fullPath=False, recursive=recursive):<EOL><INDENT>dirInfoPath = os.path.join(self.__path,dpath,self.__dirInfo)<EOL>if os.path.isfile(dirInfoPath):<EOL><INDENT>with open(dirInfoPath, '<STR_LIT:rb>') as fd:<EOL><INDENT>info = pickle.load(fd)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>info = None<EOL><DEDENT>if fullPath:<EOL><INDENT>yield (os.path.join(self.__path, dpath), info)<EOL><DEDENT>else:<EOL><INDENT>yield (dpath, info)<EOL><DEDENT><DEDENT>

Walk the repository relative path and yield tuple of two items where
first item is directory relative/full path and second item is directory
info. If directory file info is not found on disk, second item will be None.

:parameters:
    #. relativePath (string): The relative path from which start the walk.
    #. fullPath (boolean): Whether to return full or relative path.
    #. recursive (boolean): Whether walk all directories files recursively.


return self.update_file(*args, **kwargs)<EOL>


if dump is None:<EOL><INDENT>dump = '<STR_LIT>'<EOL><DEDENT>if dump.startswith('<STR_LIT>'):<EOL><INDENT>if dump == '<STR_LIT>':<EOL><INDENT>proto = protocol<EOL><DEDENT>else:<EOL><INDENT>proto = dump.strip('<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>proto = int(proto)<EOL>assert proto>=-<NUM_LIT:1><EOL><DEDENT>except:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>code = """<STR_LIT>"""<EOL><DEDENT>rt dill<EOL><INDENT>open('<STR_LIT>', '<STR_LIT:wb>') as fd:<EOL><DEDENT>dill.dump( value, fd, protocol=%i )<EOL>fd.flush()<EOL>os.fsync(fd.fileno())<EOL>proto<EOL>elif dump == '<STR_LIT>':<EOL><INDENT>code = """<STR_LIT>"""<EOL><DEDENT>rt numpy<EOL><INDENT>open('<STR_LIT>', '<STR_LIT:wb>') as fd:<EOL><DEDENT>numpy.save(file=fd, arr=value)<EOL>fd.flush()<EOL>os.fsync(fd.fileno())<EOL>elif dump == '<STR_LIT>':<EOL><INDENT>code =


return self.pull_file(*args, **kwargs)<EOL>


@path_required<EOL><INDENT>def create_package(self, path=None, name=None, mode=None):<DEDENT>

<EOL>assert mode in (None, '<STR_LIT:w>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'), '<STR_LIT>'%str(mode)<EOL>if mode is None:<EOL><INDENT>mode = '<STR_LIT>'<EOL><DEDENT>if path is None:<EOL><INDENT>root = os.path.split(self.__path)[<NUM_LIT:0>]<EOL><DEDENT>elif path.strip() in ('<STR_LIT>','<STR_LIT:.>'):<EOL><INDENT>root = os.getcwd()<EOL><DEDENT>else:<EOL><INDENT>root = os.path.realpath( os.path.expanduser(path) )<EOL><DEDENT>assert os.path.isdir(root), '<STR_LIT>'%path<EOL>if name is None:<EOL><INDENT>ext = mode.split("<STR_LIT::>")<EOL>if len(ext) == <NUM_LIT:2>:<EOL><INDENT>if len(ext[<NUM_LIT:1>]):<EOL><INDENT>ext = "<STR_LIT:.>"+ext[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>ext = '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>ext = '<STR_LIT>'<EOL><DEDENT>name = os.path.split(self.__path)[<NUM_LIT:1>]+ext<EOL><DEDENT>tarfilePath = os.path.join(root, name)<EOL>try:<EOL><INDENT>tarHandler = tarfile.TarFile.open(tarfilePath, mode=mode)<EOL><DEDENT>except Exception as e:<EOL><INDENT>raise Exception("<STR_LIT>"%e)<EOL><DEDENT>for dpath in sorted(list(self.walk_directories_path(recursive=True))):<EOL><INDENT>t = tarfile.TarInfo( dpath )<EOL>t.type = tarfile.DIRTYPE<EOL>tarHandler.addfile(t)<EOL>tarHandler.add(os.path.join(self.__path,dpath,self.__dirInfo), arcname=self.__dirInfo)<EOL><DEDENT>for fpath in self.walk_files_path(recursive=True):<EOL><INDENT>relaPath, fname = os.path.split(fpath)<EOL>tarHandler.add(os.path.join(self.__path,fpath), arcname=fname)<EOL>tarHandler.add(os.path.join(self.__path,relaPath,self.__fileInfo%fname), arcname=self.__fileInfo%fname)<EOL>tarHandler.add(os.path.join(self.__path,relaPath,self.__fileClass%fname), arcname=self.__fileClass%fname)<EOL><DEDENT>tarHandler.add(os.path.join(self.__path,self.__repoFile), arcname="<STR_LIT>")<EOL>tarHandler.close()<EOL>

Create a tar file package of all the repository files and directories.
Only files and directories that are tracked in the repository
are stored in the package tar file.

**N.B. On some systems packaging requires root permissions.**

:Parameters:
    #. path (None, string): The real absolute path where to create the
       package. If None, it will be created in the same directory as
       the repository. If '.' or an empty string is passed, the current
       working directory will be used.
    #. name (None, string): The name to give to the package file
       If None, the package directory name will be used with the
       appropriate extension added.
    #. mode (None, string): The writing mode of the tarfile.
       If None, automatically the best compression mode will be chose.
       Available modes are ('w', 'w:', 'w:gz', 'w:bz2')


self.__path   = None<EOL>self.__repo   = {'<STR_LIT>': str(uuid.uuid1()),<EOL>'<STR_LIT>': time.time(),<EOL>'<STR_LIT>': None,<EOL>'<STR_LIT>': str(__version__),<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': []}<EOL>


def walk_files_info(self, relativePath="<STR_LIT>", fullPath=False, recursive=False):

assert isinstance(fullPath, bool), "<STR_LIT>"<EOL>assert isinstance(recursive, bool), "<STR_LIT>"<EOL>relativePath = self.to_repo_relative_path(path=relativePath, split=False)<EOL>for relaPath in self.walk_files_path(relativePath=relativePath, fullPath=False, recursive=recursive):<EOL><INDENT>fpath, fname = os.path.split(relaPath)<EOL>fileInfoPath = os.path.join(self.__path,fpath,self.__fileInfo%fname)<EOL>if os.path.isfile(fileInfoPath):<EOL><INDENT>with open(fileInfoPath, '<STR_LIT:rb>') as fd:<EOL><INDENT>info = pickle.load(fd)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>info = None<EOL><DEDENT>if fullPath:<EOL><INDENT>yield (os.path.join(self.__path, relaPath), info)<EOL><DEDENT>else:<EOL><INDENT>yield (relaPath, info)<EOL><DEDENT><DEDENT>

Walk the repository relative path and yield tuple of two items where
first item is file relative/full path and second item is file info.
If file info is not found on disk, second item will be None.

:parameters:
    #. relativePath (string): The relative path from which start the walk.
    #. fullPath (boolean): Whether to return full or relative path.
    #. recursive (boolean): Whether walk all directories files recursively


@path_required<EOL><INDENT>def remove_directory(self, relativePath, clean=False, raiseError=True, ntrials=<NUM_LIT:3>):<DEDENT>

assert isinstance(raiseError, bool), "<STR_LIT>"<EOL>assert isinstance(clean, bool), "<STR_LIT>"<EOL>assert isinstance(ntrials, int), "<STR_LIT>"<EOL>assert ntrials><NUM_LIT:0>, "<STR_LIT>"<EOL>relativePath = self.to_repo_relative_path(path=relativePath, split=False)<EOL>parentPath, dirName = os.path.split(relativePath)<EOL>if relativePath == '<STR_LIT>':<EOL><INDENT>return False, "<STR_LIT>"<EOL><DEDENT>if not self.is_repository_directory(relativePath):<EOL><INDENT>return False, "<STR_LIT>"%relativePath<EOL><DEDENT>realPath = os.path.join(self.__path,relativePath)<EOL>if not os.path.isdir(realPath):<EOL><INDENT>error = "<STR_LIT>"<EOL>assert not raiseError, error<EOL>return False, error<EOL><DEDENT>LD =  Locker(filePath=None, lockPass=str(uuid.uuid1()), lockPath=os.path.join(self.__path,parentPath,self.__dirLock))<EOL>acquired, code = LD.acquire_lock()<EOL>if not acquired:<EOL><INDENT>error = "<STR_LIT>"%(code,realPath)<EOL>assert not raiseError, error<EOL>return False, error<EOL><DEDENT>LR =  Locker(filePath=None, lockPass=str(uuid.uuid1()), lockPath=os.path.join(self.__path, self.__repoLock))<EOL>acquired, code = LR.acquire_lock()<EOL>if not acquired:<EOL><INDENT>LD.release_lock()<EOL>m = "<STR_LIT>"%(code,)<EOL>assert raiseError,  Exception(m)<EOL>return False,m<EOL><DEDENT>for _trial in range(ntrials):<EOL><INDENT>error = None<EOL>try:<EOL><INDENT>dirList = self.__get_repository_parent_directory(relativePath=relativePath)<EOL>assert dirList is not None, "<STR_LIT>"%(relativePath,)<EOL>stateBefore = self.get_repository_state(relaPath=parentPath)<EOL>_files = [f for f in dirList if isinstance(f, basestring)]<EOL>_dirs  = [d for d in dirList if isinstance(d, dict)]<EOL>_dirs  = [d for d in dirList if dirName not in d]<EOL>_ = [dirList.pop(<NUM_LIT:0>) for _ in range(len(dirList))]<EOL>dirList.extend(_files)<EOL>dirList.extend(_dirs)<EOL>if clean:<EOL><INDENT>shutil.rmtree(realPath)<EOL><DEDENT>else:<EOL><INDENT>stateAfter = self.get_repository_state(relaPath=parentPath)<EOL>success, errors = self.__clean_before_after(stateBefore=stateBefore, stateAfter=stateAfter, keepNoneEmptyDirectory=True)<EOL>assert success, "<STR_LIT:\n>".join(errors)<EOL><DEDENT><DEDENT>except Exception as err:<EOL><INDENT>error = str(err)<EOL>if self.DEBUG_PRINT_FAILED_TRIALS: print("<STR_LIT>"%(_trial, inspect.stack()[<NUM_LIT:1>][<NUM_LIT:3>], str(error)))<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if error is None:<EOL><INDENT>_, error = self.__save_repository_pickle_file(lockFirst=False, raiseError=False)<EOL><DEDENT>LD.release_lock()<EOL>LR.release_lock()<EOL>assert error is None or not raiseError, "<STR_LIT>"%(relativePath, ntrials, error,)<EOL>return error is None, error<EOL>

Remove directory from repository tracking.

:Parameters:
    #. relativePath (string): The relative to the repository path of the
       directory to remove from the repository.
    #. clean (boolean): Whether to os remove directory. If False only
       tracked files will be removed along with left empty directories.
    #. raiseError (boolean): Whether to raise encountered error instead
       of returning failure.
    #. ntrials (int): After aquiring all locks, ntrials is the maximum
       number of trials allowed before failing.
       In rare cases, when multiple processes
       are accessing the same repository components, different processes
       can alter repository components between successive lock releases
       of some other process. Bigger number of trials lowers the
       likelyhood of failure due to multiple processes same time
       alteration.

:Returns:
    #. success (boolean): Whether removing the directory was successful.
    #. reason (None, string): Reason why directory was not removed.


def remove_repository(self, path=None, removeEmptyDirs=True):

assert isinstance(removeEmptyDirs, bool), "<STR_LIT>"<EOL>if path is not None:<EOL><INDENT>if path != self.__path:<EOL><INDENT>repo = Repository()<EOL>repo.load_repository(path)<EOL><DEDENT>else:<EOL><INDENT>repo = self<EOL><DEDENT><DEDENT>else:<EOL><INDENT>repo = self<EOL><DEDENT>assert repo.path is not None, "<STR_LIT>"<EOL>for fdict in reversed(repo.get_repository_state()):<EOL><INDENT>relaPath   = list(fdict)[<NUM_LIT:0>]<EOL>realPath   = os.path.join(repo.path, relaPath)<EOL>path, name = os.path.split(realPath)<EOL>if fdict[relaPath]['<STR_LIT:type>'] == '<STR_LIT:file>':<EOL><INDENT>if os.path.isfile(realPath):<EOL><INDENT>os.remove(realPath)<EOL><DEDENT>if os.path.isfile(os.path.join(repo.path,path,self.__fileInfo%name)):<EOL><INDENT>os.remove(os.path.join(repo.path,path,self.__fileInfo%name))<EOL><DEDENT>if os.path.isfile(os.path.join(repo.path,path,self.__fileLock%name)):<EOL><INDENT>os.remove(os.path.join(repo.path,path,self.__fileLock%name))<EOL><DEDENT>if os.path.isfile(os.path.join(repo.path,path,self.__fileClass%name)):<EOL><INDENT>os.remove(os.path.join(repo.path,path,self.__fileClass%name))<EOL><DEDENT><DEDENT>elif fdict[relaPath]['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>if os.path.isfile(os.path.join(realPath,self.__dirInfo)):<EOL><INDENT>os.remove(os.path.join(realPath,self.__dirInfo))<EOL><DEDENT>if os.path.isfile(os.path.join(realPath,self.__dirLock)):<EOL><INDENT>os.remove(os.path.join(realPath,self.__dirLock))<EOL><DEDENT>if not len(os.listdir(realPath)) and removeEmptyDirs:<EOL><INDENT>shutil.rmtree( realPath )<EOL><DEDENT><DEDENT><DEDENT>if os.path.isfile(os.path.join(repo.path,self.__repoFile)):<EOL><INDENT>os.remove(os.path.join(repo.path,self.__repoFile))<EOL><DEDENT>if os.path.isfile(os.path.join(repo.path,self.__repoLock)):<EOL><INDENT>os.remove(os.path.join(repo.path,self.__repoLock))<EOL><DEDENT>

Remove all repository from path along with all repository tracked files.

:Parameters:
    #. path (None, string): The path the repository to remove.
    #. removeEmptyDirs (boolean): Whether to remove remaining empty
       directories.


def load_repository(self, path, verbose=True, ntrials=<NUM_LIT:3>):

assert isinstance(ntrials, int), "<STR_LIT>"<EOL>assert ntrials><NUM_LIT:0>, "<STR_LIT>"<EOL>repo = None<EOL>for _trial in range(ntrials):<EOL><INDENT>try:<EOL><INDENT>self.__load_repository(path=path, verbose=True)<EOL><DEDENT>except Exception as err1:<EOL><INDENT>try:<EOL><INDENT>from .OldRepository import Repository<EOL>REP = Repository(path)<EOL><DEDENT>except Exception as err2:<EOL><INDENT>error = "<STR_LIT>"%(err1, err2)<EOL>if self.DEBUG_PRINT_FAILED_TRIALS: print("<STR_LIT>"%(_trial, inspect.stack()[<NUM_LIT:1>][<NUM_LIT:3>], str(error)))<EOL><DEDENT>else:<EOL><INDENT>error = None<EOL>repo  = REP<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>error = None<EOL>repo  = self<EOL>break<EOL><DEDENT><DEDENT>assert error is None, error<EOL>return repo<EOL>

Load repository from a directory path and update the current instance.
First, new repository still will be loaded. If failed, then old
style repository load will be tried.

:Parameters:
    #. path (string): The path of the directory from where to load
       the repository from. If '.' or an empty string is passed,
       the current working directory will be used.
    #. verbose (boolean): Whether to be verbose about abnormalities
    #. ntrials (int): After aquiring all locks, ntrials is the maximum
       number of trials allowed before failing.
       In rare cases, when multiple processes
       are accessing the same repository components, different processes
       can alter repository components between successive lock releases
       of some other process. Bigger number of trials lowers the
       likelyhood of failure due to multiple processes same time
       alteration.

:Returns:
     #. repository (pyrep.Repository): returns self repository with loaded data.


return copy.deepcopy(self.__get_repository_directory(relativePath))<EOL>

Get repository directory list copy.

:Parameters:
    #. relativePath (string): The relative to the repository path .

:Returns:
    #. dirList (None, list): List of directories and files in repository
       directory. If directory is not tracked in repository None is
       returned


@path_required<EOL><INDENT>def rename_directory(self, relativePath, newName, raiseError=True, ntrials=<NUM_LIT:3>):<DEDENT>

assert isinstance(raiseError, bool), "<STR_LIT>"<EOL>assert isinstance(ntrials, int), "<STR_LIT>"<EOL>assert ntrials><NUM_LIT:0>, "<STR_LIT>"<EOL>relativePath = self.to_repo_relative_path(path=relativePath, split=False)<EOL>parentPath, dirName = os.path.split(relativePath)<EOL>if relativePath == '<STR_LIT>':<EOL><INDENT>error = "<STR_LIT>"<EOL>assert not raiseError, error<EOL>return False, error<EOL><DEDENT>realPath = os.path.join(self.__path,relativePath)<EOL>newRealPath = os.path.join(os.path.dirname(realPath), newName)<EOL>if os.path.isdir(newRealPath):<EOL><INDENT>error = "<STR_LIT>"%(newRealPath,)<EOL>assert not raiseError, error<EOL>return False, error<EOL><DEDENT>LD =  Locker(filePath=None, lockPass=str(uuid.uuid1()), lockPath=os.path.join(self.__path,parentPath, self.__dirLock))<EOL>acquired, code = LD.acquire_lock()<EOL>if not acquired:<EOL><INDENT>error = "<STR_LIT>"%(code,dirPath)<EOL>assert not raiseError, error<EOL>return False, error<EOL><DEDENT>error = None<EOL>LR =  Locker(filePath=None, lockPass=str(uuid.uuid1()), lockPath=os.path.join(self.__path, self.__repoLock))<EOL>acquired, code = LR.acquire_lock()<EOL>if not acquired:<EOL><INDENT>LD.release_lock()<EOL>m = "<STR_LIT>"%(code,dirPath)<EOL>assert raiseError,  Exception(m)<EOL>return False,m<EOL><DEDENT>for _trial in range(ntrials):<EOL><INDENT>try:<EOL><INDENT>repo = self.__load_repository_pickle_file(os.path.join(self.__path, self.__repoFile))<EOL>self.__repo['<STR_LIT>'] = repo['<STR_LIT>']<EOL><DEDENT>except Exception as err:<EOL><INDENT>error = str(err)<EOL>if self.DEBUG_PRINT_FAILED_TRIALS: print("<STR_LIT>"%(_trial, inspect.stack()[<NUM_LIT:1>][<NUM_LIT:3>], str(error)))<EOL><DEDENT>else:<EOL><INDENT>error = None<EOL>break<EOL><DEDENT><DEDENT>if error is not None:<EOL><INDENT>LD.release_lock()<EOL>LR.release_lock()<EOL>assert not raiseError, Exception(error)<EOL>return False, error<EOL><DEDENT>for _trial in range(ntrials):<EOL><INDENT>error = None<EOL>try:<EOL><INDENT>dirList = self.__get_repository_parent_directory(relativePath=relativePath)<EOL>assert dirList is not None, "<STR_LIT>"%(relativePath,)<EOL>_dirDict = [nd for nd in dirList  if isinstance(nd,dict)]<EOL>_dirDict = [nd for nd in _dirDict if dirName in nd]<EOL>assert len(_dirDict) == <NUM_LIT:1>, "<STR_LIT>"<EOL>os.rename(realPath, newRealPath)<EOL>_dirDict[<NUM_LIT:0>][newName] = _dirDict[<NUM_LIT:0>][dirName]<EOL>_dirDict[<NUM_LIT:0>].pop(dirName)<EOL>self.__save_dirinfo(description=None, dirInfoPath=parentPath, create=False)<EOL><DEDENT>except Exception as err:<EOL><INDENT>error = str(err)<EOL>if self.DEBUG_PRINT_FAILED_TRIALS: print("<STR_LIT>"%(_trial, inspect.stack()[<NUM_LIT:1>][<NUM_LIT:3>], str(error)))<EOL><DEDENT>else:<EOL><INDENT>error = None<EOL>break<EOL><DEDENT><DEDENT>if error is None:<EOL><INDENT>_, error = self.__save_repository_pickle_file(lockFirst=False, raiseError=False)<EOL><DEDENT>LR.release_lock()<EOL>LD.release_lock()<EOL>assert error is None or not raiseError, "<STR_LIT>"%(relativePath, newName, ntrials, error,)<EOL>return error is None, error<EOL>

Rename a directory in the repository. It insures renaming the directory in the system.

:Parameters:
    #. relativePath (string): The relative to the repository path of
       the directory to be renamed.
    #. newName (string): The new directory name.
    #. raiseError (boolean): Whether to raise encountered error instead
       of returning failure.
    #. ntrials (int): After aquiring all locks, ntrials is the maximum
       number of trials allowed before failing.
       In rare cases, when multiple processes
       are accessing the same repository components, different processes
       can alter repository components between successive lock releases
       of some other process. Bigger number of trials lowers the
       likelyhood of failure due to multiple processes same time
       alteration.

:Returns:
    #. success (boolean): Whether renaming the directory was successful.
    #. message (None, string): Some explanatory message or error reason
       why directory was not renamed.


@path_required<EOL><INDENT>def pull_file(self, relativePath, pull=None, update=True, ntrials=<NUM_LIT:3>):<DEDENT>

assert isinstance(ntrials, int), "<STR_LIT>"<EOL>assert ntrials><NUM_LIT:0>, "<STR_LIT>"<EOL>relativePath = self.to_repo_relative_path(path=relativePath, split=False)<EOL>realPath     = os.path.join(self.__path,relativePath)<EOL>fPath, fName = os.path.split(realPath)<EOL>isRepoFile,fileOnDisk, infoOnDisk, classOnDisk = self.is_repository_file(relativePath)<EOL>if not isRepoFile:<EOL><INDENT>fileOnDisk  = ["<STR_LIT>","<STR_LIT>"][fileOnDisk]<EOL>infoOnDisk  = ["<STR_LIT>","<STR_LIT>"%self.__fileInfo%fName][infoOnDisk]<EOL>classOnDisk = ["<STR_LIT>","<STR_LIT>"%self.__fileClass%fName][classOnDisk]<EOL>assert False, "<STR_LIT>"%(relativePath,fileOnDisk,infoOnDisk,classOnDisk)<EOL><DEDENT>assert fileOnDisk, "<STR_LIT>"%(relativePath,)<EOL>if not infoOnDisk:<EOL><INDENT>if pull is not None:<EOL><INDENT>warnings.warn("<STR_LIT>"%(self.__fileInfo%fName))<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>"%(relativePath,(self.__fileInfo%fName)))<EOL><DEDENT><DEDENT>LF =  Locker(filePath=None, lockPass=str(uuid.uuid1()), lockPath=os.path.join(fPath,self.__fileLock%fName))<EOL>acquired, code = LF.acquire_lock()<EOL>if not acquired:<EOL><INDENT>error = "<STR_LIT>"%(code,relativePath)<EOL>return False, error<EOL><DEDENT>for _trial in range(ntrials):<EOL><INDENT>error = None<EOL>try:<EOL><INDENT>if pull is not None:<EOL><INDENT>pull = get_pull_method(pull)<EOL><DEDENT>else:<EOL><INDENT>with open(os.path.join(fPath,self.__fileInfo%fName), '<STR_LIT:rb>') as fd:<EOL><INDENT>info = pickle.load(fd)<EOL><DEDENT>pull = info['<STR_LIT>']<EOL><DEDENT>my_exec( pull.replace("<STR_LIT>", str(realPath) ), locals=locals(), globals=globals(), description='<STR_LIT>' )<EOL><DEDENT>except Exception as err:<EOL><INDENT>LF.release_lock()<EOL>m = str(pull).replace("<STR_LIT>", str(realPath) )<EOL>error = "<STR_LIT>"%(m,err)<EOL>if self.DEBUG_PRINT_FAILED_TRIALS: print("<STR_LIT>"%(_trial, inspect.stack()[<NUM_LIT:1>][<NUM_LIT:3>], str(error)))<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>LF.release_lock()<EOL>assert error is None, "<STR_LIT>"%(ntrials, error)<EOL>return locals()['<STR_LIT>']<EOL>

Pull a file's data from the Repository.

:Parameters:
    #. relativePath (string): The relative to the repository path from
       where to pull the file.
    #. pull (None, string): The pulling method.
       If None, the pull method saved in the file info will be used.
       If a string is given, the string should include all the necessary
       imports, a '$FILE_PATH' that replaces the absolute file path when
       the dumping will be performed and finally a PULLED_DATA variable.
       e.g "import numpy as np; PULLED_DATA=np.loadtxt(fname='$FILE_PATH')"
    #. update (boolean): If pull is not None, Whether to update the pull
       method stored in the file info by the given pull method.
    #. ntrials (int): After aquiring all locks, ntrials is the maximum
       number of trials allowed before failing.
       In rare cases, when multiple processes
       are accessing the same repository components, different processes
       can alter repository components between successive lock releases
       of some other process. Bigger number of trials lowers the
       likelyhood of failure due to multiple processes same time
       alteration.

:Returns:
    #. data (object): The pulled data from the file.


def __clean_before_after(self, stateBefore, stateAfter, keepNoneEmptyDirectory=True):

<EOL>errors    = []<EOL>afterDict = {}<EOL>[afterDict.setdefault(list(aitem)[<NUM_LIT:0>],[]).append(aitem) for aitem in stateAfter]<EOL>for bitem in reversed(stateBefore):<EOL><INDENT>relaPath = list(bitem)[<NUM_LIT:0>]<EOL>basename = os.path.basename(relaPath)<EOL>btype    = bitem[relaPath]['<STR_LIT:type>']<EOL>alist    = afterDict.get(relaPath, [])<EOL>aitem    = [a for a in alist if a[relaPath]['<STR_LIT:type>']==btype]<EOL>if len(aitem)><NUM_LIT:1>:<EOL><INDENT>errors.append("<STR_LIT>"%(basename,btype,relaPath))<EOL>continue<EOL><DEDENT>if not len(aitem):<EOL><INDENT>removeDirs  = []<EOL>removeFiles = []<EOL>if btype == '<STR_LIT>':<EOL><INDENT>if not len(relaPath):<EOL><INDENT>errors.append("<STR_LIT>")<EOL>continue<EOL><DEDENT>removeDirs.append(os.path.join(self.__path,relaPath))<EOL>removeFiles.append(os.path.join(self.__path,relaPath,self.__dirInfo))<EOL>removeFiles.append(os.path.join(self.__path,relaPath,self.__dirLock))<EOL><DEDENT>elif btype == '<STR_LIT:file>':<EOL><INDENT>removeFiles.append(os.path.join(self.__path,relaPath))<EOL>removeFiles.append(os.path.join(self.__path,relaPath,self.__fileInfo%basename))<EOL>removeFiles.append(os.path.join(self.__path,relaPath,self.__fileLock%basename))<EOL><DEDENT>else:<EOL><INDENT>removeDirs.append(os.path.join(self.__path,relaPath))<EOL>removeFiles.append(os.path.join(self.__path,relaPath,self.__fileInfo%basename))<EOL><DEDENT>for fpath in removeFiles:<EOL><INDENT>if os.path.isfile(fpath):<EOL><INDENT>try:<EOL><INDENT>os.remove(fpath)<EOL><DEDENT>except Exception as err:<EOL><INDENT>errors.append("<STR_LIT>"%(fpath, str(err)))<EOL><DEDENT><DEDENT><DEDENT>for dpath in removeDirs:<EOL><INDENT>if os.path.isdir(dpath):<EOL><INDENT>if keepNoneEmptyDirectory or not len(os.listdir(dpath)):<EOL><INDENT>try:<EOL><INDENT>shutil.rmtree(dpath)<EOL><DEDENT>except Exception as err:<EOL><INDENT>errors.append("<STR_LIT>"%(fpath, str(err)))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>return len(errors)==<NUM_LIT:0>, errors<EOL>

clean repository given before and after states


def walk_directories_path(self, relativePath="<STR_LIT>", fullPath=False, recursive=False):

assert isinstance(fullPath, bool), "<STR_LIT>"<EOL>assert isinstance(recursive, bool), "<STR_LIT>"<EOL>relativePath = self.to_repo_relative_path(path=relativePath, split=False)<EOL>dirList      = self.__get_repository_directory(relativePath=relativePath)<EOL>assert dirList is not None, "<STR_LIT>"%relativePath<EOL>def _walk(rpath, dlist,recursive):<EOL><INDENT>for ddict in dlist:<EOL><INDENT>if isinstance(ddict, dict):<EOL><INDENT>dname = list(ddict)[<NUM_LIT:0>]<EOL>if fullPath:<EOL><INDENT>yield os.path.join(self.__path, rpath, dname)<EOL><DEDENT>else:<EOL><INDENT>yield os.path.join(rpath, dname)<EOL><DEDENT><DEDENT><DEDENT>if recursive:<EOL><INDENT>for ddict in dlist:<EOL><INDENT>if isinstance(ddict, dict):<EOL><INDENT>dname = list(ddict)[<NUM_LIT:0>]<EOL>for p in _walk(rpath=os.path.join(rpath,dname), dlist=ddict[dname],recursive=recursive):<EOL><INDENT>yield p<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>return _walk(rpath=relativePath, dlist=dirList, recursive=recursive)<EOL>

Walk repository relative path and yield directory relative/full path

:parameters:
    #. relativePath (string): The relative path from which start the walk.
    #. fullPath (boolean): Whether to return full or relative path.
    #. recursive (boolean): Whether walk all directories files recursively.