[kupfer] Rewrite pretty module, static functions reuse the mixin implementation



commit 7a12736406afe0470131eaf1f3084ef29492c8c2
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Mon Feb 15 15:55:43 2010 +0100

    Rewrite pretty module, static functions reuse the mixin implementation
    
    We also add output_exc()/print_exc() to print exceptions (with full
    traceback only in debug mode)

 kupfer/pretty.py |   84 ++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 56 insertions(+), 28 deletions(-)
---
diff --git a/kupfer/pretty.py b/kupfer/pretty.py
index fa10d88..37d30a3 100644
--- a/kupfer/pretty.py
+++ b/kupfer/pretty.py
@@ -1,12 +1,23 @@
+from __future__ import print_function
+
 debug = False
 
 import functools
+import sys
+import traceback
 
 class OutputMixin (object):
 	"""
 	A mixin class providing prefixed output
 	standard output and debug output
 	"""
+	def _output_category(self):
+		return "[%s] %s:" % (type(self).__module__, type(self).__name__)
+
+	def _output_core(self, prefix, sep, end, stream, *items):
+		category = self._output_category()
+		print(prefix+category, *items, sep=sep, end=end, file=stream)
+
 	def output_info(self, *items, **kwargs):
 		"""
 		Output given items using @sep as separator,
@@ -14,37 +25,54 @@ class OutputMixin (object):
 		"""
 		sep = kwargs.get("sep", " ")
 		end = kwargs.get("end", "\n")
-		category = kwargs.get("category", "")
 		stritems = (str(it) for it in items)
-		sformat = "%s[%s] %s: %s%s"
-		try:
-			output = sformat % (category, type(self).__module__,
-					type(self).__name__, sep.join(stritems), end)
-		except Exception:
-			output = sep.join(stritems) + end
-		print output,
+		self._output_core("", sep, end, sys.stdout, *items)
+
+	def output_exc(self, exc_info=None):
+		"""Output current exception, or use @exc_info if given"""
+		etype, value, tb = (exc_info or sys.exc_info())
+		if debug:
+			self._output_core("Exception in ", "", "\n", sys.stderr)
+			traceback.print_exception(etype, value, tb, file=sys.stderr)
+		else:
+			msg = "%s: %s" % (etype.__name__, value)
+			self._output_core("Exception in ", " ", "\n", sys.stderr, msg)
 
 	def output_debug(self, *items, **kwargs):
 		if debug:
-			self.output_info(category="D ", *items, **kwargs)
+			sep = kwargs.get("sep", " ")
+			end = kwargs.get("end", "\n")
+			self._output_core("D ", sep, end, sys.stderr, *items)
+
 	def output_error(self, *items, **kwargs):
-		self.output_info(category="Error ", *items, **kwargs)
+		sep = kwargs.get("sep", " ")
+		end = kwargs.get("end", "\n")
+		self._output_core("Error ", sep, end, sys.stderr, *items)
 
-def print_info(name, *items, **kwargs):
-	"""
-	Output given items using @sep as separator,
-	ending the line with @end
-	"""
-	sep = kwargs.get("sep", " ")
-	end = kwargs.get("end", "\n")
-	category = kwargs.get("category", "")
-	stritems = (str(it) for it in items)
-	sformat = "%s[%s]: %s%s"
-	print sformat % (category, name, sep.join(stritems), end),
-
-def print_debug(name, *items, **kwargs):
-	if debug:
-		print_info(name, category="D ", *items, **kwargs)
-
-def print_error(name, *items, **kwargs):
-	print_info(name, category="Error ", *items, **kwargs)
+class _StaticOutput (OutputMixin):
+	current_calling_module = None
+	def _output_category(self):
+		return "[%s]:" % (self.current_calling_module, )
+
+	def print_info(self, modulename, *args, **kwargs):
+		self.current_calling_module = modulename
+		self.output_info(*args, **kwargs)
+
+	def print_error(self, modulename, *args, **kwargs):
+		self.current_calling_module = modulename
+		self.output_error(*args, **kwargs)
+
+	def print_exc(self, modulename, *args, **kwargs):
+		self.current_calling_module = modulename
+		self.output_exc(*args, **kwargs)
+
+	def print_debug(self, modulename, *args, **kwargs):
+		if debug:
+			self.current_calling_module = modulename
+			self.output_debug(*args, **kwargs)
+_StaticOutput = _StaticOutput()
+
+print_info = _StaticOutput.print_info
+print_debug = _StaticOutput.print_debug
+print_error = _StaticOutput.print_error
+print_exc = _StaticOutput.print_exc



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