882668f74c6dcd9e84925f07abba4d6665763836
[enigma2.git] / lib / python / Plugins / SystemPlugins / Hotplug / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from twisted.internet.protocol import Protocol, Factory
3 from twisted.internet import reactor
4 from Components.Harddisk import harddiskmanager
5
6 DEVICEDB =  \
7         { "/devices/pci0000:00/0000:00:14.2/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0": "CF Slot",
8           "/devices/pci0000:00/0000:00:14.2/usb1/1-1/1-1:1.0/host0/target1:0:0/0:0:0:0": "SD Slot"
9         }
10
11 class Hotplug(Protocol):
12         def getUserfriendlyDeviceName(self, phys):
13                 return DEVICEDB.get(phys, "USB Storage")
14
15         def connectionMade(self):
16                 self.received = ""
17
18         def dataReceived(self, data):
19                 self.received += data
20
21         def connectionLost(self, reason):
22                 data = self.received.split('\0')[:-1]
23
24                 print "hotplug:", data
25
26                 if len(data) < 4:
27                         return
28
29                 (action, device, physdev, driver) = data[:4]
30
31                 dev = device.split('/')[-1]
32
33                 if action == "add":
34                         print "Medium found in", self.getUserfriendlyDeviceName(dev)
35                         harddiskmanager.addHotplugPartition(dev, self.getUserfriendlyDeviceName(dev))
36                 elif action == "remove":
37                         harddiskmanager.removeHotplugPartition(dev)
38
39 def autostart(reason, **kwargs):
40         if reason == 0:
41                 print "starting hotplug handler"
42                 factory = Factory()
43                 factory.protocol = Hotplug
44
45                 try:
46                         import os
47                         os.remove("/tmp/hotplug.socket")
48                 except OSError:
49                         pass
50
51                 reactor.listenUNIX("/tmp/hotplug.socket", factory)
52
53 def Plugins(**kwargs):
54         return PluginDescriptor(name = "Hotplug", description = "listens to hotplug events", where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)