[MerlinEPGCenter] - implement workaround to fix zapping to channel when alternatives...
[enigma2-plugins.git] / webadmin / src / WebComponents / Sources / PkgConfList.py
1 # -*- coding: utf-8 -*-
2 from Components.Sources.Source import Source
3 from glob import glob
4 from os import statvfs
5 from os.path import basename, isfile, join
6 from shutil import move
7
8 class PkgConfList(Source):
9         LIST=0
10         SWITCH=1
11         MEM=2
12
13         sources_list_d = '/etc/apt/sources.list.d'
14
15         def __init__(self, session, func=LIST, wap=False):
16                 Source.__init__(self)
17                 self.func = func
18                 self.wap = wap
19                 self.session = session
20                 self.res = ( False, "Missing or Wrong Argument" )
21                         
22         def handleCommand(self, cmd):
23                 if cmd is not None:
24                         if self.func is self.SWITCH:
25                                 self.res = self.switch(cmd)
26                         if self.func is self.MEM:
27                                 self.res = self.getMem()
28                         elif self.func is self.LIST:
29                                 pass
30                         
31         def switch(self,cmd):
32                 if cmd:
33                         filename = cmd.get('file')
34                         if not filename or filename != basename(filename):
35                                 return (False, 'Invalid filename')
36
37                         filename = join(self.sources_list_d, filename)
38                         backup = filename + '.off'
39                         if isfile(filename):
40                                 move(filename, backup)
41                                 return (True, backup)
42                         elif isfile(backup):
43                                 move(backup, filename)
44                                 return (True, filename)
45
46         def getMem(self):
47                 try:
48                         stat = statvfs(self.sources_list_d)
49                 except OSError:
50                         return (False, "statvfs() failed")
51                 freespace = stat.f_bfree / 1000 * stat.f_bsize / 1000
52                 return (True, '%d' %freespace)
53                         
54         def getList(self):
55                 sources = []
56                 for filename in sorted(glob('%s/*.list*' % self.sources_list_d)):
57                         if filename.endswith(".list") or filename.endswith(".list.off"):
58                                 print "[PkgConfList] file ", filename
59                                 with open(filename) as f:
60                                         text = f.read()
61                                         print "[PkgConfList] text ", text
62                                         sources.append((basename(filename), text))
63                 return sources
64
65         def getResult(self):
66                 if self.func is not self.LIST:
67                         return self.res
68                 return ( False, "illegal call" )
69
70         result = property(getResult)
71         
72         list = property(getList)
73         lut = {"Name": 0
74                         , "Text": 1
75                 }