Source

circuits / circuits / debugger.py

# Module:	debugger
# Date:		2nd April 2006
# Author:	James Mills, prologic at shortcircuit dot net dot au

"""
Debugger component used to debug each event in a system by printing
each event to sys.stderr or to a Logger Component instnace.

Example:

>>> from circuits import listener, Event, Component, Debugger, Manager
>>> class Foo(Component):
	...     @listener("foo")
...     def onFOO(self):
	...             print "Hello World"
... 
>>> manager = Manager()
>>> foo = Foo()
>>> debugger = Debugger()
>>> debugger.enable()
>>> manager += foo
>>> manager += debugger
<Registered/registered (, )>
>>> manager.push(Event(1, 2, 3, a=1, b=2, c=3), "foo")
>>> manager.flush()
<Event/foo (1, 2, 3, a=1, c=3, b=2)>
Hello World
"""

import sys

from circuits.core import listener, Event, Component


class Debug(Event):
	"""Debug Event

	:param msg: Message to display
	:type msg: str
	"""


class Debugger(Component):
	"""Create a new Debugger Component

	Creates a new Debugger Component that filters all events in teh system
	printing each event to sys.stderr or a Logger Component.

	:var IgnoreEvents: list of events (str) to ignore
	:var IgnoreChannels: list of channels (str) to ignore
	:var enabled: Enabled/Disabled flag

	:param log: Logger Component instnace or None (*default*)
	"""

	IgnoreEvents = []
	IgnoreChannels = []

	enabled = True

	def __init__(self, *args, **kwargs):
		"initializes x; see x.__class__.__doc__ for signature"

		super(Debugger, self).__init__(*args, **kwargs)

		self.logger = kwargs.get("logger", False)

	def disable(self):
		"Disable Debugger"

		self.enabled = False

	def enable(self):
		"Enable Debugger"

		self.enabled = True

	def toggle(self):
		"""Toggle Debugger

		* If enabled, disable.
		* If disabled, enable.
		"""

		if self.enabled:
			self.disable()
		else:
			self.enable()

	def set(self, flag):
		"""Set Debugger's enabled flag$a

		Set Debugger's enabled flag to flag.

		:param flag: Status to set enabled flag to
		:type flag: bool
		"""

		if (not self.enabled) and flag:
			self.enable()
		elif self.enabled and (not flag):
			self.disable()

	@listener(type="filter")
	def onEVENTS(self, event, *args, **kwargs):
		"""Global Event Handler

		Event handler to listen and filter all events printing each event
		to sys.stderr or a Logger Component instnace. This behavior is
		controllbed by the :attr:`enabled flag <circuits.debugger.Debugger.enabled>`
		"""

		if self.enabled:
			channel = event.channel
			if True in [event.name == name for name in self.IgnoreEvents]:
				return
			elif channel in self.IgnoreChannels:
				return
			else:
				print self.logger
				if self.logger:
					self.push(Debug(repr(event)), "debug", "log")
				else:
					print >> sys.stderr, event
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.