[kupfer] objects, helplib: Move Mixins to helplib
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [kupfer] objects, helplib: Move Mixins to helplib
- Date: Wed, 9 Sep 2009 19:17:30 +0000 (UTC)
commit 724b403b1fb58c15c52ab5ea32abd89a9df52c2c
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date: Wed Sep 9 21:02:49 2009 +0200
objects, helplib: Move Mixins to helplib
We collect helper constructs in helplib, so we move the
PicklingHelperMixin and the FilesystemWatchMixin there.
The Mixins are still imported into objects.py; they are needed there,
but plugins importing them there will not have cache breaks either.
We also rename to _nonpersistent_token to NonpersistentToken; we fix
this ugly classname in c13; then it has never been part of any release
(apart from stillborn c12).
kupfer/helplib.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++
kupfer/objects.py | 76 +---------------------------------------------------
2 files changed, 78 insertions(+), 75 deletions(-)
---
diff --git a/kupfer/helplib.py b/kupfer/helplib.py
index a126306..6fbe533 100644
--- a/kupfer/helplib.py
+++ b/kupfer/helplib.py
@@ -7,6 +7,83 @@ more information.
import weakref
+import gio
+
+class PicklingHelperMixin (object):
+ """ This pickling helper will define __getstate__/__setstate__
+ acting simply on the class dictionary; it is up to the inheriting
+ class to set up:
+ pickle_prepare:
+ Modify the instance dict to remove any unpickleable attributes,
+ the resulting dict will be pickled
+ unpickle_finish:
+ Finish unpickling by restoring nonpickled attributes from the
+ saved class dict, or setting up change callbacks or similar
+ """
+ def pickle_prepare(self):
+ pass
+ def unpickle_finish(self):
+ pass
+ def __getstate__(self):
+ """On pickle, getstate will call self.pickle_prepare(),
+ then it will return the class' current __dict__
+ """
+ self.pickle_prepare()
+ return self.__dict__
+
+ def __setstate__(self, state):
+ """On unpickle, setstate will restore the class' __dict__,
+ then call self.unpickle_finish()
+ """
+ self.__dict__.update(state)
+ self.unpickle_finish()
+
+class NonpersistentToken (PicklingHelperMixin):
+ """A token will keep a reference until pickling, when it is deleted"""
+ def __init__(self, data):
+ self.data = data
+ def __nonzero__(self):
+ return self.data
+ def pickle_prepare(self):
+ self.data = None
+
+class FilesystemWatchMixin (object):
+ """A mixin for Sources watching directories"""
+
+ def monitor_directories(self, *directories):
+ """Register @directories for monitoring;
+
+ On changes, the Source will be marked for update.
+ This method returns a monitor token that has to be
+ stored for the monitor to be active.
+
+ The token will be a false value if nothing could be monitored.
+
+ Nonexisting directories are skipped.
+ """
+ tokens = []
+ for directory in directories:
+ gfile = gio.File(directory)
+ if not gfile.query_exists():
+ continue
+ monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
+ if monitor:
+ monitor.connect("changed", self.__directory_changed)
+ tokens.append(monitor)
+ return NonpersistentToken(tokens)
+
+ def monitor_include_file(self, gfile):
+ """Return whether @gfile should trigger an update event
+ by default, files beginning with "." are ignored
+ """
+ return not (gfile and gfile.get_basename().startswith("."))
+
+ def __directory_changed(self, monitor, file1, file2, evt_type):
+ if (evt_type in (gio.FILE_MONITOR_EVENT_CREATED,
+ gio.FILE_MONITOR_EVENT_DELETED) and
+ self.monitor_include_file(file1)):
+ self.mark_for_update()
+
class WeakCallback (object):
"""A Weak Callback object that will keep a reference to
the connecting object with weakref semantics.
diff --git a/kupfer/objects.py b/kupfer/objects.py
index 68ba8af..957332b 100644
--- a/kupfer/objects.py
+++ b/kupfer/objects.py
@@ -17,6 +17,7 @@ import gio
from kupfer import pretty
from kupfer import icons, launch, utils
from kupfer.utils import locale_sort
+from kupfer.helplib import PicklingHelperMixin, FilesystemWatchMixin
class Error (Exception):
pass
@@ -188,35 +189,6 @@ class Leaf (KupferObject):
"""Default (builtin) actions for this Leaf"""
return ()
-class PicklingHelperMixin (object):
- """ This pickling helper will define __getstate__/__setstate__
- acting simply on the class dictionary; it is up to the inheriting
- class to set up:
- pickle_prepare:
- Modify the instance dict to remove any unpickleable attributes,
- the resulting dict will be pickled
- unpickle_finish:
- Finish unpickling by restoring nonpickled attributes from the
- saved class dict, or setting up change callbacks or similar
- """
- def pickle_prepare(self):
- pass
- def unpickle_finish(self):
- pass
- def __getstate__(self):
- """On pickle, getstate will call self.pickle_prepare(),
- then it will return the class' current __dict__
- """
- self.pickle_prepare()
- return self.__dict__
-
- def __setstate__(self, state):
- """On unpickle, setstate will restore the class' __dict__,
- then call self.unpickle_finish()
- """
- self.__dict__.update(state)
- self.unpickle_finish()
-
class DummyLeaf (Leaf):
"""
Dummy Leaf, representing No Leaf available
@@ -855,52 +827,6 @@ class Source (KupferObject, pretty.OutputMixin):
"""
return ()
-class _nonpersistent_token (PicklingHelperMixin):
- """A token will keep a reference until pickling, when it is deleted"""
- def __init__(self, data):
- self.data = data
- def __nonzero__(self):
- return self.data
- def pickle_prepare(self):
- self.data = None
-
-class FilesystemWatchMixin (object):
- """A mixin for Sources watching directories"""
-
- def monitor_directories(self, *directories):
- """Register @directories for monitoring;
-
- On changes, the Source will be marked for update.
- This method returns a monitor token that has to be
- stored for the monitor to be active.
-
- The token will be a false value if nothing could be monitored.
-
- Nonexisting directories are skipped.
- """
- tokens = []
- for directory in directories:
- gfile = gio.File(directory)
- if not gfile.query_exists():
- continue
- monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
- if monitor:
- monitor.connect("changed", self.__directory_changed)
- tokens.append(monitor)
- return _nonpersistent_token(tokens)
-
- def monitor_include_file(self, gfile):
- """Return whether @gfile should trigger an update event
- by default, files beginning with "." are ignored
- """
- return not (gfile and gfile.get_basename().startswith("."))
-
- def __directory_changed(self, monitor, file1, file2, evt_type):
- if (evt_type in (gio.FILE_MONITOR_EVENT_CREATED,
- gio.FILE_MONITOR_EVENT_DELETED) and
- self.monitor_include_file(file1)):
- self.mark_for_update()
-
class FileSource (Source):
def __init__(self, dirlist, depth=0):
"""
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]