<commit_before># coding: utf-8
"""
ApplicationBase is an extendable class that provides a basic environment for a FIFE-based client.

 The unextended application reads in and initializes engine settings, sets up a simple event
listener, and pumps the engine while listening for a quit message. Specialized applications can
modify settings.py to change initial engine settings. They can provide their own event listener
by assigning to self.listenertype in their constructor. And they can override the _pump method
to define runtime behavior of the application.

"""

import fife
import fifelog

# Default, rudimentary event listener.
class ExitEventListener(fife.IKeyListener):
	def __init__(self, app):
		self.app = app
		self.engine = app.engine
		eventmanager = self.engine.getEventManager()
		eventmanager.setNonConsumableKeys([fife.IKey.ESCAPE])
		fife.IKeyListener.__init__(self)
		eventmanager.addKeyListener(self)
		self.quitRequested = False

	def keyPressed(self, evt):
		keyval = evt.getKey().getValue()
		if keyval == fife.IKey.ESCAPE:
			self.app.quit()
	
	def keyReleased(self, evt):
		pass

class ApplicationBase(object):
	def __init__(self):
		self.engine = fife.Engine()

		self.loadSettings()
		self.initLogging()
		
		self.engine.init()

		self.quitRequested = False
		self.breakRequested = False
		self.returnValues = []

	def loadSettings(self):
		import settings
		self.settings = settings
		
		engineSetting = self.engine.getSettings()
		engineSetting.setDefaultFontGlyphs(
			" abcdefghijklmnopqrstuvwxyz"
			+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
			+ ".,!?-+/:();%`'*#=[]")
		engineSetting.setDefaultFontPath('content/fonts/samanata.ttf')
		engineSetting.setDefaultFontSize(12)
		engineSetting.setBitsPerPixel(settings.BitsPerPixel)
		engineSetting.setFullScreen(settings.FullScreen)
		engineSetting.setInitialVolume(settings.InitialVolume)
		engineSetting.setRenderBackend(settings.RenderBackend)
		engineSetting.setSDLRemoveFakeAlpha(settings.SDLRemoveFakeAlpha)
		engineSetting.setScreenWidth(settings.ScreenWidth)
		engineSetting.setScreenHeight(settings.ScreenHeight)
	
	def initLogging(self):
		self.log = fifelog.LogManager(self.engine, self.settings.LogToPrompt, self.settings.LogToFile)
		if self.settings.LogModules:
			self.log.setVisibleModules(*self.settings.LogModules)

	def createListener(self):
		return ExitEventListener(self)

	def run(self):
		eventlistener = self.createListener()
		self.engine.initializePumping()
		return self.mainLoop()
	
	def mainLoop(self):
		self.returnValues.append(None)
		while not self.quitRequested:
			try:
				self.engine.pump()
			except fife.Exception, e:
				print e.getMessage()
				break

			self._pump()

			try:
				self.engine.finalizePumping()
			except fife.Exception, e:
				print e.getMessage()
				break

			if self.breakRequested:
				self.breakRequested = False
				break
		
		return self.returnValues.pop()

	def breakFromMainLoop(self,returnValue):
		self.returnValues[-1] = returnValue
		self.breakRequested = True

	# Application pump. Derived classes can specialize this for unique behavior.
	def _pump(self):
		pass

	def quit(self):
		self.quitRequested = True<commit_msg>Documentation for basicapplication.<commit_after># coding: utf-8

"""
The basic application and main loop.

See the L{ApplicationBase} documentation.
"""

import fife
import fifelog

class ExitEventListener(fife.IKeyListener):
	"""
	Default, rudimentary event listener.
	
	Will cause the application to quit on pressing ESC.
	"""
	def __init__(self, app):
		self.app = app
		self.engine = app.engine
		eventmanager = self.engine.getEventManager()
		eventmanager.setNonConsumableKeys([fife.IKey.ESCAPE])
		fife.IKeyListener.__init__(self)
		eventmanager.addKeyListener(self)
		self.quitRequested = False

	def keyPressed(self, evt):
		keyval = evt.getKey().getValue()
		if keyval == fife.IKey.ESCAPE:
			self.app.quit()
	
	def keyReleased(self, evt):
		pass

