1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/server/0.2/lib/utils.py Thu Apr 19 18:13:51 2007 +0100
1.3 @@ -0,0 +1,148 @@
1.4 +#!/usr/bin/env
1.5 +
1.6 +__author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
1.7 +__author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
1.8 +__license__ = "GPL"
1.9 +__version__ = "0.3"
1.10 +
1.11 +import os
1.12 +import stat
1.13 +import sys
1.14 +import logging
1.15 +import urllib
1.16 +import gobject
1.17 +import imp
1.18 +
1.19 +log = logging.getLogger("gmyth-stream.utils")
1.20 +
1.21 +__all__ = ("which", "load_plugins", "PluginSet")
1.22 +
1.23 +def which(app):
1.24 + """function to implement which(1) unix command"""
1.25 + pl = os.environ["PATH"].split(os.pathsep)
1.26 + for p in pl:
1.27 + path = os.path.join(p, app)
1.28 + if os.path.isfile(path):
1.29 + st = os.stat(path)
1.30 + if st[stat.ST_MODE] & 0111:
1.31 + return path
1.32 + return ""
1.33 +# which()
1.34 +
1.35 +
1.36 +def _load_module(pathlist, name):
1.37 + fp, path, desc = imp.find_module(name, pathlist)
1.38 + try:
1.39 + module = imp.load_module(name, fp, path, desc)
1.40 + return module
1.41 + finally:
1.42 + if fp:
1.43 + fp.close()
1.44 +# _load_module()
1.45 +
1.46 +
1.47 +class PluginSet(object):
1.48 + def __init__(self, basetype, *items):
1.49 + self.basetype = basetype
1.50 + self.map = {}
1.51 + self.list = []
1.52 +
1.53 + for i in items:
1.54 + self._add(i)
1.55 + self._sort()
1.56 + # __init__()
1.57 +
1.58 +
1.59 + def _add(self, item):
1.60 + self.map[item.name] = item
1.61 + self.list.append(item)
1.62 + # _add()
1.63 +
1.64 +
1.65 + def add(self, item):
1.66 + self._add()
1.67 + self._sort()
1.68 + # add()
1.69 +
1.70 +
1.71 + def __getitem__(self, spec):
1.72 + if isinstance(spec, basestring):
1.73 + return self.map[spec]
1.74 + else:
1.75 + return self.list[spec]
1.76 + # __getitem__()
1.77 +
1.78 +
1.79 + def get(self, name, default=None):
1.80 + self.map.get(name, default)
1.81 + # get()
1.82 +
1.83 +
1.84 + def __iter__(self):
1.85 + return self.list.__iter__()
1.86 + # __iter__()
1.87 +
1.88 +
1.89 + def __len__(self):
1.90 + return len(self.list)
1.91 + # __len__()
1.92 +
1.93 +
1.94 + def _sort(self):
1.95 + self.list.sort(lambda a, b: cmp(a.priority, b.priority))
1.96 + # _sort()
1.97 +
1.98 +
1.99 + def update(self, pluginset):
1.100 + self.map.update(pluginset.map)
1.101 + self.list.extend(pluginset.list)
1.102 + self._sort()
1.103 + # update()
1.104 +
1.105 +
1.106 + def load_from_directory(self, directory):
1.107 + for i in load_plugins(directory, self.basetype):
1.108 + self._add(i)
1.109 + self._sort()
1.110 + # load_from_directory()
1.111 +
1.112 +
1.113 + def __str__(self):
1.114 + lst = []
1.115 + for o in self.list:
1.116 + lst.append('"%s" (%s)' % (o.name, o.__name__))
1.117 +
1.118 + return "%s(basetype=%s, items=[%s])" % \
1.119 + (self.__class__.__name__,
1.120 + self.basetype.__name__,
1.121 + ", ".join(lst))
1.122 + # __str__()
1.123 +# PluginSet
1.124 +
1.125 +
1.126 +def load_plugins(directory, basetype):
1.127 + tn = basetype.__name__
1.128 + log.debug("Loading plugins from %s, type=%s" % (directory, tn))
1.129 +
1.130 +
1.131 + plugins = []
1.132 + for d in os.listdir(directory):
1.133 + if not d.endswith(".py"):
1.134 + continue
1.135 +
1.136 + name = d[0: -3]
1.137 + if name == "__init__":
1.138 + continue
1.139 +
1.140 + directory.replace(os.path.sep, ".")
1.141 + mod = _load_module([directory], name)
1.142 + for sym in dir(mod):
1.143 + cls = getattr(mod, sym)
1.144 + if isinstance(cls, type) and issubclass(cls, basetype) and \
1.145 + cls != basetype:
1.146 + plugins.append(cls)
1.147 + log.info("Loaded %s (%s) from %s" % \
1.148 + (cls.__name__, tn, os.path.join(directory, d)))
1.149 +
1.150 + return plugins
1.151 +# load_plugins()