def copy_file(source_file, target_directory): """ Copy a file into a directory. If there's already another file with the same name in the target directory the user is asked to enter a new file name, overwrite the existing one or just cancel the operation. Return true if the file has been copied, false otherwise. """ src = servers.get_file_server().manglepath(str(source_file)) dir_name = servers.get_file_server().manglepath(str(target_directory)) target = format.append_to_path(dir_name, format.get_name(src)) while os.path.exists(target): result = cjr.confirm_yes_no( None, "A file called '%s' already exists.\n\n" \ "Do you want to rename the new file?\n" \ "(answering 'no' will overwrite the old one)"\ % format.get_name(src) ) if result == wx.ID_YES: dlg = wx.TextEntryDialog(None, "New file name:", "Conjurer", format.get_name(target)) if dlg.ShowModal() == wx.ID_OK and dlg.GetValue() != "": target = format.append_to_path(dir, dlg.GetValue()) elif result == wx.ID_NO: break else: return False shutil.copy(src, target) return True
def set_fsm(self, fsm_path): self.fsm_path = fsm_path if self.fsm_path is None: self.text_name.set_value("") else: self.text_name.set_value(format.get_name(self.fsm_path)) self.ctrl_states.set_fsm(fsm_path)
def __update_filter_list(self): self.choice_filter.Clear() self.choice_filter.Append("<none>") scripts = fsm.get_script_conditions() for script in scripts: self.choice_filter.Append( format.get_name(script) ) self.__select_filter()
def set_fsm(self, fsm_path): self.fsm_path = fsm_path if self.fsm_path is None: self.text_name.set_value("") else: self.text_name.set_value( format.get_name(self.fsm_path) ) self.ctrl_states.set_fsm(fsm_path)
def __update_filter_list(self): self.choice_filter.Clear() self.choice_filter.Append("<none>") scripts = fsm.get_script_conditions() for script in scripts: self.choice_filter.Append(format.get_name(script)) self.__select_filter()
def __update_script_list(self): self.choice_script.Clear() if self.transition_path != None: cond_path = pynebula.lookup( self.transition_path ).getcondition().getfullname() state_path = fsm.get_state_of_transition( self.transition_path ) paths = get_free_script_conditions( state_path, cond_path ) for path in paths: index = self.choice_script.Append( format.get_name(path) ) if path == cond_path: self.choice_script.Select( index )
def __on_change_type(self, event): if self.state_path != None and not self.skip_change_type_event: # Rename the old state to keep it while building the new one old_state = self.__get_state() old_state.setname('_tmp') # Create new state for the new state type if self.GetSelection() == self.ID_NodeStatePanel: new_state = create_default_state(self.state_path, 'nnodestate') elif self.GetSelection() == self.ID_LeafStatePanel: new_state = create_default_state(self.state_path, 'nleafstate') else: new_state = create_default_state(self.state_path, 'nendstate') if new_state == None: # If creating a default state fails abort and restore old state old_state.setname(str(format.get_name(self.state_path))) self.skip_change_type_event = True self.SetSelection(event.GetOldSelection()) self.skip_change_type_event = False return fsm_ = self.__get_fsm() fsm_.addstate(new_state) # Copy info that's independent of the state type old_trans_path = fsm.get_transitions_dir_of_state( old_state.getfullname()) new_trans_path = fsm.get_transitions_dir_of_state( new_state.getfullname(), False) pynebula.lookup(str(old_trans_path)).clone(str(new_trans_path)) transitions = fsm.get_transitions(new_state.getfullname()) for t in transitions: t_obj = pynebula.lookup(t) new_state.addtransition(t_obj) if fsm_.getinitialstate() == old_state: fsm_.setinitialstate(new_state) # Update all transitions to point to the new state if pointing the old one states = fsm.get_states(fsm_.getfullname()) for s in states: transitions = fsm.get_transitions(s) for t in transitions: t_obj = pynebula.lookup(t) for i in range(t_obj.gettargetsnumber()): if t_obj.gettargetstatebyindex(i) == old_state: t_obj.settargetstatebyindex(i, new_state) # Delete old state fsm_.deletestate(old_state) old_state = None # Python, release that #$%&/)?! reference NOW! # Update type specific controls self.__update_controls() # Signal change fsmevents.signal_fsm_change(self, self.state_path)
def __update_script_list(self): self.choice_script.Clear() if self.transition_path != None: cond_path = pynebula.lookup( self.transition_path).getcondition().getfullname() state_path = fsm.get_state_of_transition(self.transition_path) paths = get_free_script_conditions(state_path, cond_path) for path in paths: index = self.choice_script.Append(format.get_name(path)) if path == cond_path: self.choice_script.Select(index)
def __on_change_type(self, event): if self.state_path != None and not self.skip_change_type_event: # Rename the old state to keep it while building the new one old_state = self.__get_state() old_state.setname("_tmp") # Create new state for the new state type if self.GetSelection() == self.ID_NodeStatePanel: new_state = create_default_state(self.state_path, "nnodestate") elif self.GetSelection() == self.ID_LeafStatePanel: new_state = create_default_state(self.state_path, "nleafstate") else: new_state = create_default_state(self.state_path, "nendstate") if new_state == None: # If creating a default state fails abort and restore old state old_state.setname(str(format.get_name(self.state_path))) self.skip_change_type_event = True self.SetSelection(event.GetOldSelection()) self.skip_change_type_event = False return fsm_ = self.__get_fsm() fsm_.addstate(new_state) # Copy info that's independent of the state type old_trans_path = fsm.get_transitions_dir_of_state(old_state.getfullname()) new_trans_path = fsm.get_transitions_dir_of_state(new_state.getfullname(), False) pynebula.lookup(str(old_trans_path)).clone(str(new_trans_path)) transitions = fsm.get_transitions(new_state.getfullname()) for t in transitions: t_obj = pynebula.lookup(t) new_state.addtransition(t_obj) if fsm_.getinitialstate() == old_state: fsm_.setinitialstate(new_state) # Update all transitions to point to the new state if pointing the old one states = fsm.get_states(fsm_.getfullname()) for s in states: transitions = fsm.get_transitions(s) for t in transitions: t_obj = pynebula.lookup(t) for i in range(t_obj.gettargetsnumber()): if t_obj.gettargetstatebyindex(i) == old_state: t_obj.settargetstatebyindex(i, new_state) # Delete old state fsm_.deletestate(old_state) old_state = None # Python, release that #$%&/)?! reference NOW! # Update type specific controls self.__update_controls() # Signal change fsmevents.signal_fsm_change(self, self.state_path)
def copy_file(source_file, target_directory): """ Copy a file into a directory. If there's already another file with the same name in the target directory the user is asked to enter a new file name, overwrite the existing one or just cancel the operation. Return true if the file has been copied, false otherwise. """ src = servers.get_file_server().manglepath( str(source_file) ) dir_name = servers.get_file_server().manglepath( str(target_directory) ) target = format.append_to_path( dir_name, format.get_name(src) ) while os.path.exists( target ): result = cjr.confirm_yes_no( None, "A file called '%s' already exists.\n\n" \ "Do you want to rename the new file?\n" \ "(answering 'no' will overwrite the old one)"\ % format.get_name(src) ) if result == wx.ID_YES: dlg = wx.TextEntryDialog( None, "New file name:", "Conjurer", format.get_name(target) ) if dlg.ShowModal() == wx.ID_OK and dlg.GetValue() != "": target = format.append_to_path( dir, dlg.GetValue() ) elif result == wx.ID_NO: break else: return False shutil.copy(src, target) return True
def __update_attach_ctrls(self): # Update FSM choicer type = self.__get_selected_list_item( self.ID_AgentType ) self.choice_fsm.Enable( type != "" ) if type != "": # Fill choice with all available FSMs self.choice_fsm.Clear() self.choice_fsm.Append("<none>") fsm_paths = fsm.get_fsms() for fsm_path in fsm_paths: fsm_name = format.get_name( fsm_path ) self.choice_fsm.Append( fsm_name ) # Select current FSM entity_class = servers.get_entity_class_server().getentityclass( str(type) ) current_fsm = entity_class.getparentfsm() if current_fsm == None: self.choice_fsm.SetStringSelection("<none>") else: self.choice_fsm.SetStringSelection( current_fsm.getname() )
def __update_attach_ctrls(self): # Update FSM choicer type = self.__get_selected_list_item(self.ID_AgentType) self.choice_fsm.Enable(type != "") if type != "": # Fill choice with all available FSMs self.choice_fsm.Clear() self.choice_fsm.Append("<none>") fsm_paths = fsm.get_fsms() for fsm_path in fsm_paths: fsm_name = format.get_name(fsm_path) self.choice_fsm.Append(fsm_name) # Select current FSM entity_class = servers.get_entity_class_server().getentityclass( str(type)) current_fsm = entity_class.getparentfsm() if current_fsm == None: self.choice_fsm.SetStringSelection("<none>") else: self.choice_fsm.SetStringSelection(current_fsm.getname())
def __on_change_name(self, event): # Change the FSM file and object names fsm = pynebula.lookup( self.fsm_path ) old_name = str( format.get_name( self.fsm_path ) ) new_name = str( self.text_name.get_value() ) fsm.setname( new_name ) servers.get_fsm_server().savefsm( fsm ) fsm.setname( old_name ) servers.get_fsm_server().erasefsm( fsm ) fsm.setname( new_name ) # Update the controls self.fsm_path = fsm.getfullname() self.ctrl_states.set_fsm( self.fsm_path ) self.ctrl_statetype.set_state(None) self.ctrl_transitions.set_state(None) # self.ctrl_emotactions.set_state(None) # Notify change to update the name in the FSM library app.get_top_window(self).emit_app_event( events.FSMNameChanged( new_name=new_name, old_name=old_name ) ) fsmevents.signal_fsm_change( self, fsmevents.ID_FSMNameChanged, self.fsm_path )
def __generate_graph_image(self): # Image dot_path = cfg.Repository.getsettingvalue(cfg.ID_DotPath) cmd = '"\"' + dot_path + '\"' cmd = cmd + ' -Tgif ' + '\"' + self.dot_path + '\"' cmd = cmd + ' -o ' + '\"' + self.image_path + '\""' stdin, stdout, stderr = os.popen3(cmd) error = stderr.read() if len(error) > 0: print "Error while generating the FSM preview image:" print error.replace('\n', ' ') return False stdin.close() stdout.close() stderr.close() # Map cmd = '"\"' + dot_path + '\"' cmd = cmd + ' -Tcmapx ' + '\"' + self.dot_path + '\"' cmd = cmd + ' -o ' + '\"' + self.map_path + '\""' stdin, stdout, stderr = os.popen3(cmd) error = stderr.read() if len(error) > 0: print "Error while generating the FSM preview client map:" print error.replace('\n', ' ') return False stdin.close() stdout.close() stderr.close() # HTML map_file = open(self.map_path, 'r') map_tag = map_file.read() map_file.close() html_file = open(self.html_path, 'w') html_file.write("<img src='" + format.get_name(self.image_path) + "' usemap=#G border=0>\n") html_file.write(map_tag) html_file.close() return True
def __on_change_name(self, event): # Change the FSM file and object names fsm = pynebula.lookup(self.fsm_path) old_name = str(format.get_name(self.fsm_path)) new_name = str(self.text_name.get_value()) fsm.setname(new_name) servers.get_fsm_server().savefsm(fsm) fsm.setname(old_name) servers.get_fsm_server().erasefsm(fsm) fsm.setname(new_name) # Update the controls self.fsm_path = fsm.getfullname() self.ctrl_states.set_fsm(self.fsm_path) self.ctrl_statetype.set_state(None) self.ctrl_transitions.set_state(None) # self.ctrl_emotactions.set_state(None) # Notify change to update the name in the FSM library app.get_top_window(self).emit_app_event( events.FSMNameChanged(new_name=new_name, old_name=old_name)) fsmevents.signal_fsm_change(self, fsmevents.ID_FSMNameChanged, self.fsm_path)
def __generate_graph_image(self): # Image dot_path = cfg.Repository.getsettingvalue( cfg.ID_DotPath ) cmd = '"\"' + dot_path + '\"' cmd = cmd + ' -Tgif ' + '\"' + self.dot_path + '\"' cmd = cmd + ' -o ' + '\"' + self.image_path + '\""' stdin, stdout, stderr = os.popen3( cmd ) error = stderr.read() if len(error) > 0: print "Error while generating the FSM preview image:" print error.replace('\n', ' ') return False stdin.close() stdout.close() stderr.close() # Map cmd = '"\"' + dot_path + '\"' cmd = cmd + ' -Tcmapx ' + '\"' + self.dot_path + '\"' cmd = cmd + ' -o ' + '\"' + self.map_path + '\""' stdin, stdout, stderr = os.popen3( cmd ) error = stderr.read() if len(error) > 0: print "Error while generating the FSM preview client map:" print error.replace('\n', ' ') return False stdin.close() stdout.close() stderr.close() # HTML map_file = open( self.map_path, 'r' ) map_tag = map_file.read() map_file.close() html_file = open( self.html_path, 'w' ) html_file.write( "<img src='" + format.get_name(self.image_path) + "' usemap=#G border=0>\n" ) html_file.write( map_tag ) html_file.close() return True
def get_brief(self): if self.filename == "": name = "<Unknown>" else: name = format.get_name( format.mangle_path(self.filename) ) return "(script) " + name
def __get_script_path(self, obj_path): script_name = format.get_name( obj_path ) + ".lua" script_path = format.append_to_path( self.disk_lib_path, script_name ) return format.mangle_path( script_path )
def get_index(obj_path, prefix): """Return the index associated to the given object path to make it unique""" return format.get_name(obj_path)[len(prefix):]
def get_brief(self): if self.filename == "": name = "<Unknown>" else: name = format.get_name(format.mangle_path(self.filename)) return "(script) " + name