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