def add_partial_invention(session): """ Add a partial invention to 'Inventions made by David Edward Hughes' This is used to test the find_entry behavior """ hughes = api.find_person(session, last_name='Hughes', return_iterator=True).one() e3 = models.Entry(title='Warp drive', abstract='Started, but never finished', location="SRID=4326;POINT (51.505946 -0.132951)", license_id=5, variable_id=1, is_partial=True) e3.contributors.append( models.PersonAssociation(relationship_type_id=1, person=hughes, order=1)) inventions = api.find_group(session, title='Awesome inventions', return_iterator=True).one() inventions.entries.append(e3) try: session.add(e3) session.commit() except Exception as e: session.rollback() raise e assert len(inventions.entries) == 3 return True
def find_composite_group(session): """ Find the Telegraph and Microphone group. """ group = api.find_group(session, title="Awesome inventions")[0] assert 'by David Edward Hughes' in group.description return group
def check_project_group(session): """ get the group. Check UUID exists and two entries are connected """ dummies = api.find_group(session, title="Dumm*")[0] assert len(dummies.uuid) == 36 assert len(dummies.entries) == 2 return True
def check_result_set_from_group(session): """ Create a ImmutableResultSet from a group """ result = api.find_group(session, title='Awesome inventions', as_result=True)[0] # assert like above assert len(result.uuids) == 3 return True
def find_by_project(session): dummies = api.find_group(session, title="Dumm%")[0] # create a project project = api.add_project(session, entry_ids=[e.id for e in dummies.entries], title='Awesome Project', description='Nice project for testing purposes') for pars in [ dict(project=project), dict(project=project.id), dict(project='Awesome%') ]: entries = api.find_entry(session, **pars) assert len(entries) == 2 assert set([e.title for e in entries]) == set(['Dummy 1', 'Dummy 2']) return True
def get_uuid(session: Session, uuid: str, not_found='raise'): """ .. versionadded:: 0.1.13 Return the Metacatalog object of given version 4 UUID. The supported objects are: - Entry - EntryGroup - Keyword .. versionadded:: 0.2.7 - Person """ # check if an Entry exists entry = api.find_entry(session, uuid=uuid) if entry is not None: return entry # check if Entrygroup exists group = api.find_group(session, uuid=uuid) if group is not None: return group # check if a Person exists person = api.find_person(session, uuid=uuid) if person is not None: return person # handle keyword keyword = api.find_keyword(session, uuid=uuid) if keyword is not None: return keyword if not_found == 'raise': raise NoResultFound("The UUID='%s' was not found." % uuid) else: return None
def check_composite_raises_error(session): with pytest.raises(TypeError) as excinfo: dummies = api.find_group(session, title="Dumm%")[0] api.find_entry(session, project=dummies) return "has to be of type 'Project'" in str(excinfo.value)
def find(args): # get the session session = connect(args) # get the entity entity = args.entity # set by to an empty list if not given if args.by is None: args.by = [] # parse out the BY arguments kwargs = dict() for by in args.by: # if len(by) != 2: kwargs[by[0]] = by[1] # switch entity if entity.lower() == 'units' or entity.lower() == 'unit': results = api.find_unit(session, **kwargs) elif entity.lower() == 'variables' or entity.lower() == 'variable': results = api.find_variable(session, **kwargs) elif entity.lower() == 'licenses' or entity.lower() == 'license': results = api.find_license(session, **kwargs) elif entity.lower() == 'keywords' or entity.lower() == 'keyword': results = api.find_keyword(session, **kwargs) elif entity.lower() == 'roles' or entity.lower() == 'role': results = api.find_role(session, **kwargs) elif entity.lower() == 'persons' or entity.lower() == 'person': results = api.find_person(session, **kwargs) elif entity.lower() == 'group_types' or entity.lower() == 'group_type': results = api.find_group_type(session, **kwargs) elif entity.lower() == 'groups' or entity.lower() == 'group': results = api.find_group(session, **kwargs) elif entity.lower() == 'entries' or entity.lower() == 'entry': results = api.find_entry(session, **kwargs) elif entity.lower() == 'thesaurus': results = api.find_thesaurus(session, **kwargs) else: cprint(args, 'Oops. Finding %s is not supported.' % entity) exit(0) # switch the output if args.json: obj = [serialize(r) for r in results] cprint(args, json.dumps(obj, indent=4)) elif args.csv: obj = [flatten(serialize(r)) for r in results] f = io.StringIO(newline='') colnames = set([n for o in obj for n in o.keys()]) writer = csv.DictWriter(f, fieldnames=colnames, quotechar='"', quoting=csv.QUOTE_NONNUMERIC, lineterminator='\r') writer.writeheader() for o in obj: writer.writerow(o) f.seek(0) cprint(args, f.getvalue()) else: # stdOut for result in results: cprint(args, result)
def find(args): # get the session session = connect(args) # get the entity entity = args.entity # set by to an empty list if not given if args.by is None: args.by = [] # parse out the BY arguments kwargs=dict() for by in args.by: # if len(by) != 2: kwargs[by[0]] = by[1] # switch entity if entity.lower() == 'units' or entity.lower() == 'unit': results = api.find_unit(session, **kwargs) elif entity.lower() == 'variables' or entity.lower() == 'variable': results = api.find_variable(session, **kwargs) elif entity.lower() == 'licenses' or entity.lower() == 'license': results = api.find_license(session, **kwargs) elif entity.lower() == 'keywords' or entity.lower() == 'keyword': results = api.find_keyword(session, **kwargs) elif entity.lower() == 'roles' or entity.lower() == 'role': results = api.find_role(session, **kwargs) elif entity.lower() == 'persons' or entity.lower() == 'person': results = api.find_person(session, **kwargs) elif entity.lower() == 'group_types' or entity.lower() == 'group_type': results = api.find_group_type(session, **kwargs) elif entity.lower() == 'groups' or entity.lower() == 'group': results = api.find_group(session, **kwargs) elif entity.lower() == 'entries' or entity.lower() == 'entry': if args.include_partial: kwargs['include_partial'] = True results = api.find_entry(session, **kwargs) elif entity.lower() == 'thesaurus': results = api.find_thesaurus(session, **kwargs) else: cprint(args, 'Oops. Finding %s is not supported.' % entity) exit(0) if args.export is not None and args.export != '': # only entry and group can be exported if entity.lower() not in ('entry', 'group'): cprint(args, 'Can only export entity=Entry and entity=Group') return # get the fmt and path path = args.export fmt = args.export.split('.')[-1] fmt = 'netCDF' if fmt == 'nc' else fmt # check amount of results if len(results) == 1: results[0].export(path=path, fmt=fmt) cprint(args, f'Wrote {path}.') else: for i, result in enumerate(results): path = '.'.join([*args.export.split('.')[:-1], f'_{i}', args.export.split('.')[-1]]) result.export(path=path, fmt=fmt) cprint(args, f'Wrote {len(results)} files.') return # switch the output if args.json: obj = [serialize(r) for r in results] cprint(args, json.dumps(obj, indent=4)) elif args.csv: obj = [flatten(serialize(r)) for r in results] f = io.StringIO(newline='') colnames = set([n for o in obj for n in o.keys()]) writer = csv.DictWriter(f, fieldnames=colnames, quotechar='"', quoting=csv.QUOTE_NONNUMERIC, lineterminator='\r') writer.writeheader() for o in obj: writer.writerow(o) f.seek(0) cprint(args, f.getvalue()) else: # stdOut for result in results: cprint(args, result)