[svn r804] Added function to retrieve backend details (total space, free space, etc)
3 __author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
4 __author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
16 log = logging.getLogger("gms.utils")
18 __all__ = ("which", "load_plugins", "PluginSet", "getHTML",
19 "progress_bar", "create_tid", "list_media_files")
22 """Function to implement which(1) unix command"""
23 pl = os.environ["PATH"].split(os.pathsep)
25 path = os.path.join(p, app)
26 if os.path.isfile(path):
28 if st[stat.ST_MODE] & 0111:
34 def _load_module(pathlist, name):
35 fp, path, desc = imp.find_module(name, pathlist)
37 module = imp.load_module(name, fp, path, desc)
45 class PluginSet(object):
46 def __init__(self, basetype, *items):
47 self.basetype = basetype
58 self.map[item.name] = item
59 self.list.append(item)
69 def __getitem__(self, spec):
70 if isinstance(spec, basestring):
73 return self.list[spec]
77 def get(self, name, default=None):
78 return self.map.get(name, default)
83 return self.list.__iter__()
93 self.list.sort(lambda a, b: cmp(a.priority, b.priority))
97 def update(self, pluginset):
98 self.map.update(pluginset.map)
99 self.list.extend(pluginset.list)
104 def load_from_directory(self, directory):
105 for i in load_plugins(directory, self.basetype):
108 # load_from_directory()
114 lst.append('"%s" (%s)' % (o.name, o.__name__))
116 return "%s(basetype=%s, items=[%s])" % \
117 (self.__class__.__name__,
118 self.basetype.__name__,
124 def load_plugins(directory, basetype):
125 """Function to load plugins from a given directory"""
126 tn = basetype.__name__
127 log.debug("Loading plugins from %s, type=%s" % (directory, tn))
131 for d in os.listdir(directory):
132 if not d.endswith(".py"):
136 if name == "__init__":
139 directory.replace(os.path.sep, ".")
140 mod = _load_module([directory], name)
142 cls = getattr(mod, sym)
143 if isinstance(cls, type) and issubclass(cls, basetype) and \
146 log.info("Loaded %s (%s) from %s" % \
147 (cls.__name__, tn, os.path.join(directory, d)))
152 def getHTML(html_file, params={}):
153 """This function parses an html file with the given
154 parameters and returns a formated web-page"""
156 filename = os.path.join(sys.path[0], "html", html_file + ".html")
157 html = open(filename).read() % params
160 return "HTML format error. Wrong keys: %s" % e
164 def _create_html_item(opt):
165 """Create an <li> item using HTML."""
166 return "<li>%s</li>\n" % opt
169 def progress_bar(value, max, barsize):
170 """Creates and displays a progressbar. By OSantana"""
171 chars = int(value * barsize / float(max))
172 percent = int((value / float(max)) * 100)
173 sys.stdout.write("#" * chars)
174 sys.stdout.write(" " * (barsize - chars + 2))
176 sys.stdout.write("done.\n\n")
178 sys.stdout.write("[%3i%%]\r" % (percent))
181 # progress_bar() by osantana
183 def create_tid(last_tid):
184 """Function to generate TIDs (ids for transcoders).
185 At first it just do +1 on last_tid but can be implemented
186 to generate more sparse TIDs"""