[kupfer] plugin.calculator: Add help() command



commit 7eb14e2adb755f94aa1fd30972ccb3c8d17167fa
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sun Nov 8 23:12:51 2009 +0100

    plugin.calculator: Add help() command
    
    Add a simple help() command that simply lists all the math functions
    available (with docstrings).

 kupfer/plugin/calculator.py |   48 +++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 46 insertions(+), 2 deletions(-)
---
diff --git a/kupfer/plugin/calculator.py b/kupfer/plugin/calculator.py
index 85f0bbd..4ecfe87 100644
--- a/kupfer/plugin/calculator.py
+++ b/kupfer/plugin/calculator.py
@@ -12,11 +12,52 @@ __description__ = _("Calculate expressions starting with '='")
 __version__ = ""
 __author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
 
+class IgnoreResultException (Exception):
+	pass
+
 class KupferSurprise (float):
-	def __call__(self):
+	def __call__(self, *args):
 		from kupfer import utils, version
 		utils.show_url(version.WEBSITE)
-		return self
+		raise IgnoreResultException
+
+class Help (object):
+	def __call__(self):
+		import textwrap
+
+		from kupfer import uiutils
+
+		environment = dict(math.__dict__)
+		environment.update(cmath.__dict__)
+		docstrings = []
+		for attr in sorted(environment):
+			if attr.startswith("_"):
+				continue
+			val = environment[attr]
+			if not callable(val):
+				docstrings.append("%s = %s" % (attr, val))
+				continue
+			try:
+				docstrings.append(val.__doc__)
+			except AttributeError:
+				pass
+		formatted = []
+		maxlen = 72
+		left_margin = 4
+		for docstr in docstrings:
+			# Wrap the description and align continued lines
+			docsplit = docstr.split("\n", 1)
+			if len(docsplit) < 2:
+				formatted.append(docstr)
+				continue
+			wrapped_lines = textwrap.wrap(docsplit[1], maxlen - left_margin)
+			wrapped = (u"\n" + u" "*left_margin).join(wrapped_lines)
+			formatted.append("%s\n    %s" % (docsplit[0], wrapped))
+		uiutils.show_text_result("\n\n".join(formatted), _("Calculator"))
+		raise IgnoreResultException
+
+	def __complex__(self):
+		return self()
 
 def format_result(res):
 	cres = complex(res)
@@ -49,6 +90,7 @@ class Calculate (Action):
 		# define some constants missing
 		if self.last_result is not None:
 			environment["_"] = self.last_result
+		environment["help"] = Help()
 		environment["kupfer"] = KupferSurprise("inf")
 		# make the builtins inaccessible
 		environment["__builtins__"] = {}
@@ -58,6 +100,8 @@ class Calculate (Action):
 			result = eval(expr, environment)
 			resultstr = format_result(result)
 			self.last_result = result
+		except IgnoreResultException:
+			return
 		except Exception, exc:
 			pretty.print_error(__name__, type(exc).__name__, exc)
 			resultstr = unicode(exc)



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