-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcli.py
135 lines (106 loc) · 3.82 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""Click command-line interface for PIDStore management."""
from __future__ import absolute_import, print_function
import click
from flask.cli import with_appcontext
from invenio_db import db
from .proxies import current_pidstore
def process_status(ctx, param, value):
"""Return status value."""
from .models import PIDStatus
# Allow empty status
if value is None:
return None
if not hasattr(PIDStatus, value):
raise click.BadParameter(
"Status needs to be one of {0}.".format(
", ".join([s.name for s in PIDStatus])
)
)
return getattr(PIDStatus, value)
#
# PIDStore management commands
#
@click.group()
def pid():
"""PID-Store management commands."""
@pid.command()
@click.argument("pid_type")
@click.argument("pid_value")
@click.option("-s", "--status", default="NEW", callback=process_status)
@click.option("-t", "--type", "object_type", default=None)
@click.option("-i", "--uuid", "object_uuid", default=None)
@with_appcontext
def create(pid_type, pid_value, status, object_type, object_uuid):
"""Create new persistent identifier."""
from .models import PersistentIdentifier
if bool(object_type) ^ bool(object_uuid):
raise click.BadParameter("Speficy both or any of --type and --uuid.")
new_pid = PersistentIdentifier.create(
pid_type,
pid_value,
status=status,
object_type=object_type,
object_uuid=object_uuid,
)
db.session.commit()
click.echo("{0.pid_type} {0.pid_value} {0.pid_provider}".format(new_pid))
@pid.command()
@click.argument("pid_type")
@click.argument("pid_value")
@click.option("-s", "--status", default=None, callback=process_status)
@click.option("-t", "--type", "object_type", required=True)
@click.option("-i", "--uuid", "object_uuid", required=True)
@click.option("--overwrite", is_flag=True, default=False)
@with_appcontext
def assign(pid_type, pid_value, status, object_type, object_uuid, overwrite):
"""Assign persistent identifier."""
from .models import PersistentIdentifier
obj = PersistentIdentifier.get(pid_type, pid_value)
if status is not None:
obj.status = status
obj.assign(object_type, object_uuid, overwrite=overwrite)
db.session.commit()
click.echo(obj.status)
@pid.command()
@click.argument("pid_type")
@click.argument("pid_value")
@with_appcontext
def unassign(pid_type, pid_value):
"""Unassign persistent identifier."""
from .models import PersistentIdentifier
obj = PersistentIdentifier.get(pid_type, pid_value)
obj.unassign()
db.session.commit()
click.echo(obj.status)
@pid.command("get")
@click.argument("pid_type")
@click.argument("pid_value")
@with_appcontext
def get_object(pid_type, pid_value):
"""Get an object behind persistent identifier."""
from .models import PersistentIdentifier
obj = PersistentIdentifier.get(pid_type, pid_value)
if obj.has_object():
click.echo("{0.object_type} {0.object_uuid} {0.status}".format(obj))
@pid.command("dereference")
@click.argument("object_type")
@click.argument("object_uuid")
@click.option("-s", "--status", default=None, callback=process_status)
@with_appcontext
def dereference_object(object_type, object_uuid, status):
"""Show linked persistent identifier(s)."""
from .models import PersistentIdentifier
pids = PersistentIdentifier.query.filter_by(
object_type=object_type, object_uuid=object_uuid
)
if status:
pids = pids.filter_by(status=status)
for found_pid in pids.all():
click.echo("{0.pid_type} {0.pid_value} {0.pid_provider}".format(found_pid))