import pygtk pygtk.require('2.0') import gtk import gobject import logger import project import workflows import workflow import sys import os GLADEFILENAME = 'system/fluent.glade' class CreateProjectDruid(gtk.Window): """A druid for creating a new project. The CreateProjectDruid gets a list of all classes derived from Workflow, and asks the user to select one of these. A new project of the selected class is added to the application.""" def __init__(self, app): gtk.Window.__init__(self) self.app = app self.widget_tree = gtk.glade.XML(GLADEFILENAME, 'new_project_druid') self.workflows = self.make_workflow_list() self.selected = None renderer = gtk.CellRendererText() wf_name = gtk.TreeViewColumn('Workflow Name', renderer, text=0) self['workflow_list'].insert_column(wf_name, 0) self.wf_info = gtk.TextBuffer() self['workflow_info'].set_buffer(self.wf_info) def workflow_classes(self, modname): """Returns a list of all subclasses of Workflow in a given module""" workflow_classes = [] __import__('workflows.%s' % modname) module = sys.modules['workflows.%s' % modname] d = module.__dict__ for wf in d.values(): try: if issubclass(wf, workflow.Workflow): workflow_classes.append(wf) except TypeError, e: pass return workflow_classes def make_workflow_list(self): """Returns a ListStore containing all new workflows""" store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT) # List all .py files that can contain workflow classes wf_path = sys.modules['workflows'].__path__ wf_files = [] for dir in wf_path: for fn in os.listdir(dir): if fn.endswith('.py'): wf_files.append(fn[:-3]) # Try to load each file and look for Workflow derived classes for fn in wf_files: try: wf_info = self.workflow_classes(fn) for wf in wf_info: store.insert_after(None, (getattr(wf, 'name'), wf)) except Exception, e: logger.log('warning', 'Cannot load workflow: %s' % fn) return store def __getitem__(self, key): return self.widget_tree.get_widget(key) def run(self): self['workflow_list'].set_model(self.workflows) self['druidpagestart1'].show() self['druidpagefinish1'].show() self['new_project_druid'].show() self['druidpagefinish1'].connect('finish', self.finish) self['workflow_list'].connect('cursor_changed', self.selection_updated) self.connect('destroy', self.delete) def delete(self, widget): return False def hide(self): self['druidpagestart1'].hide() self['druidpagefinish1'].hide() self['new_project_druid'].hide() gtk.Window.hide(self) def finish(self, *rest): tree, it = self['workflow_list'].get_selection().get_selected() wf = self.workflows.get_value(it, 1) proj = project.Project() self.app.set_workflow(wf(self.app)) self.app.set_project(proj) self.hide() self.destroy() def selection_updated(self, *rest): tree, it = self['workflow_list'].get_selection().get_selected() wf = self.workflows.get_value(it, 1) self.wf_info.set_text(wf.description)