VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxShell/mscom/vboxshell.py@ 19783

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

Python: MSCOM constants support

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1#!python
2#
3#################################################################################
4# This program is a simple interactive shell for VirtualBox. You can query #
5# information and issue commands from a simple command line. #
6# #
7# It also provides you with examples on how to use VirtualBox's Python API. #
8# This shell is even somewhat documented and supports TAB-completion and #
9# history if you have Python readline installed. #
10# #
11# Enjoy. #
12#################################################################################
13#
14#
15
16import sys, os
17from win32com import universal
18from win32com.client import gencache, DispatchWithEvents, Dispatch
19from win32com.client import constants, getevents
20import win32com.server.register
21import win32com
22import pythoncom
23import win32api
24import traceback
25
26from shellcommon import interpret
27
28class LocalManager:
29 def getSessionObject(self, vb):
30 return win32com.client.Dispatch("{3C02F46D-C9D2-4f11-A384-53F0CF917214}")
31
32vbox = None
33mgr = LocalManager()
34try:
35 vbox = win32com.client.Dispatch("{B1A7A4F2-47B9-4A1E-82B2-07CCD5323C3F}")
36 win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
37except Exception,e:
38 print "COM exception: ",e
39 traceback.print_exc()
40 sys.exit(1)
41
42class ConstantFake:
43 def __init__(self, parent, name):
44 self.__dict__['_parent'] = parent
45 self.__dict__['_name'] = name
46 try:
47 self.__dict__['_depth']=parent.__dict__['_depth']+1
48 except:
49 self.__dict__['_depth']=0
50 self.__dict__['_klazz'] = self.__class__
51 if self.__dict__['_depth'] > 4:
52 raise AttributeError
53
54 def __getattr__(self, attr):
55 if attr.startswith("__"):
56 raise AttributeError
57
58 try:
59 n = makeFullName(self) + "_" + attr
60 v = win32com.client.constants.__getattr__(n)
61 return v
62 except AttributeError,e:
63 return ConstantFake(self, attr)
64
65def makeFullName(fake):
66 name = fake._name
67 parent = fake._parent
68 while parent != None:
69 name = parent._name+'_'+name
70 parent = parent._parent
71 return name
72
73
74class InterfacesWrapper:
75 """COM constants name translator
76 """
77 def __init__(self):
78 pass
79
80 def __getattr__(self, a):
81 try:
82 return win32com.client.constants.__getattr__(a)
83 except AttributeError,e:
84 if a.startswith("__"):
85 raise e
86 return ConstantFake(None, a)
87
88ctx = {'mgr':mgr, 'vb':vbox, 'ifaces':InterfacesWrapper(),
89 'remote':False, 'type':'mscom' }
90
91interpret(ctx)
92
93del vbox
Note: See TracBrowser for help on using the repository browser.

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