Plugin System

Code-Stücke können hier veröffentlicht werden.
Antworten
uphill
User
Beiträge: 22
Registriert: Sonntag 10. Dezember 2006, 20:17

Danke erstmal, hab das ganze weitergesponnen und das ist dabei rausgekommen. Funktioniert leider nicht ganz, weil ich beim importieren irgendwas falsch mache, aber ich denke das das so ein ganz guter Ansatz ist. Vielleicht sieht wer von euch was da nicht funktioniert:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys, os, glob
print __name__
this_path = os.path.dirname(__file__)

class PluginManager:
	def __init__(self):
		self.plugin_data = {}

	def load_plugins(self):
		'''
		load all plugins
		'''
		files = glob.glob(os.path.join(this_path,'*.py'))
		files += glob.glob(os.path.join(this_path,'*.pyw'))
		for path in files:
			filename = os.path.basename(path)
			print filename
			if filename.startswith('_'):
				# Module mit underline sind "private"
				continue

			plugin_name = os.path.splitext(filename)[0]
			# Infromation's Dict aus plugin holen
			self.plugin_data[plugin_name] = __import__(__name__,globals(), locals(), [plugin_name,])


	######################################################

	def __getitem__( self, key ):
		return self.plugin_data[key]

	def iteritems( self ):
		return self.plugin_data.iteritems()

	def has_key( self, key ):
		return self.plugin_data.has_key( key )

	def __str__( self ):
		return str( self.plugin_data )

	######################################################

	def debug( self ):
		for plugin_name,plugin in self.plugin_data.iteritems():
			print plugin_name
			print dir(plugin)
			for section, value in plugin.plugin_info.iteritems():
				print "\t%s - %s" % (section,value)
			print "-"*80


if __name__ == "__main__":
	mm = PluginManager()
	mm.load_plugins()
	mm.debug()
uphill
User
Beiträge: 22
Registriert: Sonntag 10. Dezember 2006, 20:17

also die zeile 28:

Code: Alles auswählen

self.plugin_data[plugin_name] = __import__(plugin_name, globals(), locals(), [], -1)
Antworten