morphbr@718: #!/usr/bin/env morphbr@718: morphbr@718: __author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza" morphbr@718: __author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br" morphbr@718: __license__ = "GPL" morphbr@718: __version__ = "0.3" morphbr@718: morphbr@718: import os morphbr@718: import stat morphbr@718: import sys morphbr@718: import logging morphbr@718: import urllib morphbr@718: import gobject morphbr@718: import imp morphbr@718: morphbr@718: log = logging.getLogger("gms.utils") morphbr@718: morphbr@718: __all__ = ("which", "load_plugins", "PluginSet", "getHTML") morphbr@718: morphbr@718: def which(app): morphbr@718: """Function to implement which(1) unix command""" morphbr@718: pl = os.environ["PATH"].split(os.pathsep) morphbr@718: for p in pl: morphbr@718: path = os.path.join(p, app) morphbr@718: if os.path.isfile(path): morphbr@718: st = os.stat(path) morphbr@718: if st[stat.ST_MODE] & 0111: morphbr@718: return path morphbr@718: return "" morphbr@718: # which() morphbr@718: morphbr@718: morphbr@718: def _load_module(pathlist, name): morphbr@718: fp, path, desc = imp.find_module(name, pathlist) morphbr@718: try: morphbr@718: module = imp.load_module(name, fp, path, desc) morphbr@718: return module morphbr@718: finally: morphbr@718: if fp: morphbr@718: fp.close() morphbr@718: # _load_module() morphbr@718: morphbr@718: morphbr@718: class PluginSet(object): morphbr@718: def __init__(self, basetype, *items): morphbr@718: self.basetype = basetype morphbr@718: self.map = {} morphbr@718: self.list = [] morphbr@718: morphbr@718: for i in items: morphbr@718: self._add(i) morphbr@718: self._sort() morphbr@718: # __init__() morphbr@718: morphbr@718: morphbr@718: def _add(self, item): morphbr@718: self.map[item.name] = item morphbr@718: self.list.append(item) morphbr@718: # _add() morphbr@718: morphbr@718: morphbr@718: def add(self, item): morphbr@718: self._add() morphbr@718: self._sort() morphbr@718: # add() morphbr@718: morphbr@718: morphbr@718: def __getitem__(self, spec): morphbr@718: if isinstance(spec, basestring): morphbr@718: return self.map[spec] morphbr@718: else: morphbr@718: return self.list[spec] morphbr@718: # __getitem__() morphbr@718: morphbr@718: morphbr@718: def get(self, name, default=None): morphbr@718: return self.map.get(name, default) morphbr@718: # get() morphbr@718: morphbr@718: morphbr@718: def __iter__(self): morphbr@718: return self.list.__iter__() morphbr@718: # __iter__() morphbr@718: morphbr@718: morphbr@718: def __len__(self): morphbr@718: return len(self.list) morphbr@718: # __len__() morphbr@718: morphbr@718: morphbr@718: def _sort(self): morphbr@718: self.list.sort(lambda a, b: cmp(a.priority, b.priority)) morphbr@718: # _sort() morphbr@718: morphbr@718: morphbr@718: def update(self, pluginset): morphbr@718: self.map.update(pluginset.map) morphbr@718: self.list.extend(pluginset.list) morphbr@718: self._sort() morphbr@718: # update() morphbr@718: morphbr@718: morphbr@718: def load_from_directory(self, directory): morphbr@718: for i in load_plugins(directory, self.basetype): morphbr@718: self._add(i) morphbr@718: self._sort() morphbr@718: # load_from_directory() morphbr@718: morphbr@718: morphbr@718: def __str__(self): morphbr@718: lst = [] morphbr@718: for o in self.list: morphbr@718: lst.append('"%s" (%s)' % (o.name, o.__name__)) morphbr@718: morphbr@718: return "%s(basetype=%s, items=[%s])" % \ morphbr@718: (self.__class__.__name__, morphbr@718: self.basetype.__name__, morphbr@718: ", ".join(lst)) morphbr@718: # __str__() morphbr@718: # PluginSet morphbr@718: morphbr@718: morphbr@718: def load_plugins(directory, basetype): morphbr@718: """Function to load plugins from a given directory""" morphbr@718: tn = basetype.__name__ morphbr@718: log.debug("Loading plugins from %s, type=%s" % (directory, tn)) morphbr@718: morphbr@718: morphbr@718: plugins = [] morphbr@718: for d in os.listdir(directory): morphbr@718: if not d.endswith(".py"): morphbr@718: continue morphbr@718: morphbr@718: name = d[0: -3] morphbr@718: if name == "__init__": morphbr@718: continue morphbr@718: morphbr@718: directory.replace(os.path.sep, ".") morphbr@718: mod = _load_module([directory], name) morphbr@718: for sym in dir(mod): morphbr@718: cls = getattr(mod, sym) morphbr@718: if isinstance(cls, type) and issubclass(cls, basetype) and \ morphbr@718: cls != basetype: morphbr@718: plugins.append(cls) morphbr@718: log.info("Loaded %s (%s) from %s" % \ morphbr@718: (cls.__name__, tn, os.path.join(directory, d))) morphbr@718: morphbr@718: return plugins morphbr@718: # load_plugins() morphbr@718: morphbr@718: def getHTML(html_file, params={}): morphbr@718: """This function parses an html file with the given morphbr@718: parameters and returns a formated web-page""" morphbr@718: try: morphbr@718: filename = os.path.join(sys.path[0], "html", html_file + ".html") morphbr@718: html = open(filename).read() % params morphbr@718: return html morphbr@718: except Exception, e: morphbr@718: return "HTML format error. Wrong keys: %s" % e morphbr@718: morphbr@718: # getHTML morphbr@718: morphbr@718: def _create_html_item(opt): morphbr@718: """Create an