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