initial commit of my widget system. it is quite alpha, so please dont relay on the...
[enigma2-plugins.git] / widgets / src / widgets / __init__.py
1 from os import listdir
2 from os.path import isdir, isfile
3 from os.path import abspath, splitext
4 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
5 from xml.etree.cElementTree import parse
6
7 def importSingleWidget(session,widgetdir):
8     print "importing widget from",widgetdir
9     widgetname = widgetdir.split("/")[-1]
10     module_name, ext = splitext(widgetname+".widget.py") # Handles no-extension files, etc.
11     if ext == '.py' and module_name != "__init__":                
12         try:
13             #import python part
14             spam = __import__(module_name, globals(), locals(), [], -1)
15             w = spam.widget.get_widget(session)
16             w.setDir(widgetdir)
17             #import skin
18             skin = parse(widgetdir+"/"+"widget_skin.xml").getroot()
19             return widgetname,w,skin,widgetdir,module_name
20         
21         except (ImportError,IOError), e:                
22             print 'Could NOT import widget: %s' % (module_name)
23             print 'Exception Caught\n%s' %e
24     return False
25     print "#"*20
26     
27     
28
29 def importWidgets(session,):
30     widgets = []
31     dir = abspath(resolveFilename(SCOPE_PLUGINS) + "Extensions/Widgets/widgets/")
32     for widgetdir in listdir(dir):
33         abs_path =  "/".join([dir,widgetdir])
34         if isdir(abs_path):
35             if isfile(abs_path+"/widget.py"):
36                 w = importSingleWidget(session,abs_path)
37                 if w is not None:
38                     widgets.append(w)
39             else:
40                 print "found NO widget.py", abs_path+"/widget.py"
41                 continue
42     return widgets