gmyth-stream/server/0.2/lib/utils.py
branchtrunk
changeset 830 0e159c5e2d32
parent 829 0a4e6b811acc
child 831 e2baa6947dbf
     1.1 --- a/gmyth-stream/server/0.2/lib/utils.py	Tue Aug 28 15:41:35 2007 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,178 +0,0 @@
     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("gms.utils")
    1.20 -
    1.21 -__all__ = ("which", "load_plugins", "PluginSet", "getHTML")
    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 -        return 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 -    """Function to load plugins from a given directory"""
   1.128 -    tn = basetype.__name__
   1.129 -    log.debug("Loading plugins from %s, type=%s" % (directory, tn))
   1.130 -
   1.131 -
   1.132 -    plugins = []
   1.133 -    for d in os.listdir(directory):
   1.134 -        if not d.endswith(".py"):
   1.135 -            continue
   1.136 -
   1.137 -        name = d[0: -3]
   1.138 -        if name == "__init__":
   1.139 -            continue
   1.140 -
   1.141 -        directory.replace(os.path.sep, ".")
   1.142 -        mod = _load_module([directory], name)
   1.143 -        for sym in dir(mod):
   1.144 -            cls = getattr(mod, sym)
   1.145 -            if isinstance(cls, type) and issubclass(cls, basetype) and \
   1.146 -                cls != basetype:
   1.147 -                plugins.append(cls)
   1.148 -                log.info("Loaded %s (%s) from %s" % \
   1.149 -                         (cls.__name__, tn, os.path.join(directory, d)))
   1.150 -
   1.151 -    return plugins
   1.152 -# load_plugins()
   1.153 -
   1.154 -def getHTML(html_file, params={}):
   1.155 -    """This function parses a file 'html_file.html' with the given
   1.156 -    parameters and returns a formated web-page"""
   1.157 -    try:
   1.158 -        filename = os.path.join(sys.path[0], "html", html_file + ".html")
   1.159 -        html = open(filename).read() % params
   1.160 -        return html
   1.161 -    except Exception, e:
   1.162 -        return "HTML format error. Wrong keys: %s" % e
   1.163 -
   1.164 -# getHTML
   1.165 -
   1.166 -def _create_html_item(opt):
   1.167 -    return "<li>%s</li>\n" % opt
   1.168 -# _create_html_item
   1.169 -
   1.170 -def progress_bar(log, value, max, barsize):
   1.171 -   chars = int(value * barsize / float(max))
   1.172 -   percent = int((value / float(max)) * 100)
   1.173 -   sys.stdout.write("#" * chars)
   1.174 -   sys.stdout.write(" " * (barsize - chars + 2))
   1.175 -   if value >= max:
   1.176 -      sys.stdout.write("done. \n\n")
   1.177 -   else:
   1.178 -      sys.stdout.write("[%3i%%]\r" % (percent))
   1.179 -      sys.stdout.flush()
   1.180 -   return percent
   1.181 -# progress_bar by osantana