def _move_config_path(client_id, message): # should work on both files and folders # it can be used for both move and rename try: message_key, src_path, dst_path = message.split('|') except ValueError: # message is bad, skip it return src_path = src_path.replace('../', '') # prevent going up src_full_path = os.path.join(_options.db_path, src_path) dst_path = dst_path.replace('../', '') # prevent going up dst_full_path = os.path.join(_options.db_path, dst_path) try: # make sure the parent directory exists parent_dir = os.path.dirname(dst_full_path) if not os.path.isdir(parent_dir): os.makedirs(parent_dir) shutil.move(src_full_path, dst_full_path) # walk back in directory tree removing empty directories. Do this to # prevent future rename operations to inadvertely ending up inside a # "transparent" directory instead of being renamed __remove_empty_tree() msg = (protocol.CONFIG_MOVE_PATH_OK, '%s|0' % (message_key, )) except IOError as ioe: msg = (protocol.CONFIG_MOVE_PATH_FAILED, "%s|%s: %s" % (message_key, ioe.filename, ioe.strerror)) else: event.send(__name__, 'config_changed') client_id.sendall(protocol.message(*msg))
def _remove_config_file(client_id, message): try: message_key,file_path = message.split('|') except ValueError: # message is bad, skip it return file_path = file_path.replace('../','') # prevent going up full_path = os.path.join(_options.db_path, file_path) try: if os.path.isfile(full_path): os.remove(full_path) elif os.path.isdir(full_path): shutil.rmtree(full_path) # walk back in directory tree removing empty directories. Do this to # prevent future rename operations to inadvertely ending up inside a # "transparent" directory instead of being renamed __remove_empty_tree() msg = (protocol.CONFIG_REMOVE_FILE_OK, '%s|0' % (message_key,)) except IOError: msg = (protocol.CONFIG_REMOVE_FILE_FAILED, "%s|File/directory doesn't exist" % message_key) else: event.send(__name__, 'config_changed') client_id.sendall(protocol.message(*msg))
def __call__(self, data_events, nodes, info): if self._init_done is False: for acq_device_or_channel, data_node in nodes.iteritems(): if is_zerod(data_node): channel = data_node self._channel_name_2_channel[channel.name] = channel self._init_done = True if self._last_point_display == -1: self._last_point_display += 1 min_nb_points = None for channels_name, channel in self._channel_name_2_channel.iteritems(): nb_points = len(channel) if min_nb_points is None: min_nb_points = nb_points elif min_nb_points > nb_points: min_nb_points = nb_points point_nb = self._last_point_display for point_nb in range(self._last_point_display, min_nb_points): values = dict([ (ch_name, ch.get(point_nb)) for ch_name, ch in self._channel_name_2_channel.iteritems() ]) send(current_module, "scan_data", self._scan_info, values) if min_nb_points is not None: self._last_point_display = min_nb_points
def _move_config_path(client_id, message): # should work on both files and folders # it can be used for both move and rename try: message_key, src_path, dst_path = message.split('|') except ValueError: # message is bad, skip it return src_path = src_path.replace('../','') # prevent going up src_full_path = os.path.join(_options.db_path, src_path) dst_path = dst_path.replace('../','') # prevent going up dst_full_path = os.path.join(_options.db_path, dst_path) try: # make sure the parent directory exists parent_dir = os.path.dirname(dst_full_path) if not os.path.isdir(parent_dir): os.makedirs(parent_dir) shutil.move(src_full_path, dst_full_path) # walk back in directory tree removing empty directories. Do this to # prevent future rename operations to inadvertely ending up inside a # "transparent" directory instead of being renamed __remove_empty_tree() msg = (protocol.CONFIG_MOVE_PATH_OK, '%s|0' % (message_key,)) except IOError as ioe: msg = (protocol.CONFIG_MOVE_PATH_FAILED, "%s|%s: %s" % (message_key, ioe.filename, ioe.strerror)) else: event.send(__name__, 'config_changed') client_id.sendall(protocol.message(*msg))
def _write_config_db_file(client_id,message): first_pos = message.find('|') second_pos = message.find('|',first_pos + 1) if first_pos < 0 or second_pos < 0: # message malformed msg = protocol.message(protocol.CONFIG_SET_DB_FILE_FAILED, '%s|%s' % (message_key,'Malformed message')) client_id.sendall(msg) return message_key = message[:first_pos] file_path = message[first_pos + 1:second_pos] content = message[second_pos + 1:].decode("utf-8") file_path = file_path.replace('../','') # prevent going up full_path = os.path.join(_options.db_path,file_path) full_dir = os.path.dirname(full_path) if not os.path.isdir(full_dir): os.makedirs(full_dir) try: with file(full_path,'w') as f: f.write(content) msg = protocol.message(protocol.CONFIG_SET_DB_FILE_OK,'%s|0' % message_key) except: msg = protocol.message(protocol.CONFIG_SET_DB_FILE_FAILED, '%s|%s' % (message_key,traceback.format_exc())) else: event.send(__name__, 'config_changed') client_id.sendall(msg)
def _write_config_db_file(client_id, message): first_pos = message.find('|') second_pos = message.find('|', first_pos + 1) if first_pos < 0 or second_pos < 0: # message malformed msg = protocol.message(protocol.CONFIG_SET_DB_FILE_FAILED, '%s|%s' % (message_key, 'Malformed message')) client_id.sendall(msg) return message_key = message[:first_pos] file_path = message[first_pos + 1:second_pos] content = message[second_pos + 1:].decode("utf-8") file_path = file_path.replace('../', '') # prevent going up full_path = os.path.join(_options.db_path, file_path) full_dir = os.path.dirname(full_path) if not os.path.isdir(full_dir): os.makedirs(full_dir) try: with file(full_path, 'w') as f: f.write(content) msg = protocol.message(protocol.CONFIG_SET_DB_FILE_OK, '%s|0' % message_key) except: msg = protocol.message(protocol.CONFIG_SET_DB_FILE_FAILED, '%s|%s' % (message_key, traceback.format_exc())) else: event.send(__name__, 'config_changed') client_id.sendall(msg)
def _remove_config_file(client_id, message): try: message_key, file_path = message.split('|') except ValueError: # message is bad, skip it return file_path = file_path.replace('../', '') # prevent going up full_path = os.path.join(_options.db_path, file_path) try: if os.path.isfile(full_path): os.remove(full_path) elif os.path.isdir(full_path): shutil.rmtree(full_path) # walk back in directory tree removing empty directories. Do this to # prevent future rename operations to inadvertely ending up inside a # "transparent" directory instead of being renamed __remove_empty_tree() msg = (protocol.CONFIG_REMOVE_FILE_OK, '%s|0' % (message_key, )) except IOError: msg = (protocol.CONFIG_REMOVE_FILE_FAILED, "%s|File/directory doesn't exist" % message_key) else: event.send(__name__, 'config_changed') client_id.sendall(protocol.message(*msg))
def _set_moving_state(self, from_channel=False): self.__stopped = False self.__move_done.clear() self.__move_done_callback.clear() gevent.spawn(self.__emit_move_done) if from_channel: self.__move_task = None else: self.settings.set("state", AxisState("MOVING")) event.send(self, "move_done", False)
def setting_update_from_channel(value, setting_name=None, axis=None): #print 'setting update from channel', axis.name, setting_name, str(value) if not axis._hw_control: if setting_name == 'state': if 'MOVING' in str(value): axis._set_moving_state(from_channel=True) else: if axis.is_moving: axis._set_move_done(None) event.send(axis, setting_name, value)
def write_settings(): global SETTINGS_WRITER_WATCHER SETTINGS_WRITER_WATCHER.clear() try: while True: axis, setting_name, value, write_flag = SETTINGS_WRITER_QUEUE.get() if axis is None: break event.send( axis, "write_setting", axis.config, setting_name, value, write_flag) finally: SETTINGS_WRITER_WATCHER.set()
def _set_move_done(self, move_task): self._reset_motions_dict() if move_task is not None: if not move_task._being_waited: try: move_task.get() except gevent.GreenletExit: pass except: sys.excepthook(*sys.exc_info()) self.__move_done.set() event.send(self, "move_done", True)
def _do_move(self, motions_dict, polling_time): all_motions = [] event.send(self, "move_done", False) with error_cleanup(self._do_stop): for controller, motions in motions_dict.iteritems(): all_motions.extend(motions) try: controller.start_all(*motions) except NotImplementedError: for motion in motions: controller.start_one(motion) for motion in motions: motion.axis._set_moving_state() self._handle_move(all_motions, polling_time)
def set(self, axis, setting_name, value, write=True): ''' *set setting (if updated) *send event *write ''' old_value = self.get(axis, setting_name) if value == old_value: return setting_value = self._set_setting(axis, setting_name, value) try: event.send(axis, setting_name, setting_value) finally: SETTINGS_WRITER_QUEUE.put((axis, setting_name, setting_value, write))
def _start_motion(self, motions_dict): all_motions = [] event.send(self, "move_done", False) with error_cleanup(self._do_stop): if(len(motions_dict) == 1): # only one controller for the motion for controller, motions in motions_dict.iteritems(): all_motions.extend(motions) self._start_one_controller_motions(controller,motions) else: # parallel start controller_tasks = list() for controller, motions in motions_dict.iteritems(): all_motions.extend(motions) controller_tasks.append(gevent.spawn(self._start_one_controller_motions, controller,motions)) gevent.joinall(controller_tasks, raise_error=True) return all_motions
def set(self, axis, setting_name, value): ''' * set setting (if updated) * send event * write ''' old_value = get_axis_setting(axis, setting_name) if value == old_value: return convert_func = self.convert_funcs.get(setting_name) if convert_func is not None: value = convert_func(value) if setting_name not in ('state', 'position'): settings.HashSetting("axis.%s" % axis.name)[setting_name] = value axis._beacon_channels[setting_name].value = value event.send(axis, 'internal_' + setting_name, value)
def _start_motion(self, motions_dict): all_motions = [] event.send(self, "move_done", False) with error_cleanup(self._do_stop): if (len(motions_dict) == 1): # only one controller for the motion for controller, motions in motions_dict.iteritems(): all_motions.extend(motions) self._start_one_controller_motions(controller, motions) else: # parallel start controller_tasks = list() for controller, motions in motions_dict.iteritems(): all_motions.extend(motions) controller_tasks.append( gevent.spawn(self._start_one_controller_motions, controller, motions)) gevent.joinall(controller_tasks, raise_error=True) return all_motions
def _set_move_done(self, move_task): if move_task is not None: if not move_task._being_waited: try: move_task.get() except gevent.GreenletExit: pass except: sys.excepthook(*sys.exc_info()) # update settings; # as update is done before move done is set, # we need to read state from hardware to get it right # (it would return 'MOVING' otherwise) # this update is very important for position, to have # final position ok for waiters on move done event self._update_settings(state=self.state(read_hw=True)) self.__move_done.set() event.send(self, "move_done", True)
def set(self, axis, setting_name, value): ''' * set setting (if updated) * send event * write ''' old_value = get_axis_setting(axis, setting_name) if value == old_value: return convert_func = self.convert_funcs.get(setting_name) if convert_func is not None: value = convert_func(value) if setting_name not in ('state', 'position'): settings.HashSetting("axis.%s" % axis.name)[setting_name] = value axis._beacon_channels[setting_name].value = value event.send(axis, 'internal_'+setting_name, value)
def run(self): if hasattr(self._data_watch_callback, 'on_state'): call_on_prepare = self._data_watch_callback.on_state( self.PREPARE_STATE) call_on_stop = self._data_watch_callback.on_state(self.STOP_STATE) else: call_on_prepare, call_on_stop = False, False send(current_module, "scan_new", self.scan_info) if self._data_watch_callback: set_watch_event = self._data_watch_callback_event.set else: set_watch_event = None try: i = None for i in self.acq_chain: self._state = self.PREPARE_STATE with periodic_exec(0.1 if call_on_prepare else 0, set_watch_event): i.prepare(self, self.scan_info) self._state = self.START_STATE i.start() except: self._state = self.STOP_STATE with periodic_exec(0.1 if call_on_stop else 0, set_watch_event): i.stop() raise else: self._state = self.STOP_STATE if i is not None: with periodic_exec(0.1 if call_on_stop else 0, set_watch_event): i.stop() finally: self._state = self.IDLE_STATE send(current_module, "scan_end", self.scan_info) if self._writer: self._writer.close()
def __emit_move_done(self): self.__move_done.wait() try: event.send(self, "move_done", True) finally: self.__move_done_callback.set()
def sync_hard(self): """Forces an axis synchronization with the hardware""" self.settings.set("state", self.state(read_hw=True)) self._read_dial_and_update() self._set_position(self.position()) event.send(self, "sync_hard")
def _set_move_done(self, move_task): self._reset_motions_dict() self.__move_done.set() event.send(self, "move_done", True)
def _set_moving_state(self): self.__move_done.clear() self.settings.set("state", AxisState("MOVING"), write=True) #False) event.send(self, "move_done", False)