def calc_gen(self, gen): fitnessless = gen.get_fitlessness() if len(fitnessless) == 0: return tick_tack = ReplaySubject() fset = seq(fitnessless) \ .map(lambda wchr: (wchr['name'], wchr['id'])) \ .to_set() results = [] def remove_fset(result): wchr, fit = result tp = wchr['name'], wchr['id'] if tp in fset: fset.discard(tp) results.append(result) if len(fset) == 0: tick_tack.on_next(0) s1 = self.executor.sub_compl.subscribe(on_next=remove_fset) for fn in fitnessless: self.executor.sub_in.on_next(fn) if len(fset) == 0: tick_tack.on_next(0) tick_tack.to_blocking().first() tick_tack.dispose() s1.dispose() gen.init_fitnesses(results) if len(gen.get_fitlessness()) > 0: raise AssertionError("Посчитались не все гены в хромосоме")
async def db_new_impl(subject: ReplaySubject, project_name: str, new_name: str, switch: bool): project = try_loading_project(project_name, subject, 1, 4) if not project: return engine = registry().engine dbenv = DbEnvironments(project, engine) try: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Creating database environment {new_name}..." )) dbenv.new(new_name, copy_from=None) except FileExistsError: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Environment with this name already exists.", is_end=True, is_error=True )) except NameError: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Invalid name for new environment, do not use special characters.", is_end=True, is_error=True )) except Exception as ex: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Error creating environment: {ex}", is_end=True, is_error=True )) else: if switch: await db_switch_impl(subject, project, new_name, 1, 1) else: subject.on_next(ResultStep( steps=4, current_step=4, text=f"Finished creating environment: {new_name}.", is_end=True ))
def wait_compl_ids(self, ids): ids = set(ids) tick_tack = ReplaySubject() def onn(id): ids.discard(id) if len(ids) == 0: tick_tack.on_completed() s3 = self.compl_task_id_subject.subscribe(on_next=onn) for i in self.compl_tasks_ids: ids.discard(i) if len(ids) == 0: tick_tack.on_next(0) tick_tack.to_blocking().first_or_default(0) s3.dispose()
async def db_drop_impl(subject: ReplaySubject, project_name: str, name: str): project = try_loading_project(project_name, subject, 1, 4) if not project: return engine = registry().engine dbenv = DbEnvironments(project, engine) try: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Deleting database environment {name}..." )) dbenv.drop(name) except FileNotFoundError: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Environment with this name does not exist.", is_end=True, is_error=True )) except OSError: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Can not delete the environment that is currently active.", is_end=True, is_error=True )) except Exception as ex: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Error deleting environment: {ex}", is_end=True, is_error=True )) else: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Finished deleting environment: {name}.", is_end=True ))
def rx_subscribe(node, data_class=String, parse=unpickle, buffer_size=1): source = ReplaySubject(buffer_size) rospy.Subscriber( node, data_class, lambda msg: source.on_next(parse(msg) if parse is not None else msg), queue_size=buffer_size) return source.as_observable()
async def project_stop_impl(subject: ReplaySubject, project_name: str, services: List[str]): try: project = load_single_project(project_name).config except GraphQLError as ex: subject.on_next(StartStopEndStep( error_string=str(ex), is_fatal_error=True )) subject.on_completed() return engine = registry().engine if len(services) < 1: services = project["app"]["services"].keys() last_steps: Dict[str, int] = {} try: async for service_name, status, finished in engine.stop_project(project, services): _handle_update(finished, last_steps, service_name, status, subject, "Service stopped!") except Exception as err: print(traceback.format_exc()) subject.on_next(StartStopEndStep( error_string="Error stopping the services: " + str(err), is_fatal_error=True )) else: subject.on_next(StartStopEndStep()) subject.on_completed()
async def update_images_impl(subject: ReplaySubject, project_name: str): subject.on_next(ResultStep( steps=1, current_step=1, text="Starting image update..." )) project = try_loading_project(project_name, subject, 1, 1) if not project: return try: registry().engine.pull_images(project, line_reset="", update_func=lambda msg: subject.on_next( ResultStep( steps=1, current_step=1, text=msg ) )) subject.on_next(ResultStep( steps=1, current_step=1, text="Done updating images!", is_end=True )) except Exception as ex: subject.on_next(ResultStep( steps=1, current_step=1, text="Error updating an image: " + str(ex), is_end=True, is_error=True )) finally: subject.on_completed() pass
async def update_repositories_impl(subject: ReplaySubject): subject.on_next(ResultStep( steps=2, current_step=1, text="Starting repository update..." )) try: repositories.update(registry().system_config, lambda msg: subject.on_next(ResultStep( steps=2, current_step=1, text=msg ))) except Exception as e: subject.on_next(ResultStep( steps=2, current_step=1, text="Fatal error during repository update: " + str(e), is_end=True, is_error=True )) subject.on_completed() return subject.on_next(ResultStep( steps=2, current_step=2, text="Reloading configuration..." )) # Reload system config try: config_path = riptide_main_config_file() system_config = Config.from_yaml(config_path) system_config.validate() registry.system_config = system_config except FileNotFoundError as e: subject.on_next(ResultStep( steps=2, current_step=2, text="Main config file not found! Could not reload system config.", is_end=True, is_error=True )) except Exception as e: subject.on_next(ResultStep( steps=2, current_step=2, text="Could not reload system config: " + str(e), is_end=True, is_error=True )) else: subject.on_next(ResultStep( steps=2, current_step=2, text="Repository update done!", is_end=True )) finally: subject.on_completed()
class AudioBackend: class State(Enum): paused = 1 playing = 2 def __init__(self): self.playing = False self.current_position = 0 self.current_duration = 1 self.current_state = self.State.paused self.state = ReplaySubject(1) self.current_track = ReplaySubject(1) self.position = ReplaySubject(1) self.duration = ReplaySubject(1) self.end_of_track = Subject() self.end_of_track.subscribe(lambda _: logger.debug('END OF TRACK')) self.state.subscribe(self.set_state) self.position.subscribe(self.set_position) self.duration.subscribe(self.set_duration) self.time_tracking = self.position.pipe(map(self.make_time_tracking)) def destroy(self) -> None: raise NotImplementedError def set_track(self, track: Track) -> None: raise NotImplementedError def seek(self, position: int) -> None: raise NotImplementedError def play(self) -> None: raise NotImplementedError def pause(self) -> None: raise NotImplementedError def make_time_tracking(self, position: int): return TimeTrack( elapsed=utils.format_seconds(position), total=utils.format_seconds(self.current_duration), progress_percentage=position / self.current_duration, ) def set_position(self, position: int): self.current_position = position def set_duration(self, duration: int): self.current_duration = duration def set_state(self, state): self.current_state = state def play_track(self, track: Track): self.current_track.on_next(track) self.set_track(track) self.play() def rewind_by_seconds(self, offset: int): new_position = self.current_position + offset new_position = min(self.current_duration, max(0, new_position)) self.seek(new_position) def rewind_by_percentage(self, offset: float): self.rewind_by_seconds(self.current_duration * offset) def toggle_pause(self): if self.current_state == self.State.playing: self.pause() elif self.current_state == self.State.paused: self.play() else: logger.error(f'Unknown playback state "{self.current_state}"')
async def db_switch_impl( subject: ReplaySubject, project: Project, name: str, total_steps_from_ctx=0, current_step_in_ctx=0 ): """Switches db env. Has 3 total steps and can be used in other db operations.""" engine = registry().engine dbenv = DbEnvironments(project, engine) db_name = dbenv.db_service["$name"] subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 1, text="Switching database..." )) # 1. If running, stop database was_running = engine.service_status(project, db_name, registry().system_config) if was_running: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 1, text="Stopping database service..." )) async for _ in registry().engine.stop_project(project, [db_name]): pass # 2. Switch environment try: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 2, text=f"Switching environment to {name}..." )) dbenv.switch(name) except FileNotFoundError: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 2, text=f"Environment {name} does not exist.", is_end=True, is_error=True )) except Exception as ex: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 2, text=f"Error switching environment: {str(ex)}", is_end=True, is_error=True )) else: # 3. If was running: start database again if was_running: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 3, text=f"Starting database...", )) async for _ in registry().engine.start_project(project, [db_name]): pass subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 3, text=f"Finished switching database environment to {name}.", is_end=True ))
class Store: '''Handles persistence of streams information in backend.''' def __init__(self, config): self.etcd = Client(host=config.etcd_hosts, allow_reconnect=True) self.stream_changes = ReplaySubject() self.initialize_store() def initialize_store(self): '''Clears leftover store data at the beginning of execution.''' logger.info('Initializing store...') try: self.etcd.delete(STREAMS_KEY, recursive=True) except EtcdKeyNotFound: return def write_or_update_stream(self, stream): '''Adds a new stream to the store or updates the existing stream.''' key = path.join(STREAMS_KEY, str(stream.id)) value = dumps(stream.data, default=str) try: self.etcd.write(key, value, prevExist=False) logger.info('Key \'%s\' did not exist. Added new entry.' % (key)) self.stream_changes.on_next( dict(type='add', target=stream.id, changed_fields=stream.data.keys(), stream=stream)) except EtcdAlreadyExist: prevStream = self.read_stream(stream.id) if stream != prevStream: self.etcd.write(key, value, prevExist=True) logger.info('Key \'%s\' updated.' % (key)) self.stream_changes.on_next( dict(type='change', target=stream.id, changed_fields=prevStream.changed_fields(stream), stream=stream)) def read_stream(self, stream_id): '''Reads a stream by ID from the store.''' data = loads( self.etcd.read(path.join(STREAMS_KEY, str(stream_id))).value) return Stream(data) def read_streams(self): '''Reads a list of all streams from the store.''' try: return [ Stream(loads(stream.value)) for stream in self.etcd.read(STREAMS_KEY).children ] except EtcdKeyNotFound: return [] def delete_stream(self, stream): '''Deletes a stream from the store.''' try: self.etcd.delete(path.join(STREAMS_KEY, str(stream.id))) logger.info('Key \'%s\' deleted.') self.stream_changes.on_next( dict(type='delete', target=stream.id, changed_fields=stream.data.keys(), stream=stream)) except EtcdKeyNotFound: return