def populate_figure(fig_json): f = Figure({}) try: if fig_json['graphics_title'].startswith('ES'): title_fields = fig_json['graphics_title'].split('. ') title = ' '.join(title_fields[1:]) f.ordinal = re.search('\d+', title_fields[0]).group(0) else: figure_num, title = parse_title(fig_json['graphics_title']) if figure_num and figure_num.startswith('TSD'): f.ordinal = figure_num.split('.')[1] else: f.figure_num = figure_num if figure_num else None f.title = title f.identifier = fig_json['figure_id'] if fig_json[ 'figure_id'] else re.sub('\W', '_', f.title).lower() f.create_dt = fig_json['graphics_create_date'].strip() if any(fig_json['period_record']): f.time_start, f.time_end = [ d.strip() for d in fig_json['period_record'] ] f.lat_min, f.lat_max, f.lon_min, f.lon_max = fig_json['spatial_extent'] except Exception, e: warning('Figure exception: ', e) traceback.print_exc()
def test_domain_as_json(): f = Figure(json.loads(test_figure_json)) assert f.original['chapter']['identifier'] == 'our-changing-climate' assert f.original['images'] not in (None, '') assert f.original['uri'] not in (None, '') #Make sure fields specifically omitted are actually omitted fig_json_out = json.loads(f.as_json()) assert all([omitted_key not in fig_json_out for omitted_key in ['chapter', 'images', 'uri', 'href']]) i = Image(json.loads(test_image_json)) assert i.original['uri'] not in (None, '') assert i.original['href'] not in (None, '') img_json_out = json.loads(i.as_json()) assert all([omitted_key not in img_json_out for omitted_key in ['uri', 'href']]) #Make sure merges work f2_json = json.loads(test_figure_json) f2_json['caption'] = '' f2 = Figure(f2_json) assert f2.caption in ('', None) f2.merge(f) assert f2.caption == f.caption
def test_domain_as_json(): f = Figure(json.loads(test_figure_json)) assert f.original['chapter']['identifier'] == 'our-changing-climate' assert f.original['images'] not in (None, '') assert f.original['uri'] not in (None, '') #Make sure fields specifically omitted are actually omitted fig_json_out = json.loads(f.as_json()) assert all([ omitted_key not in fig_json_out for omitted_key in ['chapter', 'images', 'uri', 'href'] ]) i = Image(json.loads(test_image_json)) assert i.original['uri'] not in (None, '') assert i.original['href'] not in (None, '') img_json_out = json.loads(i.as_json()) assert all( [omitted_key not in img_json_out for omitted_key in ['uri', 'href']]) #Make sure merges work f2_json = json.loads(test_figure_json) f2_json['caption'] = '' f2 = Figure(f2_json) assert f2.caption in ('', None) f2.merge(f) assert f2.caption == f.caption
def test_parent(): f = Figure(json.loads(test_figure_json)) assert f.parents == [] ds = Dataset(json.loads(test_dataset_json)) assert isinstance(ds, Dataset) p = Parent.from_obj(ds) assert isinstance(p, Parent) assert isinstance(p.publication, Dataset) f.add_parent(p) assert len(f.parents) == 1 assert f.parents[0] == p assert f.parents[0].relationship == 'prov:wasDerivedFrom' assert f.parents[0].publication_type_identifier == 'dataset' assert f.parents[0].url == '/dataset/cddv2' assert f.parents[0].label == 'Climate Division Database Version 2'
def test_chapter_parsing(): webform_fig = Figure(json.loads(webform_json_precip), trans=trans.FIG_TRANSLATIONS) gcis_fig = Figure(json.loads(test_figure_json)) assert isinstance(webform_fig.ordinal, int) assert webform_fig.figure_num == '2.17' assert webform_fig.ordinal == 17 assert webform_fig.chapter == 2 merged_figure = webform_fig.merge(gcis_fig) #FYI, these are identical; I just wanted the variable name to reflect the merge assert id(merged_figure) == id(webform_fig) assert isinstance(gcis_fig.chapter, Chapter) assert merged_figure.figure_num == '2.17' assert merged_figure.ordinal == 17 assert isinstance(merged_figure.chapter, Chapter) assert merged_figure.chapter.number == 2
def populate_figure(fig_json): f = Figure({}) try: f.figure_num, f.title = parse_title(fig_json['graphics_title']) f.identifier = fig_json['figure_id'] if fig_json[ 'figure_id'] else re.sub('\W', '_', f.title).lower() f.create_dt = fig_json['graphics_create_date'] f.time_start, f.time_end = fig_json['period_record'] f.lat_min, f.lat_max, f.lon_min, f.lon_max = fig_json['spatial_extent'] f.remote_path = fig_json['filepath'] except Exception, e: print 'Figure exception: ', e
def populate_figure(fig_json): f = Figure({}) try: f.figure_num, f.title = parse_title(fig_json['graphics_title']) f.identifier = fig_json['figure_id'] if fig_json['figure_id'] else re.sub('\W', '_', f.title).lower() f.create_dt = fig_json['graphics_create_date'] f.time_start, f.time_end = fig_json['period_record'] f.lat_min, f.lat_max, f.lon_min, f.lon_max = fig_json['spatial_extent'] f.remote_path = fig_json['filepath'] except Exception, e: print 'Figure exception: ', e
def populate_figure(fig_json): f = Figure({}) try: figure_num, title = parse_title(fig_json['graphics_title']) f.ordinal = figure_num if figure_num else None f.title = title f.identifier = fig_json['figure_id'] if fig_json[ 'figure_id'] else re.sub('\W', '_', f.title).lower() f.create_dt = fig_json['graphics_create_date'].strip() if any(fig_json['period_record']): f.time_start, f.time_end = [ d.strip() for d in fig_json['period_record'] ] f.lat_min, f.lat_max, f.lon_min, f.lon_max = fig_json['spatial_extent'] except Exception, e: warning('Figure exception: ', e) traceback.print_exc()
def test_domain(): f = Figure(json.loads(test_figure_json)) assert isinstance(f, Gcisbase) assert isinstance(f, Figure) assert len(f.images) == 11 assert all([isinstance(i, Image) for i in f.images]) assert isinstance(f.chapter, Chapter) assert f.chapter.identifier == 'our-changing-climate' i = Image(json.loads(test_image_json)) assert isinstance(i, Image) assert len(f.contributors) == 2 assert all([isinstance(cont, Contributor) for cont in f.contributors]) empty_img = Image({}) assert empty_img.identifier is None