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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import dbus
  8. import os
  9.  
  10. def node_mountpoint(node):
  11.     
  12.     def de_mangle(raw):
  13.         return raw.replace('\\040', ' ').replace('\\011', '\t').replace('\\012', '\n').replace('\\0134', '\\')
  14.  
  15.     for line in open('/proc/mounts').readlines():
  16.         line = line.split()
  17.         if line[0] == node:
  18.             return de_mangle(line[1])
  19.     
  20.  
  21.  
  22. class UDisks(object):
  23.     
  24.     def __init__(self):
  25.         if os.environ.get('CALIBRE_DISABLE_UDISKS', False):
  26.             raise Exception('User has aborted use of UDISKS')
  27.         os.environ.get('CALIBRE_DISABLE_UDISKS', False)
  28.         self.bus = dbus.SystemBus()
  29.         self.main = dbus.Interface(self.bus.get_object('org.freedesktop.UDisks', '/org/freedesktop/UDisks'), 'org.freedesktop.UDisks')
  30.  
  31.     
  32.     def device(self, device_node_path):
  33.         devpath = self.main.FindDeviceByDeviceFile(device_node_path)
  34.         return dbus.Interface(self.bus.get_object('org.freedesktop.UDisks', devpath), 'org.freedesktop.UDisks.Device')
  35.  
  36.     
  37.     def mount(self, device_node_path):
  38.         d = self.device(device_node_path)
  39.         
  40.         try:
  41.             return unicode(d.FilesystemMount('', [
  42.                 'auth_no_user_interaction',
  43.                 'rw',
  44.                 'noexec',
  45.                 'nosuid',
  46.                 'sync',
  47.                 'nodev',
  48.                 'uid=1000',
  49.                 'gid=1000']))
  50.         except:
  51.             mp = node_mountpoint(str(device_node_path))
  52.             if mp is None:
  53.                 raise 
  54.             mp is None
  55.             return mp
  56.  
  57.  
  58.     
  59.     def unmount(self, device_node_path):
  60.         d = self.device(device_node_path)
  61.         d.FilesystemUnmount([
  62.             'force'])
  63.  
  64.     
  65.     def eject(self, device_node_path):
  66.         parent = device_node_path
  67.         while parent[-1] in '0123456789':
  68.             parent = parent[:-1]
  69.         d = self.device(parent)
  70.         d.DriveEject([])
  71.  
  72.  
  73.  
  74. def mount(node_path):
  75.     u = UDisks()
  76.     u.mount(node_path)
  77.  
  78.  
  79. def eject(node_path):
  80.     u = UDisks()
  81.     u.eject(node_path)
  82.  
  83.  
  84. def umount(node_path):
  85.     u = UDisks()
  86.     u.unmount(node_path)
  87.  
  88. if __name__ == '__main__':
  89.     import sys
  90.     dev = sys.argv[1]
  91.     print 'Testing with node', dev
  92.     u = UDisks()
  93.     print 'Mounted at:', u.mount(dev)
  94.     print 'Unmounting'
  95.     u.unmount(dev)
  96.     print 'Ejecting:'
  97.     u.eject(dev)
  98.  
  99.