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