Beispiel #1
0
    def DoSaveValue(self, obj, keyName, value):
        """
        Method used by the persistent objects to save the data.

        By default this method simply use :class:`FileConfig` but this behaviour may be
        overridden by passing a custom configuration handler in the :class:`PersistenceManager`
        constructor.

        :param `obj`: an instance of :class:`PersistentObject`;
        :param `keyName`: a string specifying the key name;
        :param `value`: the value to store in the configuration file.
        """

        kind = repr(value.__class__).split("'")[1]

        if self._customConfigHandler is not None:
            result = self._customConfigHandler.SaveValue(
                self.GetKey(obj, keyName), repr((kind, six.text_type(value))))
        else:
            config = self.GetPersistenceFile()
            result = config.Write(self.GetKey(obj, keyName),
                                  repr((kind, six.text_type(value))))
            config.Flush()

        return result
Beispiel #2
0
 def getFullName(self, item, partial=''):
     """Return a syntactically proper name for item."""
     name = self.GetItemText(item)
     parent = None
     obj = None
     if item != self.root:
         parent = self.GetItemParent(item)
         obj = self.GetItemData(parent)
     # Apply dictionary syntax to dictionary items, except the root
     # and first level children of a namepace.
     if ((isinstance(obj, dict)
         or 'BTrees' in six.text_type(type(obj))
         and hasattr(obj, 'keys'))
         and ((item != self.root and parent != self.root)
         or (parent == self.root and not self.rootIsNamespace))):
         name = '[' + name + ']'
     # Apply dot syntax to multipart names.
     if partial:
         if partial[0] == '[':
             name += partial
         else:
             name += '.' + partial
     # Repeat for everything but the root item
     # and first level children of a namespace.
     if (item != self.root and parent != self.root) \
     or (parent == self.root and not self.rootIsNamespace):
         name = self.getFullName(parent, partial=name)
     return name
Beispiel #3
0
 def getFullName(self, item, partial=''):
     """Return a syntactically proper name for item."""
     name = self.GetItemText(item)
     parent = None
     obj = None
     if item != self.root:
         parent = self.GetItemParent(item)
         obj = self.GetItemData(parent)
     # Apply dictionary syntax to dictionary items, except the root
     # and first level children of a namepace.
     if ((isinstance(obj, dict)
          or 'BTrees' in six.text_type(type(obj)) and hasattr(obj, 'keys'))
             and ((item != self.root and parent != self.root) or
                  (parent == self.root and not self.rootIsNamespace))):
         name = '[' + name + ']'
     # Apply dot syntax to multipart names.
     if partial:
         if partial[0] == '[':
             name += partial
         else:
             name += '.' + partial
     # Repeat for everything but the root item
     # and first level children of a namespace.
     if (item != self.root and parent != self.root) \
     or (parent == self.root and not self.rootIsNamespace):
         name = self.getFullName(parent, partial=name)
     return name
Beispiel #4
0
 def display(self):
     item = self.item
     if not item:
         return
     if self.IsExpanded(item):
         self.addChildren(item)
     self.setText('')
     obj = self.GetItemData(item)
     if wx.Platform == '__WXMSW__':
         if obj is None:  # Windows bug fix.
             return
     self.SetItemHasChildren(item, self.objHasChildren(obj))
     otype = type(obj)
     text = ''
     text += self.getFullName(item)
     text += '\n\nType: ' + six.text_type(otype)
     try:
         value = six.text_type(obj)
     except Exception:
         value = ''
     if isinstance(obj, six.string_types):
         value = repr(obj)
     text += u'\n\nValue: ' + value
     if otype not in SIMPLETYPES:
         try:
             text += '\n\nDocstring:\n\n"""' + \
                     inspect.getdoc(obj).strip() + '"""'
         except Exception:
             pass
     if isinstance(obj, six.class_types):
         try:
             text += '\n\nClass Definition:\n\n' + \
                     inspect.getsource(obj.__class__)
         except Exception:
             pass
     else:
         try:
             text += '\n\nSource Code:\n\n' + \
                     inspect.getsource(obj)
         except Exception:
             pass
     self.setText(text)
