Exemplo n.º 1
0
    def _export(self):
        if self.project.has_error(True):
            log.alert("All errors must be fixed before exporting.")
            return

        wildcard = 'Zip archive (*.zip)|*.zip'
        dlg = wx.FileDialog(self, message="Choose export location",
                            # defaultDir=self.project.path,
                            # defaultFile="",
                            wildcard=wildcard,
                            style=wx.SAVE | wx.FD_OVERWRITE_PROMPT)
        if wx.ID_CANCEL == dlg.ShowModal():
            dlg.Destroy()
            return False

        path = dlg.GetPath()
        dlg.Destroy()

        zf = None
        try:
            from zipfile import ZipFile
            zf = ZipFile(path, 'w')

            from structer.exporter import DefaultObjectExporter
            # from structer.exporter.type_exporter import JsTypeExporter

            for exporter_class in (DefaultObjectExporter, ):
                e = exporter_class(self.project)
                e.export()
                for name, data in e.get_files().iteritems():
                    zf.writestr(name, data)
        except Exception, e:
            log.alert(e, "Failed to export")
            return False
Exemplo n.º 2
0
 def on_menu(self, evt, action_name):
     _ = evt
     nt = self.get_node_tool()
     ctrl = self.get_focused_ctrl()
     
     if ctrl and nt:
         try:
             mth = getattr(nt, 'do_%s' % action_name)                    
             mth(ctrl.get_selected_nodes())
         except Exception, e:
             log.alert(e, 'Failed to %s', action_name)
Exemplo n.º 3
0
    def on_menu(self, evt, action_name):
        _ = evt
        nt = self.get_node_tool()
        ctrl = self.get_focused_ctrl()

        if ctrl and nt:
            try:
                mth = getattr(nt, 'do_%s' % action_name)
                mth(ctrl.get_selected_nodes())
            except Exception, e:
                log.alert(e, 'Failed to %s', action_name)
Exemplo n.º 4
0
 def OnClose(self, evt):        
     self.editor_panel.grid.DisableCellEditControl()
     
     # debug
     if self.editor_context.read_only and self.editor_context.is_modified():            
         log.error('modified in a readonly frame!')
     
     if not self.editor_context.read_only and self.editor_context.is_modified():
         self.Raise()
         r = wx.MessageBox("Save before exit?", "Confirm",  wx.YES_NO | wx.CANCEL, self)
         if r == wx.YES:
             if not self.save():
                 log.alert("save failed!")                                        
                 return                
         elif r == wx.CANCEL:                    
             return
     
     self.editor_context.project.explorer.remove_editor(self)
     evt.Skip()
Exemplo n.º 5
0
    def OnClose(self, evt):
        self.editor_panel.grid.DisableCellEditControl()

        # debug
        if self.editor_context.read_only and self.editor_context.is_modified():
            log.error('modified in a readonly frame!')

        if not self.editor_context.read_only and self.editor_context.is_modified(
        ):
            self.Raise()
            r = wx.MessageBox("Save before exit?", "Confirm",
                              wx.YES_NO | wx.CANCEL, self)
            if r == wx.YES:
                if not self.save():
                    log.alert("save failed!")
                    return
            elif r == wx.CANCEL:
                return

        self.editor_context.project.explorer.remove_editor(self)
        evt.Skip()
Exemplo n.º 6
0
    def _export(self):
        if self.project.has_error(True):
            log.alert("All errors must be fixed before exporting.")
            return

        wildcard = 'Zip archive (*.zip)|*.zip'
        dlg = wx.FileDialog(
            self,
            message="Choose export location",
            # defaultDir=self.project.path,
            # defaultFile="",
            wildcard=wildcard,
            style=wx.SAVE | wx.FD_OVERWRITE_PROMPT)
        if wx.ID_CANCEL == dlg.ShowModal():
            dlg.Destroy()
            return False

        path = dlg.GetPath()
        dlg.Destroy()

        zf = None
        try:
            from zipfile import ZipFile
            zf = ZipFile(path, 'w')

            from structer.exporter import DefaultObjectExporter
            # from structer.exporter.type_exporter import JsTypeExporter

            for exporter_class in (DefaultObjectExporter, ):
                e = exporter_class(self.project)
                e.export()
                for name, data in e.get_files().iteritems():
                    zf.writestr(name, data)
        except Exception, e:
            log.alert(e, "Failed to export")
            return False
Exemplo n.º 7
0
    def save(self):
        """Save all data

        Returns:
            True/False
        """
        success = True
        if self.is_batch():
            new, delete = self.check_save()
            if new or delete:
                msg = 'Are you sure to:\n'
                if new:
                    msg += "  Create %s %s(s)\n" % (len(new), self.clazz.name)
                if delete:
                    msg += " Delete %s %s(s)\n" % (len(delete), self.clazz.name)
                msg += "?"
                import wx
                if wx.MessageBox(msg, style=wx.YES_NO | wx.ICON_WARNING) == wx.NO:
                    return False                
                                    
            objects = copy.copy(self._objects)
            
            fs_node = self.project.fs_manager.get_node_by_uuid(objects.keys()[0])
            fs_folder = fs_node.parent
            
            for data in self.attr_data:
                uuid = data.get('__uuid__')
                data2 = copy.deepcopy(data)
                
                if uuid:  # existing obj
                    data2.pop('__uuid__')
                    obj = objects.get(uuid)                    
                    if obj:
                        objects.pop(uuid)
                        if self.clazz.atstruct.compare(data2, obj.raw_data) != 0:
                            obj.raw_data = data2
                            try:
                                self.project.save_object(obj)
                            except Exception, e:
                                log.alert(e, 'Failed to save %s', obj)
                                success = False
                    else:
                        log.alert('Internal error. Editing object not found: %s', uuid)
                        success = False
                else:  # new obj
                    try:
                        obj = self.project.create_object(fs_folder, self.clazz, data2)
                        self._objects[obj.uuid] = obj
                        data['__uuid__'] = obj.uuid
                    except Exception, e:
                        log.alert(e, "Failed to create object: %s", data2)
                        success = False                    
