move http progress downloader class into own tool py
[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 hotplugNotifier = [ ]
7
8 class Hotplug(Protocol):
9         def connectionMade(self):
10                 self.received = ""
11
12         def dataReceived(self, data):
13                 self.received += data
14
15         def connectionLost(self, reason):
16                 data = self.received.split('\0')[:-1]
17
18                 v = {}
19
20                 for x in data:
21                         i = x.find('=')
22                         var, val = x[:i], x[i+1:]
23                         v[var] = val
24
25                 print "hotplug:", v
26
27                 action = v.get("ACTION")
28                 device = v.get("DEVPATH")
29                 physdevpath = v.get("PHYSDEVPATH")
30                 media_state = v.get("X_E2_MEDIA_STATUS")
31
32                 dev = device.split('/')[-1]
33
34                 if action is not None and action == "add":
35                         harddiskmanager.addHotplugPartition(dev, physdevpath)
36                 elif action is not None and action == "remove":
37                         harddiskmanager.removeHotplugPartition(dev)
38                 elif media_state is not None:
39                         if media_state == '1':
40                                 harddiskmanager.removeHotplugPartition(dev)
41                                 harddiskmanager.addHotplugPartition(dev, physdevpath)
42                         elif media_state == '0':
43                                 harddiskmanager.removeHotplugPartition(dev)
44                 
45                 for callback in hotplugNotifier:
46                         try:
47                                 callback(dev, action or media_state)
48                         except AttributeError:
49                                 hotplugNotifier.remove(callback)
50
51 def autostart(reason, **kwargs):
52         if reason == 0:
53                 print "starting hotplug handler"
54                 factory = Factory()
55                 factory.protocol = Hotplug
56
57                 try:
58                         import os
59                         os.remove("/tmp/hotplug.socket")
60                 except OSError:
61                         pass
62
63                 reactor.listenUNIX("/tmp/hotplug.socket", factory)
64
65 def Plugins(**kwargs):
66         return PluginDescriptor(name = "Hotplug", description = "listens to hotplug events", where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)