support path_prefix
[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 = "", path_prefix = None):
46         if base[0:2] == "~/":
47                 # you can only use the ~/ if we have a prefix directory
48                 assert path_prefix is not None
49                 base = path_prefix + base[2:]
50
51         # don't resolve absolute paths
52         if base[0:1] == '/':
53                 return base
54
55         path = defaultPaths[scope]
56
57         if path[1] == PATH_CREATE:
58                 if (not pathExists(defaultPaths[scope][0])):
59                         os.mkdir(path[0])
60                         
61         #if len(base) > 0 and base[0] == '/':
62                 #path = ("", None)
63         
64         if not fileExists(path[0] + base):
65                 #try:
66                 if fallbackPaths.has_key(scope):
67                         for x in fallbackPaths[scope]:
68                                 if x[1] == FILE_COPY:
69                                         if fileExists(x[0] + base):
70                                                 os.system("cp " + x[0] + base + " " + path[0] + base)
71                                                 break
72                                 elif x[1] == FILE_MOVE:
73                                         if fileExists(x[0] + base):
74                                                 os.system("mv " + x[0] + base + " " + path[0] + base)
75                                                 break
76                                 elif x[1] == PATH_COPY:
77                                         if pathExists(x[0]):
78                                                 if not pathExists(defaultPaths[scope][0]):
79                                                         os.mkdir(path[0])
80                                                 os.system("cp -a " + x[0] + "* " + path[0])
81                                                 break
82                                 elif x[1] == PATH_MOVE:
83                                         if pathExists(x[0]):
84                                                 os.system("mv " + x[0] + " " + path[0])
85                                                 break
86
87         
88         # FIXME: we also have to handle DATADIR etc. here.
89         return path[0] + base
90
91         # this is only the BASE - an extension must be added later.
92         
93 def pathExists(path):
94         return os.path.exists(path)
95
96 def fileExists(f):
97         try:
98                 file = open(f)
99         except IOError:
100                 exists = 0
101         else:
102                 exists = 1
103         return exists
104
105 def getRecordingFilename(basename):
106                 # filter out non-allowed characters
107         non_allowed_characters = "/.\\:*?<>|\""
108         filename = ""
109         
110         basename = basename.replace('\xc2\x86', '').replace('\xc2\x87', '')
111         
112         for c in basename:
113                 if c in non_allowed_characters:
114                         c = "_"
115                 filename += c
116         
117         i = 0
118         while True:
119                 path = resolveFilename(SCOPE_HDD, filename)
120                 if i > 0:
121                         path += "_%03d" % i
122                 try:
123                         open(path + ".ts")
124                         i += 1
125                 except IOError:
126                         return path
127
128 # this is clearly a hack:
129 def InitFallbackFiles():
130         resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.tv")
131         resolveFilename(SCOPE_CONFIG, "bouquets.tv")