1 # -*- coding: utf-8 -*-
2 from os import path as os_path, mkdir, system, walk
5 SCOPE_TRANSPONDERDATA = 0
17 SCOPE_CURRENT_SKIN = 12
19 SCOPE_DEFAULTPARTITION = 14
25 SCOPE_TRANSPONDERDATA: ("/etc/", PATH_DONTCREATE),
26 SCOPE_SYSETC: ("/etc/", PATH_DONTCREATE),
27 SCOPE_FONTS: ("/usr/share/fonts/", PATH_DONTCREATE),
28 SCOPE_CONFIG: ("/etc/enigma2/", PATH_CREATE),
29 SCOPE_PLUGINS: ("/usr/lib/enigma2/python/Plugins/", PATH_CREATE),
31 SCOPE_LANGUAGE: ("/usr/share/enigma2/po/", PATH_DONTCREATE),
33 SCOPE_SKIN: ("/usr/share/enigma2/", PATH_DONTCREATE),
34 SCOPE_SKIN_IMAGE: ("/usr/share/enigma2/", PATH_DONTCREATE),
35 SCOPE_HDD: ("/hdd/movie/", PATH_DONTCREATE),
36 SCOPE_MEDIA: ("/media/", PATH_DONTCREATE),
37 SCOPE_PLAYLIST: ("/etc/enigma2/playlist/", PATH_CREATE),
39 SCOPE_USERETC: ("", PATH_DONTCREATE), # user home directory
41 SCOPE_DEFAULTDIR: ("/usr/share/enigma2/defaults/", PATH_CREATE),
42 SCOPE_DEFAULTPARTITION: ("/dev/mtdblock/4", PATH_DONTCREATE),
45 FILE_COPY = 0 # copy files from fallback dir to the basedir
46 FILE_MOVE = 1 # move files
47 PATH_COPY = 2 # copy the complete fallback dir to the basedir
48 PATH_MOVE = 3 # move the fallback dir to the basedir (can be used for changes in paths)
50 SCOPE_CONFIG: [("/home/root/", FILE_MOVE),
51 ("/usr/share/enigma2/defaults/", FILE_COPY)],
52 SCOPE_HDD: [("/hdd/movies", PATH_MOVE)]
55 def resolveFilename(scope, base = "", path_prefix = None):
57 # you can only use the ~/ if we have a prefix directory
58 assert path_prefix is not None
59 base = os_path.join(path_prefix, base[2:])
61 # don't resolve absolute paths
65 if scope == SCOPE_CURRENT_SKIN:
66 from Components.config import config
67 tmp = defaultPaths[SCOPE_SKIN]
68 pos = config.skin.primary_skin.value.rfind('/')
70 path = tmp[0]+config.skin.primary_skin.value[:pos+1]
74 tmp = defaultPaths[scope]
79 if flags == PATH_CREATE:
80 if not pathExists(path):
83 fallbackPath = fallbackPaths.get(scope)
85 if fallbackPath and not fileExists(path + base):
86 for x in fallbackPath:
88 if fileExists(x[0] + base):
89 system("cp " + x[0] + base + " " + path + base)
91 elif x[1] == FILE_MOVE:
92 if fileExists(x[0] + base):
93 system("mv " + x[0] + base + " " + path + base)
95 elif x[1] == PATH_COPY:
97 if not pathExists(defaultPaths[scope][0]):
99 system("cp -a " + x[0] + "* " + path)
101 elif x[1] == PATH_MOVE:
103 system("mv " + x[0] + " " + path)
106 # FIXME: we also have to handle DATADIR etc. here.
108 # this is only the BASE - an extension must be added later.
110 def pathExists(path):
111 return os_path.exists(path)
122 def fileExists(f, mode='r'):
131 def getRecordingFilename(basename, dirname = None):
132 # filter out non-allowed characters
133 non_allowed_characters = "/.\\:*?<>|\""
136 basename = basename.replace('\xc2\x86', '').replace('\xc2\x87', '')
139 if c in non_allowed_characters:
143 if dirname is not None:
144 filename = ''.join([dirname, filename])
148 path = resolveFilename(SCOPE_HDD, filename)
157 # this is clearly a hack:
158 def InitFallbackFiles():
159 resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.tv")
160 resolveFilename(SCOPE_CONFIG, "bouquets.tv")
161 resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.radio")
162 resolveFilename(SCOPE_CONFIG, "bouquets.radio")
164 # returns a list of tuples containing pathname and filename matching the given pattern
165 # example-pattern: match all txt-files: ".*\.txt$"
166 def crawlDirectory(directory, pattern):
167 expression = compile(pattern)
169 for root, dirs, files in walk(directory):
171 if expression.match(file) is not None:
172 list.append((root, file))