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 |
|
---|
16 | import sys, os
|
---|
17 | from win32com import universal
|
---|
18 | from win32com.client import gencache, DispatchWithEvents, Dispatch
|
---|
19 | from win32com.client import constants, getevents
|
---|
20 | import win32com.server.register
|
---|
21 | import win32com
|
---|
22 | import pythoncom
|
---|
23 | import win32api
|
---|
24 | import traceback
|
---|
25 |
|
---|
26 | from shellcommon import interpret
|
---|
27 |
|
---|
28 | class LocalManager:
|
---|
29 | def getSessionObject(self, vb):
|
---|
30 | return win32com.client.Dispatch("{3C02F46D-C9D2-4f11-A384-53F0CF917214}")
|
---|
31 |
|
---|
32 | vbox = None
|
---|
33 | mgr = LocalManager()
|
---|
34 | try:
|
---|
35 | vbox = win32com.client.Dispatch("{B1A7A4F2-47B9-4A1E-82B2-07CCD5323C3F}")
|
---|
36 | win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
|
---|
37 | except Exception,e:
|
---|
38 | print "COM exception: ",e
|
---|
39 | traceback.print_exc()
|
---|
40 | sys.exit(1)
|
---|
41 |
|
---|
42 | class 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 |
|
---|
65 | def 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 |
|
---|
74 | class 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 |
|
---|
88 | ctx = {'mgr':mgr, 'vb':vbox, 'ifaces':InterfacesWrapper(),
|
---|
89 | 'remote':False, 'type':'mscom' }
|
---|
90 |
|
---|
91 | interpret(ctx)
|
---|
92 |
|
---|
93 | del vbox
|
---|