def __init__(self, enum, default=None, filter=(), **kwargs): """ Args: enum: Instance of Enum default: Name of default value filter: list of str. If not empty, only names in filter is allowed to use. """ AttrType.__init__(self, '%s#' % enum.name, **kwargs) self.enum = enum assert not default or enum.has_name(default), u'ATEnum %s' % enum.name if not self.enum.has_name(default): if default: log.error('invalid default value for enum %s: %s' % (self.name, default)) default = enum.names[0] self._default = default if filter: for name in filter: if not enum.has_name(name): log.error('invalid filter for enum %s: %s', self.name, name) self._filter = filter
def log_all(self, project, with_path=True): _ = with_path fs_path = '' if self._uuid: obj = project.object_manager.get_object(self._uuid) if obj: from structer import fs_util fs_path = fs_util.get_object_path(project, obj) for e in self._errors: log.error(u'%s %s %s %s', project.name, fs_path, e.path, e.str())
def verify(self, val, project, recurse=True, vlog=None): u"""verify the value""" if vlog is None: vlog = AttrVerifyLogger() self._verify(val, project, recurse, vlog) if self.verifier: try: self.verifier(self, val, project, vlog.error) except Exception, e: log.error(e, 'error in verifier of %s', self.name) vlog.error('verifier error: %s %s', e.__class__.__name__, str(e))
def process(self, evt): keys = evt.get_keys() for key in keys: call_backs = self._listeners.get(key) # todo other complicated situations if call_backs: call_backs = copy.copy(call_backs) for call_back in call_backs: try: call_back(evt) except Exception, e: log.error(e, 'error in callback: %s', key)
def redo(self, grid): print 'UndoManager.redo' if self._index >= len(self._history)-1: return self._index += 1 act = self._history[self._index] print ' ', act self._lock = True try: act.redo(grid) except Exception, e: log.error(e, 'redo failed') wx.MessageBox("Redo failed, reset history: %s\n" % e) self._reset()
def redo(self, grid): print 'UndoManager.redo' if self._index >= len(self._history) - 1: return self._index += 1 act = self._history[self._index] print ' ', act self._lock = True try: act.redo(grid) except Exception, e: log.error(e, 'redo failed') wx.MessageBox("Redo failed, reset history: %s\n" % e) self._reset()
def move(self, node, parent, strategy=FOLDER_CONFLICTION_STRATEGY_ABORT): """Moves a file/folder to another location Args: node: file/folder to move parent: target location strategy: FOLDER_CONFLICTION_STRATEGY_*. what to do if a folder with the same name already exists? """ assert not node.immutable if self.is_ancestor(node, parent): raise Exception('Could not move to its descendant.') old_parent = node.parent if old_parent == parent: return old_parent if isinstance(node, Folder): dup = parent.get_sub_folder_by_name(node.name) if dup: # name collision if strategy == FOLDER_CONFLICTION_STRATEGY_ABORT: raise FolderConflictionException( 'Folder with the same already exists.') if strategy == FOLDER_CONFLICTION_STRATEGY_RENAME: # auto rename i, name = 0, None while 1: name = '%s (%s)' % (node.name, i) if not parent.get_sub_folder_by_name(name): break i += 1 node.name = name elif strategy == FOLDER_CONFLICTION_STRATEGY_IGNORE: pass elif strategy == FOLDER_CONFLICTION_STRATEGY_MERGE: try: for child in node.children: self.move(child, dup, strategy) except Exception, e: log.error(e, "Not all files/folders moved!") raise self.delete(node.uuid) return old_parent elif strategy == FOLDER_CONFLICTION_STRATEGY_REPLACE: self.delete(dup.uuid)
def undo(self, grid): print 'UndoManager.undo' if self._index < 0: return act = self._history[self._index] print ' ', act self._index -= 1 self._lock = True try: act.undo(grid) except Exception, e: log.error(e, 'undo failed') wx.MessageBox("Undo failed, reset history: %s\n" % e) self._reset()
def move(self, node, parent, strategy=FOLDER_CONFLICTION_STRATEGY_ABORT): """Moves a file/folder to another location Args: node: file/folder to move parent: target location strategy: FOLDER_CONFLICTION_STRATEGY_*. what to do if a folder with the same name already exists? """ assert not node.immutable if self.is_ancestor(node, parent): raise Exception('Could not move to its descendant.') old_parent = node.parent if old_parent == parent: return old_parent if isinstance(node, Folder): dup = parent.get_sub_folder_by_name(node.name) if dup: # name collision if strategy == FOLDER_CONFLICTION_STRATEGY_ABORT: raise FolderConflictionException('Folder with the same already exists.') if strategy == FOLDER_CONFLICTION_STRATEGY_RENAME: # auto rename i, name = 0, None while 1: name = '%s (%s)' % (node.name, i) if not parent.get_sub_folder_by_name(name): break i += 1 node.name = name elif strategy == FOLDER_CONFLICTION_STRATEGY_IGNORE: pass elif strategy == FOLDER_CONFLICTION_STRATEGY_MERGE: try: for child in node.children: self.move(child, dup, strategy) except Exception, e: log.error(e, "Not all files/folders moved!") raise self.delete(node.uuid) return old_parent elif strategy == FOLDER_CONFLICTION_STRATEGY_REPLACE: self.delete(dup.uuid)
def __init__(self, element_type, unique=False, minlen=0, maxlen=0x7FFFFFFF, delimiter='; ', unique_attrs=(), **kwargs): """ unique_attrs: If element type is ATStruct, attributes in unique_attrs must be unique. """ AttrType.__init__(self, "[%s]" % element_type.name, **kwargs) self.element_type = element_type self._unique = unique self._minlen = minlen self._maxlen = maxlen self._delimiter = delimiter self._unique_attrs = unique_attrs if unique_attrs and type(element_type) is not ATStruct: log.error('"unique_attrs" can only be applied to list of structs') self._default = []