oarepo-runtime 1.4.5__py3-none-any.whl → 1.4.6__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ import itertools
1
2
  import sys
2
3
 
3
4
  import click
@@ -85,6 +86,18 @@ def record_or_service(model):
85
86
  return record
86
87
 
87
88
 
89
+ def model_records_generator(model_class):
90
+ try:
91
+ for x in db.session.query(model_class.model_cls.id).filter(
92
+ model_class.model_cls.is_deleted.is_(False)
93
+ ):
94
+ rec_id = x[0]
95
+ yield model_class.get_record(rec_id)
96
+ except Exception as e:
97
+ if "Column expression or FROM clause expected" not in str(e):
98
+ raise
99
+
100
+
88
101
  @index.command()
89
102
  @with_appcontext
90
103
  @click.argument("model", required=False)
@@ -99,30 +112,38 @@ def reindex(model):
99
112
  service = current_service_registry.get(service_id)
100
113
  record_class = getattr(service.config, "record_cls", None)
101
114
 
102
- if not record_class or not hasattr(service, "indexer"):
103
- continue
115
+ id_generators = []
104
116
 
105
- try:
106
- id_generator = (
107
- x[0]
108
- for x in db.session.query(record_class.model_cls.id).filter(
109
- record_class.model_cls.is_deleted.is_(False)
117
+ if record_class and hasattr(service, "indexer"):
118
+ try:
119
+ id_generators.append(model_records_generator(record_class))
120
+ except Exception as e:
121
+ click.secho(
122
+ f"Could not get record ids for {service_id}, exception {e}",
123
+ file=sys.stderr,
124
+ )
125
+
126
+ draft_class = getattr(service.config, "draft_cls", None)
127
+
128
+ if draft_class and hasattr(service, "indexer"):
129
+ try:
130
+ id_generators.append(model_records_generator(draft_class))
131
+ except Exception as e:
132
+ click.secho(
133
+ f"Could not get draft record ids for {service_id}, exception {e}",
134
+ file=sys.stderr,
110
135
  )
111
- )
112
- except Exception as e:
113
- click.secho(
114
- f"Could not get record ids for {service_id}, exception {e}",
115
- file=sys.stderr,
116
- )
117
- continue
118
136
 
119
137
  click.secho(f"Indexing {service_id}", file=sys.stderr)
120
- ids = list(id_generator)
121
- for rec_id in ids:
122
- record = record_class.get_record(rec_id)
123
- service.indexer.index(record)
124
- service.indexer.refresh()
138
+ count = 0
139
+ for gen in id_generators:
140
+ for record in gen:
141
+ service.indexer.index(record)
142
+ count += 1
143
+ if count:
144
+ service.indexer.refresh()
145
+
125
146
  click.secho(
126
- f"Indexing {service_id} finished, indexed {len(ids)} records",
147
+ f"Indexing {service_id} finished, indexed {count} records",
127
148
  file=sys.stderr,
128
149
  )
@@ -22,6 +22,12 @@ class CheckOk(Exception):
22
22
  pass
23
23
 
24
24
 
25
+ def dump_data(d):
26
+ io = StringIO()
27
+ yaml.safe_dump(d, io, allow_unicode=True)
28
+ return io.getvalue()
29
+
30
+
25
31
  @oarepo.command(
26
32
  help="Validate a record. Takes one or two parameters - service name as "
27
33
  "the first one, file name or stdin with record data as the second"
@@ -51,12 +57,22 @@ def validate(service_name, record_file, verbose):
51
57
  if file_content.startswith("{"):
52
58
  data = json.loads(file_content)
53
59
  else:
54
- data = yaml.safe_load(StringIO(file_content))
60
+ data = list(yaml.safe_load_all(StringIO(file_content)))
55
61
 
56
62
  if not isinstance(data, list):
57
63
  data = [data]
58
64
  for idx, d in enumerate(data):
59
- loaded = schema().load(d)
65
+ try:
66
+ loaded = schema().load(d)
67
+ except Exception as e:
68
+ click.secho(
69
+ f"Marshmallow validation of record idx {idx + 1} failed",
70
+ fg="red",
71
+ )
72
+ click.secho(dump_data(d))
73
+ click.secho(e)
74
+ continue
75
+
60
76
  click.secho(
61
77
  f"Marshmallow validation of record idx {idx+1} has been successful",
62
78
  fg="green",
@@ -74,6 +90,14 @@ def validate(service_name, record_file, verbose):
74
90
  click.secho(
75
91
  f"Pre-commit hook of record idx {idx+1} has been successful", fg="green"
76
92
  )
93
+ except Exception as e:
94
+ click.secho(
95
+ f"Pre-commit validation of record idx {idx + 1} failed",
96
+ fg="red",
97
+ )
98
+ click.secho(dump_data(d))
99
+ click.secho(e)
100
+ continue
77
101
 
78
102
  if verbose:
79
103
  yaml.safe_dump(loaded, sys.stdout, allow_unicode=True)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: oarepo-runtime
3
- Version: 1.4.5
3
+ Version: 1.4.6
4
4
  Summary: A set of runtime extensions of Invenio repository
5
5
  Description-Content-Type: text/markdown
6
6
  License-File: LICENSE
@@ -12,8 +12,8 @@ oarepo_runtime/cli/__init__.py,sha256=-WGXmjHoSqiApR_LvYnZTimuL-frR7SynrsSklnjb3
12
12
  oarepo_runtime/cli/assets.py,sha256=XLZTnsGb88O5N8R2D3AYpZqtnO4JrbybUtRLKnL1p3w,2430
13
13
  oarepo_runtime/cli/base.py,sha256=xZMsR2rati5Mz0DZzmnlhVI7E6ePCfnOiTayrxT9cWU,259
14
14
  oarepo_runtime/cli/check.py,sha256=zQ6txhfN6LWvdWgRa3ZFdOifbuazoRQd-7ml0qwIBWg,2358
15
- oarepo_runtime/cli/index.py,sha256=2QMCEijHJeYJrf1WPFR8UhgsRwmBVIaiKx-gCFNTWM4,4010
16
- oarepo_runtime/cli/validate.py,sha256=jzvCicXibYhIK10m7aOePv1icljlCjXaZlH67rY4JAg,2177
15
+ oarepo_runtime/cli/index.py,sha256=fZMMw0Z4vwJ9fe7g8tb4wyoAMKeushxORvIUgxWQA40,4701
16
+ oarepo_runtime/cli/validate.py,sha256=HpSvHQCGHlrdgdpKix9cIlzlBoJEiT1vACZdMnOUGEY,2827
17
17
  oarepo_runtime/config/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
18
  oarepo_runtime/config/permissions_presets.py,sha256=4gWNYwOi7CmS69PnxpbjHY45sc0q7BcrvkhSFdn1SlU,5786
19
19
  oarepo_runtime/config/service.py,sha256=IDDEQJ3LWYBLTFhE_8jPXiinIDNDY52fI0zigDJ_usQ,1095
@@ -81,9 +81,9 @@ oarepo_runtime/utils/path.py,sha256=V1NVyk3m12_YLbj7QHYvUpE1wScO78bYsX1LOLeXDkI,
81
81
  oarepo_runtime/validation/__init__.py,sha256=lU7DgZq8pGD5Pa-QqL9gvLsib3IYtM-Y56k-NwHrPG0,166
82
82
  oarepo_runtime/validation/dates.py,sha256=fahqKGDdIYWux5ZeoljrEe8VD2fDZR9VpfvYmTYAmpw,1050
83
83
  tests/pkg_data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
84
- oarepo_runtime-1.4.5.dist-info/LICENSE,sha256=h2uWz0OaB3EN-J1ImdGJZzc7yvfQjvHVYdUhQ-H7ypY,1064
85
- oarepo_runtime-1.4.5.dist-info/METADATA,sha256=bkQGyaHajVGMp4a54aM5O1zr0BuTnUSA33eTkC--G0s,2694
86
- oarepo_runtime-1.4.5.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
87
- oarepo_runtime-1.4.5.dist-info/entry_points.txt,sha256=C32W4eT-8OypMCfwOO5WREioVKSneDfY51D78Uvdbp0,231
88
- oarepo_runtime-1.4.5.dist-info/top_level.txt,sha256=bHhlkT1_RQC4IkfTQCqA3iN4KCB6cSFQlsXpQMSP-bE,21
89
- oarepo_runtime-1.4.5.dist-info/RECORD,,
84
+ oarepo_runtime-1.4.6.dist-info/LICENSE,sha256=h2uWz0OaB3EN-J1ImdGJZzc7yvfQjvHVYdUhQ-H7ypY,1064
85
+ oarepo_runtime-1.4.6.dist-info/METADATA,sha256=Qz4fu5lS2SVdr_-opBBEtzkFsWoGIMF78prI4eW-EyY,2694
86
+ oarepo_runtime-1.4.6.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
87
+ oarepo_runtime-1.4.6.dist-info/entry_points.txt,sha256=C32W4eT-8OypMCfwOO5WREioVKSneDfY51D78Uvdbp0,231
88
+ oarepo_runtime-1.4.6.dist-info/top_level.txt,sha256=bHhlkT1_RQC4IkfTQCqA3iN4KCB6cSFQlsXpQMSP-bE,21
89
+ oarepo_runtime-1.4.6.dist-info/RECORD,,