sync_utils.py 3.41 KB
Newer Older
1
from __future__ import print_function
abuddenberg's avatar
abuddenberg committed
2
3
__author__ = 'abuddenberg'
from os.path import exists
4
import sys
abuddenberg's avatar
abuddenberg committed
5

6
7
from domain import GcisObject

8
9
def warning(*objs):
    print("WARNING: ", *objs, file=sys.stderr)
abuddenberg's avatar
abuddenberg committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#This function is for adding images to existing figures
def move_images_to_gcis(webform_client, gcis_client, webform_url, gcis_id, report_id, subset_images=None):
    figure = webform_client.get_webform(webform_url, download_images=True)
    #Now identifiers don't need to be matched
    figure.identifier = gcis_id

    #If a subset of identifiers has been provided, only process those
    if subset_images:
        images_to_process = [image for image in figure.images if image.identifier in subset_images]
    else:
        images_to_process = figure.images

    for image in images_to_process:
        if not exists(image.local_path):
            raise Exception('Local file missing ' + image.local_path)

        if not gcis_client.image_exists(image.identifier):
28
            print('Creating image: {img}'.format(img=image.identifier))
abuddenberg's avatar
abuddenberg committed
29
30
31
            gcis_client.create_image(image, report_id=report_id, figure_id=figure.identifier)


32
33
def sync_dataset_metadata(gcis_client, datasets, skip=[]):
    for ds in [ds for ds in datasets if ds.identifier not in skip]:
abuddenberg's avatar
abuddenberg committed
34
        gcis_client.create_or_update_dataset(ds)
35
        # gcis_client.create_or_update_activity(ds.activity)
abuddenberg's avatar
abuddenberg committed
36
37


38
def realize_contributors(gcis_client, contributors):
abuddenberg's avatar
abuddenberg committed
39
40
41
42
    for cont in contributors:
        person = cont.person
        org = cont.organization

43
44
45
46
47
48
49
50
51
        if not person.id:
            name_matches = gcis_client.lookup_person(person.first_name + ' ' + person.last_name)
            if len(name_matches) == 1:
                person.id = name_matches[0][0]
            elif len(name_matches) == 0:
                warning('No ID found for ' + person.first_name + ' ' + person.last_name)
            else:
                warning('Ambiguous results for ' + person.first_name + ' ' + person.last_name)
                warning(name_matches)
52

53
54
        if org and org.identifier in (None, '') and org.name not in (None, ''):
            warning('No ID found for ' + org.name)
55

56
    #Check if we missed any organizations in our hardcoding...
57
58
    if not all(map(lambda c: c.organization is None or c.organization.identifier is not None, contributors)):
        warning('Missing organizations: ', contributors)
59

60
61
62
63
64

def realize_parents(gcis_client, parents):
    for parent in parents:
        # print parent.publication_type_identifier, parent.label
        if parent.url:
65
            print(' '.join(('Using hint for', parent.publication_type_identifier, parent.label)))
66
67
68
69
70
            continue

        parent_matches = gcis_client.lookup_publication(parent.publication_type_identifier, parent.label)

        if len(parent_matches) == 1:
71
72
73
74
75
76
            matched_id, matched_name = parent_matches[0]
            parent.url = '/{type}/{id}'.format(type=parent.publication_type_identifier, id=matched_id)
            # Need the ability to dynamically identify and retrieve an instance of the parent publication.
            # Here's a generic, for the time being.
            parent.publication = GcisObject({'identifier': matched_id})

77
        elif len(parent_matches) == 0:
78
            warning(' '.join(('No ID found for', parent.publication_type_identifier, parent.label)))
79
        else:
80
81
            warning(' '.join(('Ambiguous results for', parent.publication_type_identifier, parent.label)))
            warning(parent_matches)