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