VirtualBox

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

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

Python: generic glue library, hiding all ugly platform details

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 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'] = 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 #win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
86
87 def getSessionObject(self):
88 from win32com.client import Dispatch
89 return win32com.client.Dispatch("{3C02F46D-C9D2-4f11-A384-53F0CF917214}")
90
91 def getVirtualBox(self):
92 from win32com.client import Dispatch
93 return win32com.client.Dispatch("{3C02F46D-C9D2-4f11-A384-53F0CF917214}")
94
95 def getConstants(self):
96 return InterfacesWrapper()
97
98 def getType(self):
99 return 'MSCOM'
100
101 def getRemote(self):
102 return False
103
104 def getArray(self, obj, field):
105 return obj.__getattr__(field)
106
107
108class PlatformXPCOM:
109 def __init__(self, params):
110 sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
111 import xpcom.vboxxpcom
112 import xpcom
113 import xpcom.components
114
115 def getSessionObject(self):
116 import xpcom.components
117 return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
118
119 def getVirtualBox(self):
120 import xpcom.components
121 return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
122
123 def getConstants(self):
124 import xpcom.components
125 return xpcom.components.interfaces
126
127 def getType(self):
128 return 'XPCOM'
129
130 def getRemote(self):
131 return False
132
133 def getArray(self, obj, field):
134 return obj.__getattr__('get'+field.capitalize())()
135
136class PlatformWEBSERVICE:
137 def __init__(self, params):
138 sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
139 import VirtualBox_services
140 import VirtualBox_wrappers
141 from VirtualBox_wrappers import IWebsessionManager
142 from VirtualBox_wrappers import g_port
143 from VirtualBox_wrappers import g_reflectionInfo
144 self.wsmgr = IWebsessionManager()
145 self.port = g_port
146 self.constants = g_reflectionInfo
147 self.user = ""
148 self.password = ""
149
150 def getSessionObject(self):
151 return self.wsmgr.getSessionObject()
152
153 def getVirtualBox(self):
154 return self.wsmgr.logon(self.user, self.password)
155
156 def getConstants(self):
157 from VirtualBox_wrappers import g_reflectionInfo
158 return g_reflectionInfo
159
160 def getType(self):
161 return 'WEBSERVICE'
162
163 def getRemote(self):
164 return True
165
166 def getArray(self, obj, field):
167 return obj.__getattr__(field)
168
169
170class SessionManager:
171 def __init__(self, mgr):
172 self.mgr = mgr
173
174 def getSessionObject(self, vbox):
175 return self.mgr.platform.getSessionObject()
176
177class VirtualBoxManager:
178 def __init__(self, style, platparams):
179 if style is None:
180 if sys.platform == 'win32':
181 style = "MSCOM"
182 else:
183 style = "XPCOM"
184 try:
185 exec "self.platform = Platform"+style+"(platparams)"
186 self.vbox = self.platform.getVirtualBox()
187 self.mgr = SessionManager(self)
188 self.constants = self.platform.getConstants()
189 self.type = self.platform.getType()
190 self.remote = self.platform.getRemote()
191 except Exception,e:
192 print "init exception: ",e
193 traceback.print_exc()
194 raise e
195
196 def getArray(self, obj, field):
197 return self.platform.getArray(obj, field)
198
199 def getVirtualBox(self):
200 return self.platform.getVirtualBox()
201
202 def __del__(self):
203 if hasattr(self, "vbox"):
204 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