dont show pip options in extension menu when no pip is available
[enigma2.git] / lib / python / Tools / Directories.py
1 # -*- coding: utf-8 -*-
2 from os import path as os_path, mkdir, system
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                         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                                                 system("cp " + x[0] + base + " " + path[0] + base)
74                                                 break
75                                 elif x[1] == FILE_MOVE:
76                                         if fileExists(x[0] + base):
77                                                 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                                                         mkdir(path[0])
83                                                 system("cp -a " + x[0] + "* " + path[0])
84                                                 break
85                                 elif x[1] == PATH_MOVE:
86                                         if pathExists(x[0]):
87                                                 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                 mkdir(path)
102         except:
103                 ret = 0
104         else:
105                 ret = 1
106         return ret
107
108 def fileExists(f, mode='r'):
109         try:
110                 file = open(f, mode)
111         except IOError:
112                 exists = 0
113         else:
114                 exists = 1
115         return exists
116
117 def getRecordingFilename(basename, dirname = None):
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         if dirname is not None:
130                 filename = ''.join([dirname, filename])
131
132         i = 0
133         while True:
134                 path = resolveFilename(SCOPE_HDD, filename)
135                 if i > 0:
136                         path += "_%03d" % i
137                 try:
138                         open(path + ".ts")
139                         i += 1
140                 except IOError:
141                         return path
142
143 # this is clearly a hack:
144 def InitFallbackFiles():
145         resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.tv")
146         resolveFilename(SCOPE_CONFIG, "bouquets.tv")
147         resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.radio")
148         resolveFilename(SCOPE_CONFIG, "bouquets.radio")