-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaims3couchdb.py
executable file
·1307 lines (1162 loc) · 57.1 KB
/
faims3couchdb.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import logging
import requests
import re
from tqdm.auto import tqdm
from uuid import uuid4
import base64
import traceback
from slugify import slugify
from collections import OrderedDict, defaultdict
import datetime
import geojson
import json
from shapely.geometry import shape
from pprint import pprint
# from flatten_json import flatten
from pprint import pformat
import logging
import tempfile
import pandas
from mimetypes import guess_extension, guess_type
LOCAL_TIMEZONE = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
class TqdmLoggingHandler(logging.Handler):
# https://stackoverflow.com/a/38739634
def __init__(self, level=logging.NOTSET):
super().__init__(level)
def emit(self, record):
try:
msg = self.format(record)
tqdm.write(msg)
self.flush()
except Exception:
self.handleError(record)
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
log.addHandler(TqdmLoggingHandler())
def create_new_revision(
*, avps, record_id, new_id, parents, created_by, created, type, deleted=False
):
"""
Create a new revision (format v1).
avps must be a dictionary mapping field names to avp id
parents must be a list of revision ids
"""
return {
"_id": new_id,
"revision_format_version": 1,
"avps": avps,
"record_id": record_id,
"parents": parents,
"created": str(created),
"created_by": created_by,
"type": type,
"deleted": deleted,
}
def create_new_avp(
*, data, revision_id, record_id, annotations, type, attachments=None
):
"""
Create a new attribute-value pair (format v1).
data can be anything that can be json encoded
"""
avp = {
"_id": f"avp-{uuid4()}",
"avp_format_version": 1,
"type": type,
"record_id": record_id,
"revision_id": revision_id,
"data": data,
"annotations": annotations,
}
if attachments:
avp["_attachments"] = attachments
return avp
class BearerAuth(requests.auth.AuthBase):
# https://stackoverflow.com/a/58055668
def __init__(self, token):
self.token = token
def __call__(self, r):
r.headers["authorization"] = "Bearer " + self.token
return r
class CouchDBHelper:
def __init__(
self,
*,
user,
token,
base_url,
project_key,
include_deleted=False,
for_export=True,
bearer_token=None,
):
self.user = user
self.token = token
self.bearer_token = bearer_token
self.auth_token = None
self.base_url = base_url
self.records = []
self.project = None
self.metadata = None
self.project_id = None
self.project_metadata = {}
self.record_type_names = {}
self.record_definitions = None
self.record_fieldnames = defaultdict(list)
self.record_fieldnames_odict = defaultdict(dict)
self.include_deleted = include_deleted
self.identifiers = {}
self.forms_from_record_id = {}
project_url = f"{self.base_url}/projects/{project_key}"
"""
Initialise by getting project data, and project metadata keys and project id
"""
if user:
self.auth_token = (user, token)
if bearer_token:
self.auth_token = BearerAuth(bearer_token)
# logging.debug(f"Initialising with {project_url}")
r = requests.get(project_url, auth=self.auth_token)
r.raise_for_status()
project_data = r.json()
# logging.debug(f"Seen: {project_data}")
assert project_data.get(
"name"
), f"Project: {self.project_key} not found on server: {self.base_url}. Aborting."
assert project_data.get(
"metadata_db"
), f"Project: {self.project_key} not found on server: {self.base_url}. Aborting."
assert project_data.get(
"data_db"
), f"Project: {self.project_key} not found on server: {self.base_url}. Aborting."
self.project_id = project_data["name"]
self.project = project_data["data_db"]["db_name"]
self.metadata = project_data["metadata_db"]["db_name"]
self.project_metadata_attachments = {}
self.multivalued_fields = self.get_multivalued_fields()
self.record_count = defaultdict(int)
self.fetch_field_metadata()
# if for_export:
# self.fetch_and_flatten_records()
self.fetch_project_metadata()
def make_request_get(self, url):
# print(self.user, self.token, self.bearer_token)
# if self.user and self.token:
# print("Using user auth")
# r = requests.get(url, auth=(self.user, self.token))
# r.raise_for_status()
# return r
# if self.bearer_token:
# print("using bearer token")
# r = requests.get(url, headers= {"Authorization": f"Bearer {self.bearer_token}"})
# r.raise_for_status()
# return r
r = requests.get(url, auth=self.auth_token)
r.raise_for_status()
return r
raise ValueError("Unable to authenticate with credentials provided")
def get_multivalued_fields(self):
"""
Get field names of fields which support multiple stored values.
Right now it only supports multi-select-fields. Returns a dict of
fields and their possible values.
"""
multivalued_fields = {}
url = f"{self.base_url}/{self.metadata}/ui-specification"
# r = requests.get(url, auth=(self.user, self.token))
# r.raise_for_status()
r = self.make_request_get(url)
for element in r.json()["fields"]:
data = r.json()["fields"][element]
if data["component-parameters"].get("SelectProps", {}).get("multiple"):
# print("multi", data)
multivalued_fields[element] = {
"name": element,
"values": data["component-parameters"]
.get("ElementProps", {})
.get("options", []),
}
return multivalued_fields
def fetch_field_metadata(self):
"""
Fetch field metadata and set field_mapping, field_types, field_metadata
The human_dict_name_map allows easy mapping of field-names to human-displayed InputLabelProps.
The field_types maps field-names to their type-returned
And field_metadata is the entire field metadata for processing elsewhere.
"""
human_dict_name_map = {}
record_type_names = {}
field_types = {}
field_metadata = {}
views = {}
record_types = defaultdict(OrderedDict)
element_hierarchy = defaultdict(defaultdict)
dupe_check = defaultdict(list)
url = f"{self.base_url}/{self.metadata}/ui-specification"
# r = requests.get(url, auth=(self.user, self.token))
# r.raise_for_status()
r = self.make_request_get(url)
for record in r.json()["viewsets"]:
label = r.json()["viewsets"][record]["label"]
record_type_names[record] = label
logging.debug(f"{record=}{label=}")
for element in r.json()["fields"]:
data = r.json()["fields"][element]
human_element = (
data["component-parameters"]
.get("InputLabelProps", {})
.get("label", None)
or data["component-parameters"]
.get("FormControlLabelProps", {})
.get("label", None)
or data["component-parameters"].get("label", None)
or element
)
# print(element, human_element)
# pprint(data)
field_types[element] = data["type-returned"]
dupe_check[human_element].append(element)
human_dict_name_map[element] = human_element
field_metadata[element] = data
if data.get("meta", {}).get("annotation"):
anno_element = f"{element} annotation"
anno_human_element = (
f"{human_element} ({data['meta']['annotation_label']})"
)
field_types[anno_element] = "faims-core::String"
dupe_check[anno_human_element].append(anno_element)
human_dict_name_map[anno_element] = anno_human_element
field_metadata[anno_element] = data
if data.get("meta", {}).get("uncertainty").get("include"):
anno_element = f"{element} uncertainty"
anno_human_element = (
f"{human_element} ({data['meta']['uncertainty']['label']})"
)
# print("uncertain!", anno_element, anno_human_element)
field_types[anno_element] = "faims-core::Boolean"
dupe_check[anno_human_element].append(anno_element)
human_dict_name_map[anno_element] = anno_human_element
field_metadata[anno_element] = data
for human_element in dupe_check:
if len(dupe_check[human_element]) > 1:
# print("dupe", human_element)
for element in dupe_check[human_element]:
human_dict_name_map[element] = f"{human_element} ({element})"
# for view in r.json()['fviews']:
# #print(f"{view=}")
# views[view] = r.json()['fviews'][view]
# #pprint(views)
# for viewset in r.json()['viewsets']:
# #print(f"{viewset=}")
# for view in r.json()['viewsets'][viewset]['views']:
# #print(f"{view=}")
# for element in views[view]['fields']:
# #print(f"{element=}")
# record_types[viewset][element] = field_metadata[element]
# element_hierarchy[element] = {"viewset":viewset,
# "view":view,
# "element": field_metadata[element]}
# pprint(element_hierarchy, width=150)
self.record_definitions = record_types
self.field_mapping = human_dict_name_map
self.field_types = field_types
self.field_metadata = field_metadata
self.element_hierarchy = element_hierarchy
self.record_type_names = record_type_names
def get_records(self):
"""
Get all records for a particular project.
Returns a list of dictionaries, with each dictionary containing the
details of a specific record. Specifics of the dictionary come from the
``EncodedRecord`` interface in the datamodel, but of most interest will
be ``created`` and ``created_by`` to find out who created the record
initially.
"""
url = f"{self.base_url}/{self.project}/_find"
limit = 25
result = []
def page_results(url, bookmark=None, limit=25):
r = requests.post(
url,
auth=self.auth_token,
json={
"selector": {
"record_format_version": 1,
},
"bookmark": bookmark,
# we're going to get everything, we could do filtering as per
# https://docs.couchdb.org/en/stable/api/database/find.html
},
)
r.raise_for_status()
# print("get_records")
# pprint(r.json())
return r.json()
req = page_results(url, None, limit)
current_docs = req["docs"]
bookmark = req["bookmark"]
result = result + current_docs
while len(current_docs) >= limit:
# Note that the presence of a bookmark doesn’t guarantee that there are more results. You can to test whether you have reached the end of the result set by comparing the number of results returned with the page size requested - if results returned < limit, there are no more.
# https://docs.couchdb.org/en/stable/api/database/find.html#pagination
req = page_results(url, bookmark, limit)
current_docs = req["docs"]
bookmark = req["bookmark"]
result = result + current_docs
# print(bookmark, len(current_docs))
return result
def get_head_revisions_for_record(self, record):
"""
Get all head revisions for a particular record.
Returns a dictionary of dictionaries, with the key being the revision
id, and the value dictionary containing the details of a specific
revision. Specifics of the dictionary come from the ``Revision``
interface in the datamodel.
"""
url = f"{self.base_url}/{self.project}/_all_docs"
r = requests.post(
url,
auth=self.auth_token,
json={
"keys": record["heads"],
"include_docs": True,
},
)
r.raise_for_status()
# print("head revs")
# pprint({row["doc"]["_id"]: row["doc"] for row in r.json()["rows"]})
try:
return {row["doc"]["_id"]: row["doc"] for row in r.json()["rows"]}
except:
return None
def get_all_revisions_for_record(self, record):
"""
Get all revisions for a particular record.
Returns a dictionary of dictionaries, with the key being the revision
id, and the value dictionary containing the details of a specific
revision. Specifics of the dictionary come from the ``Revision``
interface in the datamodel.
"""
url = f"{self.base_url}/{self.project}/_all_docs"
r = requests.post(
url,
auth=self.auth_token,
json={
"keys": record["revisions"],
"include_docs": True,
},
)
r.raise_for_status()
return {row["doc"]["_id"]: row["doc"] for row in r.json()["rows"]}
def get_all_avps_for_revision(self, revision):
"""
Get all attribute value pairs (avps) for a particular revision.
Returns a dictionary of dictionaries, with the key being the avp
id, and the value dictionary containing the details of a specific
avp. Specifics of the dictionary come from the ``AttributeValuePair``
interface in the datamodel.
"""
# print(revision)
url = f"{self.base_url}/{self.project}/_all_docs"
r = requests.post(
url,
auth=self.auth_token,
json={
"keys": list(revision["avps"].values()),
"include_docs": True,
},
)
r.raise_for_status()
return {row["doc"]["_id"]: row["doc"] for row in r.json()["rows"]}
def _upload_docs_to_couchdb(self, docs):
"""
Upload a large number of documents to couchdb
This takes a list of documents to be uploaded, and returns the status of
them (which needs to be checked by the caller of this function. If there
is a non-success status given back by couchdb, this raises it.
"""
url = f"{self.base_url}/{self.project}/_bulk_docs"
r = requests.post(
url,
auth=self.auth_token,
json={
"docs": docs,
},
)
# pprint(r.json())
r.raise_for_status()
return r.json()
def _get_document_from_couchdb(self, document_id):
"""
Get a single document from coucbdb by its id.
"""
url = f"{self.base_url}/{self.project}/{document_id}"
r = self.make_request_get(url)
# r = requests.get(url, auth=(self.user, self.token))
# r.raise_for_status()
return r.json()
def _upload_document_to_couchdb(self, doc):
"""
Upload a single document to couchdb
"""
# pprint(doc)
logging.debug(pformat(doc))
url = f'{self.base_url}/{self.project}/{doc["_id"]}'
r = requests.put(url, auth=self.auth_token, json=doc)
r.raise_for_status()
return r.json()
def update_record_reference(self, record_id, base_revision_ids, new_revision_id):
"""
Update record details to reference new revision
"""
# We assume the following:
# 1. The record has not been deleted at the couchdb level (records not
# handle deletion, revisions do).
# 2. There are no intermediate revisions, that is `new_revision_id` is a
# child
record = self._get_document_from_couchdb(record_id)
revisions = set(record["revisions"])
heads = set(record["heads"])
logging.debug(pformat(revisions))
logging.debug(pformat(base_revision_ids))
if not revisions >= set(base_revision_ids):
# checking if base revision ids is a strict subset. This seems... wrong?
# @aragilar double check your logic. I've added a >= so this doesn't fail,
# but I'm not quite sure what you're cehcking here. I also had to cast
# base_revision_ids because set > list fails.
raise ValueError("The base revisions are not all existing revisions")
revisions.add(new_revision_id)
for rev_id in base_revision_ids:
heads.discard(rev_id)
heads.add(new_revision_id)
record["revisions"] = sorted(list(revisions))
record["heads"] = sorted(list(heads))
self._upload_document_to_couchdb(record)
def update_existing_record(self, new_revision, new_avps):
"""
Update a record with new data. This assumes that the revision has
already been set up with new ids
"""
failures = []
logging.debug(new_avps)
for result in self._upload_docs_to_couchdb(new_avps):
# BBS
logging.debug(f"uploaded avp {result}")
if "error" in result:
failures.append(result)
if failures:
raise RuntimeError("Some AVPs failed to upload", failures=failures)
self._upload_document_to_couchdb(new_revision)
logging.info("uploaded revision")
self.update_record_reference(
new_revision["record_id"],
new_revision["parents"],
new_revision["_id"],
)
def flatten_records(
self,
hide_empty=True,
per_field_users=False,
external_attachments=True,
iterator="text",
):
"""
Gets all records from a FAIMS3 CouchDB instance.
Given a faims object produced by the faims3couchdb class, flatten and get
the latest avps for all record types.
"""
# TODO remove empty cols for uncertainty, anntoations
# Remove per-field user details (toggleable)
records = self.fetch_records_for_roundtrip(iterator=iterator)
dataframes = {}
attachments = []
shapes = {}
for faims_record_key in records:
record_list = []
# print(f"{faims_record_key=}")
record_name = self.record_type_names[faims_record_key]
record = records[faims_record_key]
dataframe = pandas.DataFrame()
for key in record:
# logging.debug(key)
# try:
identifier = record[key]["metadata"].get("identifier") or key
for item in record[key]:
# logging.debug((item, pformat(record[key][item].keys())))
for item_key in [
"conflict_history",
"newest_avp_id",
"record_id",
"element",
"label",
"type",
]:
if item == "metadata":
record[key]["metadata"]["record_name"] = record_name
if record[key]["metadata"]["identifier"]:
identifier = record[key]["metadata"]["identifier"]
record[key]["metadata"]["updates"] = str(
record[key]["metadata"]["updates"]
)
if "relationship" in record[key]["metadata"]:
logging.debug(record[key]["metadata"])
# logging.debug(pformat(record[key]["metadata"]))
if "parents" in record[key]["metadata"]:
del record[key]["metadata"]["parents"]
else:
if not per_field_users and "metadata" in record[key][item]:
del record[key][item]["metadata"]
if (
"in_conflict" in record[key][item]
and not record[key][item]["in_conflict"]
):
del record[key][item]["in_conflict"]
if item_key in record[key][item]:
del record[key][item][item_key]
if isinstance(
record[key][item].get("data", {}).get("value"), list
):
if (
record[key][item]["data"]["value"]
and "record_label"
in record[key][item]["data"]["value"][0]
):
new_data = []
for sub_item in record[key][item]["data"]["value"]:
new_data.append(
sub_item.get(
"record_label", "label_unknown"
)
)
record[key][item]["data"]["value"] = new_data
if not record[key][item]["data"]["value"]:
record[key][item]["data"]["value"] = None
if isinstance(
record[key][item].get("data", {}).get("value"), dict
):
if "geometry" in record[key][item]["data"]["value"]:
orig = record[key][item]["data"]["value"].copy()
orig_json = geojson.loads(json.dumps(orig))
orig_json["id"] = identifier
orig_json["properties"]["title"] = identifier
orig_json["record_id"] = key
# logging.debug(orig_json)
if record_name not in shapes:
shapes[record_name] = {}
if item not in shapes[record_name]:
shapes[record_name][item] = []
shapes[record_name][item].append(orig_json)
geo_shape = shape(orig["geometry"])
record[key][item]["data"]["value"] = {}
record[key][item]["data"]["value"][
"geojson"
] = geojson.dumps(orig_json)
record[key][item]["data"]["value"][
"wkt"
] = geo_shape.wkt
record[key][item]["data"]["value"][
"y_latitude"
] = orig["geometry"]["coordinates"][1]
record[key][item]["data"]["value"][
"x_longitude"
] = orig["geometry"]["coordinates"][0]
record[key][item]["data"]["value"][
"accuracy"
] = orig["properties"]["accuracy"]
record[key][item]["data"]["value"][
"timestamp"
] = datetime.datetime.fromtimestamp(
orig["properties"].get("timestamp", 0) / 1000.0,
LOCAL_TIMEZONE,
)
elif "geojson" in record[key][item]["data"]["value"]:
pass
elif (
"record_label" in record[key][item]["data"]["value"]
):
record[key][item]["data"]["value"] = record[key][
item
]["data"]["value"]["record_label"]
else:
record[key][item]["data"]["value"] = pformat(
record[key][item]["data"]["value"]
)
# if record[key][item].get("faims_attachments"):
# logging.debug((item, record[key][item].keys()))
if external_attachments and record[key][item].get(
"attachments"
):
# logging.debug(identifier)
record_attachments = record[key][item]["attachments"]
record[key][item]["attached_files"] = []
counter = defaultdict(int)
for attachment in record_attachments:
orig_filename = attachment["filename"]
header, file = attachment["file"].split(",")
header = re.sub(
r"data:", r"", re.sub(";base64", "", header)
)
if orig_filename and "." in orig_filename:
extension = ".".join(
[
slugify(
x,
max_length=64,
allow_unicode=True,
lowercase=False,
)
for x in orig_filename.split(".")
]
)
extension = f".{extension}"
# logging.debug((orig_filename, extension))
# base_filename = f".{orig_filename}"
else:
extension = guess_extension(header)
# base_filename = ""
counter[key] += 1
# logging.debug(
# f"*****attach****\n,{key},{identifier},{counter[key]},{extension}"
# )
#
# attachment_path.mkdir(parents=True, exist_ok=True)
record_nametype = f"{record_name}"
attachment = {
"path": f"{slugify(record_nametype, max_length=128, allow_unicode=True, lowercase=False)}/{slugify(item, max_length=128, allow_unicode=True, lowercase=False)}",
"filename": f"{slugify(identifier, max_length=128, allow_unicode=True, lowercase=False)}.{slugify(item, max_length=64, allow_unicode=True, lowercase=False)}.{counter[key]}{extension}",
"data": base64.standard_b64decode(file),
}
record[key][item]["attached_files"].append(
str(
f"{attachment['path']}/{attachment['filename']}"
)
)
attachments.append(attachment)
if not record[key][item]["attached_files"]:
del record[key][item]["attached_files"]
if "attachments" in record[key][item]:
del record[key][item]["attachments"]
# logging.debug(pformat(record[key][item]))
record_list.append(record[key])
df = pandas.json_normalize(record_list)
df = df.rename(columns=lambda x: re.sub(".data.value", "", x))
# df.set_index("metadata.identifier", inplace=True)
if hide_empty:
anno_map = df.columns[df.columns.str.endswith("annotation")]
anno_isna = df[anno_map].isna().all(axis=0)
anno_map = anno_map[
anno_isna == True
] # Ok, this is only the list of droppable columns. Argh.
# logging.debug(annoisna)
# logging.debug(annomap)
df.drop(columns=anno_map, inplace=True)
# df.drop(columns=annoisna, axis=1)
cert_map = df.columns[df.columns.str.endswith("certainty")]
cert_isfalse = df[cert_map].eq(False).all(axis=0)
cert_map = cert_map[cert_isfalse]
# logging.debug(certmap)
# logging.debug(certisfalse)
df.drop(columns=cert_map, inplace=True)
# colmap = annomap | certmap
# https://stackoverflow.com/questions/24775648/element-wise-logical-or-in-pandas
# https://stackoverflow.com/questions/46864740/selecting-a-subset-using-dropna-to-select-multiple-columns
# logging.debug(colmap)
# BBS Continue. Hideempty needs to match only annotations or certainty
# df = df.filter(regex=r".*annotation$", axis=1).dropna(how="all", axis=1)
# df = df.T[~df[certmap].eq(False).all()].T
# except KeyError:
# logging.error(f"KeyError {key}")
dataframes[record_name] = df
return (dataframes, attachments, shapes)
# for key in record_avps:
# avp = record_avps[key]
# avp_element = type_rev_lookup[avp["_id"]]
# avp_type = self.field_mapping.get(
# avp_element, avp_element
# ) # try to lookup the human element name, if not return the generic element internal name
# print(f"\n\n{avp_element}")
# pprint(avp)
# if avp_element in self.multivalued_fields:
# multi_element = {}
# cols = [
# x["value"]
# for x in self.multivalued_fields[avp_element]["values"]
# ]
# for col in cols:
# # print(f"{avp_type} {col}")
# value = None
# # record_keys.add(f"{avp_type}_{col}")
# if col in avp["data"]:
# value = col
# multi_element[col] = value
# self.record_fieldnames_odict[record_type][
# f"{avp_type}_{col}"
# ] = f"{avp_element}::flattened"
# record[avp_type] = multi_element
# elif "data" in avp and avp.get("type", "") == "faims-pos::Location":
# # print("geojson")
# record[avp_type] = json.dumps(avp["data"])
# elif "data" in avp:
# record[avp_type] = avp["data"]
# else:
# record_iter.write(
# f"Flattenerror: No data in {avp['_id']}, {avp_element}, {avp_type}"
# )
# print(avp)
# raise ValueError
# # TODO Build in annotation dumping, this is temporary so I can handle annotation columns
# # if "annotations" in avp:
# # for key, annotation in avp['annotations'].items():
# # if annotation:
# # pprint(avp)
# # pprint(key)
# # pprint(annotation)
# # annotation_column_name = self.field_mapping[f"{avp_element} {key}"]
# # record[annotation_column_name] = annotation
# # print("AVP", avp_type, avp['data'])
# record_union = record | flatten(record)
# self.record_fieldnames_odict[record_type][avp_type] = avp_element
# for key in record_union.keys():
# if key not in self.record_fieldnames[record_type]:
# self.record_fieldnames[record_type].append(key)
# # if avp_element in self.record_fieldnames_odict[record_type]:
# # self.record_fieldnames_odict[record_type][avp_element].insert(0,key)
# # else:
# # self.record_fieldnames_odict[record_type] = {avp_element:[key]}
# # self.record_fieldnames[record_type] = list(self.record_fieldnames[record_type]) + list(record_union.keys())
# # print(record_type)
# # pprint(self.record_fieldnames[record_type])
# # pprint(record_union.keys())
# self.record_count[record_type] += 1
# records[record_type].append(
# record_union
# ) # 3.9 feature of dict union operator. Works exactly the way I wanted it to.
# self.records = records
# return records
def fetch_records_for_roundtrip(
self,
match_uuids=[],
disable_progress_bars=False,
include_attachments=True,
iterator="text",
):
"""
Gets all records from a FAIMS3 CouchDB instance.
Given a faims object produced by the faims3couchdb class, flatten to JSON and get
the latest avps for all record types.
"""
records = {}
logging.info(f"Exporting: {self.project}")
record_iter = tqdm(
self.get_records(), desc=f"JSON records", disable=disable_progress_bars
)
# new_revision_id = str(uuid4())
for faims_record in record_iter:
# print(faims_record)
if match_uuids and faims_record["_id"] not in match_uuids:
continue
# record_iter.write(pformat(f"{faims_record=}"))
record_type = faims_record["type"]
created = faims_record["created"]
# logging.debug(created)
created_by = faims_record["created_by"]
record_id = faims_record["_id"]
# pprint(faims_record)
# print(record_type)
# sys.exit(0)
try:
all_revisions = self.get_all_revisions_for_record(faims_record).items()
except Exception as e:
continue
try:
revisions = self.get_head_revisions_for_record(faims_record).items()
except Exception as e:
continue
"""
TODO
1. Exporter needs to obey "delete this record" (and figure out how it's being set)
2. Make sure each line is one and only one uuid
2a. That conflicts in avps are listed in each avp instead
2b. choose a value for each avp if there is only one
2c. show username, timestamp, value for each avp
3. Export label as part of each avp
"""
revision_authordate = OrderedDict()
revision_bykey = {}
isdeleted = False
for revision_key, revision in all_revisions:
# print(revision_key)
revision_authordate[revision["created"]] = {
"created_by": revision["created_by"],
"created_at": revision["created"],
"revision_key": revision_key,
"deleted": revision.get("deleted", False),
}
revision_bykey[revision_key] = {
"created_by": revision["created_by"],
"created_at": revision["created"],
}
if "relationship" in revision:
revision_bykey[revision_key]["relationship"] = revision[
"relationship"
]
for revision_key, revision in revisions:
isdeleted = revision.get("deleted", False)
if isdeleted and not self.include_deleted:
continue
identifier = ""
# Revisions... should be only one.
for revision_key, revision in revisions:
record = OrderedDict()
updated_at = revision_bykey[revision_key]["created_at"]
updated_by = revision_bykey[revision_key]["created_by"]
record["metadata"] = {
"identifier": None,
"record_type": record_type,
"updated_at": updated_at,
"updated_by": updated_by,
"in_conflict": False,
"deleted": isdeleted,
"parents": [],
"record_id": faims_record["_id"],
}
if revision_bykey[revision_key].get("relationship"):
if "parent" in revision_bykey[revision_key]["relationship"]:
this_reln = revision_bykey[revision_key]["relationship"][
"parent"
]
logging.debug(pformat(this_reln))
# pprint(this_reln)
if this_reln["relation_type_vocabPair"]:
record["metadata"]["relationship_verb"] = this_reln[
"relation_type_vocabPair"
][0]
else:
record["metadata"]["relationship_verb"] = "linked with"
record["metadata"]["relationship_parent_record_hrid"] = None
record["metadata"]["relationship_parent_record_form"] = None
record["metadata"]["relationship_parent_record_id"] = this_reln[
"record_id"
]
record["metadata"]["relationship_parent_field_id"] = this_reln[
"field_id"
]
elif "linked" in revision_bykey[revision_key]["relationship"]:
# BBS Resume
this_reln = revision_bykey[revision_key]["relationship"][
"linked"
]
print(pformat(this_reln))
logging.debug(pformat(this_reln))
record["metadata"]["relationship_verb"] = this_reln[
"relation_type_vocabPair"
][0]
record["metadata"]["relationship_linked_record_hrid"] = None
record["metadata"]["relationship_linked_record_form"] = None
record["metadata"]["relationship_linked_record_id"] = this_reln[
"record_id"
]
record["metadata"]["relationship_linked_field_id"] = this_reln[
"field_id"
]
# get_all_revisions_for_record in case historical versions are indicated
# print("revision", revision_key)
record["metadata"]["parents"].append(revision_key)
record["metadata"]["updates"] = revision_authordate
type_rev_lookup = {}
for avp_type in revision["avps"]:
type_rev_lookup[revision["avps"][avp_type]] = avp_type
# print(f"foo {record_type}")
# pprint(type_rev_lookup)
record_avps = self.get_all_avps_for_revision(revision)
# record_keys = dict.from_keys(['record_type', 'created_by', 'created_at'])
for key in record_avps:
avp = record_avps[key]
# print(avp)
avp_id = avp["_id"]
avp_element = type_rev_lookup[avp_id]
avp_type = self.field_mapping.get(
avp_element, avp_element