3 __author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
4 __author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
18 log = logging.getLogger("gms.utils")
19 config = gmsconfig.GmsConfig()
21 __all__ = ("which", "load_plugins", "PluginSet", "getHTML",
22 "progress_bar", "create_tid", "list_media_files")
25 """Function to implement which(1) unix command"""
26 pl = os.environ["PATH"].split(os.pathsep)
28 path = os.path.join(p, app)
29 if os.path.isfile(path):
31 if st[stat.ST_MODE] & 0111:
37 def _load_module(pathlist, name):
38 fp, path, desc = imp.find_module(name, pathlist)
40 module = imp.load_module(name, fp, path, desc)
48 class PluginSet(object):
49 def __init__(self, basetype, *items):
50 self.basetype = basetype
61 self.map[item.name] = item
62 self.list.append(item)
72 def __getitem__(self, spec):
73 if isinstance(spec, basestring):
76 return self.list[spec]
80 def get(self, name, default=None):
81 return self.map.get(name, default)
86 return self.list.__iter__()
96 self.list.sort(lambda a, b: cmp(a.priority, b.priority))
100 def update(self, pluginset):
101 self.map.update(pluginset.map)
102 self.list.extend(pluginset.list)
107 def load_from_directory(self, directory):
108 for i in load_plugins(directory, self.basetype):
111 # load_from_directory()
117 lst.append('"%s" (%s)' % (o.name, o.__name__))
119 return "%s(basetype=%s, items=[%s])" % \
120 (self.__class__.__name__,
121 self.basetype.__name__,
127 def load_plugins(directory, basetype):
128 """Function to load plugins from a given directory"""
129 tn = basetype.__name__
130 log.debug("Loading plugins from %s, type=%s" % (directory, tn))
134 for d in os.listdir(directory):
135 if not d.endswith(".py"):
139 if name == "__init__":
142 directory.replace(os.path.sep, ".")
143 mod = _load_module([directory], name)
145 cls = getattr(mod, sym)
146 if isinstance(cls, type) and issubclass(cls, basetype) and \
149 log.info("Loaded %s (%s) from %s" % \
150 (cls.__name__, tn, os.path.join(directory, d)))
155 def getHTML(html_file, params={}):
156 """This function parses an html file with the given
157 parameters and returns a formated web-page"""
159 filename = os.path.join(sys.path[0], "html", html_file + ".html")
160 html = open(filename).read() % params
163 return "HTML format error. Wrong keys: %s" % e
167 def _create_html_item(opt):
168 """Create an <li> item using HTML."""
169 return "<li>%s</li>\n" % opt
172 def progress_bar(value, max, barsize):
173 """Creates and displays a progressbar. By OSantana"""
174 chars = int(value * barsize / float(max))
175 percent = int((value / float(max)) * 100)
176 sys.stdout.write("#" * chars)
177 sys.stdout.write(" " * (barsize - chars + 2))
179 sys.stdout.write("done.\n\n")
181 sys.stdout.write("[%3i%%]\r" % (percent))
184 # progress_bar() by osantana
186 def create_tid(last_tid):
187 """Function to generate TIDs (ids for transcoders).
188 At first it just do +1 on last_tid but can be implemented
189 to generate more sparse TIDs"""