VirtualBox

Ignore:
Timestamp:
May 19, 2009 11:57:59 AM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
47464
Message:

Python: better constants wrapper, refactoring of shell's Windows port

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxShell/mscom/vboxshell.py

    r19783 r19814  
    2222import pythoncom
    2323import win32api
    24 import traceback
    2524
    2625from shellcommon import interpret
    2726
    2827class LocalManager:
    29     def getSessionObject(self, vb):
    30         return win32com.client.Dispatch("{3C02F46D-C9D2-4f11-A384-53F0CF917214}")
     28        def getSessionObject(self, vb):
     29                return win32com.client.Dispatch("{3C02F46D-C9D2-4f11-A384-53F0CF917214}")
     30
     31class ConstantFake:
     32        def __init__(self, parent, name):
     33                self.__dict__['_parent'] = parent
     34                self.__dict__['_name'] = name
     35                self.__dict__['_consts'] = {}
     36                try:
     37                        self.__dict__['_depth']=parent.__dict__['_depth']+1
     38                except:
     39                        self.__dict__['_depth']=0
     40                if self.__dict__['_depth'] > 4:
     41                        raise AttributeError
     42
     43        def __getattr__(self, attr):
     44                if attr.startswith("__"):
     45                        raise AttributeError
     46
     47                consts = self.__dict__['_consts']
     48                fake = consts.get(attr, None)
     49                if fake != None:
     50                        return fake 
     51                try:
     52                        name = makeFullName(self, attr)
     53                        return win32com.client.constants.__getattr__(name)
     54                except AttributeError,e:
     55                        fake = ConstantFake(self, attr)
     56                        consts[attr] = fake
     57                        return fake     
     58
     59def makeFullName(fake, attr):
     60        name = fake._name
     61        parent = fake._parent
     62        while parent != None:
     63                if parent._name is not None:
     64                        name = parent._name+'_'+name
     65                parent = parent._parent
     66
     67        if name:
     68                name += "_" + attr
     69        else:
     70                name = attr
     71        return name
     72
     73
     74class InterfacesWrapper:
     75        def __init__(self):
     76                self.__dict__['_rootFake'] = ConstantFake(None, None)
     77
     78        def __getattr__(self, a):
     79                if a.startswith("__"):
     80                        raise AttributeError
     81                try:
     82                        return win32com.client.constants.__getattr__(a)
     83                except AttributeError,e:
     84                        return self.__dict__['_rootFake'].__getattr__(a)
     85
    3186
    3287vbox = None
    3388mgr = LocalManager()
    3489try:
     90        win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
    3591        vbox = win32com.client.Dispatch("{B1A7A4F2-47B9-4A1E-82B2-07CCD5323C3F}")
    36         win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
    3792except Exception,e:
    38     print "COM exception: ",e
    39     traceback.print_exc()
    40     sys.exit(1)
     93        print "COM exception: ",e
     94        traceback.print_exc()
     95        sys.exit(1)
    4196
    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)
    8797
    8898ctx = {'mgr':mgr, 'vb':vbox, 'ifaces':InterfacesWrapper(),
Note: See TracChangeset for help on using the changeset viewer.

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