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