[kupfer] core: Break up core plugin into a package of submodules



commit df567420cbf642d0601c112c5d8db944d9db3fcd
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Tue Dec 15 15:09:58 2009 +0100

    core: Break up core plugin into a package of submodules
    
    This way it is easier to structure the different features that we
    include in kupfer by using the core plugin.

 kupfer/plugin/core.py          |  249 ----------------------------------------
 kupfer/plugin/core/__init__.py |   49 ++++++++
 kupfer/plugin/core/contents.py |  102 ++++++++++++++++
 kupfer/plugin/core/debug.py    |  136 ++++++++++++++++++++++
 4 files changed, 287 insertions(+), 249 deletions(-)
---
diff --git a/kupfer/plugin/core/__init__.py b/kupfer/plugin/core/__init__.py
new file mode 100644
index 0000000..3da7d25
--- /dev/null
+++ b/kupfer/plugin/core/__init__.py
@@ -0,0 +1,49 @@
+import gtk
+
+from kupfer.objects import Leaf, Action, Source
+from kupfer import objects
+
+__kupfer_name__ = u"Core"
+__kupfer_sources__ = ()   # Updated later
+__kupfer_actions__ = (    # Updated later
+	"SearchInside",
+	)
+__description__ = u"Core actions and items"
+__version__ = ""
+__author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
+
+def _register_subplugin(module):
+	global __kupfer_sources__
+	global __kupfer_actions__
+	__kupfer_sources__ += module.__kupfer_sources__
+	__kupfer_actions__ += module.__kupfer_actions__
+	globals().update((attr, getattr(module, attr)) for attr in module.__all__)
+
+from kupfer.plugin.core import contents, debug
+_register_subplugin(contents)
+_register_subplugin(debug)
+
+class SearchInside (Action):
+	"""
+	A factory action: works on a Leaf object with content
+	Return a new source with the contents of the Leaf
+	"""
+	def __init__(self):
+		super(SearchInside, self).__init__(_("Search Content..."))
+
+	def is_factory(self):
+		return True
+	def activate(self, leaf):
+		if not leaf.has_content():
+			raise objects.InvalidLeafError("Must have content")
+		return leaf.content_source()
+
+	def item_types(self):
+		yield Leaf
+	def valid_for_item(self, leaf):
+		return leaf.has_content()
+
+	def get_description(self):
+		return _("Search inside this catalog")
+	def get_icon_name(self):
+		return "search"
diff --git a/kupfer/plugin/core/contents.py b/kupfer/plugin/core/contents.py
new file mode 100644
index 0000000..8ca3109
--- /dev/null
+++ b/kupfer/plugin/core/contents.py
@@ -0,0 +1,102 @@
+import gtk
+
+from kupfer.objects import Leaf, Action, Source
+from kupfer.objects import RunnableLeaf, AppLeafContentMixin
+from kupfer import objects, pretty
+from kupfer import kupferui
+
+__kupfer_sources__ = ("KupferSource", )
+__kupfer_actions__ = ()
+__author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
+
+__all__ = __kupfer_sources__ + __kupfer_actions__
+
+def _is_debug():
+	# Return True if Kupfer is in debug mode
+	return pretty.debug
+
+class DebugRestart (RunnableLeaf):
+	def __init__(self):
+		RunnableLeaf.__init__(self, None, u"Restart Kupfer")
+
+	@classmethod
+	def _exec_new_kupfer(cls, executable, argv):
+		import os
+		os.execvp(executable, [executable] + argv)
+
+	def run(self):
+		import atexit
+		import sys
+		gtk.main_quit()
+		atexit.register(self._exec_new_kupfer, sys.executable, sys.argv)
+
+	def get_description(self):
+		return u"Restart Kupfer quickly (for internal kupfer use)"
+	def get_icon_name(self):
+		return gtk.STOCK_REFRESH
+
+class Quit (RunnableLeaf):
+	qf_id = "quit"
+	def __init__(self, name=None):
+		if not name: name = _("Quit")
+		super(Quit, self).__init__(name=name)
+	def run(self):
+		gtk.main_quit()
+	def get_description(self):
+		return _("Quit Kupfer")
+	def get_icon_name(self):
+		return gtk.STOCK_QUIT
+
+class About (RunnableLeaf):
+	def __init__(self, name=None):
+		if not name: name = _("About Kupfer")
+		super(About, self).__init__(name=name)
+	def run(self):
+		kupferui.show_about_dialog()
+	def get_description(self):
+		return _("Show information about Kupfer authors and license")
+	def get_icon_name(self):
+		return gtk.STOCK_ABOUT
+
+class Help (RunnableLeaf):
+	def __init__(self, name=None):
+		if not name: name = _("Kupfer Help")
+		super(Help, self).__init__(name=name)
+	def run(self):
+		kupferui.show_help()
+	def get_description(self):
+		return _("Get help with Kupfer")
+	def get_icon_name(self):
+		return "help-browser"
+
+class Preferences (RunnableLeaf):
+	def __init__(self, name=None):
+		if not name: name = _("Kupfer Preferences")
+		super(Preferences, self).__init__(name=name)
+	def run(self):
+		kupferui.show_preferences()
+	def get_description(self):
+		return _("Show preferences window for Kupfer")
+	def get_icon_name(self):
+		return gtk.STOCK_PREFERENCES
+
+class KupferSource (AppLeafContentMixin, Source):
+	appleaf_content_id = "kupfer"
+	def __init__(self, name=_("Kupfer")):
+		Source.__init__(self, name)
+	def is_dynamic(self):
+		return True
+	def get_items(self):
+		yield Preferences()
+		yield Help()
+		yield About()
+		yield Quit()
+		if _is_debug():
+			yield DebugRestart()
+
+	def get_description(self):
+		return _("Kupfer items and actions")
+	def get_icon_name(self):
+		return "search"
+	def provides(self):
+		yield RunnableLeaf
diff --git a/kupfer/plugin/core/debug.py b/kupfer/plugin/core/debug.py
new file mode 100644
index 0000000..ec8598c
--- /dev/null
+++ b/kupfer/plugin/core/debug.py
@@ -0,0 +1,136 @@
+from kupfer.objects import Action, Leaf
+from kupfer import objects
+from kupfer import pretty
+
+__kupfer_sources__ = ()
+__kupfer_actions__ = (
+		"Rescan",
+		"DebugInfo",
+	)
+__author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
+
+__all__ = __kupfer_sources__ + __kupfer_actions__
+
+def _is_debug():
+	# Return True if Kupfer is in debug mode
+	return pretty.debug
+
+class Rescan (Action):
+	"""A source action: Rescan a source!
+
+	This is a debug action because normal users should not need to use it;
+	it is only confusing and inconsistent:
+
+	* Sources with internal caching don't really rescan anyway.
+	* Change callbacks make it redundant
+	"""
+	rank_adjust = -5
+	def __init__(self):
+		Action.__init__(self, _("Rescan"))
+
+	def activate(self, leaf):
+		if not leaf.has_content():
+			raise objects.InvalidLeafError("Must have content")
+		source = leaf.content_source()
+		source.get_leaves(force_update=True)
+
+	def get_description(self):
+		return _("Force reindex of this source")
+	def get_icon_name(self):
+		return "gtk-refresh"
+
+	def item_types(self):
+		if _is_debug():
+			yield objects.AppLeaf
+			yield objects.SourceLeaf
+	def valid_for_item(self, item):
+		if not item.has_content():
+			return False
+		return not item.content_source().is_dynamic()
+
+
+class DebugInfo (Action):
+	""" Print debug info to terminal """
+	rank_adjust = -50
+	def __init__(self):
+		Action.__init__(self, u"Debug Info")
+
+	def activate(self, leaf):
+		import itertools
+		import StringIO
+		from kupfer import qfurl
+		from kupfer import uiutils
+
+		output = StringIO.StringIO()
+		def print_func(*args):
+			print >>output, " ".join(unicode(a) for a in args)
+			pretty.print_debug("debug", *args)
+
+		print_func("Debug info about", leaf)
+		print_func(leaf, repr(leaf))
+		def get_qfurl(leaf):
+			try:
+				return qfurl.qfurl(leaf)
+			except qfurl.QfurlError:
+				pass
+		def get_object_fields(leaf):
+			return {
+				"repr" : leaf,
+				"description": leaf.get_description(),
+				"thumb" : leaf.get_thumbnail(32, 32),
+				"gicon" : leaf.get_gicon(),
+				"icon" : leaf.get_icon(),
+				"icon-name": leaf.get_icon_name(),
+				"type" : type(leaf),
+				"module" : leaf.__module__,
+				"aliases" : getattr(leaf, "name_aliases", None),
+				"qfurl" : get_qfurl(leaf),
+				}
+		def get_leaf_fields(leaf):
+			base = get_object_fields(leaf)
+			base.update( {
+				"object" : leaf.object,
+				"object-type" : type(leaf.object),
+				"content" : leaf.content_source(),
+				"content-alt" : leaf.content_source(alternate=True),
+				"builtin-actions": list(leaf.get_actions()),
+				} )
+			return base
+		def get_source_fields(src):
+			base = get_object_fields(src)
+			base.update({
+				"dynamic" : src.is_dynamic(),
+				"sort" : src.should_sort_lexically(),
+				"parent" : src.get_parent(),
+				"leaf" : src.get_leaf_repr(),
+				"provides" : list(src.provides()),
+				"cached_items": type(src.cached_items),
+				"len": isinstance(src.cached_items, list) and len(src.cached_items),
+				} )
+			return base
+
+		def print_fields(fields):
+			for field in sorted(fields):
+				val = fields[field]
+				rep = repr(val)
+				print_func("%-15s:" % field, rep)
+				if str(val) not in rep:
+					print_func("%-15s:" % field, val)
+		leafinfo = get_leaf_fields(leaf)
+		print_fields(leafinfo)
+		if leafinfo["content"]:
+			print_func("Content ============")
+			print_fields(get_source_fields(leafinfo["content"]))
+		if leafinfo["content"] != leafinfo["content-alt"]:
+			print_func("Content-Alt ========")
+			print_fields(get_source_fields(leafinfo["content-alt"]))
+		uiutils.show_text_result(output.getvalue())
+
+	def get_description(self):
+		return u"Print debug output (for interal kupfer use)"
+	def get_icon_name(self):
+		return "emblem-system"
+	def item_types(self):
+		if _is_debug():
+			yield Leaf
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]