home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2465 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.3 KB  |  145 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import string
  6. import os
  7. import win32ras
  8. stateMap = { }
  9. for name, val in win32ras.__dict__.items():
  10.     if name[:6] == 'RASCS_':
  11.         stateMap[val] = name[6:]
  12.         continue
  13.  
  14. import win32event
  15. callbackEvent = win32event.CreateEvent(None, 0, 0, None)
  16.  
  17. def Callback(hras, msg, state, error, exterror):
  18.     stateName = stateMap.get(state, 'Unknown state?')
  19.     print 'Status is %s (%04lx), error code is %d' % (stateName, state, error)
  20.     finished = state in [
  21.         win32ras.RASCS_Connected]
  22.     if finished:
  23.         win32event.SetEvent(callbackEvent)
  24.     
  25.     if error != 0 or int(state) == win32ras.RASCS_Disconnected:
  26.         print 'Detected call failure: %s' % win32ras.GetErrorString(error)
  27.         HangUp(hras)
  28.         win32event.SetEvent(callbackEvent)
  29.     
  30.  
  31.  
  32. def ShowConnections():
  33.     print 'All phone-book entries:'
  34.     for name, in win32ras.EnumEntries():
  35.         print ' ', name
  36.     
  37.     print 'Current Connections:'
  38.     for con in win32ras.EnumConnections():
  39.         print ' ', con
  40.     
  41.  
  42.  
  43. def EditEntry(entryName):
  44.     
  45.     try:
  46.         win32ras.EditPhonebookEntry(0, None, entryName)
  47.     except win32ras.error:
  48.         (rc, function, msg) = None
  49.         print 'Can not edit/find the RAS entry -', msg
  50.  
  51.  
  52.  
  53. def HangUp(hras):
  54.     
  55.     try:
  56.         win32ras.HangUp(hras)
  57.     except:
  58.         print "Tried to hang up gracefully on error, but didn't work...."
  59.  
  60.  
  61.  
  62. def Connect(entryName, bUseCallback):
  63.     if bUseCallback:
  64.         theCallback = Callback
  65.         win32event.ResetEvent(callbackEvent)
  66.     else:
  67.         theCallback = None
  68.     
  69.     try:
  70.         (dp, b) = win32ras.GetEntryDialParams(None, entryName)
  71.     except:
  72.         print "Couldn't find DUN entry: %s" % entryName
  73.  
  74.     (hras, rc) = win32ras.Dial(None, None, (entryName, '', '', dp[3], dp[4], ''), theCallback)
  75.     if not bUseCallback and rc != 0:
  76.         print 'Could not dial the RAS connection:', win32ras.GetErrorString(rc)
  77.         hras = HangUp(hras)
  78.     elif bUseCallback and win32event.WaitForSingleObject(callbackEvent, 60000) != win32event.WAIT_OBJECT_0:
  79.         print 'Gave up waiting for the process to complete!'
  80.         
  81.         try:
  82.             cs = win32ras.GetConnectStatus(hras)
  83.         except:
  84.             hras = HangUp(hras)
  85.  
  86.         if int(cs[0]) == win32ras.RASCS_Disconnected:
  87.             hras = HangUp(hras)
  88.         
  89.     
  90.     return (hras, rc)
  91.  
  92.  
  93. def Disconnect(rasEntry):
  94.     name = string.lower(rasEntry)
  95.     for hcon, entryName, devName, devType in win32ras.EnumConnections():
  96.         if string.lower(entryName) == name:
  97.             win32ras.HangUp(hcon)
  98.             print 'Disconnected from', rasEntry
  99.             break
  100.             continue
  101.     else:
  102.         print 'Could not find an open connection to', entryName
  103.  
  104. usage = '\nUsage: %s [-s] [-l] [-c connection] [-d connection]\n-l : List phone-book entries and current connections.\n-s : Show status while connecting/disconnecting (uses callbacks)\n-c : Connect to the specified phonebook name.\n-d : Disconnect from the specified phonebook name.\n-e : Edit the specified phonebook entry.\n'
  105.  
  106. def main():
  107.     import getopt
  108.     
  109.     try:
  110.         (opts, args) = getopt.getopt(sys.argv[1:], 'slc:d:e:')
  111.     except getopt.error:
  112.         why = None
  113.         print why
  114.         print usage % os.path.basename(sys.argv[0])
  115.         return None
  116.  
  117.     bCallback = 0
  118.     if args or not opts:
  119.         print usage % os.path.basename(sys.argv[0])
  120.         return None
  121.     for opt, val in opts:
  122.         if opt == '-s':
  123.             bCallback = 1
  124.         
  125.         if opt == '-l':
  126.             ShowConnections()
  127.         
  128.         if opt == '-c':
  129.             (hras, rc) = Connect(val, bCallback)
  130.             if hras != None:
  131.                 print 'hras: 0x%8lx, rc: 0x%04x' % (hras, rc)
  132.             
  133.         
  134.         if opt == '-d':
  135.             Disconnect(val)
  136.         
  137.         if opt == '-e':
  138.             EditEntry(val)
  139.             continue
  140.     
  141.  
  142. if __name__ == '__main__':
  143.     main()
  144.  
  145.