-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_command.py
167 lines (149 loc) · 5.52 KB
/
main_command.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import sys
import ConfigParser
from bmplugin import *
main=None
class main_command:
"""blackMoon main class"""
def __init__(self):
global main
self.maintive=lib_interactive.interactive()
self.maintive.setdefaultgroups(PLUGINS_GROUPS)
self.gdict={'plugins':{},'objects':{}}
self.maintive.share=self.gdict
self.pcf=lib_config.bmconfig(CONFIG_PATH)
funcdict={'plugins':self.plugins,
'search':self.search,
'varobj':self.listobj,
'clear':self.clear,
'eval':self.bmeval,
'dump':self.dump,
'load':self.load,
'reload':self.reload}
main=self
for cmd,func in funcdict.iteritems():
self.maintive.regcommand(cmd,func,func.__doc__)
def plugins(self,paras=None):
"""list loaded plugins"""
if not paras:
lib_func.printlist(self.gdict['plugins'].keys(),1)
def reloadconfig(self):
self.pcf=lib_config.bmconfig(CONFIG_PATH)
def search(self,paras):
"""search plugin for you key"""
pass
def listobj(self,paras):
"""list objection in the space"""
for space,dt in self.gdict['objects'].iteritems():
lib_func.printstr(space,"#####")
for key in dt.keys():
lib_func.printstr(key,"Object:")
def clear(self,paras):
"""clear [-a] [-s space.name] """
try:
pd=lib_func.getparasdict(paras,"as:")
if not pd:
lib_func.printstr("You should input the vaild parameters",1)
lib_func.printstr(self.clear.__doc__)
return
except Exception:
lib_func.printstr(self.clear.__doc__)
return
if pd.has_key('a'):
self.gdict['objects']=dict()
return
if pd.has_key('s'):
sn=pd['s'].split('.',1)
if len(sn)!=2:
lib_func.printstr(self.clear.__doc__)
return
if self.gdict['objects'].has_key(sn[0]) and self.gdict['objects'][sn[0]].has_key(sn[1]):
self.gdict['objects'][sn[0]].pop(sn[1])
def start(self):
self.maintive.start()
def regobj(self,obj,name,space='buildins'):
if self.gdict['objects'].has_key(space):
if self.gdict['objects'][space].has_key(name):
lib_func.printstr("you can't reg same name objects in %s" %space)
return
else:
self.gdict['objects'][space][name]=obj
else:
self.gdict['objects'][space]=dict()
self.gdict['objects'][space][name]=obj
lib_func.printstr("You reg object %s in %s succfully" %(name,space))
def getobj_(self,name,space='buildins'):
if self.gdict['objects'].has_key(space):
if self.gdict['objects'][space].has_key(name):
return self.gdict['objects'][space][name]
lib_func.printstr("Have not %s in %s" %(name,space),1)
return None
def bmeval(self,sh):
"""eval python_segment"""
try:
rs=eval(sh)
print rs
except Exception:
print "Error"
def dump(self,paras):
"""dump [-o filename] [--space=space] objname"""
try:
pd=lib_func.getparasdict(paras,"o:",['space='])
if (not pd) or len(pd['args'])!=1:
lib_func.printstr("You should input the vaild parameters",1)
lib_func.printstr(self.dump.__doc__)
return
except Exception:
lib_func.printstr(self.dump.__doc__)
return
space=None
if pd.has_key('space'):
space=pd['space']
if pd.has_key('o'):
fp=pd['o']
else:
fp="%s/%.pkl" %(PWD_PATH,lib_func.getrandomstr())
obj=getobj(pd['args'][0],space)
if obj:
lib_func.dumpobj(obj,fp)
else:
lib_func.printstr("Have not %s" %pd['args'][0])
def load(self,paras):
"""load [-o objname] filename"""
try:
pd=lib_func.getparasdict(paras,"o:")
if (not pd) or len(pd['args'])!=1:
lib_func.printstr("You should input the vaild parameters",1)
lib_func.printstr(self.load.__doc__)
return
except Exception:
lib_func.printstr(self.load.__doc__)
return
if pd.has_key('o'):
objname=pd['o']
else:
objname="pkl_%s" %lib_func.getrandomstr()
obj=lib_func.loadobj(pd['args'][0])
if obj:
self.regobj(obj,objname)
def reload(self,paras):
"""reload module_name"""
try:
pd=lib_func.getparasdict(paras,"")
except Exception:
lib_func.pris(self.reload.__doc__)
return
ddict=dict()
if lib_func.setparas(pd,ddict,1):
pass
else:
lib_func.pris(self.reload.__doc__)
def getobj(self,name,space=None):
if space:
if self.gdict['objects'].has_key(space):
if self.gdict['objects'][space].has_key(name):
return self.gdict['objects'][space][name]
for space in self.gdict['objects']:
if self.gdict['objects'][space].has_key(name):
return self.gdict['objects'][space][name]
return None