1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/server/lib/utils.py Mon Mar 03 11:33:49 2008 +0000
1.3 @@ -0,0 +1,193 @@
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 +import gmsconfig
1.20 +
1.21 +log = logging.getLogger("gms.utils")
1.22 +config = gmsconfig.GmsConfig()
1.23 +
1.24 +__all__ = ("which", "load_plugins", "PluginSet", "getHTML",
1.25 + "progress_bar", "create_tid", "list_media_files")
1.26 +
1.27 +def which(app):
1.28 + """Function to implement which(1) unix command"""
1.29 + pl = os.environ["PATH"].split(os.pathsep)
1.30 + for p in pl:
1.31 + path = os.path.join(p, app)
1.32 + if os.path.isfile(path):
1.33 + st = os.stat(path)
1.34 + if st[stat.ST_MODE] & 0111:
1.35 + return path
1.36 + return ""
1.37 +# which()
1.38 +
1.39 +
1.40 +def _load_module(pathlist, name):
1.41 + fp, path, desc = imp.find_module(name, pathlist)
1.42 + try:
1.43 + module = imp.load_module(name, fp, path, desc)
1.44 + return module
1.45 + finally:
1.46 + if fp:
1.47 + fp.close()
1.48 +# _load_module()
1.49 +
1.50 +
1.51 +class PluginSet(object):
1.52 + def __init__(self, basetype, *items):
1.53 + self.basetype = basetype
1.54 + self.map = {}
1.55 + self.list = []
1.56 +
1.57 + for i in items:
1.58 + self._add(i)
1.59 + self._sort()
1.60 + # __init__()
1.61 +
1.62 +
1.63 + def _add(self, item):
1.64 + self.map[item.name] = item
1.65 + self.list.append(item)
1.66 + # _add()
1.67 +
1.68 +
1.69 + def add(self, item):
1.70 + self._add()
1.71 + self._sort()
1.72 + # add()
1.73 +
1.74 +
1.75 + def __getitem__(self, spec):
1.76 + if isinstance(spec, basestring):
1.77 + return self.map[spec]
1.78 + else:
1.79 + return self.list[spec]
1.80 + # __getitem__()
1.81 +
1.82 +
1.83 + def get(self, name, default=None):
1.84 + return self.map.get(name, default)
1.85 + # get()
1.86 +
1.87 +
1.88 + def __iter__(self):
1.89 + return self.list.__iter__()
1.90 + # __iter__()
1.91 +
1.92 +
1.93 + def __len__(self):
1.94 + return len(self.list)
1.95 + # __len__()
1.96 +
1.97 +
1.98 + def _sort(self):
1.99 + self.list.sort(lambda a, b: cmp(a.priority, b.priority))
1.100 + # _sort()
1.101 +
1.102 +
1.103 + def update(self, pluginset):
1.104 + self.map.update(pluginset.map)
1.105 + self.list.extend(pluginset.list)
1.106 + self._sort()
1.107 + # update()
1.108 +
1.109 +
1.110 + def load_from_directory(self, directory):
1.111 + for i in load_plugins(directory, self.basetype):
1.112 + self._add(i)
1.113 + self._sort()
1.114 + # load_from_directory()
1.115 +
1.116 +
1.117 + def __str__(self):
1.118 + lst = []
1.119 + for o in self.list:
1.120 + lst.append('"%s" (%s)' % (o.name, o.__name__))
1.121 +
1.122 + return "%s(basetype=%s, items=[%s])" % \
1.123 + (self.__class__.__name__,
1.124 + self.basetype.__name__,
1.125 + ", ".join(lst))
1.126 + # __str__()
1.127 +# PluginSet
1.128 +
1.129 +
1.130 +def load_plugins(directory, basetype):
1.131 + """Function to load plugins from a given directory"""
1.132 + tn = basetype.__name__
1.133 + log.debug("Loading plugins from %s, type=%s" % (directory, tn))
1.134 +
1.135 +
1.136 + plugins = []
1.137 + for d in os.listdir(directory):
1.138 + if not d.endswith(".py"):
1.139 + continue
1.140 +
1.141 + name = d[0: -3]
1.142 + if name == "__init__":
1.143 + continue
1.144 +
1.145 + directory.replace(os.path.sep, ".")
1.146 + mod = _load_module([directory], name)
1.147 + for sym in dir(mod):
1.148 + cls = getattr(mod, sym)
1.149 + if isinstance(cls, type) and issubclass(cls, basetype) and \
1.150 + cls != basetype:
1.151 + plugins.append(cls)
1.152 + log.info("Loaded %s (%s) from %s" % \
1.153 + (cls.__name__, tn, os.path.join(directory, d)))
1.154 +
1.155 + return plugins
1.156 +# load_plugins()
1.157 +
1.158 +def getHTML(html_file, params={}):
1.159 + """This function parses an html file with the given
1.160 + parameters and returns a formated web-page"""
1.161 + try:
1.162 + filename = os.path.join(sys.path[0], "html", html_file + ".html")
1.163 + html = open(filename).read() % params
1.164 + return html
1.165 + except Exception, e:
1.166 + return "HTML format error. Wrong keys: %s" % e
1.167 +
1.168 +# getHTML
1.169 +
1.170 +def _create_html_item(opt):
1.171 + """Create an <li> item using HTML."""
1.172 + return "<li>%s</li>\n" % opt
1.173 +# _create_html_item
1.174 +
1.175 +def progress_bar(value, max, barsize):
1.176 + """Creates and displays a progressbar. By OSantana"""
1.177 + chars = int(value * barsize / float(max))
1.178 + percent = int((value / float(max)) * 100)
1.179 + sys.stdout.write("#" * chars)
1.180 + sys.stdout.write(" " * (barsize - chars + 2))
1.181 + if value >= max:
1.182 + sys.stdout.write("done.\n\n")
1.183 + else:
1.184 + sys.stdout.write("[%3i%%]\r" % (percent))
1.185 + sys.stdout.flush()
1.186 + return percent
1.187 +# progress_bar() by osantana
1.188 +
1.189 +def create_tid(last_tid):
1.190 + """Function to generate TIDs (ids for transcoders).
1.191 + At first it just do +1 on last_tid but can be implemented
1.192 + to generate more sparse TIDs"""
1.193 + tid = last_tid + 1
1.194 + return tid
1.195 +# create_id()
1.196 +