sync_utils.py 2.17 KB
Newer Older
abuddenberg's avatar
abuddenberg committed
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
__author__ = 'abuddenberg'
from os.path import exists


#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):
            print 'Creating image: {img}'.format(img=image.identifier)
            gcis_client.create_image(image, report_id=report_id, figure_id=figure.identifier)


26
27
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
28
        gcis_client.create_or_update_dataset(ds)
29
        # gcis_client.create_or_update_activity(ds.activity)
abuddenberg's avatar
abuddenberg committed
30
31


32
def realize_contributors(gcis_client, contributors):
abuddenberg's avatar
abuddenberg committed
33
34
35
36
    for cont in contributors:
        person = cont.person
        org = cont.organization

37
38
39
40
        #Hack for F. Chapin
        if '.' in person.first_name:
            person.first_name = ''

41
42
43
44
        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:
45
            print '\t', 'No ID found for ' + person.first_name + ' ' + person.last_name
abuddenberg's avatar
abuddenberg committed
46
        else:
47
            print '\t', 'Ambiguous results for ' + person.first_name + ' ' + person.last_name
48
49
            print '\t\t', name_matches

50
        if org.identifier in (None, '') and org.name not in (None, ''):
51

52
            print 'No ID found for ' + org.name
53

54
55
56
    #Check if we missed any organizations in our hardcoding...
    if not all(map(lambda c: c.organization.identifier is not None, contributors)):
        print contributors
57