VirtualBox

source: vbox/trunk/src/VBox/Main/glue/vboxapi.py@ 19855

Last change on this file since 19855 was 19855, checked in by vboxsync, 16 years ago

Python: win wrapper

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1#!/usr/bin/env python
2
3import sys,os
4import traceback
5
6VboxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)
7VboxSdkDir = os.environ.get("VBOX_SDK_PATH", None)
8
9if VboxBinDir is None:
10 # @todo: To be set by installer
11 VboxBinDir = "/home/nike/work/ws/out/linux.amd64/debug/bin/"
12
13if VboxSdkDir is None:
14 VboxSdkDir = VboxBinDir+"/sdk"
15
16os.environ["VBOX_PROGRAM_PATH"] = VboxBinDir
17os.environ["VBOX_SDK_PATH"] = VboxSdkDir
18sys.path.append(VboxBinDir)
19sys.path.append(VboxSdkDir+"/bindings/glue/python")
20
21class PlatformMSCOM:
22 class ConstantFake:
23 def __init__(self, parent, name):
24 self.__dict__['_parent'] = parent
25 self.__dict__['_name'] = name
26 self.__dict__['_consts'] = {}
27 try:
28 self.__dict__['_depth']=parent.__dict__['_depth']+1
29 except:
30 self.__dict__['_depth']=0
31 if self.__dict__['_depth'] > 4:
32 raise AttributeError
33
34 def __getattr__(self, attr):
35 if attr.startswith("__"):
36 raise AttributeError
37
38 consts = self.__dict__['_consts']
39 fake = consts.get(attr, None)
40 if fake != None:
41 return fake
42 try:
43 name = makeFullName(self, attr)
44 return win32com.client.constants.__getattr__(name)
45 except AttributeError,e:
46 fake = ConstantFake(self, attr)
47 consts[attr] = fake
48 return fake
49
50 class InterfacesWrapper:
51 def __init__(self):
52 self.__dict__['_rootFake'] = PlatformMSCOM.ConstantFake(None, None)
53
54 def __getattr__(self, a):
55 if a.startswith("__"):
56 raise AttributeError
57 try:
58 return win32com.client.constants.__getattr__(a)
59 except AttributeError,e:
60 return self.__dict__['_rootFake'].__getattr__(a)
61
62 def makeFullName(fake, attr):
63 name = fake._name
64 parent = fake._parent
65 while parent != None:
66 if parent._name is not None:
67 name = parent._name+'_'+name
68 parent = parent._parent
69
70 if name is not None:
71 name += "_" + attr
72 else:
73 name = attr
74 return name
75
76 def __init__(self, params):
77 sys.path.append(VboxSdkDir+'/bindings/mscom/python/')
78 from win32com import universal
79 from win32com.client import gencache, DispatchWithEvents, Dispatch
80 from win32com.client import constants, getevents
81 import win32com.server.register
82 import win32com
83 import pythoncom
84 import win32api
85 self.constants = PlatformMSCOM.InterfacesWrapper()
86 win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
87
88 def getSessionObject(self):
89 import win32com
90 from win32com.client import Dispatch
91 return win32com.client.Dispatch("{3C02F46D-C9D2-4f11-A384-53F0CF917214}")
92
93 def getVirtualBox(self):
94 import win32com
95 from win32com.client import Dispatch
96 return win32com.client.Dispatch("{3C02F46D-C9D2-4f11-A384-53F0CF917214}")
97
98 def getConstants(self):
99 return self.constants
100
101 def getType(self):
102 return 'MSCOM'
103
104 def getRemote(self):
105 return False
106
107 def getArray(self, obj, field):
108 return obj.__getattr__(field)
109
110
111class PlatformXPCOM:
112 def __init__(self, params):
113 sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
114 import xpcom.vboxxpcom
115 import xpcom
116 import xpcom.components
117
118 def getSessionObject(self):
119 import xpcom.components
120 return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
121
122 def getVirtualBox(self):
123 import xpcom.components
124 return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
125
126 def getConstants(self):
127 import xpcom.components
128 return xpcom.components.interfaces
129
130 def getType(self):
131 return 'XPCOM'
132
133 def getRemote(self):
134 return False
135
136 def getArray(self, obj, field):
137 return obj.__getattr__('get'+field.capitalize())()
138
139class PlatformWEBSERVICE:
140 def __init__(self, params):
141 sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
142 import VirtualBox_services
143 import VirtualBox_wrappers
144 from VirtualBox_wrappers import IWebsessionManager
145 from VirtualBox_wrappers import g_port
146 from VirtualBox_wrappers import g_reflectionInfo
147 self.wsmgr = IWebsessionManager()
148 self.port = g_port
149 self.constants = g_reflectionInfo
150 self.user = ""
151 self.password = ""
152
153 def getSessionObject(self):
154 return self.wsmgr.getSessionObject()
155
156 def getVirtualBox(self):
157 return self.wsmgr.logon(self.user, self.password)
158
159 def getConstants(self):
160 from VirtualBox_wrappers import g_reflectionInfo
161 return g_reflectionInfo
162
163 def getType(self):
164 return 'WEBSERVICE'
165
166 def getRemote(self):
167 return True
168
169 def getArray(self, obj, field):
170 return obj.__getattr__(field)
171
172
173class SessionManager:
174 def __init__(self, mgr):
175 self.mgr = mgr
176
177 def getSessionObject(self, vbox):
178 return self.mgr.platform.getSessionObject()
179
180class VirtualBoxManager:
181 def __init__(self, style, platparams):
182 if style is None:
183 if sys.platform == 'win32':
184 style = "MSCOM"
185 else:
186 style = "XPCOM"
187 try:
188 exec "self.platform = Platform"+style+"(platparams)"
189 self.vbox = self.platform.getVirtualBox()
190 self.mgr = SessionManager(self)
191 self.constants = self.platform.getConstants()
192 self.type = self.platform.getType()
193 self.remote = self.platform.getRemote()
194 except Exception,e:
195 print "init exception: ",e
196 traceback.print_exc()
197 raise e
198
199 def getArray(self, obj, field):
200 return self.platform.getArray(obj, field)
201
202 def getVirtualBox(self):
203 return self.platform.getVirtualBox()
204
205 def __del__(self):
206 if hasattr(self, "vbox"):
207 del self.vbox
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette