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