Index: channels/file.py ================================================================== --- channels/file.py +++ channels/file.py @@ -5,11 +5,11 @@ # category: local # version: 0.1 # priority: optional # depends: mutagen # config: -# { name: file_browser_dir, type: text, value: "~/Music, /media/music", description: "List of directories to scan for audio files." }, +# { name: file_browser_dir, type: text, value: "$XDG_MUSIC_DIR, ~/MP3", description: "List of directories to scan for audio files." }, # { name: file_browser_ext, type: text, value: "mp3,ogg, m3u,pls,xspf, avi,flv,mpg,mp4", description: "File type/extension filter." }, # # Local file browser. Presents files from configured directories. @@ -87,11 +87,11 @@ # prepare def __init__(self, parent): # data dirs - self.dir = [s.strip() for s in conf.file_browser_dir.split(",")] + self.dir = [self.env_dir(s) for s in conf.file_browser_dir.split(",")] self.ext = [s.strip() for s in conf.file_browser_ext.split(",")] # first run if not self.categories or not self.streams: self.scan_dirs() @@ -102,28 +102,38 @@ #{editable:8} # add custom context menu #self.gtk_list.connect('button-press-event', self.context_menu) - - - # save list? - #save = lambda *x: None - # yeah, give it a try + + # Interpolate $VARS and XDG_SPECIAL_DIRS + def env_dir(self, path): + path = path.strip() + env = self.fvars() + # Replace $XDG_ ourselfes and normal $ENV vars per expandvars (because os.environ.update() doesn't do) + path = re.sub("\$(XDG\w+)", lambda m: env.get(m.group(1), m.group(0)), path) + path = os.path.expandvars(path) + return os.path.expanduser(path) + + # Read user-dirs config + def fvars(self, fn="$HOME/.config/user-dirs.dirs"): + fn = os.path.expandvars(fn) + src = open(fn, "r").read() if os.path.exists(fn) else "" + env = re.findall('^(\w+)=[\"\']?(.+?)[\"\']', src, re.M) + return dict(env) + # don't load cache file cache = lambda *x: None - # read dirs def scan_dirs(self): self.categories = [] # add main directory for main in self.dir: - main = os.path.expanduser(os.path.expandvars(main)) if os.path.exists(main): self.categories.append(main) # prepare subdirectories list sub = [] @@ -130,20 +140,24 @@ self.categories.append(sub) # look through for dir, subdirs, files in os.walk(main): name = os.path.basename(dir) - while name in self.categories: - name = name + "2" + sfx = "" + while name+sfx in self.categories: + sfx = str(int(sfx)+1) if sfx else "2" + name += sfx # files in subdir if files: sub.append(name) self.streams[name] = [self.file_entry(fn, dir) for fn in files if self.we_like_that_extension(fn)] # plant a maindir reference to shortname - self.streams[main] = self.streams[os.path.basename(main)] + main_base = os.path.basename(main) + if self.streams.get(main_base): + self.streams[main] = self.streams[main_base] # extract meta data def file_entry(self, fn, dir): # basic data @@ -171,10 +185,8 @@ # same as init def update_streams(self, cat, x=0): self.scan_dirs() - print(self.streams) - print(self.categories) return self.streams.get(os.path.basename(cat))