make use of fallback files in Tools.Directories when file or path is not in place
[vuplus_dvbapp] / lib / python / Tools / Directories.py
1 import os
2
3 SCOPE_TRANSPONDERDATA = 0
4 SCOPE_SYSETC = 1
5 SCOPE_FONTS = 2
6 SCOPE_SKIN = 3
7 SCOPE_SKIN_IMAGE = 4
8 SCOPE_USERETC = 5
9 SCOPE_CONFIG = 6
10 SCOPE_LANGUAGE = 7
11 SCOPE_HDD = 8
12
13 PATH_CREATE = 0
14 PATH_DONTCREATE = 1
15 PATH_FALLBACK = 2
16 defaultPaths = {
17                 SCOPE_TRANSPONDERDATA: ("/etc/", PATH_DONTCREATE),
18                 SCOPE_SYSETC: ("/etc/", PATH_DONTCREATE),
19                 SCOPE_FONTS: ("/usr/share/fonts/", PATH_DONTCREATE),
20                 SCOPE_CONFIG: ("/etc/enigma2/", PATH_CREATE),
21                                             
22                 SCOPE_LANGUAGE: ("/usr/share/enigma2/po/", PATH_CREATE),
23
24                 SCOPE_SKIN: ("/usr/share/enigma2/", PATH_DONTCREATE),
25                 SCOPE_SKIN_IMAGE: ("/usr/share/enigma2/", PATH_DONTCREATE),
26                 SCOPE_HDD: ("/hdd/movie/", PATH_DONTCREATE),
27                 
28                 SCOPE_USERETC: ("", PATH_DONTCREATE) # user home directory
29         }
30         
31 FILE_COPY = 0 # copy files from fallback dir to the basedir
32 FILE_MOVE = 1 # move files
33 PATH_COPY = 2 # copy the complete fallback dir to the basedir
34 PATH_MOVE = 3 # move the fallback dir to the basedir (can be used for changes in paths)
35 fallbackPaths = {
36                 SCOPE_CONFIG: [("/home/root/", FILE_MOVE),
37                                            ("/usr/share/enigma2/defaults/", FILE_COPY)],
38                 SCOPE_HDD: [("/hdd/movies", PATH_MOVE)]
39         }
40
41 def resolveFilename(scope, base = ""):
42         print "getting scope", scope, "with base", base
43         path = defaultPaths[scope]
44         print "path:", path
45         
46         if not fileExists(path[0] + base):
47                 #try:
48                 if fallbackPaths.has_key(scope):
49                         print 1
50                         for x in fallbackPaths[scope]:
51                                 print x
52                                 if x[1] == FILE_COPY:
53                                         if fileExists(x[0] + base):
54                                                 os.system("cp " + x[0] + base + " " + path[0] + base)
55                                 elif x[1] == FILE_MOVE:
56                                         if fileExists(x[0] + base):
57                                                 os.system("mv " + x[0] + base + " " + path[0] + base)
58                                 elif x[1] == PATH_COPY:
59                                         if pathExists(x[0]):
60                                                 if not pathExists(defaultPaths[scope][0]):
61                                                         os.mkdir(path[0])
62                                                 os.system("cp -a " + x[0] + "* " + path[0])
63                                 elif x[1] == PATH_MOVE:
64                                         if pathExists(x[0]):
65                                                 os.system("mv " + x[0] + " " + path[0])
66
67                 if path[1] == PATH_CREATE:
68                         if (not pathExists(defaultPaths[scope][0])):
69                                 os.mkdir(path[0])
70         
71         # FIXME: we also have to handle DATADIR etc. here.
72         return path[0] + base
73
74         # this is only the BASE - an extension must be added later.
75         
76 def pathExists(path):
77         return os.path.exists(path)
78
79 def fileExists(f):
80         try:
81                 file = open(f)
82         except IOError:
83                 exists = 0
84         else:
85                 exists = 1
86         return exists
87
88 def getRecordingFilename(basename):
89         
90                 # filter out non-allowed characters
91         non_allowed_characters = "/.\\"
92         
93         filename = ""
94         for c in basename:
95                 if c in non_allowed_characters:
96                         c = "_"
97                 filename += c
98         
99         i = 0
100         while True:
101                 path = resolveFilename(SCOPE_HDD, filename)
102                 if i > 0:
103                         path += "_%03d" % i
104                 try:
105                         open(path + ".ts")
106                         i += 1
107                 except IOError:
108                         return path
109
110 # this is clearly a hack:
111 def InitFallbackFiles():
112         resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.tv")
113         resolveFilename(SCOPE_CONFIG, "bouquets.tv")