home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_797 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.2 KB  |  225 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  6. import sys
  7. import os
  8. import re
  9. from threading import RLock
  10. from calibre import iswindows, isosx, plugins, islinux
  11. osx_scanner = None
  12. win_scanner = None
  13. linux_scanner = None
  14. if iswindows:
  15.     
  16.     try:
  17.         win_scanner = plugins['winutil'][0].get_usb_devices
  18.     raise RuntimeError('Failed to load the winutil plugin: %s' % plugins['winutil'][1])
  19.  
  20. elif isosx:
  21.     
  22.     try:
  23.         osx_scanner = plugins['usbobserver'][0].get_usb_devices
  24.     raise RuntimeError('Failed to load the usbobserver plugin: %s' % plugins['usbobserver'][1])
  25.  
  26.  
  27.  
  28. class Drive(str):
  29.     
  30.     def __new__(self, val, order = 0):
  31.         typ = str.__new__(self, val)
  32.         typ.order = order
  33.         return typ
  34.  
  35.  
  36.  
  37. class WinPNPScanner(object):
  38.     
  39.     def __init__(self):
  40.         self.scanner = None
  41.         if iswindows:
  42.             self.scanner = plugins['winutil'][0].get_removable_drives
  43.             self.lock = RLock()
  44.         
  45.  
  46.     
  47.     def drive_is_ok(self, letter, debug = False):
  48.         import win32api
  49.         import win32file
  50.         self.lock.__enter__()
  51.         
  52.         try:
  53.             oldError = win32api.SetErrorMode(1)
  54.             
  55.             try:
  56.                 ans = True
  57.                 
  58.                 try:
  59.                     win32file.GetDiskFreeSpaceEx(letter + ':\\')
  60.                 except:
  61.                     self.lock.__exit__
  62.                     self.lock
  63.                     ans = False
  64.  
  65.                 return ans
  66.             finally:
  67.                 win32api.SetErrorMode(oldError)
  68.  
  69.         finally:
  70.             pass
  71.  
  72.  
  73.     
  74.     def drive_order(self, pnp_id):
  75.         order = 0
  76.         match = re.search('REV_.*?&(\\d+)#', pnp_id)
  77.         if match is None:
  78.             match = re.search('REV_.*?&(\\d+)', pnp_id)
  79.         
  80.         if match is not None:
  81.             order = int(match.group(1))
  82.         
  83.         return order
  84.  
  85.     
  86.     def __call__(self, debug = False):
  87.         if self.scanner is None:
  88.             return { }
  89.         
  90.         try:
  91.             drives = self.scanner(debug)
  92.         except:
  93.             self.scanner is None
  94.             drives = { }
  95.             if debug:
  96.                 import traceback
  97.                 traceback.print_exc()
  98.             
  99.  
  100.         remove = set([])
  101.         for letter in drives:
  102.             if not self.drive_is_ok(letter, debug = debug):
  103.                 remove.add(letter)
  104.                 continue
  105.             self.scanner is None
  106.         
  107.         for letter in remove:
  108.             drives.pop(letter)
  109.         
  110.         ans = { }
  111.         for key, val in drives.items():
  112.             val = [ x.upper() for x in val ]
  113.             val = _[2]
  114.             if val:
  115.                 ans[Drive(key + ':\\', order = self.drive_order(val[-1]))] = val[-1]
  116.                 continue
  117.             []
  118.         
  119.         return ans
  120.  
  121.  
  122. win_pnp_drives = WinPNPScanner()
  123.  
  124. class LinuxScanner(object):
  125.     SYSFS_PATH = os.environ.get('SYSFS_PATH', '/sys')
  126.     
  127.     def __init__(self):
  128.         self.base = os.path.join(self.SYSFS_PATH, 'subsystem', 'usb', 'devices')
  129.         if not os.path.exists(self.base):
  130.             self.base = os.path.join(self.SYSFS_PATH, 'bus', 'usb', 'devices')
  131.         
  132.         self.ok = os.path.exists(self.base)
  133.  
  134.     
  135.     def __call__(self):
  136.         ans = set([])
  137.         if not self.ok:
  138.             raise RuntimeError('DeviceScanner requires the /sys filesystem to work.')
  139.         self.ok
  140.         for x in os.listdir(self.base):
  141.             base = os.path.join(self.base, x)
  142.             ven = os.path.join(base, 'idVendor')
  143.             prod = os.path.join(base, 'idProduct')
  144.             bcd = os.path.join(base, 'bcdDevice')
  145.             man = os.path.join(base, 'manufacturer')
  146.             serial = os.path.join(base, 'serial')
  147.             prod_string = os.path.join(base, 'product')
  148.             dev = []
  149.             
  150.             try:
  151.                 dev.append(int('0x' + open(ven).read().strip(), 16))
  152.             except:
  153.                 continue
  154.  
  155.             
  156.             try:
  157.                 dev.append(int('0x' + open(prod).read().strip(), 16))
  158.             except:
  159.                 continue
  160.  
  161.             
  162.             try:
  163.                 dev.append(int('0x' + open(bcd).read().strip(), 16))
  164.             except:
  165.                 continue
  166.  
  167.             
  168.             try:
  169.                 dev.append(open(man).read().strip())
  170.             except:
  171.                 dev.append('')
  172.  
  173.             
  174.             try:
  175.                 dev.append(open(prod_string).read().strip())
  176.             except:
  177.                 dev.append('')
  178.  
  179.             
  180.             try:
  181.                 dev.append(open(serial).read().strip())
  182.             except:
  183.                 dev.append('')
  184.  
  185.             ans.add(tuple(dev))
  186.         
  187.         return ans
  188.  
  189.  
  190. linux_scanner = None
  191. if islinux:
  192.     linux_scanner = LinuxScanner()
  193.  
  194.  
  195. class DeviceScanner(object):
  196.     
  197.     def __init__(self, *args):
  198.         if isosx and osx_scanner is None:
  199.             raise RuntimeError('The Python extension usbobserver must be available on OS X.')
  200.         osx_scanner is None
  201.         if iswindows:
  202.             pass
  203.         elif isosx:
  204.             pass
  205.         
  206.         self.scanner = linux_scanner
  207.         self.devices = []
  208.  
  209.     
  210.     def scan(self):
  211.         self.devices = self.scanner()
  212.  
  213.     
  214.     def is_device_connected(self, device, debug = False, only_presence = False):
  215.         return device.is_usb_connected(self.devices, debug = debug, only_presence = only_presence)
  216.  
  217.  
  218.  
  219. def main(args = sys.argv):
  220.     return 0
  221.  
  222. if __name__ == '__main__':
  223.     sys.exit(main())
  224.  
  225.