[Goodies-dev] Proposed goodies contribution: Recent Folders v0.1.3

Serge Stroobandt serge at stroobandt.com
Sun Jul 15 12:12:04 CEST 2012


Hello,
Below is a slightly improved version of the proposed goodie plugin.
Recently used folders are now listed in alphabetical order, which works 
much better than chronological order.
I will contact the developers of http://pyxfce.xfce.org to see if I can 
get some assistance with proper plugin integration.

Furthermore, I would really appreciate to receive some feedback from 
this group.
If this contribution would be considered futile by this group, I would 
like to know why
so that I can move on and get it hosted elsewhere (freedesktop.org for 
example). But that would be a real pity.
All I can tell is that this plugin is helping me a lot during my day to 
day real work using the XFCE desktop!

#!/usr/bin/python3

# VERSION
# 0.1.3

# DESCRIPTION
# Opens a list of recently used folders on the desktop.
# The chosen folder is opened with the default file manager.

# USAGE
# $xfce4-recentfolders-plugin

# REQUIRES
# python3, python3-lxml, zenity

# COPYRIGHT
# Copyright (C) 2012  Serge YMR Stroobandt

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# CONTACT
# serge at stroobandt.com

# All imports are standard on most modern Python implementations
import os
from lxml import etree
import urllib.parse
import subprocess
import sys

# XDG Recent File Spec 0.2
recently = os.path.expanduser('~/.recently-used')
try:
     tree = etree.parse(recently)
except:
     # XFCE4 is not yet compliant with XDG Recent File Spec 0.2.
     # Hence, module xdg.RecentFiles was not employed.
     recently = os.path.expanduser('~/.local/share/recently-used.xbel')
     try:
         tree = etree.parse(recently)
     except:
         print()
         print('Recent Folders - Error: recently-used file was not found 
or is corrupt. Exiting.')
         print()
         sys.exit(1)

chosen = ''
while chosen == '':
     # All bookmarks retrieved by the parser
     bookmarks = tree.findall('bookmark')
     hrefs = []
     folders = []
     for bookmark in bookmarks:
         # Get href attribute and remove file:// and file name
         href = bookmark.attrib['href']
         href = href.replace('file://', '')
         href = href.rpartition('/')[0] + '/'
         # Remove %20 etc.
         folder = urllib.parse.unquote(href)
         if folder == '/tmp/' or folder.find('/.cache') >= 0:
             continue
         # Test if the folder still exists
         if os.path.exists(folder):
             try:
                 # Test if the folder is already in the folders list
                 folders.index(folder)
             except ValueError:
                 # If not, add the folder to the folders list
                 folders.append(folder)
     # Sort the folders alphabetically
     folders.sort()
     # Add a clear folder list option
     folders.append('CLEAR FOLDER LIST')

     try:
         # Choose a folder from the list
         chosen = subprocess.check_output('zenity --width=860 
--height=610 --title'.split() + ['Recent Folders'] + '--list 
--text'.split() + ['Open ...'] + '--column Folder'.split() + folders)
         # Convert to a string and slice off the newline
         chosen = str(chosen,  'utf-8')[:-1]
     except:
         # Main window was cancelled
         sys.exit()

     if  chosen == 'CLEAR FOLDER LIST':
         try:
             # Confirm deletion
             subprocess.check_output('zenity --question --title'.split() 
+ ['Recent Folders'] + '--text'.split() + ['Delete the recently-used 
list?'] + '--ok-label Delete --cancel-label Abort'.split())
             # Delete bookmarks
             for bookmark in bookmarks:
                 bookmark.getparent().remove(bookmark)
             tree.write(recently, pretty_print=True, 
xml_declaration=True,  encoding='UTF-8')
             chosen = ''
         except:
             # Deletion aborted
             chosen = ''

# Open the chosen folder
subprocess.Popen(['/usr/bin/xdg-open', chosen])



More information about the Goodies-dev mailing list