-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlib.py
More file actions
82 lines (69 loc) · 2.44 KB
/
lib.py
File metadata and controls
82 lines (69 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# This file is part of the OpenWire Framework.
#
# OpenWire is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenWire is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenWIre. If not, see <http://www.gnu.org/licenses/>.
import sys
import glob
import os
class libraries:
def __init__(self, framework):
#self.framework = framework
self.framework = framework
self._liblist = self.preloadLibs()
for lib in self._liblist:
if '__init__' in lib or '.pyc' in lib:
continue
# Create an instance of the library
def loadLibrary(self, lib):
if lib == "libs." or "__init__" in lib or ".pyc" in lib:
return False
libf = "libs."+str(lib)
if not os.path.isfile("libs/"+lib+".py"):
print self.framework.prompt + " " + libf + " is not a valid library"
return False
#print self.framework.prompt+" Loading Library " + lib
library = self.ezImport(libf)
setattr(self, str(lib), library(self.framework))
# Import Library
def ezImport(self, name):
components = name.split('.')
comp = components[1]
mod = __import__(name, fromlist=[comp])
mod = getattr(mod, comp)
return mod
# Load modules into global namespace
def preloadLibs(self):
sys.path.append("libs")
lib = glob.glob("libs/*.py")
lib_files = []
for i in range(len(lib)):
self.loadLibrary(lib[i][5:-3])
lib[i] = lib[i].split(".")[0].split("/")[1]
lib[i] = "libs."+lib[i]
globals()[lib[i]] = self.ezImport(lib[i])
lib_files.append(lib[i])
return lib_files
# List installed libraries
def listLibs(self):
classes = glob.glob("libs/*.py")
for i in range(len(classes)):
classes[i] = classes[i].split(".")[0].split("/")[1]
if classes[i] == '__init__':
continue
classes[i] = "libs."+classes[i]
if classes[i] in globals():
lib = self.ezImport(classes[i])
library = lib(self)
print self.framework.prompt+self.framework.libs.colours.cstring(' ~> ', 'white')+self.framework.libs.colours.cstring(library.name+" - "+library.description, "blue")
library = None
return True