89e536bab79c0123721b1857d717df1cb11122cc
[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                 v = {}
25
26                 for x in data:
27                         i = x.find('=')
28                         var, val = x[:i], x[i+1:]
29                         v[var] = val
30
31                 print "hotplug:", v
32
33                 action = v.get("ACTION")
34                 device = v.get("DEVPATH")
35                 physdevpath = v.get("PHYSDEVPATH")
36
37                 dev = device.split('/')[-1]
38
39                 if action == "add":
40                         print "Medium found in", self.getUserfriendlyDeviceName(dev)
41                         harddiskmanager.addHotplugPartition(dev, self.getUserfriendlyDeviceName(physdevpath))
42                 elif action == "remove":
43                         harddiskmanager.removeHotplugPartition(dev)
44
45 def autostart(reason, **kwargs):
46         if reason == 0:
47                 print "starting hotplug handler"
48                 factory = Factory()
49                 factory.protocol = Hotplug
50
51                 try:
52                         import os
53                         os.remove("/tmp/hotplug.socket")
54                 except OSError:
55                         pass
56
57                 reactor.listenUNIX("/tmp/hotplug.socket", factory)
58
59 def Plugins(**kwargs):
60         return PluginDescriptor(name = "Hotplug", description = "listens to hotplug events", where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)