VirtualBox

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

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

Python: typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1#
2# Copyright (C) 2009 Sun Microsystems, Inc.
3#
4# This file is part of VirtualBox Open Source Edition (OSE), as
5# available from http://www.215389.xyz. This file is free software;
6# you can redistribute it and/or modify it under the terms of the GNU
7# General Public License (GPL) as published by the Free Software
8# Foundation, in version 2 as it comes in the "COPYING" file of the
9# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
10# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
11#
12# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
13# Clara, CA 95054 USA or visit http://www.sun.com if you need
14# additional information or have any questions.
15#
16import sys,os
17import traceback
18
19VboxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)
20VboxSdkDir = os.environ.get("VBOX_SDK_PATH", None)
21
22if VboxBinDir is None:
23 # Will be set by the installer
24 VboxBinDir = "%VBOX_INSTALL_PATH%"
25
26if VboxSdkDir is None:
27 VboxSdkDir = VboxBinDir+"/sdk"
28
29os.environ["VBOX_PROGRAM_PATH"] = VboxBinDir
30os.environ["VBOX_SDK_PATH"] = VboxSdkDir
31sys.path.append(VboxBinDir)
32# This directory's content goes to the site-wide directory
33#sys.path.append(VboxSdkDir+"/bindings/glue/python")
34
35from VirtualBox_constants import VirtualBoxReflectionInfo
36
37class PlatformMSCOM:
38 class ConstantFake:
39 def __init__(self, parent, name):
40 self.__dict__['_parent'] = parent
41 self.__dict__['_name'] = name
42 self.__dict__['_consts'] = {}
43 try:
44 self.__dict__['_depth']=parent.__dict__['_depth']+1
45 except:
46 self.__dict__['_depth']=0
47 if self.__dict__['_depth'] > 4:
48 raise AttributeError
49
50 def __getattr__(self, attr):
51 import win32com
52 from win32com.client import constants
53
54 if attr.startswith("__"):
55 raise AttributeError
56
57 consts = self.__dict__['_consts']
58
59 fake = consts.get(attr, None)
60 if fake != None:
61 return fake
62 try:
63 name = self.__dict__['_name']
64 parent = self.__dict__['_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 print "ask",name
75 return win32com.client.constants.__getattr__(name)
76 except AttributeError,e:
77 fake = PlatformMSCOM.ConstantFake(self, attr)
78 consts[attr] = fake
79 return fake
80
81
82 class InterfacesWrapper:
83 def __init__(self):
84 self.__dict__['_rootFake'] = PlatformMSCOM.ConstantFake(None, None)
85
86 def __getattr__(self, a):
87 import win32com
88 from win32com.client import constants
89 if a.startswith("__"):
90 raise AttributeError
91 try:
92 return win32com.client.constants.__getattr__(a)
93 except AttributeError,e:
94 return self.__dict__['_rootFake'].__getattr__(a)
95
96 def __init__(self, params):
97 sys.path.append(VboxSdkDir+'/bindings/mscom/python/')
98 from win32com import universal
99 from win32com.client import gencache, DispatchWithEvents, Dispatch
100 from win32com.client import constants, getevents
101 import win32com
102 import pythoncom
103 import win32api
104 self.constants = PlatformMSCOM.InterfacesWrapper()
105 #win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
106 #win32com.client.gencache.EnsureDispatch('VirtualBox.VirtualBox')
107
108 def getSessionObject(self, vbox):
109 import win32com
110 from win32com.client import Dispatch
111 return win32com.client.Dispatch("VirtualBox.Session")
112
113 def getVirtualBox(self):
114 import win32com
115 from win32com.client import Dispatch
116 return win32com.client.Dispatch("VirtualBox.VirtualBox")
117
118 def getConstants(self):
119 return self.constants
120
121 def getType(self):
122 return 'MSCOM'
123
124 def getRemote(self):
125 return False
126
127 def getArray(self, obj, field):
128 return obj.__getattr__(field)
129
130 def initPerThread(self):
131 import pythoncom
132 pythoncom.CoInitializeEx(0)
133
134 def deinitPerThread(self):
135 import pythoncom
136 pythoncom.CoUninitialize()
137
138
139class PlatformXPCOM:
140 def __init__(self, params):
141 sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
142 import xpcom.vboxxpcom
143 import xpcom
144 import xpcom.components
145
146 def getSessionObject(self, vbox):
147 import xpcom.components
148 return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
149
150 def getVirtualBox(self):
151 import xpcom.components
152 return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
153
154 def getConstants(self):
155 import xpcom.components
156 return xpcom.components.interfaces
157
158 def getType(self):
159 return 'XPCOM'
160
161 def getRemote(self):
162 return False
163
164 def getArray(self, obj, field):
165 return obj.__getattr__('get'+field.capitalize())()
166
167 def initPerThread(self):
168 pass
169
170 def deinitPerThread(self):
171 pass
172
173class PlatformWEBSERVICE:
174 def __init__(self, params):
175 sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
176 import VirtualBox_services
177 import VirtualBox_wrappers
178 from VirtualBox_wrappers import IWebsessionManager2
179 if params is not None:
180 self.user = params.get("user", "")
181 self.password = params.get("password", "")
182 self.url = params.get("url", "")
183 else:
184 self.user = ""
185 self.password = ""
186 self.url = None
187 self.wsmgr = IWebsessionManager2(self.url)
188
189 def getSessionObject(self, vbox):
190 return self.wsmgr.getSessionObject(vbox)
191
192 def getVirtualBox(self):
193 return self.wsmgr.logon(self.user, self.password)
194
195 def getConstants(self):
196 return None
197
198 def getType(self):
199 return 'WEBSERVICE'
200
201 def getRemote(self):
202 return True
203
204 def getArray(self, obj, field):
205 return obj.__getattr__(field)
206
207 def initPerThread(self):
208 pass
209
210 def deinitPerThread(self):
211 pass
212
213class SessionManager:
214 def __init__(self, mgr):
215 self.mgr = mgr
216
217 def getSessionObject(self, vbox):
218 return self.mgr.platform.getSessionObject(vbox)
219
220class VirtualBoxManager:
221 def __init__(self, style, platparams):
222 if style is None:
223 if sys.platform == 'win32':
224 style = "MSCOM"
225 else:
226 style = "XPCOM"
227 try:
228 exec "self.platform = Platform"+style+"(platparams)"
229 self.vbox = self.platform.getVirtualBox()
230 self.mgr = SessionManager(self)
231 self.constants = VirtualBoxReflectionInfo()
232 self.type = self.platform.getType()
233 self.remote = self.platform.getRemote()
234 except Exception,e:
235 print "init exception: ",e
236 traceback.print_exc()
237 raise e
238
239 def getArray(self, obj, field):
240 return self.platform.getArray(obj, field)
241
242 def getVirtualBox(self):
243 return self.platform.getVirtualBox()
244
245 def __del__(self):
246 if hasattr(self, "vbox"):
247 del self.vbox
248
249 def initPerThread(self):
250 self.platform.initPerThread()
251
252 def openMachineSession(self, machineId):
253 session = self.mgr.getSessionObject(self.vbox)
254 self.vbox.openSession(session, machineId)
255 return session
256
257 def closeMachineSession(self, session):
258 session.close()
259
260 def deinitPerThread(self):
261 self.platform.deinitPerThread()
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