class ApplicationBase(object):
	"""
	ApplicationBase is an extendable class that provides a basic environment for a FIFE-based client.
	
	The unextended application reads in and initializes engine settings, sets up a simple event
	listener, and pumps the engine while listening for a quit message. Specialized applications can
	modify settings.py to change initial engine settings. They can provide their own event listener
	by overriding L{createListener}. And they can override the L{_pump} method
	to define runtime behavior of the application.
	
	"""
	def __init__(self):
		self.engine = fife.Engine()

		self.loadSettings()
		self.initLogging()
		
		self.engine.init()

		self.quitRequested = False
		self.breakRequested = False
		self.returnValues = []

	def loadSettings(self):
		"""
		Load the settings from a python file and load them into the engine.
		Called in the ApplicationBase constructor.
		"""
		import settings
		self.settings = settings
		
		engineSetting = self.engine.getSettings()
		engineSetting.setDefaultFontGlyphs(
			" abcdefghijklmnopqrstuvwxyz"
			+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
			+ ".,!?-+/:();%`'*#=[]")
		engineSetting.setDefaultFontPath('content/fonts/samanata.ttf')
		engineSetting.setDefaultFontSize(12)
		engineSetting.setBitsPerPixel(settings.BitsPerPixel)
		engineSetting.setFullScreen(settings.FullScreen)
		engineSetting.setInitialVolume(settings.InitialVolume)
		engineSetting.setRenderBackend(settings.RenderBackend)
		engineSetting.setSDLRemoveFakeAlpha(settings.SDLRemoveFakeAlpha)
		engineSetting.setScreenWidth(settings.ScreenWidth)
		engineSetting.setScreenHeight(settings.ScreenHeight)
	
	def initLogging(self):
		"""
		Initialize the LogManager.
		"""
		self.log = fifelog.LogManager(self.engine, self.settings.LogToPrompt, self.settings.LogToFile)
		if self.settings.LogModules:
			self.log.setVisibleModules(*self.settings.LogModules)

	def createListener(self):
		"""
		This creates a default event listener, which will just close the program
		after pressing ESC.
		
		You should override this method to provide your own event handling.
		"""
		return ExitEventListener(self)

	def run(self):
		"""
		Initialize the event listener and event loop - and start it.
		"""
		eventlistener = self.createListener()
		self.engine.initializePumping()
		return self.mainLoop()
	
	def mainLoop(self):
		"""
		The programs main loop.
		
		Do not override this, instead provide your own L{_pump} method.
		You can call this recursively, e.g. to provide synchronous
		Dialogs :-) and break out of the current mainLoop by calling
		L{breakFromMainLoop}. It will return the argument passed
		to L{breakFromMainLoop}.
		"""
		self.returnValues.append(None)
		while not self.quitRequested:
			try:
				self.engine.pump()
			except fife.Exception, e:
				print e.getMessage()
				break

			self._pump()

			try:
				self.engine.finalizePumping()
			except fife.Exception, e:
				print e.getMessage()
				break

			if self.breakRequested:
				self.breakRequested = False
				break
		
		return self.returnValues.pop()

	def breakFromMainLoop(self,returnValue):
		"""
		Break from the currently running L{mainLoop}.
		
		The passed argument will be returned by the mainLoop.
		"""
		self.returnValues[-1] = returnValue
		self.breakRequested = True

	
	def _pump(self):
		"""
		Application pump.
		
		Derived classes can specialize this for unique behavior.
		This is called every frame.
		"""

	def quit(self):
		"""
		Quit the application. Really!
		"""
		self.quitRequested = True
{'fraction_non_alphanumeric': 0.09348175965665236, 'fraction_numerical': 0.0037553648068669528, 'mean_word_length': 4.407541696881799, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6422739', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2250, 'n_words': 586}