Exemplo n.º 8
0
class FrameEditorContext(EditorContext):
    def __init__(self, project, objects, readonly=False):
        """
        Args:
            objects: can be one object, or a list of objects
        """
        
        if type(objects) is list:
            self._objects = dict([(obj.uuid, obj) for obj in objects])
            clazz = objects[0].clazz
            attr_type = ATList(clazz.atstruct, unique_attrs=clazz.unique_attrs)
            attr_data = []
            for obj in objects:
                data = copy.deepcopy(obj.raw_data)
                # use this hidden value to locate which object this data belongs to
                data['__uuid__'] = obj.uuid
                attr_data.append(data)
        else:
            self._objects = objects            
            clazz = objects.clazz
            attr_type = clazz.atstruct
            attr_data = copy.deepcopy(objects.raw_data)
        
        self.clazz = clazz
        EditorContext.__init__(self, project, attr_type, attr_data, UndoManager(), readonly)
    
#     @property
#     def objects(self):
#         return self._objects
    def is_batch(self):
        return type(self._objects) is dict
        
    def contains_object(self, obj):
        if self.is_batch():
            return obj.uuid in self._objects
        
        return obj == self._objects 
    
    def check_save(self):
        """Check uuid changes before saving
        
        Returns:
            (new_uuids, delete_uuids). 
            new_uuids is a list of uuids which are going to be created when saved
            delete_uuids is a list of uuids which are going to be deleted when saved
        """
        
        if not self.is_batch():
            return [], []
        
        objects = copy.copy(self._objects)
        new = []
        for data in self.attr_data:
            uuid = data.get('__uuid__')
            if uuid:  # existing obj                
                objects.pop(uuid)                        
            else:  # new obj
                new.append(uuid)
                                            
        delete = objects.keys()
        return new, delete
    
    def is_modified(self):        
        cmp_ = self.clazz.atstruct.compare
        
        if self.is_batch():
            if len(self._objects) != len(self.attr_data):
                return True
            for data in self.attr_data:
                uuid = data.get('__uuid__')
                obj = self._objects.get(uuid)
                if not obj:
                    return True
                if cmp_(data, obj.raw_data) != 0:
                    return True            
            return False
        
        else:
            return cmp_(self.attr_data, self._objects.raw_data) != 0
    
    def save(self):
        """Save all data

        Returns:
            True/False
        """
        success = True
        if self.is_batch():
            new, delete = self.check_save()
            if new or delete:
                msg = 'Are you sure to:\n'
                if new:
                    msg += "  Create %s %s(s)\n" % (len(new), self.clazz.name)
                if delete:
                    msg += " Delete %s %s(s)\n" % (len(delete), self.clazz.name)
                msg += "?"
                import wx
                if wx.MessageBox(msg, style=wx.YES_NO | wx.ICON_WARNING) == wx.NO:
                    return False                
                                    
            objects = copy.copy(self._objects)
            
            fs_node = self.project.fs_manager.get_node_by_uuid(objects.keys()[0])
            fs_folder = fs_node.parent
            
            for data in self.attr_data:
                uuid = data.get('__uuid__')
                data2 = copy.deepcopy(data)
                
                if uuid:  # existing obj
                    data2.pop('__uuid__')
                    obj = objects.get(uuid)                    
                    if obj:
                        objects.pop(uuid)
                        if self.clazz.atstruct.compare(data2, obj.raw_data) != 0:
                            obj.raw_data = data2
                            try:
                                self.project.save_object(obj)
                            except Exception, e:
                                log.alert(e, 'Failed to save %s', obj)
                                success = False
                    else:
                        log.alert('Internal error. Editing object not found: %s', uuid)
                        success = False
                else:  # new obj
                    try:
                        obj = self.project.create_object(fs_folder, self.clazz, data2)
                        self._objects[obj.uuid] = obj
                        data['__uuid__'] = obj.uuid
                    except Exception, e:
                        log.alert(e, "Failed to create object: %s", data2)
                        success = False                    
            
            # to be deleted
            for obj in objects.itervalues():
                try:
                    self.project.delete_object(obj)
                    self._objects.pop(obj.uuid)
                except Exception, e:
                    log.alert(e, "Failed to delete object: %s", obj)
                    success = False
Exemplo n.º 9
0
 def OnMenu_menu_open(self, evt):
     try:
         self._open()
     except Exception, e:
         from structerui import log
         log.alert(e, 'Failed to open/create project.')