PEP8: Fix whitespace
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / Converter / ClientsStreaming.py
1 from Converter import Converter
2 from Poll import Poll
3 from Components.Element import cached
4 from enigma import eStreamServer
5 from ServiceReference import ServiceReference
6 import socket
7
8 class ClientsStreaming(Converter, Poll, object):
9         UNKNOWN = -1
10         REF = 0
11         IP = 1
12         NAME = 2
13         ENCODER = 3
14         NUMBER = 4
15         SHORT_ALL = 5
16         ALL = 6
17         INFO = 7
18         INFO_RESOLVE = 8
19         INFO_RESOLVE_SHORT = 9
20
21         def __init__(self, type):
22                 Converter.__init__(self, type)
23                 Poll.__init__(self)
24                 self.poll_interval = 30000
25                 self.poll_enabled = True
26                 if type == "REF":
27                         self.type = self.REF
28                 elif type == "IP":
29                         self.type = self.IP
30                 elif type == "NAME":
31                         self.type = self.NAME
32                 elif type == "ENCODER":
33                         self.type = self.ENCODER
34                 elif type == "NUMBER":
35                         self.type = self.NUMBER
36                 elif type == "SHORT_ALL":
37                         self.type = self.SHORT_ALL
38                 elif type == "ALL":
39                         self.type = self.ALL
40                 elif type == "INFO":
41                         self.type = self.INFO
42                 elif type == "INFO_RESOLVE":
43                         self.type = self.INFO_RESOLVE
44                 elif type == "INFO_RESOLVE_SHORT":
45                         self.type = self.INFO_RESOLVE_SHORT
46                 else:
47                         self.type = self.UNKNOWN
48
49                 self.streamServer = eStreamServer.getInstance()
50
51         @cached
52         def getText(self):
53                 if self.streamServer is None:
54                         return ""
55
56                 clients = []
57                 refs = []
58                 ips = []
59                 names = []
60                 encoders = []
61                 info = ""
62
63                 for x in self.streamServer.getConnectedClients():
64                         refs.append((x[1]))
65                         servicename = ServiceReference(x[1]).getServiceName() or "(unknown service)"
66                         service_name = servicename
67                         names.append((service_name))
68                         ip = x[0]
69
70                         ips.append((ip))
71
72                         if int(x[2]) == 0:
73                                 strtype = "S"
74                                 encoder = _('NO')
75                         else:
76                                 strtype = "T"
77                                 encoder = _('YES')
78
79                         encoders.append((encoder))
80
81                         if self.type == self.INFO_RESOLVE or self.type == self.INFO_RESOLVE_SHORT:
82                                 try:
83                                         raw = socket.gethostbyaddr(ip)
84                                         ip  = raw[0]
85                                 except:
86                                         pass
87
88                                 if self.type == self.INFO_RESOLVE_SHORT:
89                                         ip, sep, tail = ip.partition('.')
90
91                         info += ("%s %-8s %s\n") % (strtype, ip, service_name)
92
93                         clients.append((ip, service_name, encoder))
94
95                 if self.type == self.REF:
96                         return ' '.join(refs)
97                 elif self.type == self.IP:
98                         return ' '.join(ips)
99                 elif self.type == self.NAME:
100                         return ' '.join(names)
101                 elif self.type == self.ENCODER:
102                         return _("Transcoding: ") + ' '.join(encoders)
103                 elif self.type == self.NUMBER:
104                         return str(len(clients))
105                 elif self.type == self.SHORT_ALL:
106                         return _("Total clients streaming: %d (%s)") % (len(clients), ' '.join(names))
107                 elif self.type == self.ALL:
108                         return '\n'.join(' '.join(elems) for elems in clients)
109                 elif self.type == self.INFO or self.type == self.INFO_RESOLVE or self.type == self.INFO_RESOLVE_SHORT:
110                         return info
111                 else:
112                         return "(unknown)"
113
114                 return ""
115
116         text = property(getText)
117
118         @cached
119         def getBoolean(self):
120                 if self.streamServer is None:
121                         return False
122                 return self.streamServer.getConnectedClients() and True or False
123
124         boolean = property(getBoolean)
125
126         def changed(self, what):
127                 Converter.changed(self, (self.CHANGED_POLL,))
128
129         def doSuspend(self, suspended):
130                 pass