Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
 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"]))
     )
Exemplo n.º 4
0
 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']))
     )
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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()))
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
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)))
Exemplo n.º 11
0
 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())
Exemplo n.º 12
0
    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)
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
    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)
Exemplo n.º 15
0
    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)))
Exemplo n.º 16
0
    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)
Exemplo n.º 17
0
    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)
Exemplo n.º 18
0
    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)
Exemplo n.º 19
0
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))
Exemplo n.º 20
0
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())
Exemplo n.º 21
0
    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))
Exemplo n.º 22
0
 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)))
Exemplo n.º 23
0
    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)
Exemplo n.º 24
0
    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)
Exemplo n.º 25
0
    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)
Exemplo n.º 26
0
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)))
Exemplo n.º 27
0
    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))
Exemplo n.º 28
0
    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)
Exemplo n.º 29
0
    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)
Exemplo n.º 30
0
    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)