Beispiel #1
0
 def callback(ctx, param, value):
     state = ctx.ensure_object(State)
     if value:
         if state.verbose:
             raise BadArgumentUsage(
                 "--verbose and --quiet are mutually exclusive! Please choose one!",
                 ctx=ctx)
         state.quiet = True
         setup_verbosity(ctx, param, -1)
Beispiel #2
0
async def cli(ctx: Context, rearq: str, verbose):
    splits = rearq.split(":")
    rearq_path = splits[0]
    rearq = splits[1]
    try:
        module = importlib.import_module(rearq_path)
        rearq = getattr(module, rearq, None)

        ctx.ensure_object(dict)
        ctx.obj["rearq"] = rearq
        ctx.obj["verbose"] = verbose

    except (ModuleNotFoundError, AttributeError) as e:
        raise BadArgumentUsage(ctx=ctx, message=f"Init rearq error, {e}.")
Beispiel #3
0
def client_redirect_remove(ctx, redirect_uri):
    client = _find_client(ctx.obj['client_id'])
    if client is None:
        return 1

    uri_list = client.redirect_uris
    if redirect_uri not in uri_list:
        raise BadArgumentUsage('Uri not in list: ' + redirect_uri)

    uri_list.remove(redirect_uri)
    metadata = client.client_metadata
    metadata['redirect_uris'] = uri_list
    client.set_client_metadata(metadata)
    db.session.commit()
    click.echo('Removed: ' + redirect_uri)
Beispiel #4
0
def assign(
    path: click.Path,
    annotator: str,
    shas: Tuple[str],
    sha_file: click.Path = None,
    name_file: click.Path = None,
    all: bool = False,
):
    """
    Assign pdfs and annotators for a project.

    Use assign to assign annotators to a project, or assign them
    pdfs in the specified directory.

    Annotators must be assigned a username corresponding
    to a gmail email address, such as `[email protected]`.

    Add an annotator:

        `pawls assign <path to pawls directory> [email protected]`

    To assign all current pdfs in the project to an annotator, use:

        `pawls assign <path to pawls directory> <annotator> --all`
    """
    shas = set(shas)

    pdfs = glob.glob(os.path.join(path, "*/*.pdf"))
    project_shas = {p.split("/")[-2] for p in pdfs}
    if all:
        # If --all flag, we use all pdfs in the current project.
        shas.update(project_shas)

    if sha_file is not None:
        extra_ids = [x.strip("\n") for x in open(sha_file, "r")]
        shas.update(extra_ids)

    diff = shas.difference(project_shas)
    if diff:
        error = f"Found shas which are not present in path {path} .\n"
        error = (
            error
            + f"Add pdf files in the specified directory, one per sub-directory."
        )
        for sha in diff:
            error = error + f"{sha}\n"
        raise UsageError(error)

    if all:
        # If --all flag, we use all pdfs in the current project.
        shas.update(project_shas)

    if sha_file is not None:
        extra_ids = [x.strip("\n") for x in open(sha_file, "r")]
        shas.update(extra_ids)

    result = re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", annotator)

    if not result or result.group(0) != annotator:
        raise BadArgumentUsage("Provided annotator was not a valid email.")

    status_dir = os.path.join(path, "status")
    os.makedirs(status_dir, exist_ok=True)

    status_path = os.path.join(status_dir, f"{annotator}.json")

    pdf_status = {}
    if os.path.exists(status_path):
        pdf_status = json.load(open(status_path))

    name_mapping = {}
    if name_file is not None:
        name_mapping = json.load(open(name_file))
    else:
        print("Warning: --name-file was not provided, using shas as pdf names.")

    for sha in shas:
        if sha in pdf_status:
            continue
        else:

            name = name_mapping.get(sha, None)
            if name is None:
                name = sha

            pdf_status[sha] = {
                "sha": sha,
                "name": name,
                "annotations": 0,
                "relations": 0,
                "finished": False,
                "junk": False,
                "comments": "",
                "completedAt": None,
            }

    with open(status_path, "w+") as out:
        json.dump(pdf_status, out)