def practice_cancel(self, impulse: CancelSkillBuildJobImpulse, config: Union[Callable[[CanceledSkillBuildJobImpulseReq], None], None] = None) -> Observable[ CanceledSkillBuildJobImpulseRes]: if config is not None: practice_req: Callable[[PracticeReq], None] = lambda p: p.cancel(impulse, config) mutation_req: Callable[[MutationReq], None] = lambda m: m.practice(practice_req) mutation_res: Callable[[MutationRes], CanceledSkillBuildJobImpulseRes] = lambda ms: ms.practice.cancel obs = rx.from_callable(lambda: self.client.mutation(GaiaRequest.mutation(mutation_req)), self._scheduler) return mapM(obs, mutation_res) else: def job_cfg(b: SkillBuildJobReq): b.reference() b.name() b.tag() b.skill_ref() b.tenant_id() b.created() def r_cfg(x: CanceledSkillBuildJobImpulseReq): x.id() x.data(job_cfg) practice_req: Callable[[PracticeReq], None] = lambda p: p.cancel(impulse, r_cfg) mutation_req: Callable[[MutationReq], None] = lambda m: m.practice(practice_req) mutation_res: Callable[[MutationRes], CanceledSkillBuildJobImpulseRes] = lambda ms: ms.practice.cancel obs = rx.from_callable(lambda: self.client.mutation(GaiaRequest.mutation(mutation_req)), self._scheduler) return mapM(obs, mutation_res)
def introspect_build_jobs(self, tenant_id: Uuid, config: Union[Callable[[SkillBuildJobReq], None], None] = None) -> \ Observable[SkillBuildJobRes]: if config is not None: introspect_req: Callable[[IntrospectionReq], None] = lambda irq: irq.build_jobs(tenant_id, config) query_req: Callable[[QueryReq], None] = lambda qr: qr.introspect(introspect_req) query_res: Callable[[QueryRes], List[SkillBuildJobRes]] = lambda qs: qs.introspect.build_jobs obs = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return flat_mapQ(obs, query_res) else: def failure_cfg(f: FailureReq): f.failure_type() f.exit_code() f.affected_container() f.reason() def status_cfg(b: SkillStatusReq): b.health() b.pending() b.running() b.failures(failure_cfg) def job_cfg(b: SkillBuildJobReq): b.reference() b.name() b.tag() b.tenant_id() b.created() b.status(status_cfg) introspect_req: Callable[[IntrospectionReq], None] = lambda irq: irq.build_jobs(tenant_id, job_cfg) query_req: Callable[[QueryReq], None] = lambda qr: qr.introspect(introspect_req) query_res: Callable[[QueryRes], List[SkillBuildJobRes]] = lambda qs: qs.introspect.build_jobs obs = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return flat_mapQ(obs, query_res)
def build(self) -> Observable[SkillBuildResponse]: return rx.from_callable(lambda: self._client.post_json({'uri': self._uri}, url_postfix="/skill/build"), self._scheduler) \ .pipe( ops.map(lambda r: json.loads(r.content)), ops.map(lambda r: SkillBuildResponse(r["reference"])) )
def logs(self, number_of_lines: int) -> Observable[str]: return rx.from_callable(lambda: self._client.post_json({'uri': self._uri, 'numberOfLines': number_of_lines}, url_postfix="/skill/logs"), self._scheduler) \ .pipe( ops.map(lambda r: json.loads(r.content)), ops.flat_map(lambda r: rx.from_iterable(r['logLines'])) )
def perceive(self, config: Callable[[PerceptionReq], None]) -> Observable[PerceptionRes]: mutation_req: Callable[[MutationReq], None] = lambda x: x.perceive(config) mutation_res: Callable[[MutationRes], PerceptionRes] = lambda x: x.perceive observable = rx.from_callable(lambda: self.client.mutation(GaiaRequest.mutation(mutation_req)), self._scheduler) return mapM(observable, mutation_res)
def add(self, file_name: str, content: Union[bytes, Path], override: bool = False, config: DataRefRequestConfig = None) -> Observable['DataRef']: r"""Uploads a file with a given name and content to the storage. :param file_name: Name of the file to be uploaded. :param content: Content of the file to be uploaded in bytes or a Path pointing to a file location. :param override: (optional) Flag to specify if existing files should be overwritten. If set to false, the method will throw an exception when trying to overwrite an existing file. :param config: (optional) Interface which currently only contains onUploadProgress callback :return: :class:`Observable[DataRef]` object: Reference to the newly uploaded file :exception HttpError: Error thrown if the upload fails due to override being set to false. """ file_uri = DataRef.concat_uri(self.uri, file_name) if isinstance(content, bytes): upload_content = DataUploadContentBytes(content) else: upload_content = DataUploadContentPath(content) number_of_chunks = ceil(upload_content.total_size_in_bytes / CHUNK_SIZE) upload = DataUpload(file_uri, upload_content, number_of_chunks, override, config) def execute_upload() -> DataRef: self.logger.debug(f"Started upload to uri {file_uri}") new_file_data_ref = upload.execute(self.client, self.scheduler) self.logger.debug(f"Finished upload to uri {file_uri}") return new_file_data_ref return rx.from_callable(execute_upload).pipe(ops.do_action(on_error=lambda: upload_content.close(), on_completed=lambda: upload_content.close()))
def retrieve_knowledge(self, config: Callable[[KnowledgeReq], None]) -> Observable[KnowledgeRes]: retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.knowledge(config) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], KnowledgeRes] = lambda x: x.retrieve.knowledge observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return mapQ(observable, query_res)
def retrieve_identity(self, identityId: Uuid, config: Callable[[IdentityReq], None]) -> Observable[IdentityRes]: identities_req: Callable[[IdentityReq], None] = lambda x: x.identity(identityId, config) retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.knowledge(identities_req) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], IdentityRes] = lambda x: x.retrieve.knowledge.identity observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return mapQ(observable, query_res)
def retrieve_edge(self, source: Uuid, edgeId: Uuid, config: Callable[[EdgeReq], None]) -> Observable[EdgeRes]: knowledge_req: Callable[[KnowledgeReq], None] = lambda x: x.edge(source, edgeId, config) retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.knowledge(knowledge_req) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], EdgeRes] = lambda x: x.retrieve.knowledge.edge observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return mapQ(observable, query_res)
def set_band_names(band_names: list, files: Union[str, list]) -> Observable: def action(): set_band_metadata('BAND_NAME', band_names, files) return defer(lambda _: gdal_queue.enqueue( from_callable(action), retries=3, description='set_band_names({}, {})'.format(band_names, files)))
def get_internal_temperature(self) -> Observable: """ Upon subscription, use side effect to send a query command, and then register the resolver to read value :return: """ return rx.from_callable( lambda: self.command_queue.on_next("IN_PV_00\r")).pipe( self._serial_reading_operator())
def perceive_data(self, impulse: PerceiveDataImpulse) -> Observable[PerceivedImpulse]: perceive_data_req: Callable[[PerceptionReq], None] = lambda x: x.perceive_data(impulse, lambda e: e.id()) mutation_req: Callable[[MutationReq], None] = lambda x: x.perceive(perceive_data_req) mutation_res: Callable[[MutationRes], PerceivedImpulse] = lambda x: x.perceive.perceive_data observable = rx.from_callable(lambda: self.client.mutation(GaiaRequest.mutation(mutation_req)), self._scheduler) return mapM(observable, mutation_res)
def retrieve_code(self, identityId: Uuid, reference: Uuid, config: Callable[[CodeReq], None]) -> Observable[ CodeRes]: code_req: Callable[[CodeReq], None] = lambda x: x.code(identityId, reference, config) retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.knowledge(code_req) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], CodeRes] = lambda x: x.retrieve.knowledge.code observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return mapQ(observable, query_res)
def retrieve_codes(self, identityId: Uuid, config: Callable[[CodeReq], None], limit: int, offset: int) -> \ Observable[CodeRes]: code_req: Callable[[CodeReq], None] = lambda x: x.codes(identityId, limit, offset, None, None, config) retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.knowledge(code_req) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], CodeRes] = lambda x: x.retrieve.knowledge.codes observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return flat_mapQ(observable, query_res)
def mapper_to_observable(value): def initializing_mapper(): sepal.drive.InitializeThread(credentials) return mapper(value) return of(True).pipe( delay(0.1, TimeoutScheduler()), flat_map(lambda _: from_callable(initializing_mapper, _drive_executions.scheduler)))
def retrieve_fulfilment(self, identityId: Uuid, reference: Uuid, config: Callable[[FulfilmentReq], None]) -> \ Observable[FulfilmentRes]: fulfilment_req: Callable[[FulfilmentReq], None] = lambda x: x.fulfilment(identityId, reference, config) retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.knowledge(fulfilment_req) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], FulfilmentRes] = lambda x: x.retrieve.knowledge.fulfilment observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return mapQ(observable, query_res)
def retrieve_identities(self, config: Callable[[IdentityReq], None], limit: int, offset: int) -> Observable[ IdentityRes]: identities_req: Callable[[IdentityReq], None] = lambda x: x.identities(limit, offset, None, None, config) retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.knowledge(identities_req) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], IdentityRes] = lambda x: x.retrieve.knowledge.identities observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return flat_mapQ(observable, query_res)
def retrieve_behaviour(self, identityId: Uuid, reference: Uuid, config: Callable[[BehaviourReq], None]) -> \ Observable[BehaviourRes]: behaviour_req: Callable[[BehaviourReq], None] = lambda x: x.behaviour(identityId, reference, config) retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.knowledge(behaviour_req) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], BehaviourRes] = lambda x: x.retrieve.knowledge.behaviour observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return mapQ(observable, query_res)
def execute(credentials, mapper: Mapper = None, retries: int = 3, description: str = None): return rx.pipe( enqueue(credentials, queue=_ee_executions, mapper=lambda value: from_callable(lambda: mapper(value)), description=description, retries=retries))
def from_process(worker: Callable, *args, executor: ProcessPoolExecutor = None, **kwargs): executor = executor if executor else default_process_executor def run_as_process(): future = executor.submit(worker, *args, **kwargs) return future.result() return rx.from_callable(run_as_process, NewThreadScheduler())
def as_bytes(self) -> Observable[bytes]: r"""Downloads the file at the current uri and returns it as bytes. :return: :class:`Observable[bytes]` object: File as bytes. :exception HttpError: Error thrown if the download operation fails. """ return rx.from_callable( lambda: self.client.post_json(BinaryReadImpulse(self.uri), url_postfix="/data/source"), self.scheduler) \ .pipe( ops.map(lambda response: response.content))
def evaluate(self, payload: dict, contract: str = None) -> Observable[SkillEvaluation]: request = {'uri': self._uri, 'payload': payload} if contract is not None: request['contract'] = contract return rx.from_callable( lambda: self._client.post_json(request, url_postfix="/skill/evaluate"), self._scheduler) \ .pipe( ops.map(lambda response: json.loads(response.content)), ops.map(lambda d: SkillEvaluation(d)))
def retrieve_behaviour_execution(self, identity_id: Uuid, process_instance_id: Uuid, config: Callable[[BehaviourExecutionDetailReq], None]) \ -> Observable[BehaviourExecutionDetailRes]: behaviour_exec_query: Callable[[BehaviourExecutionDetailRes], None] = lambda x: x.behaviour_execution( identity_id, process_instance_id, config) retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.experience(behaviour_exec_query) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], BehaviourExecutionDetailRes] = lambda \ x: x.retrieve.experience.behaviour_execution observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return mapQ(observable, query_res)
def list(self) -> Observable[List[FileListing]]: r"""Lists all files sharing the current uri as prefix. :return: :class:`Observable[List[FileListing]]` object: List of files that share the current uri as prefix. :exception HttpError: Error thrown if the list operation fails. """ def execute_list() -> List[FileListing]: self.logger.debug(f"Started list at uri {self.uri}") response = self.client.post_json(ListFilesImpulse(self.uri), "/data/list").json() self.logger.debug(f"Completed list at uri {self.uri}") return [FileListing(listing) for listing in response] return rx.from_callable(execute_list)
def retrieve_behaviour_executions(self, identity_id: Uuid, config: Callable[[BehaviourExecutionReq], None], limit: int = None, offset: int = None, startDate: str = None, endDate: str = None) \ -> Observable[BehaviourExecutionRes]: executions_req: Callable[[BehaviourExecutionReq], None] = lambda x: x.behaviour_executions(identity_id, limit, offset, startDate, endDate, config) retrieval_req: Callable[[RetrievalReq], None] = lambda x: x.experience(executions_req) query_req: Callable[[QueryReq], None] = lambda x: x.retrieve(retrieval_req) query_res: Callable[[QueryRes], BehaviourExecutionRes] = lambda x: x.retrieve.experience.behaviour_executions observable = rx.from_callable(lambda: self.client.query(GaiaRequest.query(query_req)), self._scheduler) return flat_mapQ(observable, query_res)
def build_vrt(destination: str, files: Union[str, list]) -> Observable: def action(): if isinstance(files, str): files_ = glob(files) else: files_ = files gdal.SetConfigOption('VRT_SHARED_SOURCE', '0') vrt = gdal.BuildVRT(destination, files_) if vrt: vrt.FlushCache() return defer(lambda _: gdal_queue.enqueue(from_callable(action), retries=3, description='build_vrt({}, {})'. format(destination, files)))
def export(self, identity_id: str) -> Observable[bytes]: r"""Exports the identity with the current identity ID and returns it as bytes. :param identity_id: ID of the identity to be exported. :return: :class:`Observable[bytes]` object: Identity as bytes. :exception HttpError: Error thrown if the export operation fails. """ self._logger.debug(f"Started export of {identity_id}") return rx.from_callable( lambda: self._client.post_json(IdentitySourceRequestImpulse(identity_id), url_postfix="/identity/source"), self._scheduler) \ .pipe( ops.map(lambda response: response.content))
def remove_file(self, file_name: str) -> Observable[FileRemoved]: r"""Removes the specified file in the directory at the current uri. :param file_name: Name of the file at the current uri to be removed. :return: :class:`Observable[FileRemoved]` object: Response to remove request that contains information if the file existed. :exception HttpError: Error thrown if the remove operation fails. """ def execute_remove_file() -> FileRemoved: file_uri = DataRef.concat_uri(self.uri, file_name) self.logger.debug(f"Started removing file at {file_uri}") response = self.client.post_json(RemoveFileImpulse(file_uri), "/data/remove").json() self.logger.debug(f"Completed removing file at {file_uri}") return FileRemoved(response) return rx.from_callable(execute_remove_file)
def as_file(self, file_path: str = f"SDK-DataRef.as_file-{time.time()}") -> Observable[Path]: r"""Downloads the file at the current uri and saves it at specified/returned location. :param file_path: Location where the file should be saved. :return: :class:`Observable[bytes]` object: File as bytes. :exception HttpError: Error thrown if the download operation fails. """ def send_request_and_copy(fp: str) -> Path: response: Response = self.client.post_json(BinaryReadImpulse(self.uri), url_postfix="/data/source") # https://stackoverflow.com/a/39217788/4644044 with open(fp, 'wb+') as f: shutil.copyfileobj(response.raw, f) return Path(fp) return rx.from_callable(lambda: send_request_and_copy(file_path), self.scheduler)
def preserve_delete_edges(self, impulses: List[DeleteEdgeImpulse]) -> Observable[DeletedEdgeImpulse]: def data_req(x): x.source() x.edge_id() def edge_req(x): x.id() x.data(data_req) delete_edges: Callable[[PreservationReq], None] = lambda x: x.delete(lambda e: e.edges(impulses, edge_req)) mutation_req: Callable[[MutationReq], None] = lambda x: x.preserve(delete_edges) mutation_res: Callable[[MutationRes], DeletedEdgeImpulse] = lambda x: x.preserve.delete.edges observable = rx.from_callable(lambda: self.client.mutation(GaiaRequest.mutation(mutation_req)), self._scheduler) return flat_mapM(observable, mutation_res)