def test_inject_client(): c = Client(NEUPRINT_SERVER, DATASET) c2 = Client(NEUPRINT_SERVER, DATASET) set_default_client(c) @inject_client def f(*, client): return client # Uses default client unless client was specified assert f() is c assert f(client=c2) is c2 with pytest.raises(AssertionError): # Wrong signature -- asserts @inject_client def f2(client): pass
def setup_dataset(dataset, published): """ Insert or update a data set in Mongo Keyword arguments: dataset: data set published: True=public, False=private Returns: last_uid: last UID assigned action: what to do with bodies in this data set (ignore, insert, or update) """ LOGGER.info("Initializing Client for %s %s", ARG.SERVER, dataset) npc = Client(ARG.SERVER, dataset=dataset) set_default_client(npc) result = fetch_meta(client=npc) if ':' in dataset: name, version = dataset.split(':') version = version.replace('v', '') else: name = dataset version = '' coll = DBM.emDataSet check = coll.find_one({ "name": name, "version": version, "published": published }) action = 'ignore' if not check: if result['dataset'] not in GENDER: LOGGER.error("%s does not have a gender defined", result['dataset']) sys.exit(-1) payload = { "class": "org.janelia.model.domain.flyem.EMDataSet", "ownerKey": "group:flyem", "readers": ["group:flyem"], "writers": ["group:flyem"], "name": result['dataset'], "version": version, "gender": GENDER[result['dataset']], "creationDate": to_datetime(result['lastDatabaseEdit']), "updatedDate": to_datetime(result['lastDatabaseEdit']), "active": True, "published": published } if published: payload['readers'] = ["group:flyem", "group:workstation_users"] last_uid = generate_uid() payload['_id'] = last_uid LOGGER.debug(payload) if ARG.WRITE: post_id = coll.insert_one(payload).inserted_id else: post_id = last_uid if post_id != last_uid: LOGGER.critical("Could not insert to Mongo with requested _id") sys.exit(-1) LOGGER.info("Inserted data set %s (UID: %s, datetime: %s)", dataset, post_id, result['lastDatabaseEdit']) action = 'insert' else: LOGGER.info("%s already exists in Mongo (UID: %s)", dataset, check['_id']) last_uid = check['_id'] neuprint_dt = to_datetime(result['lastDatabaseEdit']) if neuprint_dt > check['updatedDate'] or ARG.FORCE: LOGGER.warning("Update required for %s (last changed %s)", dataset, result['lastDatabaseEdit']) payload = {"updatedDate": datetime.now(), "active": True} if ARG.WRITE: coll.update_one({"_id": check['_id']}, {"$set": payload}) action = 'update' else: LOGGER.info("No update required for %s (last changed %s)", dataset, check['updatedDate']) return last_uid, action
def client(): c = Client(NEUPRINT_SERVER, DATASET) set_default_client(c) assert default_client() is c return c