Beispiel #5
0
 def display(self):
     item = self.item
     if not item:
         return
     if self.IsExpanded(item):
         self.addChildren(item)
     self.setText('')
     obj = self.GetItemData(item)
     if wx.Platform == '__WXMSW__':
         if obj is None: # Windows bug fix.
             return
     self.SetItemHasChildren(item, self.objHasChildren(obj))
     otype = type(obj)
     text = ''
     text += self.getFullName(item)
     text += '\n\nType: ' + six.text_type(otype)
     try:
         value = six.text_type(obj)
     except Exception:
         value = ''
     if isinstance(obj, six.string_types):
         value = repr(obj)
     text += u'\n\nValue: ' + value
     if otype not in SIMPLETYPES:
         try:
             text += '\n\nDocstring:\n\n"""' + \
                     inspect.getdoc(obj).strip() + '"""'
         except Exception:
             pass
     if isinstance(obj, six.class_types):
         try:
             text += '\n\nClass Definition:\n\n' + \
                     inspect.getsource(obj.__class__)
         except Exception:
             pass
     else:
         try:
             text += '\n\nSource Code:\n\n' + \
                     inspect.getsource(obj)
         except Exception:
             pass
     self.setText(text)
Beispiel #6
0
 def addChildren(self, item):
     self.DeleteChildren(item)
     obj = self.GetItemData(item)
     children = self.objGetChildren(obj)
     if not children:
         return
     keys = sorted(children.keys(), key=lambda x: six.text_type(x).lower())
     for key in keys:
         itemtext = six.text_type(key)
         # Show string dictionary items with single quotes, except
         # for the first level of items, if they represent a
         # namespace.
         if isinstance(obj, dict) \
         and isinstance(key, six.string_types) \
         and (item != self.root
              or (item == self.root and not self.rootIsNamespace)):
             itemtext = repr(key)
         child = children[key]
         branch = self.AppendItem(parent=item, text=itemtext, data=child)
         self.SetItemHasChildren(branch, self.objHasChildren(child))
Beispiel #7
0
 def addChildren(self, item):
     self.DeleteChildren(item)
     obj = self.GetItemData(item)
     children = self.objGetChildren(obj)
     if not children:
         return
     keys = sorted(children.keys(), key=lambda x: six.text_type(x).lower())
     for key in keys:
         itemtext = six.text_type(key)
         # Show string dictionary items with single quotes, except
         # for the first level of items, if they represent a
         # namespace.
         if isinstance(obj, dict) \
         and isinstance(key, six.string_types) \
         and (item != self.root
              or (item == self.root and not self.rootIsNamespace)):
             itemtext = repr(key)
         child = children[key]
         branch = self.AppendItem(parent=item, text=itemtext, data=child)
         self.SetItemHasChildren(branch, self.objHasChildren(child))
Beispiel #8
0
 def objGetChildren(self, obj):
     """Return dictionary with attributes or contents of object."""
     busy = wx.BusyCursor()
     otype = type(obj)
     if (isinstance(obj, dict)
             or 'BTrees' in six.text_type(otype) and hasattr(obj, 'keys')):
         return obj
     d = {}
     if isinstance(obj, (list, tuple)):
         for n in range(len(obj)):
             key = '[' + six.text_type(n) + ']'
             d[key] = obj[n]
     if otype not in COMMONTYPES:
         for key in introspect.getAttributeNames(obj):
             # Believe it or not, some attributes can disappear,
             # such as the exc_traceback attribute of the sys
             # module. So this is nested in a try block.
             try:
                 d[key] = getattr(obj, key)
             except Exception:
                 pass
     return d
Beispiel #9
0
 def objGetChildren(self, obj):
     """Return dictionary with attributes or contents of object."""
     busy = wx.BusyCursor()
     otype = type(obj)
     if (isinstance(obj, dict)
         or 'BTrees' in six.text_type(otype)
         and hasattr(obj, 'keys')):
         return obj
     d = {}
     if isinstance(obj, (list, tuple)):
         for n in range(len(obj)):
             key = '[' + six.text_type(n) + ']'
             d[key] = obj[n]
     if otype not in COMMONTYPES:
         for key in introspect.getAttributeNames(obj):
             # Believe it or not, some attributes can disappear,
             # such as the exc_traceback attribute of the sys
             # module. So this is nested in a try block.
             try:
                 d[key] = getattr(obj, key)
             except Exception:
                 pass
     return d
    def DoSaveValue(self, obj, keyName, value):
        """
        Method used by the persistent objects to save the data.

        By default this method simply use :class:`FileConfig` but this behaviour may be
        overridden by passing a custom configuration handler in the :class:`PersistenceManager`
        constructor.

        :param `obj`: an instance of :class:`PersistentObject`;
        :param `keyName`: a string specifying the key name;
        :param `value`: the value to store in the configuration file.
        """

        kind = repr(value.__class__).split("'")[1]

        if self._customConfigHandler is not None:
            result = self._customConfigHandler.SaveValue(self.GetKey(obj, keyName), repr((kind, six.text_type(value))))
        else:
            config = self.GetPersistenceFile()
            result = config.Write(self.GetKey(obj, keyName), repr((kind, six.text_type(value))))
            config.Flush()

        return result