def object_list_generator(self): objdict = get_available_objects() if self.return_strings: objlist = objdict.keys() else: objlist = [(name, objdict[name]) for name in objdict.keys()] sort_case_insensitive(objlist) for item in self.aguidata['prepend_list']: objlist.insert(0, item) for item in self.aguidata['append_list']: objlist.append(item) return objlist
def save_object(obj, name=None, parentWindow=None): """save_object(obj): Export obj as a class to objects folder name: the name to save the object as. If not provided, a dialog will be opened parentWindow: the parent window of name dialog. If not provided, the wx.ActiveWindow will be used """ if not isinstance(obj, Node): raise TypeError('save_object() arg 1 must be a Node') if not name: if obj.archetype: name = obj.gname else: if obj.gname: name = obj.gname else: name = "MyClass" if not name: name = obj.__class__.__name__ if parentWindow == None: parentWindow = wx.GetActiveWindow() objName = '' # we generally don't want to save with the same name as # a base class of the same object superclasses = getmro(obj.__class__)[1:] for cls in superclasses: if name == cls.__name__ or name=='Sprite': name = ''.join(['My',name]) break default = make_valid_attr_name(name) dlg = wx.TextEntryDialog( parentWindow, "Enter the object's class/file name", "Save Object", default) while not objName: if dlg.ShowModal() == wx.ID_OK: name = dlg.GetValue() errorDlg = None if name == 'Sprite': errorDlg = wx.MessageDialog( dlg, "You can't use the names 'Sprite'", "Reserved Name", wx.OK) elif name != make_valid_attr_name(name): errorDlg = wx.MessageDialog( dlg, "Name cannot contain spaces or special characters"+\ "\nand cannot start with a number", "Invalid Name", wx.OK) if errorDlg: errorDlg.ShowModal() errorDlg.Destroy() dlg.SetValue(default) dlg.SetFocus() continue path = os.path.join('objects',''.join([name,'.py'])) old_module = obj.__class__.__module__.split('.') if old_module[-2:-1][0] == 'objects' and \ old_module[-1:][0] != name: # verify overwrite try: test = file(path) except: objName = name else: test.close() confirmDlg = wx.MessageDialog( dlg, "\n".join([path, "File already exists. Overwrite?"]), "Confirm Replace", wx.YES_NO | wx.NO_DEFAULT) if confirmDlg.ShowModal() == wx.ID_YES: if sys.platform == "win32": files = os.listdir('objects') testname = name + '.py' for f in files: if f.lower() == testname.lower(): sceneName = os.path.splitext(f)[0] break else: objName = name confirmDlg.Destroy() else: objName = name else: dlg.Destroy() return dlg.Destroy() else: name = make_valid_attr_name(name) objName = name path = os.path.join('objects',''.join([name,'.py'])) try: if getattr(obj, 'archetype', False): # we don't want every instance to be an archetype obj.archetype = False archetype = True else: archetype = False from pig.Sprite import Sprite exporter = code_exporter( obj, path, True, {'name':objName, 'base_class':Sprite}) objDict = get_available_objects( True) oldclass = obj.__class__ if oldclass != objDict[objName]: obj.__class__ = objDict[objName] if archetype: # return archetype status after saving obj.archetype = True obj.gname = objName if exporter.file_changed: archetype_changed( obj, oldclass, exporter) return exporter except: show_exception_dialog()
def save_object(obj, name=None, parentWindow=None): """save_object(obj): Export obj as a class to objects folder name: the name to save the object as. If not provided, a dialog will be opened parentWindow: the parent window of name dialog. If not provided, the wx.ActiveWindow will be used """ if not isinstance(obj, Node): raise TypeError('save_object() arg 1 must be a Node') if not name: if obj.archetype: name = obj.gname else: if obj.gname: name = obj.gname else: name = "MyClass" if not name: name = obj.__class__.__name__ if parentWindow == None: parentWindow = wx.GetActiveWindow() objName = '' # we generally don't want to save with the same name as # a base class of the same object superclasses = getmro(obj.__class__)[1:] for cls in superclasses: if name == cls.__name__ or name == 'Sprite': name = ''.join(['My', name]) break default = make_valid_attr_name(name) dlg = wx.TextEntryDialog(parentWindow, "Enter the object's class/file name", "Save Object", default) while not objName: if dlg.ShowModal() == wx.ID_OK: name = dlg.GetValue() errorDlg = None if name == 'Sprite': errorDlg = wx.MessageDialog( dlg, "You can't use the names 'Sprite'", "Reserved Name", wx.OK) elif name != make_valid_attr_name(name): errorDlg = wx.MessageDialog( dlg, "Name cannot contain spaces or special characters"+\ "\nand cannot start with a number", "Invalid Name", wx.OK) if errorDlg: errorDlg.ShowModal() errorDlg.Destroy() dlg.SetValue(default) dlg.SetFocus() continue path = os.path.join('objects', ''.join([name, '.py'])) old_module = obj.__class__.__module__.split('.') if old_module[-2:-1][0] == 'objects' and \ old_module[-1:][0] != name: # verify overwrite try: test = file(path) except: objName = name else: test.close() confirmDlg = wx.MessageDialog( dlg, "\n".join( [path, "File already exists. Overwrite?"]), "Confirm Replace", wx.YES_NO | wx.NO_DEFAULT) if confirmDlg.ShowModal() == wx.ID_YES: if sys.platform == "win32": files = os.listdir('objects') testname = name + '.py' for f in files: if f.lower() == testname.lower(): sceneName = os.path.splitext(f)[0] break else: objName = name confirmDlg.Destroy() else: objName = name else: dlg.Destroy() return dlg.Destroy() else: name = make_valid_attr_name(name) objName = name path = os.path.join('objects', ''.join([name, '.py'])) try: if getattr(obj, 'archetype', False): # we don't want every instance to be an archetype obj.archetype = False archetype = True else: archetype = False from pig.Sprite import Sprite exporter = code_exporter(obj, path, True, { 'name': objName, 'base_class': Sprite }) objDict = get_available_objects(True) oldclass = obj.__class__ if oldclass != objDict[objName]: obj.__class__ = objDict[objName] if archetype: # return archetype status after saving obj.archetype = True obj.gname = objName if exporter.file_changed: archetype_changed(obj, oldclass, exporter) return exporter except: show_exception_dialog()