integration_id = int(integration_id)<EOL>with open(private_key_path, '<STR_LIT:rb>') as f:<EOL><INDENT>cert_bytes = f.read()<EOL><DEDENT>now = datetime.datetime.now()<EOL>expiration_time = now + datetime.timedelta(minutes=<NUM_LIT:9>)<EOL>payload = {<EOL>'<STR_LIT>': int(now.timestamp()),<EOL>'<STR_LIT>': int(expiration_time.timestamp()),<EOL>'<STR_LIT>': integration_id<EOL>}<EOL>return jwt.encode(payload, cert_bytes, algorithm='<STR_LIT>')<EOL>

Create a JSON Web Token to authenticate a GitHub Integration or
    installation.

    Parameters
    ----------
    integration_id : `int`
        Integration ID. This is available from the GitHub integration's
        homepage.
    private_key_path : `str`
        Path to the integration's private key (a ``.pem`` file).

    Returns
    -------
    jwt : `bytes`
        JSON Web Token that is good for 9 minutes.

    Notes
    -----
    The JWT is encoded with the RS256 algorithm. It includes a payload with
    fields:

    - ``'iat'``: The current time, as an `int` timestamp.
    - ``'exp'``: Expiration time, as an `int timestamp. The expiration
      time is set of 9 minutes in the future (maximum allowance is 10 minutes).
    - ``'iss'``: The integration ID (`int`).

    For more information, see
    https://developer.github.com/early-access/integrations/authentication/.


def make_raw_content_url(repo_slug, git_ref, file_path):

if isinstance(repo_slug, RepoSlug):<EOL><INDENT>slug_str = repo_slug.full<EOL><DEDENT>else:<EOL><INDENT>slug_str = repo_slug<EOL><DEDENT>if file_path.startswith('<STR_LIT:/>'):<EOL><INDENT>file_path = file_path.lstrip('<STR_LIT:/>')<EOL><DEDENT>template = '<STR_LIT>'<EOL>return template.format(<EOL>slug=slug_str,<EOL>git_ref=git_ref,<EOL>path=file_path)<EOL>

Make a raw content (raw.githubusercontent.com) URL to a file.

    Parameters
    ----------
    repo_slug : `str` or `RepoSlug`
        The repository slug, formatted as either a `str` (``'owner/name'``)
        or a `RepoSlug` object (created by `parse_repo_slug_from_url`).
    git_ref : `str`
        The git ref: a branch name, commit hash, or tag name.
    file_path : `str`
        The POSIX path of the file in the repository tree.


<EOL>if url.endswith('<STR_LIT>'):<EOL><INDENT>url = url[:-<NUM_LIT:4>]<EOL><DEDENT>return url<EOL>

Normalize a GitHub URL into the root repository URL.

    Parameters
    ----------
    url : `str`
        A GitHub URL

    Returns
    -------
    url : `str`
        Normalized URL of a GitHub repository.

    Examples
    --------
    >>> normalize_repo_root_url('https://github.com/lsst/LDM-151.git')
    'https://github.com/lsst/LDM-151'


async def process_ltd_doc_products(session, product_urls, github_api_token,<EOL>mongo_collection=None):

tasks = [asyncio.ensure_future(<EOL>process_ltd_doc(session, github_api_token,<EOL>product_url,<EOL>mongo_collection=mongo_collection))<EOL>for product_url in product_urls]<EOL>await asyncio.gather(*tasks)<EOL>

Run a pipeline to process extract, transform, and load metadata for
    multiple LSST the Docs-hosted projects

    Parameters
    ----------
    session : `aiohttp.ClientSession`
        Your application's aiohttp client session.
        See http://aiohttp.readthedocs.io/en/stable/client.html.
    product_urls : `list` of `str`
        List of LSST the Docs product URLs.
    github_api_token : `str`
        A GitHub personal API token. See the `GitHub personal access token
        guide`_.
    mongo_collection : `motor.motor_asyncio.AsyncIOMotorCollection`, optional
        MongoDB collection. This should be the common MongoDB collection for
        LSST projectmeta JSON-LD records.


parser = argparse.ArgumentParser(<EOL>description='<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>parser.add_argument(<EOL>'<STR_LIT>',<EOL>dest='<STR_LIT>',<EOL>help='<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>parser.add_argument(<EOL>'<STR_LIT>',<EOL>help='<STR_LIT>')<EOL>parser.add_argument(<EOL>'<STR_LIT>',<EOL>help='<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>parser.add_argument(<EOL>'<STR_LIT>',<EOL>default='<STR_LIT>',<EOL>help='<STR_LIT>')<EOL>parser.add_argument(<EOL>'<STR_LIT>',<EOL>default='<STR_LIT>',<EOL>help='<STR_LIT>')<EOL>args = parser.parse_args()<EOL>stream_handler = logging.StreamHandler()<EOL>stream_formatter = logging.Formatter(<EOL>'<STR_LIT>')<EOL>stream_handler.setFormatter(stream_formatter)<EOL>root_logger = logging.getLogger()<EOL>root_logger.addHandler(stream_handler)<EOL>root_logger.setLevel(logging.WARNING)<EOL>app_logger = logging.getLogger('<STR_LIT>')<EOL>app_logger.setLevel(logging.DEBUG)<EOL>if args.mongodb_uri is not None:<EOL><INDENT>mongo_client = AsyncIOMotorClient(args.mongodb_uri, ssl=True)<EOL>collection = mongo_client[args.mongodb_db][args.mongodb_collection]<EOL><DEDENT>else:<EOL><INDENT>collection = None<EOL><DEDENT>loop = asyncio.get_event_loop()<EOL>if args.ltd_product_url is not None:<EOL><INDENT>loop.run_until_complete(run_single_ltd_doc(args.ltd_product_url,<EOL>args.github_token,<EOL>collection))<EOL><DEDENT>else:<EOL><INDENT>loop.run_until_complete(run_bulk_etl(args.github_token,<EOL>collection))<EOL><DEDENT>

Command line entrypoint to reduce technote metadata.


if dt.tzinfo is None:<EOL><INDENT>dt = dt.replace(tzinfo=datetime.timezone.utc)<EOL><DEDENT>dt = dt.astimezone(datetime.timezone.utc)<EOL>return dt.strftime('<STR_LIT>')<EOL>

Encode a datetime in the format '%Y-%m-%dT%H:%M:%SZ'.

        The datetime can be naieve (doesn't have timezone info) or aware
        (it does have a tzinfo attribute set). Regardless, the datetime
        is transformed into UTC.


decoder = json.JSONDecoder(object_pairs_hook=_decode_object_pairs)<EOL>return decoder.decode(jsonld_text)<EOL>

Decode a JSON-LD dataset, including decoding datetime
    strings into `datetime.datetime` objects.

    Parameters
    ----------
    encoded_dataset : `str`
        The JSON-LD dataset encoded as a string.

    Returns
    -------
    jsonld_dataset : `dict`
        A JSON-LD dataset.

    Examples
    --------

    >>> doc = '{"dt": "2018-01-01T12:00:00Z"}'
    >>> decode_jsonld(doc)
    {'dt': datetime.datetime(2018, 1, 1, 12, 0, tzinfo=datetime.timezone.utc)}


if isinstance(obj, datetime.datetime):<EOL><INDENT>return self._encode_datetime(obj)<EOL><DEDENT>return json.JSONEncoder.default(self, obj)<EOL>

Encode values as JSON strings.

        This method overrides the default implementation from
        `json.JSONEncoder`.


def reduce_technote_metadata(github_url, metadata, github_data,<EOL>ltd_product_data):

repo_slug = parse_repo_slug_from_url(github_url)<EOL>jsonld = {<EOL>'<STR_LIT>': [<EOL>"<STR_LIT>"<EOL>"<STR_LIT>",<EOL>"<STR_LIT>"],<EOL>'<STR_LIT>': ['<STR_LIT>', '<STR_LIT>'],<EOL>'<STR_LIT>': github_url<EOL>}<EOL>if '<STR_LIT:url>' in metadata:<EOL><INDENT>url = metadata['<STR_LIT:url>']<EOL><DEDENT>elif '<STR_LIT>' in ltd_product_data:<EOL><INDENT>url = ltd_product_data['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>'<EOL>'<STR_LIT:{}>'.format(github_url))<EOL><DEDENT>jsonld['<STR_LIT>'] = url<EOL>jsonld['<STR_LIT:url>'] = url<EOL>if '<STR_LIT>' in metadata and '<STR_LIT>' in metadata:<EOL><INDENT>jsonld['<STR_LIT>'] = '<STR_LIT>'.format(**metadata)<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>'.format(github_url))<EOL><DEDENT>if '<STR_LIT>' in metadata:<EOL><INDENT>jsonld['<STR_LIT:name>'] = metadata['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT:description>' in metadata:<EOL><INDENT>jsonld['<STR_LIT:description>'] = metadata['<STR_LIT:description>']<EOL><DEDENT>if '<STR_LIT>' in metadata:<EOL><INDENT>jsonld['<STR_LIT>'] = [{'<STR_LIT>': '<STR_LIT>', '<STR_LIT:name>': author_name}<EOL>for author_name in metadata['<STR_LIT>']]<EOL><DEDENT>if '<STR_LIT>' in metadata:<EOL><INDENT>jsonld['<STR_LIT>'] = datetime.datetime.strptime(<EOL>metadata['<STR_LIT>'],<EOL>'<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>_repo_data = github_data['<STR_LIT:data>']['<STR_LIT>']<EOL>_master_data = _repo_data['<STR_LIT>']<EOL>jsonld['<STR_LIT>'] = datetime.datetime.strptime(<EOL>_master_data['<STR_LIT:target>']['<STR_LIT>'],<EOL>'<STR_LIT>')<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>try:<EOL><INDENT>_license_data = github_data['<STR_LIT:data>']['<STR_LIT>']['<STR_LIT>']<EOL>_spdxId = _license_data['<STR_LIT>']<EOL>if _spdxId is not None:<EOL><INDENT>_spdx_url = '<STR_LIT>'.format(_spdxId)<EOL>jsonld['<STR_LIT>'] = _spdx_url<EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>_master_data = github_data['<STR_LIT:data>']['<STR_LIT>']['<STR_LIT>']<EOL>_files = _master_data['<STR_LIT:target>']['<STR_LIT>']['<STR_LIT>']<EOL>for _node in _files:<EOL><INDENT>filename = _node['<STR_LIT:name>']<EOL>normalized_filename = filename.lower()<EOL>if normalized_filename.startswith('<STR_LIT>'):<EOL><INDENT>readme_url = make_raw_content_url(repo_slug, '<STR_LIT>',<EOL>filename)<EOL>jsonld['<STR_LIT>'] = readme_url<EOL>break<EOL><DEDENT><DEDENT><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>travis_url = '<STR_LIT>'.format(repo_slug.full)<EOL>jsonld['<STR_LIT>'] = travis_url<EOL>return jsonld<EOL>

Reduce a technote project's metadata from multiple sources into a
    single JSON-LD resource.

    Parameters
    ----------
    github_url : `str`
        URL of the technote's GitHub repository.
    metadata : `dict`
        The parsed contents of ``metadata.yaml`` found in a technote's
        repository.
    github_data : `dict`
        The contents of the ``technote_repo`` GitHub GraphQL API query.
    ltd_product_data : `dict`
        JSON dataset for the technote corresponding to the
        ``/products/<product>`` of LTD Keeper.

    Returns
    -------
    metadata : `dict`
        JSON-LD-formatted dictionary.

    .. `GitHub personal access token guide`: https://ls.st/41d


async def _upload_to_mongodb(collection, jsonld):

document = {<EOL>'<STR_LIT:data>': jsonld<EOL>}<EOL>query = {<EOL>'<STR_LIT>': jsonld['<STR_LIT>']<EOL>}<EOL>await collection.update(query, document, upsert=True, multi=False)<EOL>

Upsert the technote resource into the projectmeta MongoDB collection.

    Parameters
    ----------
    collection : `motor.motor_asyncio.AsyncIOMotorCollection`
        The MongoDB collection.
    jsonld : `dict`
        The JSON-LD document that represents the document resource.


if repo is None:<EOL><INDENT>repo = git.repo.base.Repo(path=repo_path,<EOL>search_parent_directories=True)<EOL><DEDENT>head_commit = repo.head.commit<EOL>return head_commit.committed_datetime<EOL>

Obtain the timestamp from the current head commit of a Git repository.

    Parameters
    ----------
    repo_path : `str`, optional
        Path to the Git repository. Leave as `None` to use the current working
        directory.

    Returns
    -------
    commit_timestamp : `datetime.datetime`
        The datetime of the head commit.


def read_git_commit_timestamp_for_file(filepath, repo_path=None, repo=None):

logger = logging.getLogger(__name__)<EOL>if repo is None:<EOL><INDENT>repo = git.repo.base.Repo(path=repo_path,<EOL>search_parent_directories=True)<EOL><DEDENT>repo_path = repo.working_tree_dir<EOL>head_commit = repo.head.commit<EOL>logger.debug('<STR_LIT>', repo_path)<EOL>filepath = os.path.relpath(<EOL>os.path.abspath(filepath),<EOL>start=repo_path)<EOL>logger.debug('<STR_LIT>', filepath)<EOL>for commit in head_commit.iter_items(repo,<EOL>head_commit,<EOL>[filepath],<EOL>skip=<NUM_LIT:0>):<EOL><INDENT>return commit.committed_datetime<EOL><DEDENT>raise IOError('<STR_LIT>'.format(filepath))<EOL>

Obtain the timestamp for the most recent commit to a given file in a
    Git repository.

    Parameters
    ----------
    filepath : `str`
        Absolute or repository-relative path for a file.
    repo_path : `str`, optional
        Path to the Git repository. Leave as `None` to use the current working
        directory or if a ``repo`` argument is provided.
    repo : `git.Repo`, optional
        A `git.Repo` instance.

    Returns
    -------
    commit_timestamp : `datetime.datetime`
        The datetime of the most recent commit to the given file.

    Raises
    ------
    IOError
        Raised if the ``filepath`` does not exist in the Git repository.


self._path_to_tagger = path_to_tagger<EOL>self._dir_to_tagger = os.path.dirname(path_to_tagger)<EOL>self._tagger = subprocess.Popen('<STR_LIT>'+os.path.basename(path_to_tagger),<EOL>cwd=self._dir_to_tagger,<EOL>stdin=subprocess.PIPE, stdout=subprocess.PIPE)<EOL>


return _gen(parse(s), limit, count=True)<EOL>

Counts all matching strings to a given regular expression

    :param s: Regular expression
    :type s: str
    :param limit: Range limit
    :type limit: int
    :rtype: int
    :returns: number of matching strings


def _gen(d, limit=<NUM_LIT:20>, count=False, grouprefs=None):

if grouprefs is None:<EOL><INDENT>grouprefs = {}<EOL><DEDENT>ret = ['<STR_LIT>']<EOL>strings = <NUM_LIT:0><EOL>literal = False<EOL>for i in d:<EOL><INDENT>if i[<NUM_LIT:0>] == sre_parse.IN:<EOL><INDENT>subs = _in(i[<NUM_LIT:1>])<EOL>if count:<EOL><INDENT>strings = (strings or <NUM_LIT:1>) * len(subs)<EOL><DEDENT>ret = comb(ret, subs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.LITERAL:<EOL><INDENT>literal = True<EOL>ret = mappend(ret, unichr(i[<NUM_LIT:1>]))<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.CATEGORY:<EOL><INDENT>subs = CATEGORIES.get(i[<NUM_LIT:1>], ['<STR_LIT>'])<EOL>if count:<EOL><INDENT>strings = (strings or <NUM_LIT:1>) * len(subs)<EOL><DEDENT>ret = comb(ret, subs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ANY:<EOL><INDENT>subs = CATEGORIES['<STR_LIT>']<EOL>if count:<EOL><INDENT>strings = (strings or <NUM_LIT:1>) * len(subs)<EOL><DEDENT>ret = comb(ret, subs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.MAX_REPEAT or i[<NUM_LIT:0>] == sre_parse.MIN_REPEAT:<EOL><INDENT>items = list(i[<NUM_LIT:1>][<NUM_LIT:2>])<EOL>if i[<NUM_LIT:1>][<NUM_LIT:1>] + <NUM_LIT:1> - i[<NUM_LIT:1>][<NUM_LIT:0>] >= limit:<EOL><INDENT>r1 = i[<NUM_LIT:1>][<NUM_LIT:0>]<EOL>r2 = i[<NUM_LIT:1>][<NUM_LIT:0>] + limit<EOL><DEDENT>else:<EOL><INDENT>r1 = i[<NUM_LIT:1>][<NUM_LIT:0>]<EOL>r2 = i[<NUM_LIT:1>][<NUM_LIT:1>] + <NUM_LIT:1><EOL><DEDENT>ran = range(r1, r2)<EOL>if count:<EOL><INDENT>branch_count = <NUM_LIT:0><EOL>for p in ran:<EOL><INDENT>branch_count += pow(_gen(items, limit, True, grouprefs), p)<EOL><DEDENT>strings = (strings or <NUM_LIT:1>) * branch_count<EOL><DEDENT>ret = prods(ret, ran, items, limit, grouprefs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.BRANCH:<EOL><INDENT>if count:<EOL><INDENT>for x in i[<NUM_LIT:1>][<NUM_LIT:1>]:<EOL><INDENT>strings += _gen(x, limit, True, grouprefs) or <NUM_LIT:1><EOL><DEDENT><DEDENT>ret = concit(ret, i[<NUM_LIT:1>][<NUM_LIT:1>], limit, grouprefs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.SUBPATTERN or i[<NUM_LIT:0>] == sre_parse.ASSERT:<EOL><INDENT>subexpr = i[<NUM_LIT:1>][<NUM_LIT:1>]<EOL>if IS_PY36_OR_GREATER and i[<NUM_LIT:0>] == sre_parse.SUBPATTERN:<EOL><INDENT>subexpr = i[<NUM_LIT:1>][<NUM_LIT:3>]<EOL><DEDENT>if count:<EOL><INDENT>strings = (<EOL>strings or <NUM_LIT:1>) * (sum(ggen([<NUM_LIT:0>], _gen, subexpr, limit=limit, count=True, grouprefs=grouprefs)) or <NUM_LIT:1>)<EOL><DEDENT>ret = ggen(ret, _gen, subexpr, limit=limit, count=False, grouprefs=grouprefs, groupref=i[<NUM_LIT:1>][<NUM_LIT:0>])<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.AT:<EOL><INDENT>continue<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.NOT_LITERAL:<EOL><INDENT>subs = list(CATEGORIES['<STR_LIT>'])<EOL>if unichr(i[<NUM_LIT:1>]) in subs:<EOL><INDENT>subs.remove(unichr(i[<NUM_LIT:1>]))<EOL><DEDENT>if count:<EOL><INDENT>strings = (strings or <NUM_LIT:1>) * len(subs)<EOL><DEDENT>ret = comb(ret, subs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.GROUPREF:<EOL><INDENT>ret = dappend(ret, grouprefs, i[<NUM_LIT:1>])<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ASSERT_NOT:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>' + repr(i))<EOL><DEDENT><DEDENT>if count:<EOL><INDENT>if strings == <NUM_LIT:0> and literal:<EOL><INDENT>inc = True<EOL>for i in d:<EOL><INDENT>if i[<NUM_LIT:0>] not in (sre_parse.AT, sre_parse.LITERAL):<EOL><INDENT>inc = False<EOL><DEDENT><DEDENT>if inc:<EOL><INDENT>strings = <NUM_LIT:1><EOL><DEDENT><DEDENT>return strings<EOL><DEDENT>return ret<EOL>


return _gen(parse(s), limit)<EOL>

Creates a generator that generates all matching strings to a given regular expression

    :param s: Regular expression
    :type s: str
    :param limit: Range limit
    :type limit: int
    :returns: string generator object


ret = u'<STR_LIT>'<EOL>for i in sre_obj:<EOL><INDENT>if i[<NUM_LIT:0>] == sre_parse.IN:<EOL><INDENT>prefix = '<STR_LIT>'<EOL>if len(i[<NUM_LIT:1>]) and i[<NUM_LIT:1>][<NUM_LIT:0>][<NUM_LIT:0>] == sre_parse.NEGATE:<EOL><INDENT>prefix = '<STR_LIT>'<EOL><DEDENT>ret += u'<STR_LIT>'.format(prefix, sre_to_string(i[<NUM_LIT:1>], paren=paren))<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.LITERAL:<EOL><INDENT>u = unichr(i[<NUM_LIT:1>])<EOL>ret += u if u not in sre_parse.SPECIAL_CHARS else '<STR_LIT>'.format(u)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.CATEGORY:<EOL><INDENT>ret += REVERSE_CATEGORIES[i[<NUM_LIT:1>]]<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ANY:<EOL><INDENT>ret += '<STR_LIT:.>'<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.BRANCH:<EOL><INDENT>parts = [sre_to_string(x, paren=paren) for x in i[<NUM_LIT:1>][<NUM_LIT:1>]]<EOL>if not any(parts):<EOL><INDENT>continue<EOL><DEDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>]:<EOL><INDENT>if len(parts) == <NUM_LIT:1>:<EOL><INDENT>paren = False<EOL><DEDENT>prefix = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>prefix = '<STR_LIT>'<EOL><DEDENT>branch = '<STR_LIT:|>'.join(parts)<EOL>if paren:<EOL><INDENT>ret += '<STR_LIT>'.format(prefix, branch)<EOL><DEDENT>else:<EOL><INDENT>ret += '<STR_LIT>'.format(branch)<EOL><DEDENT><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.SUBPATTERN:<EOL><INDENT>subexpr = i[<NUM_LIT:1>][<NUM_LIT:1>]<EOL>if IS_PY36_OR_GREATER and i[<NUM_LIT:0>] == sre_parse.SUBPATTERN:<EOL><INDENT>subexpr = i[<NUM_LIT:1>][<NUM_LIT:3>]<EOL><DEDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>]:<EOL><INDENT>ret += '<STR_LIT>'.format(sre_to_string(subexpr, paren=False))<EOL><DEDENT>else:<EOL><INDENT>ret += '<STR_LIT>'.format(sre_to_string(subexpr, paren=paren))<EOL><DEDENT><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.NOT_LITERAL:<EOL><INDENT>ret += '<STR_LIT>'.format(unichr(i[<NUM_LIT:1>]))<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.MAX_REPEAT:<EOL><INDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>] == i[<NUM_LIT:1>][<NUM_LIT:1>]:<EOL><INDENT>range_str = '<STR_LIT>'.format(i[<NUM_LIT:1>][<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>] == <NUM_LIT:0> and i[<NUM_LIT:1>][<NUM_LIT:1>] - i[<NUM_LIT:1>][<NUM_LIT:0>] == sre_parse.MAXREPEAT:<EOL><INDENT>range_str = '<STR_LIT:*>'<EOL><DEDENT>elif i[<NUM_LIT:1>][<NUM_LIT:0>] == <NUM_LIT:1> and i[<NUM_LIT:1>][<NUM_LIT:1>] - i[<NUM_LIT:1>][<NUM_LIT:0>] == sre_parse.MAXREPEAT - <NUM_LIT:1>:<EOL><INDENT>range_str = '<STR_LIT:+>'<EOL><DEDENT>else:<EOL><INDENT>range_str = '<STR_LIT>'.format(i[<NUM_LIT:1>][<NUM_LIT:0>], i[<NUM_LIT:1>][<NUM_LIT:1>])<EOL><DEDENT><DEDENT>ret += sre_to_string(i[<NUM_LIT:1>][<NUM_LIT:2>], paren=paren) + range_str<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.MIN_REPEAT:<EOL><INDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>] == <NUM_LIT:0> and i[<NUM_LIT:1>][<NUM_LIT:1>] == sre_parse.MAXREPEAT:<EOL><INDENT>range_str = '<STR_LIT>'<EOL><DEDENT>elif i[<NUM_LIT:1>][<NUM_LIT:0>] == <NUM_LIT:1> and i[<NUM_LIT:1>][<NUM_LIT:1>] == sre_parse.MAXREPEAT:<EOL><INDENT>range_str = '<STR_LIT>'<EOL><DEDENT>elif i[<NUM_LIT:1>][<NUM_LIT:1>] == sre_parse.MAXREPEAT:<EOL><INDENT>range_str = '<STR_LIT>'.format(i[<NUM_LIT:1>][<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>range_str = '<STR_LIT>'.format(i[<NUM_LIT:1>][<NUM_LIT:0>], i[<NUM_LIT:1>][<NUM_LIT:1>])<EOL><DEDENT>ret += sre_to_string(i[<NUM_LIT:1>][<NUM_LIT:2>], paren=paren) + range_str<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.GROUPREF:<EOL><INDENT>ret += '<STR_LIT>'.format(i[<NUM_LIT:1>])<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.AT:<EOL><INDENT>if i[<NUM_LIT:1>] == sre_parse.AT_BEGINNING:<EOL><INDENT>ret += '<STR_LIT>'<EOL><DEDENT>elif i[<NUM_LIT:1>] == sre_parse.AT_END:<EOL><INDENT>ret += '<STR_LIT:$>'<EOL><DEDENT><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.NEGATE:<EOL><INDENT>pass<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.RANGE:<EOL><INDENT>ret += '<STR_LIT>'.format(unichr(i[<NUM_LIT:1>][<NUM_LIT:0>]), unichr(i[<NUM_LIT:1>][<NUM_LIT:1>]))<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ASSERT:<EOL><INDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>]:<EOL><INDENT>ret += '<STR_LIT>'.format(sre_to_string(i[<NUM_LIT:1>][<NUM_LIT:1>], paren=False))<EOL><DEDENT>else:<EOL><INDENT>ret += '<STR_LIT>'.format(sre_to_string(i[<NUM_LIT:1>][<NUM_LIT:1>], paren=paren))<EOL><DEDENT><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ASSERT_NOT:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>' % str(i))<EOL><DEDENT><DEDENT>return ret<EOL>

sre_parse object to string

    :param sre_obj: Output of sre_parse.parse()
    :type sre_obj: list
    :rtype: str


def _update_settings(self, new_settings, enforce_helpstring=True):

for raw_setting_name, value in six.iteritems(new_settings):<EOL><INDENT>setting_name = raw_setting_name.replace("<STR_LIT:_>", "<STR_LIT:->")<EOL>setting_already_exists = setting_name in self._instance_settings<EOL>value_is_list_len_2 = isinstance(value, list) and len(value) == <NUM_LIT:2><EOL>treat_as_tuple = not setting_already_exists and value_is_list_len_2<EOL>if isinstance(value, tuple) or treat_as_tuple:<EOL><INDENT>self._instance_settings[setting_name] = value<EOL><DEDENT>else:<EOL><INDENT>if setting_name not in self._instance_settings:<EOL><INDENT>if enforce_helpstring:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise InternalCashewException(msg % setting_name)<EOL><DEDENT>else:<EOL><INDENT>self._instance_settings[setting_name] = ('<STR_LIT>', value,)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>orig = self._instance_settings[setting_name]<EOL>self._instance_settings[setting_name] = (orig[<NUM_LIT:0>], value,)<EOL><DEDENT><DEDENT><DEDENT>

This method does the work of updating settings. Can be passed with
enforce_helpstring = False which you may want if allowing end users to
add arbitrary metadata via the settings system.

Preferable to use update_settings (without leading _) in code to do the
right thing and always have docstrings.


if isinstance(alias_or_aliases, string_types):<EOL><INDENT>return [alias_or_aliases]<EOL><DEDENT>else:<EOL><INDENT>return alias_or_aliases<EOL><DEDENT>

Make sure we don't attempt to iterate over an alias string thinking
it's an array.


attrs = self.setting_values()<EOL>attrs.update(self.__dict__)<EOL>skip = ["<STR_LIT>", "<STR_LIT>"]<EOL>for a in skip:<EOL><INDENT>del attrs[a]<EOL><DEDENT>return attrs<EOL>

Return a combined dictionary of setting values and attribute values.


if not skip:<EOL><INDENT>skip = []<EOL><DEDENT>return dict(<EOL>(k, v[<NUM_LIT:1>])<EOL>for k, v in six.iteritems(self._instance_settings)<EOL>if not k in skip)<EOL>

Returns dict of all setting values (removes the helpstrings).


docstring = inspect.getdoc(cls)<EOL>if not docstring:<EOL><INDENT>breadcrumbs = "<STR_LIT>".join(t.__name__ for t in inspect.getmro(cls)[:-<NUM_LIT:1>][::-<NUM_LIT:1>])<EOL>msg = "<STR_LIT>"<EOL>args = (cls.__name__, breadcrumbs, cls.__module__)<EOL>raise InternalCashewException(msg % args)<EOL><DEDENT>max_line_length = cls._class_settings.get('<STR_LIT>')<EOL>if max_line_length:<EOL><INDENT>for i, line in enumerate(docstring.splitlines()):<EOL><INDENT>if len(line) > max_line_length:<EOL><INDENT>msg = "<STR_LIT>" <EOL>args = (i, cls.__name__, len(line) - max_line_length)<EOL>raise Exception(msg % args)<EOL><DEDENT><DEDENT><DEDENT>return docstring<EOL>

Asserts that the class has a docstring, returning it if successful.


if stream is None:<EOL><INDENT>stream = self.std_out<EOL><DEDENT>

Block until given bytes appear in the stream.


return self._process.pid<EOL>


self._status_code = self._process.wait()<EOL>

Blocks until command finishes. Returns Response instance.


def run(command, data=None, timeout=None, kill_timeout=None, env=None, cwd=None):

command = expand_args(command)<EOL>history = []<EOL>for c in command:<EOL><INDENT>if len(history):<EOL><INDENT>data = history[-<NUM_LIT:1>].std_out[<NUM_LIT:0>:<NUM_LIT:10>*<NUM_LIT>]<EOL><DEDENT>cmd = Command(c)<EOL>try:<EOL><INDENT>out, err = cmd.run(data, timeout, kill_timeout, env, cwd)<EOL>status_code = cmd.returncode<EOL><DEDENT>except OSError as e:<EOL><INDENT>out, err = '<STR_LIT>', u"<STR_LIT:\n>".join([e.strerror, traceback.format_exc()])<EOL>status_code = <NUM_LIT><EOL><DEDENT>r = Response(process=cmd)<EOL>r.command = c<EOL>r.std_out = out<EOL>r.std_err = err<EOL>r.status_code = status_code<EOL>history.append(r)<EOL><DEDENT>r = history.pop()<EOL>r.history = history<EOL>return r<EOL>

Executes a given commmand and returns Response.

    Blocks until process is complete, or timeout is reached.


def send(self, str, end='<STR_LIT:\n>'):

return self._process.stdin.write(str+end)<EOL>


self.get_stack_data(frame, None, '<STR_LIT>')<EOL>

This function is called when we stop or break at this line.


def user_return(self, frame, return_value):

This function is called when a return trap is set here.


if isinstance(obj, primitive_types):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, sequence_types):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, array_types):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, key_value_types):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, types.ModuleType):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, (types.FunctionType, types.MethodType)):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, type):<EOL><INDENT>if hasattr(obj, '<STR_LIT>'):<EOL><INDENT>return ('<STR_LIT:class>', obj.__name__)<EOL><DEDENT><DEDENT>if isinstance(type(obj), type):<EOL><INDENT>if hasattr(obj, '<STR_LIT>'):<EOL><INDENT>cls_name = type(obj).__name__<EOL>if cls_name == '<STR_LIT>':<EOL><INDENT>cls_name = obj.__name__<EOL>return ('<STR_LIT:class>', '<STR_LIT:{}>'.format(cls_name))<EOL><DEDENT>if cls_name == '<STR_LIT>':<EOL><INDENT>cls_name = obj.__class__.__name__<EOL><DEDENT>return ('<STR_LIT>', '<STR_LIT>'.format(cls_name))<EOL><DEDENT><DEDENT>return ('<STR_LIT>', type(obj).__name__)<EOL>

Get type information for a Python object

    Args:
        obj: The Python object

    Returns:
        tuple: (object type "catagory", object type name)


old_stdout, sys.stdout = sys.stdout, new_stdout<EOL>try:<EOL><INDENT>yield None<EOL><DEDENT>finally:<EOL><INDENT>sys.stdout = old_stdout<EOL><DEDENT>

Redirect the stdout

    Args:
        new_stdout (io.StringIO): New stdout to use instead


ret = {}<EOL>for key, value in d.items():<EOL><INDENT>if key not in exclude:<EOL><INDENT>ret.update({key: value})<EOL><DEDENT><DEDENT>return ret<EOL>

Return a new dict with specified keys excluded from the origional dict

    Args:
        d (dict): origional dict
        exclude (list): The keys that are excluded


if os.path.split(program)[<NUM_LIT:0>]:<EOL><INDENT>program_path = find_exe(program)<EOL>if program_path:<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for path in get_path_list():<EOL><INDENT>program_path = find_exe(os.path.join(path, program))<EOL>if program_path:<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT><DEDENT>return None<EOL>

Identify the location of an executable file.


if sys.version_info[<NUM_LIT:0>] < <NUM_LIT:3>:<EOL><INDENT>text = text.encode('<STR_LIT:utf-8>')<EOL><DEDENT>print(text)<EOL>


@classmethod<EOL><INDENT>def _get_languages(cls) -> set:<DEDENT>

cls._start_server_if_needed()<EOL>url = urllib.parse.urljoin(cls._url, '<STR_LIT>')<EOL>languages = set()<EOL>for e in cls._get_root(url, num_tries=<NUM_LIT:1>):<EOL><INDENT>languages.add(e.get('<STR_LIT>'))<EOL>languages.add(e.get('<STR_LIT>'))<EOL><DEDENT>return languages<EOL>

Get supported languages (by querying the server).


def correct(self, text: str, srctext=None) -> str:

return correct(text, self.check(text, srctext))<EOL>

Automatically apply suggestions to the text.


self.disabled.update(self._spell_checking_rules)<EOL>


cls._start_server_if_needed()<EOL>params = {'<STR_LIT>': FAILSAFE_LANGUAGE, '<STR_LIT:text>': '<STR_LIT>'}<EOL>data = urllib.parse.urlencode(params).encode()<EOL>root = cls._get_root(cls._url, data, num_tries=<NUM_LIT:1>)<EOL>return root.attrib<EOL>


return locale.getlocale()[<NUM_LIT:0>] or locale.getdefaultlocale()[<NUM_LIT:0>]<EOL>

Get the language code for the current locale setting.


try:<EOL><INDENT>languages = cache['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>languages = LanguageTool._get_languages()<EOL>cache['<STR_LIT>'] = languages<EOL><DEDENT>return languages<EOL>


def _check_api(self, text: str, srctext=None) -> bytes:

root = self._get_root(self._url, self._encode(text, srctext))<EOL>return (b'<STR_LIT>' +<EOL>ElementTree.tostring(root) + b"<STR_LIT:\n>")<EOL>

Match text against enabled rules (result in XML format).


old_path = get_directory()<EOL>terminate_server()<EOL>cache.clear()<EOL>if path:<EOL><INDENT>cache['<STR_LIT>'] = path<EOL>try:<EOL><INDENT>get_jar_info()<EOL><DEDENT>except Error:<EOL><INDENT>cache['<STR_LIT>'] = old_path<EOL>raise<EOL><DEDENT><DEDENT>


def is_exe(path):<EOL><INDENT>return os.path.isfile(path) and os.access(path, os.X_OK)<EOL><DEDENT>def _get_path_list():<EOL><INDENT>return os.environ['<STR_LIT>'].split(os.pathsep)<EOL><DEDENT>if os.name == '<STR_LIT>':<EOL><INDENT>def find_exe(program):<EOL><INDENT>root, ext = os.path.splitext(program)<EOL>if ext:<EOL><INDENT>if is_exe(program):<EOL><INDENT>return program<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for ext in os.environ['<STR_LIT>'].split(os.pathsep):<EOL><INDENT>program_path = root + ext.lower()<EOL>if is_exe(program_path):<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT><DEDENT>return None<EOL><DEDENT>def get_path_list():<EOL><INDENT>paths = _get_path_list()<EOL>if win_allow_cross_arch:<EOL><INDENT>alt_sys_path = os.path.expandvars(r"<STR_LIT>")<EOL>if os.path.isdir(alt_sys_path):<EOL><INDENT>paths.insert(<NUM_LIT:0>, alt_sys_path)<EOL><DEDENT>else:<EOL><INDENT>alt_sys_path = os.path.expandvars(r"<STR_LIT>")<EOL>if os.path.isdir(alt_sys_path):<EOL><INDENT>paths.append(alt_sys_path)<EOL><DEDENT><DEDENT><DEDENT>return paths<EOL><DEDENT><DEDENT>else:<EOL><INDENT>def find_exe(program):<EOL><INDENT>return program if is_exe(program) else None<EOL><DEDENT>get_path_list = _get_path_list<EOL><DEDENT>if os.path.split(program)[<NUM_LIT:0>]:<EOL><INDENT>program_path = find_exe(program)<EOL>if program_path:<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for path in get_path_list():<EOL><INDENT>program_path = find_exe(os.path.join(path, program))<EOL>if program_path:<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT><DEDENT>return None<EOL>


if (any(arg.startswith('<STR_LIT>') for arg in sys.argv) and<EOL>os.path.isdir(PY2K_DIR) != IS_PY2K and os.path.isdir(LIB_DIR)):<EOL><INDENT>shutil.rmtree(LIB_DIR)<EOL><DEDENT>if IS_PY2K and any(arg.startswith(('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:test>'))<EOL>for arg in sys.argv):<EOL><INDENT>generate_py2k(config)<EOL>packages_root = get_cfg_value(config, '<STR_LIT>', '<STR_LIT>')<EOL>packages_root = os.path.join(PY2K_DIR, packages_root)<EOL>set_cfg_value(config, '<STR_LIT>', '<STR_LIT>', packages_root)<EOL><DEDENT>


items = [v.strip() for v in value.split('<STR_LIT:U+002C>')]<EOL>if len(items) == <NUM_LIT:1>:<EOL><INDENT>items = value.split()<EOL><DEDENT>return items<EOL>

Split a string with comma or space-separated elements into a list.


args = BASE_ARGS_3TO2 if args is None else BASE_ARGS_3TO2 + args<EOL>try:<EOL><INDENT>proc = subprocess.Popen(['<STR_LIT>'] + args, stderr=subprocess.PIPE)<EOL><DEDENT>except OSError:<EOL><INDENT>for path in glob.glob('<STR_LIT>'):<EOL><INDENT>if os.path.isdir(path) and path not in sys.path:<EOL><INDENT>sys.path.append(path)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>from lib3to2.main import main as lib3to2_main<EOL><DEDENT>except ImportError:<EOL><INDENT>raise OSError('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>if lib3to2_main('<STR_LIT>', args):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>num_errors = <NUM_LIT:0><EOL>while proc.poll() is None:<EOL><INDENT>line = proc.stderr.readline()<EOL>sys.stderr.write(line)<EOL>num_errors += line.count('<STR_LIT>')<EOL><DEDENT>if proc.returncode or num_errors:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT>


return [element for element in (line.strip() for line in value.split('<STR_LIT:\n>'))<EOL>if element]<EOL>

Split a multiline string into a list, excluding blank lines.


if not isinstance(file_list, list):<EOL><INDENT>file_list = [file_list]<EOL><DEDENT>python_re = re.compile(br"<STR_LIT>")<EOL>coding_re = re.compile(br"<STR_LIT>")<EOL>new_line_re = re.compile(br"<STR_LIT>")<EOL>version_3 = LooseVersion('<STR_LIT:3>')<EOL>for file in file_list:<EOL><INDENT>if not os.path.getsize(file):<EOL><INDENT>continue<EOL><DEDENT>rewrite_needed = False<EOL>python_found = False<EOL>coding_found = False<EOL>lines = []<EOL>f = open(file, '<STR_LIT:rb>')<EOL>try:<EOL><INDENT>while len(lines) < <NUM_LIT:2>:<EOL><INDENT>line = f.readline()<EOL>match = python_re.match(line)<EOL>if match:<EOL><INDENT>python_found = True<EOL>version = LooseVersion(match.group(<NUM_LIT:2>).decode() or '<STR_LIT:2>')<EOL>try:<EOL><INDENT>version_test = version >= version_3<EOL><DEDENT>except TypeError:<EOL><INDENT>version_test = True<EOL><DEDENT>if version_test:<EOL><INDENT>line = python_re.sub(br"<STR_LIT>", line)<EOL>rewrite_needed = True<EOL><DEDENT><DEDENT>elif coding_re.search(line):<EOL><INDENT>coding_found = True<EOL><DEDENT>lines.append(line)<EOL><DEDENT>if not coding_found:<EOL><INDENT>match = new_line_re.search(lines[<NUM_LIT:0>])<EOL>newline = match.group(<NUM_LIT:1>) if match else b"<STR_LIT:\n>"<EOL>line = b"<STR_LIT>" + newline<EOL>lines.insert(<NUM_LIT:1> if python_found else <NUM_LIT:0>, line)<EOL>rewrite_needed = True<EOL><DEDENT>if rewrite_needed:<EOL><INDENT>lines += f.readlines()<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT>if rewrite_needed:<EOL><INDENT>f = open(file, '<STR_LIT:wb>')<EOL>try:<EOL><INDENT>f.writelines(lines)<EOL><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT><DEDENT><DEDENT>

Write Python 2 shebang and add encoding cookie if needed.


try:<EOL><INDENT>from urllib.request import urlopen<EOL><DEDENT>except ImportError:<EOL><INDENT>from urllib2 import urlopen<EOL><DEDENT>src = dst = None<EOL>try:<EOL><INDENT>src = urlopen(url)<EOL>data = src.read()<EOL>dst = open(target, "<STR_LIT:wb>")<EOL>dst.write(data)<EOL><DEDENT>finally:<EOL><INDENT>if src:<EOL><INDENT>src.close()<EOL><DEDENT>if dst:<EOL><INDENT>dst.close()<EOL><DEDENT><DEDENT>

Use Python to download the file, even though it cannot authenticate the
connection.


target = os.path.abspath(target)<EOL>cmd = [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>"<STR_LIT>" % vars(),<EOL>]<EOL>_clean_check(cmd, target)<EOL>

Download the file at url to target using Powershell (which will validate
trust). Raise an exception if the command cannot complete.


options = _parse_args()<EOL>archive = download_setuptools(<EOL>version=options.version,<EOL>download_base=options.download_base,<EOL>downloader_factory=options.downloader_factory,<EOL>)<EOL>return _install(archive, _build_install_args(options))<EOL>

Install or upgrade setuptools and EasyInstall


parser = optparse.OptionParser()<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', metavar="<STR_LIT>",<EOL>default=DEFAULT_URL,<EOL>help='<STR_LIT>')<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', action='<STR_LIT>',<EOL>const=lambda: download_file_insecure, default=get_best_downloader,<EOL>help='<STR_LIT>'<EOL>)<EOL>parser.add_option(<EOL>'<STR_LIT>', help="<STR_LIT>",<EOL>default=DEFAULT_VERSION,<EOL>)<EOL>options, args = parser.parse_args()<EOL>return options<EOL>


return ['<STR_LIT>'] if options.user_install else []<EOL>

Build the arguments to 'python setup.py install' on the setuptools package


if hasattr(timezone, '<STR_LIT>') and value not in (datetime.datetime.min, datetime.datetime.max):<EOL><INDENT>return timezone.localize(value, is_dst=None)<EOL><DEDENT>else:<EOL><INDENT>return value.replace(tzinfo=timezone)<EOL><DEDENT>

Makes a naive datetime.datetime in a given time zone aware.


query = self.to_timezone(query)<EOL>return any(self.intervals(range_start=query, range_end=query))<EOL>

Does this schedule include the provided time?
        query should be a datetime (naive or timezone-aware)


def intervals(self, range_start=datetime.datetime.min, range_end=datetime.datetime.max):

<EOL>current_period = None<EOL>max_continuous_days = <NUM_LIT><EOL>range_start = self.to_timezone(range_start)<EOL>range_end = self.to_timezone(range_end)<EOL>for period in self._daily_periods(range_start.date(), range_end.date()):<EOL><INDENT>if period.end < range_start or period.start > range_end:<EOL><INDENT>continue<EOL><DEDENT>if current_period is None:<EOL><INDENT>current_period = period<EOL><DEDENT>else:<EOL><INDENT>if ( ((period.start < current_period.end)<EOL>or (period.start - current_period.end) <= datetime.timedelta(minutes=<NUM_LIT:1>))<EOL>and (current_period.end - current_period.start) < datetime.timedelta(days=max_continuous_days)):<EOL><INDENT>current_period = Period(current_period.start, period.end)<EOL><DEDENT>else:<EOL><INDENT>yield current_period<EOL>current_period = period<EOL><DEDENT><DEDENT><DEDENT>if current_period:<EOL><INDENT>yield current_period<EOL><DEDENT>

Returns an iterator of Period tuples for continuous stretches of time during
        which this event is in effect, between range_start and range_end.


def includes(self, query_date, query_time=None):

if self.start_date and query_date < self.start_date:<EOL><INDENT>return False<EOL><DEDENT>if self.end_date and query_date > self.end_date:<EOL><INDENT>return False<EOL><DEDENT>if query_date.weekday() not in self.weekdays:<EOL><INDENT>return False<EOL><DEDENT>if not query_time:<EOL><INDENT>return True<EOL><DEDENT>if query_time >= self.period.start and query_time <= self.period.end:<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

Does this schedule include the provided time?
        query_date and query_time are date and time objects, interpreted
        in this schedule's timezone


if after is None:<EOL><INDENT>after = timezone.now()<EOL><DEDENT>after = self.to_timezone(after)<EOL>return next(self.intervals(range_start=after), None)<EOL>

Returns the next Period this event is in effect, or None if the event
        has no remaining periods.


ex = {}<EOL>for sd in self.root.xpath('<STR_LIT>'):<EOL><INDENT>bits = str(sd.text).split('<STR_LIT:U+0020>')<EOL>date = text_to_date(bits.pop(<NUM_LIT:0>))<EOL>ex.setdefault(date, []).extend([<EOL>_time_text_to_period(t)<EOL>for t in bits<EOL>])<EOL><DEDENT>return ex<EOL>

A dict of dates -> [Period time tuples] representing exceptions
        to the base recurrence pattern.


assert root.tag == '<STR_LIT>'<EOL>if root.xpath('<STR_LIT>'):<EOL><INDENT>return _ScheduleIntervals(root, timezone)<EOL><DEDENT>elif root.xpath('<STR_LIT>'):<EOL><INDENT>return _ScheduleRecurring(root, timezone)<EOL><DEDENT>raise NotImplementedError<EOL>

Return a Schedule object based on an lxml Element for the <schedule>
        tag. timezone is a tzinfo object, ideally from pytz.


if timezone.is_aware(dt):<EOL><INDENT>return dt.astimezone(self.timezone)<EOL><DEDENT>else:<EOL><INDENT>return timezone.make_aware(dt, self.timezone)<EOL><DEDENT>

Converts a datetime to the timezone of this Schedule.


start_time = self.root.findtext('<STR_LIT>')<EOL>if start_time:<EOL><INDENT>return Period(text_to_time(start_time), text_to_time(self.root.findtext('<STR_LIT>')))<EOL><DEDENT>return Period(datetime.time(<NUM_LIT:0>, <NUM_LIT:0>), datetime.time(<NUM_LIT>, <NUM_LIT>))<EOL>

A Period tuple representing the daily start and end time.


def _daily_periods(self, range_start, range_end):

specific = set(self.exceptions.keys())<EOL>return heapq.merge(self.exception_periods(range_start, range_end), *[<EOL>sched.daily_periods(range_start=range_start, range_end=range_end, exclude_dates=specific)<EOL>for sched in self._recurring_schedules<EOL>])<EOL>

Returns an iterator of Period tuples for every day this event is in effect, between range_start
        and range_end.


def _tmdd_datetime_to_iso(dt, include_offset=True, include_seconds=True):

datestring = dt.findtext('<STR_LIT:date>')<EOL>timestring = dt.findtext('<STR_LIT:time>')<EOL>assert len(datestring) == <NUM_LIT:8><EOL>assert len(timestring) >= <NUM_LIT:6><EOL>iso = datestring[<NUM_LIT:0>:<NUM_LIT:4>] + '<STR_LIT:->' + datestring[<NUM_LIT:4>:<NUM_LIT:6>] + '<STR_LIT:->' + datestring[<NUM_LIT:6>:<NUM_LIT:8>] + '<STR_LIT:T>'+ timestring[<NUM_LIT:0>:<NUM_LIT:2>] + '<STR_LIT::>' + timestring[<NUM_LIT:2>:<NUM_LIT:4>]<EOL>if include_seconds:<EOL><INDENT>iso += '<STR_LIT::>' + timestring[<NUM_LIT:4>:<NUM_LIT:6>]<EOL><DEDENT>if include_offset:<EOL><INDENT>offset = dt.findtext('<STR_LIT>')<EOL>if offset:<EOL><INDENT>assert len(offset) == <NUM_LIT:5><EOL>iso += offset[<NUM_LIT:0>:<NUM_LIT:3>] + '<STR_LIT::>' + offset[<NUM_LIT:3>:<NUM_LIT:5>]<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>" % etree.tostring(dt))<EOL><DEDENT><DEDENT>return iso<EOL>

dt is an xml Element with <date>, <time>, and optionally <offset> children.
returns an ISO8601 string


severities = c.feu.xpath('<STR_LIT>')<EOL>impacts = c.feu.xpath('<STR_LIT>')<EOL>severities = [convert_severity[s] for s in severities]<EOL>impacts = [convert_impact[i] for i in impacts]<EOL>return ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'][max(itertools.chain(severities, impacts))]<EOL>

1. Collect all <severity> and <impact-level> values.
2. Convert impact-level of 1-3 to MINOR, 4-7 to MODERATE, 8-10 to MAJOR
3. Map severity -> none to MINOR, natural-disaster to MAJOR, other to UNKNOWN
4. Pick the highest severity.


assert format in ('<STR_LIT>', '<STR_LIT>')<EOL>if getattr(doc, '<STR_LIT>', None) == '<STR_LIT>':<EOL><INDENT>if format == '<STR_LIT>':<EOL><INDENT>return xml_to_json(doc)<EOL><DEDENT><DEDENT>elif isinstance(doc, dict) and '<STR_LIT>' in doc:<EOL><INDENT>if format == '<STR_LIT>':<EOL><INDENT>return json_doc_to_xml(doc)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return doc<EOL>

Ensures that the provided document is an lxml Element or json dict.


j = {}<EOL>if len(root) == <NUM_LIT:0>:  <EOL><INDENT>return _maybe_intify(root.text)<EOL><DEDENT>if len(root) == <NUM_LIT:1> and root[<NUM_LIT:0>].tag.startswith('<STR_LIT:{>' + NS_GML):  <EOL><INDENT>return gml_to_geojson(root[<NUM_LIT:0>])<EOL><DEDENT>if root.tag == '<STR_LIT>':<EOL><INDENT>j['<STR_LIT>'] = {'<STR_LIT:version>': root.get('<STR_LIT:version>')}<EOL><DEDENT>for elem in root:<EOL><INDENT>name = elem.tag<EOL>if name == '<STR_LIT>' and elem.get('<STR_LIT>'):<EOL><INDENT>name = elem.get('<STR_LIT>') + '<STR_LIT>'<EOL>if name == '<STR_LIT>':<EOL><INDENT>name = '<STR_LIT:url>'<EOL><DEDENT>if root.tag == '<STR_LIT>':<EOL><INDENT>j['<STR_LIT>'][name] = elem.get('<STR_LIT>')<EOL>continue<EOL><DEDENT><DEDENT>elif name.startswith('<STR_LIT:{>' + NS_PROTECTED):<EOL><INDENT>name = '<STR_LIT:!>' + name[name.index('<STR_LIT:}>') + <NUM_LIT:1>:] <EOL><DEDENT>elif name[<NUM_LIT:0>] == '<STR_LIT:{>':<EOL><INDENT>name = '<STR_LIT:+>' + name[name.index('<STR_LIT:}>') + <NUM_LIT:1>:]<EOL><DEDENT>if name in j:<EOL><INDENT>continue  <EOL><DEDENT>elif elem.tag == '<STR_LIT>' and not elem.text:<EOL><INDENT>j[name] = elem.get('<STR_LIT>')<EOL><DEDENT>elif len(elem):<EOL><INDENT>if name == '<STR_LIT>':<EOL><INDENT>j[name] = [xml_link_to_json(child, to_dict=False) for child in elem]<EOL><DEDENT>elif name in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>j[name] = [xml_link_to_json(child, to_dict=True) for child in elem]<EOL><DEDENT>elif all((name == pluralize(child.tag) for child in elem)):<EOL><INDENT>j[name] = [xml_to_json(child) for child in elem]<EOL><DEDENT>else:<EOL><INDENT>j[name] = xml_to_json(elem)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if root.tag == '<STR_LIT>' and name.endswith('<STR_LIT:s>') and not elem.text:<EOL><INDENT>j[name] = []<EOL><DEDENT>else:<EOL><INDENT>j[name] = _maybe_intify(elem.text)<EOL><DEDENT><DEDENT><DEDENT>return j<EOL>

Convert an Open511 XML document or document fragment to JSON.

    Takes an lxml Element object. Returns a dict ready to be JSON-serialized.


tag = el.tag.replace('<STR_LIT>' % NS_GML, '<STR_LIT>')<EOL>if tag == '<STR_LIT>':<EOL><INDENT>coordinates = [float(c) for c in el.findtext('<STR_LIT>' % NS_GML).split('<STR_LIT:U+002C>')]<EOL><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>coordinates = [<EOL>[float(x) for x in pair.split('<STR_LIT:U+002C>')]<EOL>for pair in el.findtext('<STR_LIT>' % NS_GML).split('<STR_LIT:U+0020>')<EOL>]<EOL><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>coordinates = []<EOL>for ring in el.xpath('<STR_LIT>', namespaces=NSMAP)+ el.xpath('<STR_LIT>', namespaces=NSMAP):<EOL><INDENT>coordinates.append([<EOL>[float(x) for x in pair.split('<STR_LIT:U+002C>')]<EOL>for pair in ring.text.split('<STR_LIT:U+0020>')<EOL>])<EOL><DEDENT><DEDENT>elif tag in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if tag == '<STR_LIT>':<EOL><INDENT>single_type = '<STR_LIT>'<EOL>member_tag = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>single_type = tag[<NUM_LIT:5>:]<EOL>member_tag = single_type[<NUM_LIT:0>].lower() + single_type[<NUM_LIT:1>:] + '<STR_LIT>'<EOL><DEDENT>coordinates = [<EOL>gml_to_geojson(member)['<STR_LIT>']<EOL>for member in el.xpath('<STR_LIT>' % (member_tag, single_type), namespaces=NSMAP)<EOL>]<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError<EOL><DEDENT>return {<EOL>'<STR_LIT:type>': tag,<EOL>'<STR_LIT>': coordinates<EOL>}<EOL>

Translates a deprecated GML 2.0 geometry to GeoJSON


if geom.srs.srid != <NUM_LIT>:<EOL><INDENT>raise NotImplementedError("<STR_LIT>")<EOL><DEDENT>return geojson_to_gml(json.loads(geom.geojson))<EOL>

Transform a GEOS or OGR geometry object into an lxml Element
    for the GML geometry.


def json_struct_to_xml(json_obj, root, custom_namespace=None):

if isinstance(root, (str, unicode)):<EOL><INDENT>if root.startswith('<STR_LIT:!>'):<EOL><INDENT>root = etree.Element('<STR_LIT>' % (NS_PROTECTED, root[<NUM_LIT:1>:]))<EOL><DEDENT>elif root.startswith('<STR_LIT:+>'):<EOL><INDENT>if not custom_namespace:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>root = etree.Element('<STR_LIT>' % (custom_namespace, root[<NUM_LIT:1>:]))<EOL><DEDENT>else:<EOL><INDENT>root = etree.Element(root)<EOL><DEDENT><DEDENT>if root.tag in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>for link in json_obj:<EOL><INDENT>root.append(json_link_to_xml(link))<EOL><DEDENT><DEDENT>elif isinstance(json_obj, (str, unicode)):<EOL><INDENT>root.text = json_obj<EOL><DEDENT>elif isinstance(json_obj, (int, float)):<EOL><INDENT>root.text = unicode(json_obj)<EOL><DEDENT>elif isinstance(json_obj, dict):<EOL><INDENT>if frozenset(json_obj.keys()) == frozenset(('<STR_LIT:type>', '<STR_LIT>')):<EOL><INDENT>root.append(geojson_to_gml(json_obj))<EOL><DEDENT>else:<EOL><INDENT>for key, val in json_obj.items():<EOL><INDENT>if key == '<STR_LIT:url>' or key.endswith('<STR_LIT>'):<EOL><INDENT>el = json_link_to_xml(val, json_link_key_to_xml_rel(key))<EOL><DEDENT>else:<EOL><INDENT>el = json_struct_to_xml(val, key, custom_namespace=custom_namespace)<EOL><DEDENT>if el is not None:<EOL><INDENT>root.append(el)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>elif isinstance(json_obj, list):<EOL><INDENT>tag_name = root.tag<EOL>if tag_name.endswith('<STR_LIT>'):<EOL><INDENT>tag_name = tag_name[:-<NUM_LIT:3>] + '<STR_LIT:y>'<EOL><DEDENT>elif tag_name.endswith('<STR_LIT:s>'):<EOL><INDENT>tag_name = tag_name[:-<NUM_LIT:1>]<EOL><DEDENT>for val in json_obj:<EOL><INDENT>el = json_struct_to_xml(val, tag_name, custom_namespace=custom_namespace)<EOL>if el is not None:<EOL><INDENT>root.append(el)<EOL><DEDENT><DEDENT><DEDENT>elif json_obj is None:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError<EOL><DEDENT>return root<EOL>

Converts a Open511 JSON fragment to XML.

    Takes a dict deserialized from JSON, returns an lxml Element.

    This won't provide a conforming document if you pass in a full JSON document;
    it's for translating little fragments, and is mostly used internally.


def shuffle(self, count=None, utc=None):

url = PATHS['<STR_LIT>'] % self.profile_id<EOL>post_data = '<STR_LIT>'<EOL>if count:<EOL><INDENT>post_data += '<STR_LIT>' % count<EOL><DEDENT>if utc:<EOL><INDENT>post_data += '<STR_LIT>' % utc<EOL><DEDENT>return self.api.post(url=url, data=post_data)<EOL>

Randomize the order at which statuses for the specified social media
profile will be sent out of the buffer.


pending_updates = []<EOL>url = PATHS['<STR_LIT>'] % self.profile_id<EOL>response = self.api.get(url=url)<EOL>for update in response['<STR_LIT>']:<EOL><INDENT>pending_updates.append(Update(api=self.api, raw_response=update))<EOL><DEDENT>self.__pending = pending_updates<EOL>return self.__pending<EOL>

Returns an array of updates that are currently in the buffer for an
individual social media profile.


interactions = []<EOL>url = PATHS['<STR_LIT>'] % self.id<EOL>response = self.api.get(url=url)<EOL>for interaction in response['<STR_LIT>']:<EOL><INDENT>interactions.append(ResponseObject(interaction))<EOL><DEDENT>self.__interactions = interactions<EOL>return self.__interactions<EOL>

Returns the detailed information on individual interactions with the social
media update such as favorites, retweets and likes.


url = PATHS['<STR_LIT>'] % self.id<EOL>data_format = "<STR_LIT>"<EOL>post_data = "<STR_LIT>"<EOL>for format_type, values in schedules.iteritems():<EOL><INDENT>for value in values:<EOL><INDENT>post_data += data_format % (format_type, value)<EOL><DEDENT><DEDENT>self.api.post(url=url, data=post_data)<EOL>

Set the posting schedules for the specified social media profile.


url = PATHS['<STR_LIT>'] % self.id<EOL>self.__schedules = self.api.get(url=url)<EOL>return self.__schedules<EOL>

Returns details of the posting schedules associated with a social media
profile.


if hasattr(self, "<STR_LIT>"):<EOL><INDENT>return None<EOL><DEDENT>self.app_label = self.kwargs.get('<STR_LIT>')<EOL>self.document_name = self.kwargs.get('<STR_LIT>')<EOL>self.models_name = self.kwargs.get('<STR_LIT>', '<STR_LIT>')<EOL>self.model_name = "<STR_LIT>".format(self.app_label, self.models_name)<EOL>self.models = import_module(self.model_name)<EOL>

Sets a number of commonly used attributes


def set_embedded_doc(self, document, form_key, current_key, remaining_key):

embedded_doc = getattr(document, current_key, False)<EOL>if not embedded_doc:<EOL><INDENT>embedded_doc = document._fields[current_key].document_type_obj()<EOL><DEDENT>new_key, new_remaining_key_array = trim_field_key(embedded_doc, remaining_key)<EOL>self.process_document(embedded_doc, form_key, make_key(new_key, new_remaining_key_array))<EOL>setattr(document, current_key, embedded_doc)<EOL>

Get the existing embedded document if it exists, else created it.


if hasattr(self, "<STR_LIT>"):<EOL><INDENT>return None<EOL><DEDENT>if not hasattr(self, "<STR_LIT>"):<EOL><INDENT>self.set_mongonaut_base()<EOL><DEDENT>for mongoadmin in self.get_mongoadmins():<EOL><INDENT>for model in mongoadmin['<STR_LIT>'].models:<EOL><INDENT>if model.name == self.document_name:<EOL><INDENT>self.mongoadmin = model.mongoadmin<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not hasattr(self, "<STR_LIT>"):<EOL><INDENT>raise NoMongoAdminSpecified("<STR_LIT>".format(self.app_label, self.document_name))<EOL><DEDENT>

Returns the MongoAdmin object for an app_label/document_name style view


apps = []<EOL>for app_name in settings.INSTALLED_APPS:<EOL><INDENT>mongoadmin = "<STR_LIT>".format(app_name)<EOL>try:<EOL><INDENT>module = import_module(mongoadmin)<EOL><DEDENT>except ImportError as e:<EOL><INDENT>if str(e).startswith("<STR_LIT>"):<EOL><INDENT>continue<EOL><DEDENT>raise e<EOL><DEDENT>app_store = AppStore(module)<EOL>apps.append(dict(<EOL>app_name=app_name,<EOL>obj=app_store<EOL>))<EOL><DEDENT>return apps<EOL>

Returns a list of all mongoadmin implementations for the site


def post(self, request, *args, **kwargs):

<EOL>form_class = self.get_form_class()<EOL>form = self.get_form(form_class)<EOL>mongo_ids = self.get_initial()['<STR_LIT>']<EOL>for form_mongo_id in form.data.getlist('<STR_LIT>'):<EOL><INDENT>for mongo_id in mongo_ids:<EOL><INDENT>if form_mongo_id == mongo_id:<EOL><INDENT>self.document.objects.get(pk=mongo_id).delete()<EOL><DEDENT><DEDENT><DEDENT>return self.form_invalid(form)<EOL>


if hasattr(self, "<STR_LIT>") and self.queryset:<EOL><INDENT>return self.queryset<EOL><DEDENT>self.set_mongonaut_base()<EOL>self.set_mongoadmin()<EOL>self.document = getattr(self.models, self.document_name)<EOL>queryset = self.document.objects.all()<EOL>if self.mongoadmin.ordering:<EOL><INDENT>queryset = queryset.order_by(*self.mongoadmin.ordering)<EOL><DEDENT>q = self.request.GET.get('<STR_LIT:q>')<EOL>queryset = self.get_qset(queryset, q)<EOL>try:<EOL><INDENT>self.page = int(self.request.GET.get('<STR_LIT>', '<STR_LIT:1>'))<EOL><DEDENT>except ValueError:<EOL><INDENT>self.page = <NUM_LIT:1><EOL><DEDENT>obj_count = queryset.count()<EOL>self.total_pages = math.ceil(obj_count / self.documents_per_page)<EOL>if self.page > self.total_pages:<EOL><INDENT>self.page = self.total_pages<EOL><DEDENT>if self.page < <NUM_LIT:1>:<EOL><INDENT>self.page = <NUM_LIT:1><EOL><DEDENT>start = (self.page - <NUM_LIT:1>) * self.documents_per_page<EOL>end = self.page * self.documents_per_page<EOL>queryset = queryset[start:end] if obj_count else queryset<EOL>self.queryset = queryset<EOL>return queryset<EOL>

Replicates Django CBV `get_queryset()` method, but for MongoEngine.


context = super(DocumentAddFormView, self).get_context_data(**kwargs)<EOL>self.set_mongoadmin()<EOL>context = self.set_permissions_in_context(context)<EOL>self.document_type = getattr(self.models, self.document_name)<EOL>context['<STR_LIT>'] = self.app_label<EOL>context['<STR_LIT>'] = self.document_name<EOL>context['<STR_LIT>'] = reverse('<STR_LIT>', args=[self.kwargs.get('<STR_LIT>'),<EOL>self.kwargs.get('<STR_LIT>')])<EOL>return context<EOL>

TODO - possibly inherit this from DocumentEditFormView. This is same thing minus:
            self.ident = self.kwargs.get('id')
            self.document = self.document_type.objects.get(pk=self.ident)


if self.mongoadmin.search_fields and q:<EOL><INDENT>params = {}<EOL>for field in self.mongoadmin.search_fields:<EOL><INDENT>if field == '<STR_LIT:id>':<EOL><INDENT>if is_valid_object_id(q):<EOL><INDENT>return queryset.filter(pk=q)<EOL><DEDENT>continue<EOL><DEDENT>search_key = "<STR_LIT>".format(field=field)<EOL>params[search_key] = q<EOL><DEDENT>queryset = queryset.filter(**params)<EOL><DEDENT>return queryset<EOL>

Performs filtering against the default queryset returned by
            mongoengine.


return request.user.is_authenticated and request.user.is_active and request.user.is_staff<EOL>


return request.user.is_authenticated and request.user.is_active<EOL>

Returns True if the given HttpRequest has permission to view
*at least one* page in the mongonaut site.


sep = kwargs.get('<STR_LIT>', u"<STR_LIT:_>")<EOL>exclude_last_string = kwargs.get('<STR_LIT>', False)<EOL>string_array = []<EOL>for arg in args:<EOL><INDENT>if isinstance(arg, list):<EOL><INDENT>string_array.append(six.text_type(sep.join(arg)))<EOL><DEDENT>else:<EOL><INDENT>if exclude_last_string:<EOL><INDENT>new_key_array = arg.split(sep)[:-<NUM_LIT:1>]<EOL>if len(new_key_array) > <NUM_LIT:0>:<EOL><INDENT>string_array.append(make_key(new_key_array))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>string_array.append(six.text_type(arg))<EOL><DEDENT><DEDENT><DEDENT>return sep.join(string_array)<EOL>

Given any number of lists and strings will join them in order as one
string separated by the sep kwarg.  sep defaults to u"_".

Add exclude_last_string=True as a kwarg to exclude the last item in a
given string after being split by sep.  Note if you only have one word
in your string you can end up getting an empty string.

Example uses:

>>> from mongonaut.forms.form_utils import make_key
>>> make_key('hi', 'my', 'firend')
>>> u'hi_my_firend'

>>> make_key('hi', 'my', 'firend', sep='i')
>>> 'hiimyifirend'

>>> make_key('hi', 'my', 'firend',['this', 'be', 'what'], sep='i')
>>> 'hiimyifirendithisibeiwhat'

>>> make_key('hi', 'my', 'firend',['this', 'be', 'what'])
>>> u'hi_my_firend_this_be_what'


def create_list_dict(self, document, list_field, doc_key):

list_dict = {"<STR_LIT>": document}<EOL>if isinstance(list_field.field, EmbeddedDocumentField):<EOL><INDENT>list_dict.update(self.create_document_dictionary(document=list_field.field.document_type_obj,<EOL>owner_document=document))<EOL><DEDENT>list_dict.update({"<STR_LIT>": list_field.field,<EOL>"<STR_LIT>": doc_key,<EOL>"<STR_LIT>": ListField,<EOL>"<STR_LIT>": get_widget(list_field.field),<EOL>"<STR_LIT>": getattr(document, doc_key, None)})<EOL>return list_dict<EOL>

Genereates a dictionary representation of the list field. Document
should be the document the list_field comes from.

DO NOT CALL DIRECTLY


def create_document_dictionary(self, document, document_key=None,<EOL>owner_document=None):

doc_dict = self.create_doc_dict(document, document_key, owner_document)<EOL>for doc_key, doc_field in doc_dict.items():<EOL><INDENT>if doc_key.startswith("<STR_LIT:_>"):<EOL><INDENT>continue<EOL><DEDENT>if isinstance(doc_field, ListField):<EOL><INDENT>doc_dict[doc_key] = self.create_list_dict(document, doc_field, doc_key)<EOL><DEDENT>elif isinstance(doc_field, EmbeddedDocumentField):<EOL><INDENT>doc_dict[doc_key] = self.create_document_dictionary(doc_dict[doc_key].document_type_obj,<EOL>doc_key)<EOL><DEDENT>else:<EOL><INDENT>doc_dict[doc_key] = {"<STR_LIT>": document,<EOL>"<STR_LIT>": doc_key,<EOL>"<STR_LIT>": doc_field,<EOL>"<STR_LIT>": get_widget(doc_dict[doc_key], getattr(doc_field, '<STR_LIT>', False))}<EOL><DEDENT><DEDENT>return doc_dict<EOL>

Given document generates a dictionary representation of the document.
Includes the widget for each for each field in the document.


try:<EOL><INDENT>return document.__unicode__()<EOL><DEDENT>except AttributeError:<EOL><INDENT>return six.text_type(document)<EOL><DEDENT>

Safely converts MongoDB document strings to unicode.


def set_form_fields(self, form_field_dict, parent_key=None, field_type=None):

for form_key, field_value in form_field_dict.items():<EOL><INDENT>form_key = make_key(parent_key, form_key) if parent_key is not None else form_key<EOL>if isinstance(field_value, tuple):<EOL><INDENT>set_list_class = False<EOL>base_key = form_key<EOL>if ListField in (field_value.field_type, field_type):<EOL><INDENT>if parent_key is None or ListField == field_value.field_type:<EOL><INDENT>if field_type != EmbeddedDocumentField:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'.format(form_key)<EOL><DEDENT>set_list_class = True<EOL><DEDENT>else:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'<EOL><DEDENT>list_keys = [field_key for field_key in self.form.fields.keys()<EOL>if has_digit(field_key)]<EOL>key_int = <NUM_LIT:0><EOL>while form_key in list_keys:<EOL><INDENT>key_int += <NUM_LIT:1><EOL><DEDENT>form_key = make_key(form_key, key_int)<EOL><DEDENT>if parent_key is not None:<EOL><INDENT>valid_base_keys = [model_key for model_key in self.model_map_dict.keys()<EOL>if not model_key.startswith("<STR_LIT:_>")]<EOL>while base_key not in valid_base_keys and base_key:<EOL><INDENT>base_key = make_key(base_key, exclude_last_string=True)<EOL><DEDENT>embedded_key_class = None<EOL>if set_list_class:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += "<STR_LIT>".format(base_key)<EOL>embedded_key_class = make_key(field_key, exclude_last_string=True)<EOL><DEDENT>field_value.widget.attrs['<STR_LIT:class>'] += "<STR_LIT>"<EOL>if base_key == parent_key:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'.format(base_key)<EOL><DEDENT>else:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'.format(base_key, parent_key)<EOL><DEDENT>if embedded_key_class is not None:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'.format(embedded_key_class)<EOL><DEDENT><DEDENT>default_value = self.get_field_value(form_key)<EOL>if isinstance(default_value, list) and len(default_value) > <NUM_LIT:0>:<EOL><INDENT>key_index = int(form_key.split("<STR_LIT:_>")[-<NUM_LIT:1>])<EOL>new_base_key = make_key(form_key, exclude_last_string=True)<EOL>for list_value in default_value:<EOL><INDENT>list_widget = deepcopy(field_value.widget)<EOL>new_key = make_key(new_base_key, six.text_type(key_index))<EOL>list_widget.attrs['<STR_LIT:class>'] += "<STR_LIT>".format(make_key(base_key, key_index))<EOL>self.set_form_field(list_widget, field_value.document_field, new_key, list_value)<EOL>key_index += <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.set_form_field(field_value.widget, field_value.document_field,<EOL>form_key, default_value)<EOL><DEDENT><DEDENT>elif isinstance(field_value, dict):<EOL><INDENT>self.set_form_fields(field_value, form_key, field_value.get("<STR_LIT>", None))<EOL><DEDENT><DEDENT>

Set the form fields for every key in the form_field_dict.

Params:
  form_field_dict -- a dictionary created by get_form_field_dict
  parent_key -- the key for the previous key in the recursive call
  field_type -- used to determine what kind of field we are setting


try:<EOL><INDENT>OBJECT_ID.validate(value)<EOL>return True<EOL><DEDENT>except ValidationError:<EOL><INDENT>return False<EOL><DEDENT>

Validates BSON IDs using mongoengine's ObjectIdField field.


return {'<STR_LIT:size>': self._size, '<STR_LIT>': self._hits, '<STR_LIT>': self._misses}<EOL>

>>> c = LRUCache()
>>> sorted(c.stats().keys())
['hits', 'misses', 'size']


(_value, mem) = LRUCache.get(self, key)<EOL>self._mem -= mem<EOL>LRUCache.delete(self, key)<EOL>


mem = sys.getsizeof(value)<EOL>if self._mem + mem > self._maxmem:<EOL><INDENT>self.delete(self.last())<EOL><DEDENT>LRUCache.put(self, key, (value, mem))<EOL>self._mem += mem<EOL>

>>> c = MemSizeLRUCache(maxmem=24*4)
>>> c.put(1, 1)
>>> c.mem() # 24-bytes per integer
24
>>> c.put(2, 2)
>>> c.put(3, 3)
>>> c.put(4, 4)
>>> c.get(1)
1
>>> c.mem()
96
>>> c.size()
4
>>> c.put(5, 5)
>>> c.size()
4
>>> c.get(2)
Traceback (most recent call last):
    ...
KeyError: 2


self._cache[key] = value<EOL>self._order.push(key)<EOL>self._size += <NUM_LIT:1><EOL>


del self._cache[key]<EOL>self._order.delete(key)<EOL>self._size -= <NUM_LIT:1><EOL>

>>> c = LRUCache()
>>> c.put(1, 'one')
>>> c.get(1)
'one'
>>> c.delete(1)
>>> c.get(1)
Traceback (most recent call last):
    ...
KeyError: 1
>>> c.delete(1)
Traceback (most recent call last):
    ...
KeyError: 1


<EOL>if self.size() == self._maxsize:<EOL><INDENT>self.delete(self.last())<EOL><DEDENT>LRUCache.put(self, key, value)<EOL>

>>> c = FixedSizeLRUCache(maxsize=5)
>>> c.put(1, 'one')
>>> c.get(1)
'one'
>>> c.size()
1
>>> c.put(2, 'two')
>>> c.put(3, 'three')
>>> c.put(4, 'four')
>>> c.put(5, 'five')
>>> c.get(5)
'five'
>>> c.size()
5
>>> c.put(6, 'six')
>>> c.size()
5
>>> c.get(1)
Traceback (most recent call last):
    ...
KeyError: 1
>>> c.get(2)
'two'
>>> c.put(7, 'seven')
>>> c.get(2)
'two'
>>> c.get(3)
Traceback (most recent call last):
    ...
KeyError: 3


try:<EOL><INDENT>value = self._cache[key]<EOL>self._order.push(key)<EOL>self._hits += <NUM_LIT:1><EOL>return value<EOL><DEDENT>except KeyError as e:<EOL><INDENT>self._misses += <NUM_LIT:1><EOL>raise<EOL><DEDENT>

>>> c = LRUCache()
>>> c.get('toto')
Traceback (most recent call last):
    ...
KeyError: 'toto'
>>> c.stats()['misses']
1
>>> c.put('toto', 'tata')
>>> c.get('toto')
'tata'
>>> c.stats()['hits']
1


return self._size<EOL>


return self._first<EOL>


if self._last == node:<EOL><INDENT>self._last = node.previous<EOL><DEDENT>if self._first == node:<EOL><INDENT>self._first = node.next<EOL><DEDENT>node.pop()<EOL>del self._index[node.value]<EOL>self._size -= <NUM_LIT:1><EOL>


if not self._first:<EOL><INDENT>self._first = self._last = node = DLL.Node(k)<EOL><DEDENT>elif self._first.value == k:<EOL><INDENT>return<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>self.delete(k) <EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>self._first = node = self._first.insert_before(k)<EOL><DEDENT>self._index[k] = node<EOL>self._size += <NUM_LIT:1><EOL>


k = self._last.value<EOL>self.deletenode(self._last)<EOL>return k<EOL>


if len(toks) == <NUM_LIT:5> and toks[<NUM_LIT:1>] == '<STR_LIT:{>' and toks[<NUM_LIT:4>] == '<STR_LIT:}>':<EOL><INDENT>return strategies.dictionaries(<EOL>keys=toks[<NUM_LIT:2>], values=toks[<NUM_LIT:3>], max_size=<NUM_LIT:20>)<EOL><DEDENT>if len(toks) == <NUM_LIT:2>:<EOL><INDENT>return strategies.lists(elements=toks[<NUM_LIT:1>], max_size=<NUM_LIT:20>)<EOL><DEDENT>raise ValueError("<STR_LIT>")<EOL>

Generate the correct strategy for an array signature.

:param toks: the list of parsed tokens
:returns: strategy that generates an array or dict as appropriate
:rtype: strategy


<EOL>if isinstance(dbus_object, dbus.Dictionary):<EOL><INDENT>key_levels = [_descending(x) for x in dbus_object.keys()]<EOL>value_levels = [_descending(x) for x in dbus_object.values()]<EOL>if any(k is None for k in key_levels) orany(v is None for v in value_levels):<EOL><INDENT>return None<EOL><DEDENT>max_key_level = max(key_levels) if key_levels != [] else <NUM_LIT:0><EOL>max_value_level = max(value_levels) if value_levels != [] else <NUM_LIT:0><EOL>max_level = max(max_key_level, max_value_level)<EOL>variant_level = dbus_object.variant_level<EOL>if variant_level == <NUM_LIT:0>:<EOL><INDENT>return max_level<EOL><DEDENT>return None if variant_level < max_level + <NUM_LIT:1> else variant_level<EOL><DEDENT>if isinstance(dbus_object, (dbus.Array, dbus.Struct)):<EOL><INDENT>levels = [_descending(x) for x in dbus_object]<EOL>if any(l is None for l in levels):<EOL><INDENT>return None<EOL><DEDENT>max_level = max(levels) if levels != [] else <NUM_LIT:0><EOL>variant_level = dbus_object.variant_level<EOL>if variant_level == <NUM_LIT:0>:<EOL><INDENT>return max_level<EOL><DEDENT>return None if variant_level < max_level + <NUM_LIT:1> else variant_level<EOL><DEDENT>return dbus_object.variant_level<EOL>

Verify levels of variant values always descend.

:param object dbus_object: a dbus object
:returns: None if there was a failure of the property, otherwise the level
:rtype: int or NoneType

None is a better choice than False, for 0, a valid variant level, is always
interpreted as False.


cmd = ["<STR_LIT>", namespace.package] + arg_map[namespace.package]<EOL>if namespace.ignore:<EOL><INDENT>cmd.append("<STR_LIT>" % namespace.ignore)<EOL><DEDENT>return cmd<EOL>

Get the pylint command for these arguments.

:param `Namespace` namespace: the namespace


if len(toks) == <NUM_LIT:5> and toks[<NUM_LIT:1>] == '<STR_LIT:{>' and toks[<NUM_LIT:4>] == '<STR_LIT:}>':<EOL><INDENT>subtree = toks[<NUM_LIT:2>:<NUM_LIT:4>]<EOL>signature = '<STR_LIT>'.join(s for (_, s) in subtree)<EOL>[key_func, value_func] = [f for (f, _) in subtree]<EOL>def the_dict_func(a_dict, variant=<NUM_LIT:0>):<EOL><INDENT>"""<STR_LIT>"""<EOL>elements =[(key_func(x), value_func(y)) for (x, y) in a_dict.items()]<EOL>level = <NUM_LIT:0> if elements == []else max(max(x, y) for ((_, x), (_, y)) in elements)<EOL>(obj_level, func_level) =_ToDbusXformer._variant_levels(level, variant)<EOL>return (dbus.types.Dictionary(<EOL>((x, y) for ((x, _), (y, _)) in elements),<EOL>signature=signature,<EOL>variant_level=obj_level), func_level)<EOL><DEDENT>return (the_dict_func, '<STR_LIT>' + signature + '<STR_LIT:}>')<EOL><DEDENT>if len(toks) == <NUM_LIT:2>:<EOL><INDENT>(func, sig) = toks[<NUM_LIT:1>]<EOL>def the_array_func(a_list, variant=<NUM_LIT:0>):<EOL><INDENT>"""<STR_LIT>"""<EOL>if isinstance(a_list, dict):<EOL><INDENT>raise IntoDPValueError(a_list, "<STR_LIT>",<EOL>"<STR_LIT>")<EOL><DEDENT>elements = [func(x) for x in a_list]<EOL>level = <NUM_LIT:0> if elements == [] else max(x for (_, x) in elements)<EOL>(obj_level, func_level) =_ToDbusXformer._variant_levels(level, variant)<EOL>return (dbus.types.Array(<EOL>(x for (x, _) in elements),<EOL>signature=sig,<EOL>variant_level=obj_level), func_level)<EOL><DEDENT>return (the_array_func, '<STR_LIT:a>' + sig)<EOL><DEDENT>raise IntoDPValueError(toks, "<STR_LIT>",<EOL>"<STR_LIT>")<EOL>

Generate the correct function for an array signature.

:param toks: the list of parsed tokens
:returns: function that returns an Array or Dictionary value
:rtype: ((or list dict) -> ((or Array Dictionary) * int)) * str


def the_func(a_tuple, variant=<NUM_LIT:0>):<EOL><INDENT>"""<STR_LIT>"""<EOL>(signature, an_obj) = a_tuple<EOL>(func, sig) = self.COMPLETE.parseString(signature)[<NUM_LIT:0>]<EOL>assert sig == signature<EOL>(xformed, _) = func(an_obj, variant=variant + <NUM_LIT:1>)<EOL>return (xformed, xformed.variant_level)<EOL><DEDENT>return (the_func, '<STR_LIT:v>')<EOL>

Generate the correct function for a variant signature.

:returns: function that returns an appropriate value
:rtype: ((str * object) or list)-> object


@functools.wraps(func)<EOL>def the_func(expr):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>return func(expr)<EOL><DEDENT>except (TypeError, ValueError) as err:<EOL><INDENT>raise IntoDPValueError(expr, "<STR_LIT>", "<STR_LIT>")from err<EOL><DEDENT><DEDENT>return the_func<EOL>

Wraps a generated function so that it catches all Type- and ValueErrors
and raises IntoDPValueErrors.

:param func: the transforming function


def the_func(value, variant=<NUM_LIT:0>):<EOL><INDENT>"""<STR_LIT>"""<EOL>(obj_level, func_level) = _ToDbusXformer._variant_levels(<EOL><NUM_LIT:0>, variant)<EOL>return (klass(value, variant_level=obj_level), func_level)<EOL><DEDENT>return lambda: (the_func, symbol)<EOL>

Handle a base case.

:param type klass: the class constructor
:param str symbol: the type code


return (level + variant, level + variant)if variant != <NUM_LIT:0> else (variant, level)<EOL>

Gets the level for the variant.

:param int level: the current variant level
:param int variant: the value for this level if variant

:returns: a level for the object and one for the function
:rtype: int * int


def __init__(self, value, param, msg=None):

<EOL>self._value = value<EOL>self._param = param<EOL>self._msg = msg<EOL>

Initializer.

            :param object value: the value
            :param str param: the parameter
            :param str msg: an explanatory message


if isfile(changeset_file):<EOL><INDENT>self.filename = changeset_file<EOL><DEDENT>else:<EOL><INDENT>self.path = mkdtemp()<EOL>self.filename = join(self.path, basename(changeset_file))<EOL>download(changeset_file, self.path)<EOL><DEDENT>self.xml = ET.fromstring(gzip.open(self.filename).read())<EOL>if not isfile(changeset_file):<EOL><INDENT>rmtree(self.path)<EOL><DEDENT>

Download the replication changeset file or read it directly from the
        filesystem (to test purposes).


url = '<STR_LIT>'.format(<EOL>changeset<EOL>)<EOL>return ET.fromstring(requests.get(url).content)<EOL>

Get the changeset using the OSM API and return the content as a XML
    ElementTree.

    Args:
        changeset: the id of the changeset.


keys = [tag.attrib.get('<STR_LIT:k>') for tag in changeset.getchildren()]<EOL>keys += ['<STR_LIT:id>', '<STR_LIT:user>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>values = [tag.attrib.get('<STR_LIT:v>') for tag in changeset.getchildren()]<EOL>values += [<EOL>changeset.get('<STR_LIT:id>'), changeset.get('<STR_LIT:user>'), changeset.get('<STR_LIT>'),<EOL>get_bounds(changeset), changeset.get('<STR_LIT>')<EOL>]<EOL>return dict(zip(keys, values))<EOL>

Return a dictionary with id, user, user_id, bounds, date of creation
    and all the tags of the changeset.

    Args:
        changeset: the XML string of the changeset.


self.count()<EOL>self.verify_words()<EOL>self.verify_user()<EOL>if self.review_requested == '<STR_LIT:yes>':<EOL><INDENT>self.label_suspicious('<STR_LIT>')<EOL><DEDENT>

Execute the count and verify_words methods.


self.suspicion_reasons.append(reason)<EOL>self.is_suspect = True<EOL>

Add suspicion reason and set the suspicious flag.


if self.comment:<EOL><INDENT>if find_words(self.comment, self.suspect_words, self.excluded_words):<EOL><INDENT>self.label_suspicious('<STR_LIT>')<EOL><DEDENT><DEDENT>if self.source:<EOL><INDENT>for word in self.illegal_sources:<EOL><INDENT>if word in self.source.lower():<EOL><INDENT>self.label_suspicious('<STR_LIT>')<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if self.imagery_used:<EOL><INDENT>for word in self.illegal_sources:<EOL><INDENT>if word in self.imagery_used.lower():<EOL><INDENT>self.label_suspicious('<STR_LIT>')<EOL>break<EOL><DEDENT><DEDENT><DEDENT>self.suspicion_reasons = list(set(self.suspicion_reasons))<EOL>

Verify the fields source, imagery_used and comment of the changeset
        for some suspect words.


powerful_editors = [<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>'<EOL>]<EOL>if self.editor is not None:<EOL><INDENT>for editor in powerful_editors:<EOL><INDENT>if editor in self.editor.lower():<EOL><INDENT>self.powerfull_editor = True<EOL>break<EOL><DEDENT><DEDENT>if '<STR_LIT>' in self.editor:<EOL><INDENT>trusted_hosts = [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>]<EOL>if self.host.split('<STR_LIT>')[-<NUM_LIT:1>].strip('<STR_LIT:/>') not in trusted_hosts:<EOL><INDENT>self.label_suspicious('<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>self.powerfull_editor = True<EOL>self.label_suspicious('<STR_LIT>')<EOL><DEDENT>

Verify if the software used in the changeset is a powerfull_editor.


@click.command('<STR_LIT>')<EOL>@click.argument('<STR_LIT:id>', type=int, metavar='<STR_LIT>')<EOL>def cli(id):

ch = Analyse(id)<EOL>ch.full_analysis()<EOL>click.echo(<EOL>'<STR_LIT>' % (ch.create, ch.modify, ch.delete)<EOL>)<EOL>if ch.is_suspect:<EOL><INDENT>click.echo('<STR_LIT>'.format(<EOL>id,<EOL>'<STR_LIT:U+002CU+0020>'.join(ch.suspicion_reasons)<EOL>))<EOL><DEDENT>else:<EOL><INDENT>click.echo('<STR_LIT>' % id)<EOL><DEDENT>


import argparse<EOL>parser = argparse.ArgumentParser()<EOL>parser.add_argument('<STR_LIT>', nargs='<STR_LIT:*>', help='<STR_LIT>')<EOL>return parser.parse_args()<EOL>

Return processed arguments (options and positional arguments).


return <NUM_LIT:0> if check(process_args()) else <NUM_LIT:1><EOL>


with open('<STR_LIT>') as input_file:<EOL><INDENT>for line in input_file:<EOL><INDENT>if line.startswith('<STR_LIT>'):<EOL><INDENT>return ast.parse(line).body[<NUM_LIT:0>].value.s<EOL><DEDENT><DEDENT><DEDENT>


text = '<STR_LIT>'<EOL>previous_line = '<STR_LIT>'<EOL>last_row = <NUM_LIT:0><EOL>last_column = -<NUM_LIT:1><EOL>last_non_whitespace_token_type = None<EOL>for (token_type, token_string, start, end, line) in tokens:<EOL><INDENT>if TOKENIZE_HAS_ENCODING and token_type == tokenize.ENCODING:<EOL><INDENT>continue<EOL><DEDENT>(start_row, start_column) = start<EOL>(end_row, end_column) = end<EOL>if (<EOL>last_non_whitespace_token_type != tokenize.COMMENT and<EOL>start_row > last_row and<EOL>previous_line.endswith(('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>):<EOL><INDENT>text += previous_line[len(previous_line.rstrip('<STR_LIT>')):]<EOL><DEDENT>if start_row > last_row:<EOL><INDENT>last_column = <NUM_LIT:0><EOL><DEDENT>if start_column > last_column:<EOL><INDENT>text += line[last_column:start_column]<EOL><DEDENT>text += token_string<EOL>previous_line = line<EOL>last_row = end_row<EOL>last_column = end_column<EOL>if token_type not in WHITESPACE_TOKENS:<EOL><INDENT>last_non_whitespace_token_type = token_type<EOL><DEDENT><DEDENT>return text<EOL>

Return source code based on tokens.

    This is like tokenize.untokenize(), but it preserves spacing between
    tokens. So if the original soure code had multiple spaces between
    some tokens or if escaped newlines were used, those things will be
    reflected by untokenize().


<EOL>etag = generate_etag(current_ext.content_version.encode('<STR_LIT:utf8>'))<EOL>self.check_etag(etag, weak=True)<EOL>res = jsonify(current_ext.styles)<EOL>res.set_etag(etag)<EOL>return res<EOL>


if app:<EOL><INDENT>self._state = self.init_app(app)<EOL><DEDENT>


state = _InvenioCSLRESTState(app)<EOL>app.extensions['<STR_LIT>'] = state<EOL>return state<EOL>


def element_or_none(self, using, value):

try:<EOL><INDENT>return self._execute(Command.FIND_ELEMENT, {<EOL>'<STR_LIT>': using,<EOL>'<STR_LIT:value>': value<EOL>})<EOL><DEDENT>except:<EOL><INDENT>return None<EOL><DEDENT>

Check if an element in the current context.

        Support:
            Android iOS Web(WebView)

        Args:
            using(str): The element location strategy.
            value(str): The value of the location strategy.

        Returns:
            Return Element if the element does exists and return None otherwise.

        Raises:
            WebDriverException.


return self._execute(Command.GET_WINDOW_SIZE,<EOL>{'<STR_LIT>': window_handle})<EOL>

Gets the width and height of the current window.

        Support:
            Web(WebView)

        Args:
            window_handle(str): Identifier of window_handle,
                default to 'current'.

        Returns:
            A dict contains width and height


return self._execute(Command.FIND_ELEMENT, {<EOL>'<STR_LIT>': using,<EOL>'<STR_LIT:value>': value<EOL>})<EOL>

Find an element in the current context.

        Support:
            Android iOS Web(WebView)

        Args:
            using(str): The element location strategy.
            value(str): The value of the location strategy.

        Returns:
            WebElement Object.

        Raises:
            WebDriverException.


@fluent<EOL><INDENT>def set_window_size(self, width, height, window_handle='<STR_LIT>'):<DEDENT>

self._execute(Command.SET_WINDOW_SIZE, {<EOL>'<STR_LIT:width>': int(width),<EOL>'<STR_LIT>': int(height),<EOL>'<STR_LIT>': window_handle})<EOL>

Sets the width and height of the current window.

        Support:
            Web(WebView)

        Args:
            width(int): the width in pixels.
            height(int): the height in pixels.
            window_handle(str): Identifier of window_handle,
                default to 'current'.

        Returns:
            WebDriver Object.


return self._execute(Command.GET_PAGE_SOURCE)<EOL>

Gets the source of the current page.

        Support:
            Android iOS Web(WebView)

        Returns:
            Return the source of the current page.


if not isinstance(cookie_dict, dict):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if not cookie_dict.get(<EOL>'<STR_LIT:name>', None<EOL>) or not cookie_dict.get(<EOL>'<STR_LIT:value>', None):<EOL><INDENT>raise KeyError('<STR_LIT>')<EOL><DEDENT>self._execute(Command.ADD_COOKIE, {'<STR_LIT>': cookie_dict})<EOL>

Set a cookie.

        Support:
            Web(WebView)

        Args:
            cookie_dict: A dictionary contain keys: "name", "value",
                ["path"], ["domain"], ["secure"], ["httpOnly"], ["expiry"].

        Returns:
            WebElement Object.


self._execute(Command.SET_ALERT_VALUE, {<EOL>'<STR_LIT:text>': keys<EOL>})<EOL>

Sends keystrokes to a JavaScript prompt() dialog.

        Support:
            iOS

        Args:
            keys(str): The keys send to.


return self._execute(Command.CONTEXT_HANDLES)<EOL>

returns a list of available contexts

        Support:
            Android iOS


if isinstance(value, dict) and '<STR_LIT>' in value:<EOL><INDENT>element_id = value.get('<STR_LIT>')<EOL>return WebElement(element_id, self)<EOL><DEDENT>elif isinstance(value, list) and not isinstance(value, str):<EOL><INDENT>return [self._unwrap_el(item) for item in value]<EOL><DEDENT>else:<EOL><INDENT>return value<EOL><DEDENT>

Convert {'Element': 1234} to WebElement Object

        Args:
            value(str|list|dict): The value field in the json response.

        Returns:
            The unwrapped value.


data = {<EOL>'<STR_LIT:name>': window_name<EOL>}<EOL>self._execute(Command.SWITCH_TO_WINDOW, data)<EOL>

Switch to the given window.

        Support:
            Web(WebView)

        Args:
            window_name(str): The window to change focus to.

        Returns:
            WebDriver Object.


return self._execute(Command.GET_CURRENT_WINDOW_HANDLE)<EOL>

Returns the handle of the current window.

        Support:
            Web(WebView)


@fluent<EOL><INDENT>def move_to(self, element, x=<NUM_LIT:0>, y=<NUM_LIT:0>):<DEDENT>

self._execute(Command.MOVE_TO, {<EOL>'<STR_LIT>': element.element_id,<EOL>'<STR_LIT:x>': x,<EOL>'<STR_LIT:y>': y<EOL>})<EOL>

Deprecated use element.touch('drag', { toX, toY, duration(s) }) instead.
            Move the mouse by an offset of the specificed element.

        Support:
            Android

        Args:
            element(WebElement): WebElement Object.
            x(float): X offset to move to, relative to the
                      top-left corner of the element.
            y(float): Y offset to move to, relative to the
                      top-left corner of the element.

        Returns:
            WebDriver object.


self.session_id = session_id<EOL>

Attach to given Session.

        Support:
            Android iOS Web(WebView)

        Args:
            session_id(str): The given session ID

        Returns:
            WebDriver Object.


self._execute(Command.SINGLE_TAP, {<EOL>'<STR_LIT>': element.element_id,<EOL>})<EOL>

Deprecated use element.touch('tap') instead.
            Single tap on the touch enabled device.

        Support:
            Android iOS

        Args:
            element(WebElement): WebElement Object to single tap on.

        Returns:
            WebDriver object.


def execute_script(self, script, *args):

return self._execute(Command.EXECUTE_SCRIPT, {<EOL>'<STR_LIT>': script,<EOL>'<STR_LIT:args>': list(args)})<EOL>

Execute JavaScript Synchronously in current context.

        Support:
            Web(WebView)

        Args:
            script: The JavaScript to execute.
            *args: Arguments for your JavaScript.

        Returns:
            Returns the return value of the function.


self._execute(Command.DISMISS_ALERT)<EOL>

Dismisses the alert available.

        Support:
            iOS


def element_if_exists(self, using, value):

try:<EOL><INDENT>self._execute(Command.FIND_ELEMENT, {<EOL>'<STR_LIT>': using,<EOL>'<STR_LIT:value>': value<EOL>})<EOL>return True<EOL><DEDENT>except:<EOL><INDENT>return False<EOL><DEDENT>

Check if an element in the current context.

        Support:
            Android iOS Web(WebView)

        Args:
            using(str): The element location strategy.
            value(str): The value of the location strategy.

        Returns:
            Return True if the element does exists and return False otherwise.

        Raises:
            WebDriverException.


return self._execute(Command.GET_ACTIVE_ELEMENT)<EOL>

Returns the active element in current context.

        Support:
            Web(WebView)


return self._execute(Command.SCREENSHOT)<EOL>

Gets the screenshot of the current window
           as a base64 encoded string.

        Support:
            Android iOS Web(WebView)

        Returns:
            Base64 encoded string of the screenshot.


Closes the current window.

        Support:
            Web(WebView)

        Returns:
            WebDriver Object.


self._execute(Command.SWITCH_TO_CONTEXT, {"<STR_LIT:name>": new_context})<EOL>

sets the current context

        Support:
            Android iOS


resp = self._execute(Command.NEW_SESSION, {<EOL>'<STR_LIT>': self.desired_capabilities<EOL>}, False)<EOL>resp.raise_for_status()<EOL>self.session_id = str(resp.session_id)<EOL>self.capabilities = resp.value<EOL>

Create Session by desiredCapabilities

        Support:
            Android iOS Web(WebView)

        Returns:
            WebDriver Object.


return self._execute(Command.GET_ALL_SESSIONS)<EOL>

Gets all the sessions of the webdriver server.

        Support:
            Android iOS Web(WebView)

        Returns:
            Return the URL of the current page.


def wait_for_element(<EOL>self, using, value, timeout=<NUM_LIT>,<EOL>interval=<NUM_LIT:1000>, asserter=is_displayed):

if not callable(asserter):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>@retry(<EOL>retry_on_exception=lambda ex: isinstance(ex, WebDriverException),<EOL>stop_max_delay=timeout,<EOL>wait_fixed=interval<EOL>)<EOL>def _wait_for_element(ctx, using, value):<EOL><INDENT>el = ctx.element(using, value)<EOL>asserter(el)<EOL>return el<EOL><DEDENT>return _wait_for_element(self, using, value)<EOL>

Wait for element till satisfy the given condition

        Support:
            Android iOS Web(WebView)

        Args:
            using(str): The element location strategy.
            value(str): The value of the location strategy.
            timeout(int): How long we should be retrying stuff.
            interval(int): How long between retries.
            asserter(callable): The asserter func to determine the result.

        Returns:
            Return the Element.

        Raises:
            WebDriverException.


return self._execute(Command.GET_CURRENT_URL)<EOL>

Gets the URL of the current page.

        Support:
            Web(WebView)

        Returns:
            Return the URL of the current page.


self._execute(Command.MAXIMIZE_WINDOW,<EOL>{"<STR_LIT>": "<STR_LIT>"})<EOL>

Maximizes the current window.

        Support:
            Web(WebView)

        Returns:
            WebDriver Object.


return self._execute(Command.GET_WINDOW_HANDLES)<EOL>

Returns the handles of all windows within the current session.

        Support:
            Web(WebView)


def wait_for_elements(<EOL>self, using, value, timeout=<NUM_LIT>,<EOL>interval=<NUM_LIT:1000>, asserter=is_displayed):

if not callable(asserter):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>@retry(<EOL>retry_on_exception=lambda ex: isinstance(ex, WebDriverException),<EOL>stop_max_delay=timeout,<EOL>wait_fixed=interval<EOL>)<EOL>def _wait_for_elements(ctx, using, value):<EOL><INDENT>els = ctx.elements(using, value)<EOL>if not len(els):<EOL><INDENT>raise WebDriverException('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>el = els[<NUM_LIT:0>]<EOL>asserter(el)<EOL>return els<EOL><DEDENT><DEDENT>return _wait_for_elements(self, using, value)<EOL>

Wait for elements till satisfy the given condition

        Support:
            Android iOS Web(WebView)

        Args:
            using(str): The element location strategy.
            value(str): The value of the location strategy.
            timeout(int): How long we should be retrying stuff.
            interval(int): How long between retries.
            asserter(callable): The asserter func to determine the result.

        Returns:
            Return the list of Element if any of them satisfy the condition.

        Raises:
            WebDriverException.


self._execute(Command.SEND_KEYS_TO_ACTIVE_ELEMENT, {<EOL>'<STR_LIT:value>': value_to_key_strokes(value)<EOL>})<EOL>

Send a sequence of key strokes.

        Support:
            Android iOS Web(WebView)

        Args:
            value(str|int|list): value can be a string,
              int or a list contains defined Keys.


self._execute(Command.GET, {'<STR_LIT:url>': url})<EOL>

Loads a web page in the current browser session.

        Support:
            Web(WebView)

        Args:
            url(str): The URL to navigate to.

        Returns:
            WebDriver Object.

        Raises:
            WebDriverException.


if isinstance(value, dict):<EOL><INDENT>return {k: self._wrap_el(v) for k, v in value.items()}<EOL><DEDENT>elif isinstance(value, WebElement):<EOL><INDENT>return {'<STR_LIT>': value.element_id}<EOL><DEDENT>elif isinstance(value, list) and not isinstance(value, str):<EOL><INDENT>return [self._wrap_el(item) for item in value]<EOL><DEDENT>else:<EOL><INDENT>return value<EOL><DEDENT>

Convert WebElement Object to {'Element': 1234}

        Args:
            value(str|list|dict): The local value.

        Returns:
            The wrapped value.


@fluent<EOL><INDENT>def execute_async_script(self, script, *args):<DEDENT>

return self._execute(Command.EXECUTE_ASYNC_SCRIPT, {<EOL>'<STR_LIT>': script,<EOL>'<STR_LIT:args>': list(args)})<EOL>

Execute JavaScript Asynchronously in current context.

        Support:
            Web(WebView)

        Args:
            script: The JavaScript to execute.
            *args: Arguments for your JavaScript.

        Returns:
            Returns the return value of the function.


return self._execute(Command.GET_WINDOW_POSITION, {<EOL>'<STR_LIT>': window_handle})<EOL>

Gets the x,y position of the current window.

        Support:
            Web(WebView)

        Args:
            window_handle(str): Identifier of window_handle,
                default to 'current'.

        Usage:
            driver.get_window_position()


def format_map(self, format_string, mapping):

return self.vformat(format_string, args=None, kwargs=mapping)<EOL>

format a string by a map

        Args:
            format_string(str): A format string
            mapping(dict): A map to format the string

        Returns:
            A formatted string.

        Raises:
            KeyError: if key is not provided by the given map.


def add_element_method(Klass, using):<EOL><INDENT>locator = using.name.lower()<EOL>find_element_name = "<STR_LIT>" + locator<EOL>find_element_if_exists_name = "<STR_LIT>" + locator + "<STR_LIT>"<EOL>find_element_or_none_name = "<STR_LIT>" + locator + "<STR_LIT>"<EOL>wait_for_element_name = "<STR_LIT>" + locator<EOL>find_elements_name = "<STR_LIT>" + locator<EOL>wait_for_elements_name = "<STR_LIT>" + locator<EOL>def find_element(self, value):<EOL><INDENT>return self.element(using.value, value)<EOL><DEDENT>find_element.__name__ = find_element_name<EOL>find_element.__doc__ = (<EOL>"<STR_LIT>".format(using.value) +<EOL>"<STR_LIT>"<EOL>)<EOL>def find_element_if_exists(self, value):<EOL><INDENT>return self.element_if_exists(using.value, value)<EOL><DEDENT>find_element_if_exists.__name__ = find_element_if_exists_name<EOL>find_element_if_exists.__doc__ = (<EOL>"<STR_LIT>".format(using.value) +<EOL>"<STR_LIT>"<EOL>)<EOL>def find_element_or_none(self, value):<EOL><INDENT>return self.element_or_none(using.value, value)<EOL><DEDENT>find_element_or_none.__name__ = find_element_or_none_name<EOL>find_element_or_none.__doc__ = (<EOL>"<STR_LIT>".format(using.value) +<EOL>"<STR_LIT>"<EOL>)<EOL>def wait_for_element_by(self, *args, **kwargs):<EOL><INDENT>return self.wait_for_element(using.value, *args, **kwargs)<EOL><DEDENT>wait_for_element_by.__name__ = wait_for_element_name<EOL>wait_for_element_by.__doc__ = (<EOL>"<STR_LIT>".format(using.value) +<EOL>"<STR_LIT>"<EOL>)<EOL>def find_elements(self, value):<EOL><INDENT>return self.elements(using.value, value)<EOL><DEDENT>find_elements.__name__ = find_elements_name<EOL>find_elements.__doc__ = (<EOL>"<STR_LIT>".format(using.value) +<EOL>"<STR_LIT>"<EOL>)<EOL>def wait_for_elements_available(self, *args, **kwargs):<EOL><INDENT>return self.wait_for_elements(using.value, *args, **kwargs)<EOL><DEDENT>wait_for_elements_available.__name__ = wait_for_elements_name<EOL>wait_for_elements_available.__doc__ = (<EOL>"<STR_LIT>".format(using.value) +<EOL>"<STR_LIT>"<EOL>)<EOL>setattr(Klass, find_element_name, find_element)<EOL>setattr(Klass, find_element_if_exists_name, find_element_if_exists)<EOL>setattr(Klass, find_element_or_none_name, find_element_or_none)<EOL>setattr(Klass, wait_for_element_name, wait_for_element_by)<EOL>setattr(Klass, find_elements_name, find_elements)<EOL>setattr(Klass, wait_for_elements_name, wait_for_elements_available)<EOL><DEDENT>for locator in iter(Locator):<EOL><INDENT>add_element_method(Klass, locator)<EOL><DEDENT>

Add element_by alias and extension' methods(if_exists/or_none).


@wraps(func)<EOL>def fluent_interface(instance, *args, **kwargs):<EOL><INDENT>ret = func(instance, *args, **kwargs)<EOL>if ret is not None:<EOL><INDENT>return ret<EOL><DEDENT>return instance<EOL><DEDENT>return fluent_interface<EOL>

Fluent interface decorator to return self if method return None.


result = []<EOL>if isinstance(value, Integral):<EOL><INDENT>value = str(value)<EOL><DEDENT>for v in value:<EOL><INDENT>if isinstance(v, Keys):<EOL><INDENT>result.append(v.value)<EOL><DEDENT>elif isinstance(v, Integral):<EOL><INDENT>result.append(str(v))<EOL><DEDENT>else:<EOL><INDENT>result.append(v)<EOL><DEDENT><DEDENT>return result<EOL>

Convert value to a list of key strokes
    >>> value_to_single_key_strokes(123)
    ['1', '2', '3']
    >>> value_to_single_key_strokes('123')
    ['1', '2', '3']
    >>> value_to_single_key_strokes([1, 2, 3])
    ['1', '2', '3']
    >>> value_to_single_key_strokes(['1', '2', '3'])
    ['1', '2', '3']
    Args:
        value(int|str|list)
    Returns:
        A list of string.


return self._used_kwargs<EOL>


def __init__(self, error=None, message=None, screen=None, stacktrace=None):

self.error = error<EOL>self.message = message<EOL>self.screen = screen<EOL>self.stacktrace = stacktrace<EOL>


return self._execute(Command.GET_ELEMENT_TEXT)<EOL>

Return the text of the element.
           This is equivalent to calling element.innerText.

        Support:
            Android iOS Web(WebView)


Deprecated use touch('tap') instead.
            Single tap on the touch enabled device.

        Support:
            Android iOS

        Args:
            element(WebElement): WebElement Object to single tap on.

        Returns:
            WebElement object.


@fluent<EOL><INDENT>def touch(self, name, args=None):<DEDENT>

if isinstance(name, list) and not isinstance(name, str):<EOL><INDENT>for obj in name:<EOL><INDENT>obj['<STR_LIT>'] = self.element_id<EOL><DEDENT>actions = name<EOL><DEDENT>elif isinstance(name, str):<EOL><INDENT>if not args:<EOL><INDENT>args = {}<EOL><DEDENT>args['<STR_LIT:type>'] = name<EOL>args['<STR_LIT>'] = self.element_id<EOL>actions = [args]<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>self._driver._execute(Command.PERFORM_ACTIONS, {<EOL>'<STR_LIT>': actions<EOL>})<EOL>

Apply touch actions on devices. Such as, tap/doubleTap/press/pinch/rotate/drag.
            See more on https://github.com/alibaba/macaca/issues/366.

        Support:
            Android iOS

        Args:
            name(str): Name of the action
            args(dict): Arguments of the action

        Returns:
            WebDriver Object.

        Raises:
            WebDriverException.


@fluent<EOL><INDENT>def save_screenshot(self, filename, quietly = False):<DEDENT>

imgData = self.take_screenshot()<EOL>try:<EOL><INDENT>with open(filename, "<STR_LIT:wb>") as f:<EOL><INDENT>f.write(b64decode(imgData.encode('<STR_LIT:ascii>')))<EOL><DEDENT><DEDENT>except IOError as err:<EOL><INDENT>if not quietly:<EOL><INDENT>raise err<EOL><DEDENT><DEDENT>

Save the screenshot to local.

        Support:
            Android iOS Web(WebView)

        Args:
            filename(str): The path to save the image.
            quietly(bool): If True, omit the IOError when
                failed to save the image.

        Returns:
            WebElement Object.

        Raises:
            WebDriverException.
            IOError.


self._execute(Command.SEND_KEYS_TO_ELEMENT, {<EOL>'<STR_LIT:value>': value_to_key_strokes(value)})<EOL>

Send a sequence of key strokes to an element.

        Support:
            Android iOS Web(WebView)

        Args:
            value(str|int|list): value can be a string,
              int or a list contains defined Keys.


def wait_for(<EOL>self, timeout=<NUM_LIT>, interval=<NUM_LIT:1000>,<EOL>asserter=lambda x: x):

if not callable(asserter):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>@retry(<EOL>retry_on_exception=lambda ex: isinstance(ex, WebDriverException),<EOL>stop_max_delay=timeout,<EOL>wait_fixed=interval<EOL>)<EOL>def _wait_for(el):<EOL><INDENT>asserter(el)<EOL>return el<EOL><DEDENT>return _wait_for(self)<EOL>

Wait for element till given condition

        Support:
            Android iOS Web(WebView)

        Args:
            timeout(int): How long we should be retrying stuff.
            interval(int): How long between retries.
            asserter(callable): The asserter func to determine the result.

        Returns:
            Return the Element.

        Raises:
            WebDriverException.


self._execute(Command.CLEAR_ELEMENT)<EOL>

The Element Clear command scrolls into view
           a submittable element excluding buttons or
           editable element, and then attempts to clear
           its value, checkedness, or text content.

        Support:
            Android iOS Web(WebView)


return self._execute(Command.GET_ELEMENT_SIZE)<EOL>

The size of the given web element in pixels.

        Support:
            Web(WebView)

        Returns:
            A dict contains:
            height(float): Height of the web element's bounding rectangle.
            width(float): Width of the web element's bounding rectangle.


return self._execute(Command.IS_ELEMENT_DISPLAYED)<EOL>

Whether the element is visible.

        Support:
            Android Web(WebView)


return self._execute(Command.ELEMENT_SCREENSHOT)<EOL>

Gets the screenshot of the current element
           as a base64 encoded string.

        Support:
            Android iOS Web(WebView)

        Returns:
            Base64 encoded string of the screenshot.


return self._execute(Command.FIND_CHILD_ELEMENTS, {<EOL>'<STR_LIT>': using,<EOL>'<STR_LIT:value>': value<EOL>})<EOL>

find elements in the current element.

        Support:
            Android iOS Web(WebView)

        Args:
            using(str): The element location strategy.
            value(str): The value of the location strategy.

        Returns:
            Return a List<Element | None>, if no element matched, the list is empty.

        Raises:
            WebDriverException.


self._execute(Command.CLICK_ELEMENT)<EOL>

The Element Click command scrolls into view
           the element and then attempts to click the
           centre of the visible area of the first element
           of the DOMRect sequence. In case the element is
           not displayed, an element not visible error is returned.

        Support:
            Android iOS Web(WebView)