morphbr@565
|
1 |
#!/usr/bin/env
|
morphbr@565
|
2 |
|
morphbr@565
|
3 |
__author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
|
morphbr@565
|
4 |
__author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
|
morphbr@565
|
5 |
__license__ = "GPL"
|
morphbr@565
|
6 |
__version__ = "0.3"
|
morphbr@565
|
7 |
|
morphbr@565
|
8 |
import os
|
morphbr@565
|
9 |
import stat
|
morphbr@565
|
10 |
import sys
|
morphbr@565
|
11 |
import logging
|
morphbr@565
|
12 |
import urllib
|
morphbr@565
|
13 |
import gobject
|
morphbr@565
|
14 |
import imp
|
morphbr@565
|
15 |
|
morphbr@577
|
16 |
log = logging.getLogger("gms.utils")
|
morphbr@565
|
17 |
|
morphbr@585
|
18 |
__all__ = ("which", "load_plugins", "PluginSet", "getHTML")
|
morphbr@565
|
19 |
|
morphbr@565
|
20 |
def which(app):
|
morphbr@585
|
21 |
"""Function to implement which(1) unix command"""
|
morphbr@565
|
22 |
pl = os.environ["PATH"].split(os.pathsep)
|
morphbr@565
|
23 |
for p in pl:
|
morphbr@565
|
24 |
path = os.path.join(p, app)
|
morphbr@565
|
25 |
if os.path.isfile(path):
|
morphbr@565
|
26 |
st = os.stat(path)
|
morphbr@565
|
27 |
if st[stat.ST_MODE] & 0111:
|
morphbr@565
|
28 |
return path
|
morphbr@565
|
29 |
return ""
|
morphbr@565
|
30 |
# which()
|
morphbr@565
|
31 |
|
morphbr@565
|
32 |
|
morphbr@565
|
33 |
def _load_module(pathlist, name):
|
morphbr@565
|
34 |
fp, path, desc = imp.find_module(name, pathlist)
|
morphbr@565
|
35 |
try:
|
morphbr@565
|
36 |
module = imp.load_module(name, fp, path, desc)
|
morphbr@565
|
37 |
return module
|
morphbr@565
|
38 |
finally:
|
morphbr@565
|
39 |
if fp:
|
morphbr@565
|
40 |
fp.close()
|
morphbr@565
|
41 |
# _load_module()
|
morphbr@565
|
42 |
|
morphbr@565
|
43 |
|
morphbr@565
|
44 |
class PluginSet(object):
|
morphbr@565
|
45 |
def __init__(self, basetype, *items):
|
morphbr@565
|
46 |
self.basetype = basetype
|
morphbr@565
|
47 |
self.map = {}
|
morphbr@565
|
48 |
self.list = []
|
morphbr@565
|
49 |
|
morphbr@565
|
50 |
for i in items:
|
morphbr@565
|
51 |
self._add(i)
|
morphbr@565
|
52 |
self._sort()
|
morphbr@565
|
53 |
# __init__()
|
morphbr@565
|
54 |
|
morphbr@565
|
55 |
|
morphbr@565
|
56 |
def _add(self, item):
|
morphbr@565
|
57 |
self.map[item.name] = item
|
morphbr@565
|
58 |
self.list.append(item)
|
morphbr@565
|
59 |
# _add()
|
morphbr@565
|
60 |
|
morphbr@565
|
61 |
|
morphbr@565
|
62 |
def add(self, item):
|
morphbr@565
|
63 |
self._add()
|
morphbr@565
|
64 |
self._sort()
|
morphbr@565
|
65 |
# add()
|
morphbr@565
|
66 |
|
morphbr@565
|
67 |
|
morphbr@565
|
68 |
def __getitem__(self, spec):
|
morphbr@565
|
69 |
if isinstance(spec, basestring):
|
morphbr@565
|
70 |
return self.map[spec]
|
morphbr@565
|
71 |
else:
|
morphbr@565
|
72 |
return self.list[spec]
|
morphbr@565
|
73 |
# __getitem__()
|
morphbr@565
|
74 |
|
morphbr@565
|
75 |
|
morphbr@565
|
76 |
def get(self, name, default=None):
|
morphbr@577
|
77 |
return self.map.get(name, default)
|
morphbr@565
|
78 |
# get()
|
morphbr@565
|
79 |
|
morphbr@565
|
80 |
|
morphbr@565
|
81 |
def __iter__(self):
|
morphbr@565
|
82 |
return self.list.__iter__()
|
morphbr@565
|
83 |
# __iter__()
|
morphbr@565
|
84 |
|
morphbr@565
|
85 |
|
morphbr@565
|
86 |
def __len__(self):
|
morphbr@565
|
87 |
return len(self.list)
|
morphbr@565
|
88 |
# __len__()
|
morphbr@565
|
89 |
|
morphbr@565
|
90 |
|
morphbr@565
|
91 |
def _sort(self):
|
morphbr@565
|
92 |
self.list.sort(lambda a, b: cmp(a.priority, b.priority))
|
morphbr@565
|
93 |
# _sort()
|
morphbr@565
|
94 |
|
morphbr@565
|
95 |
|
morphbr@565
|
96 |
def update(self, pluginset):
|
morphbr@565
|
97 |
self.map.update(pluginset.map)
|
morphbr@565
|
98 |
self.list.extend(pluginset.list)
|
morphbr@565
|
99 |
self._sort()
|
morphbr@565
|
100 |
# update()
|
morphbr@565
|
101 |
|
morphbr@565
|
102 |
|
morphbr@565
|
103 |
def load_from_directory(self, directory):
|
morphbr@565
|
104 |
for i in load_plugins(directory, self.basetype):
|
morphbr@565
|
105 |
self._add(i)
|
morphbr@565
|
106 |
self._sort()
|
morphbr@565
|
107 |
# load_from_directory()
|
morphbr@565
|
108 |
|
morphbr@565
|
109 |
|
morphbr@565
|
110 |
def __str__(self):
|
morphbr@565
|
111 |
lst = []
|
morphbr@565
|
112 |
for o in self.list:
|
morphbr@565
|
113 |
lst.append('"%s" (%s)' % (o.name, o.__name__))
|
morphbr@565
|
114 |
|
morphbr@565
|
115 |
return "%s(basetype=%s, items=[%s])" % \
|
morphbr@565
|
116 |
(self.__class__.__name__,
|
morphbr@565
|
117 |
self.basetype.__name__,
|
morphbr@565
|
118 |
", ".join(lst))
|
morphbr@565
|
119 |
# __str__()
|
morphbr@565
|
120 |
# PluginSet
|
morphbr@565
|
121 |
|
morphbr@565
|
122 |
|
morphbr@565
|
123 |
def load_plugins(directory, basetype):
|
morphbr@585
|
124 |
"""Function to load plugins from a given directory"""
|
morphbr@565
|
125 |
tn = basetype.__name__
|
morphbr@565
|
126 |
log.debug("Loading plugins from %s, type=%s" % (directory, tn))
|
morphbr@565
|
127 |
|
morphbr@565
|
128 |
|
morphbr@565
|
129 |
plugins = []
|
morphbr@565
|
130 |
for d in os.listdir(directory):
|
morphbr@565
|
131 |
if not d.endswith(".py"):
|
morphbr@565
|
132 |
continue
|
morphbr@565
|
133 |
|
morphbr@565
|
134 |
name = d[0: -3]
|
morphbr@565
|
135 |
if name == "__init__":
|
morphbr@565
|
136 |
continue
|
morphbr@565
|
137 |
|
morphbr@565
|
138 |
directory.replace(os.path.sep, ".")
|
morphbr@565
|
139 |
mod = _load_module([directory], name)
|
morphbr@565
|
140 |
for sym in dir(mod):
|
morphbr@565
|
141 |
cls = getattr(mod, sym)
|
morphbr@565
|
142 |
if isinstance(cls, type) and issubclass(cls, basetype) and \
|
morphbr@565
|
143 |
cls != basetype:
|
morphbr@565
|
144 |
plugins.append(cls)
|
morphbr@565
|
145 |
log.info("Loaded %s (%s) from %s" % \
|
morphbr@565
|
146 |
(cls.__name__, tn, os.path.join(directory, d)))
|
morphbr@565
|
147 |
|
morphbr@565
|
148 |
return plugins
|
morphbr@565
|
149 |
# load_plugins()
|
morphbr@585
|
150 |
|
morphbr@585
|
151 |
def getHTML(html_file, params={}):
|
morphbr@585
|
152 |
"""This function parses a file 'html_file.html' with the given
|
morphbr@585
|
153 |
parameters and returns a formated web-page"""
|
morphbr@585
|
154 |
try:
|
morphbr@585
|
155 |
filename = os.path.join(sys.path[0], "html", html_file + ".html")
|
morphbr@585
|
156 |
html = open(filename).read() % params
|
morphbr@585
|
157 |
return html
|
morphbr@585
|
158 |
except Exception, e:
|
morphbr@585
|
159 |
return "HTML format error. Wrong keys: %s" % e
|
morphbr@585
|
160 |
|
morphbr@585
|
161 |
# getHTML
|
morphbr@651
|
162 |
|
morphbr@651
|
163 |
def progress_bar(log, value, max, barsize):
|
morphbr@651
|
164 |
chars = int(value * barsize / float(max))
|
morphbr@651
|
165 |
percent = int((value / float(max)) * 100)
|
morphbr@651
|
166 |
sys.stdout.write("#" * chars)
|
morphbr@651
|
167 |
sys.stdout.write(" " * (barsize - chars + 2))
|
morphbr@651
|
168 |
if value >= max:
|
morphbr@651
|
169 |
sys.stdout.write("done. \n\n")
|
morphbr@651
|
170 |
else:
|
morphbr@651
|
171 |
sys.stdout.write("[%3i%%]\r" % (percent))
|
morphbr@651
|
172 |
sys.stdout.flush()
|
morphbr@653
|
173 |
return percent
|
morphbr@651
|
174 |
# progress_bar by osantana
|