Advertisement
Dobbie03

places.py

Jan 23rd, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Dynamic places menu generator for openbox
  3. # Uses a list of parent directories to provide a menu of subdirectories
  4. # After removing any entries found in an ignore list
  5. #
  6. #
  7. # Originally created by: Kev at http://crunchbanglinux.org/forums/topic/373/dynamic-places-pipe-menu/
  8. #
  9. #
  10. # 1. Save this script to a place you prefer (mine is in ~/.config/openbox/scripts/)
  11. # 2. Make it executable
  12. # 3. Insert a line into your ~/.config/openbox/menu.xml like:
  13. # <menu execute="perl ~/.config/openbox/scripts/places.py menu" id="pipe-places" label="Places"/>
  14. # 4. Call the pipe-menu in your menu by adding the following line to the same file:
  15. # <menu id="pipe-places"/>
  16. # 5. Reconfigure OpenBox
  17. # 6. You're done
  18. #
  19. # Important variables if you want to customise the generated menu:
  20. # manager - File manager which you want to use (nautilus, thunar, pcmanfm, rox-filer, konqueror, dolphin, etc).
  21. # dirs - The script lists and displays the subdirectories of these directories
  22. # ignore - Any item exactly matching an entry in this list will not be displayed
  23. # items - If you want to add a single directory entry to the menu you can do it here
  24. #
  25.  
  26. import glob
  27. import fnmatch
  28. from os.path import expanduser
  29.  
  30. # File manager you want to use to open directories
  31. manager = "thunar"
  32.  
  33. # User home directory. If you hard code this location instead of relying on
  34. # expanduser() then you can remove its import above
  35. home = expanduser('~')
  36.  
  37. # List of directories whose subdirectories we want to display
  38. dirs = [home, '/mnt/server/']
  39.  
  40. # List of directories to ignore
  41. # Shell-style wildcards as used by fnmatch are supported
  42. ignore = ['/media/cdrom0']
  43.  
  44. # Our list of menu items
  45. # If you want to add single directories to the menu without including
  46. # all of their subdirs then put them in here
  47. items = []
  48.  
  49. # Iterate through dirs
  50. for dir in dirs:
  51. # Get a list of subdirectories for each dir
  52. subdirs = glob.glob(dir + '/*/')
  53. # Alphabetise subdirs. Sorting here is less efficient but preserves
  54. # directory order specified above. If this doesn't matter to you then
  55. # remove this line and uncomment "items.sort(key=str.lower)" below
  56. subdirs.sort(key=str.lower)
  57. # Append each subdir to the items list
  58. for sub in subdirs:
  59. # Replace /home/user with ~ before appending to items (looks better)
  60. sub = sub.replace(home, '~')
  61. # Strip trailing / characters
  62. sub = sub.rstrip('/')
  63. items.append(sub)
  64.  
  65. # Iterate through ignore list and remove matches from items
  66. for i in ignore:
  67. matches = fnmatch.filter(items, i)
  68. for m in matches:
  69. try:
  70. items.remove(m)
  71. except ValueError:
  72. pass
  73.  
  74. # Alphabetise directory list. Read comment for sort() above before uncommenting
  75. #items.sort(key=str.lower)
  76.  
  77. # Output xml for the openbox menu
  78. print '<openbox_pipe_menu>'
  79. # Each item becomes a menu entry
  80. for i in items:
  81. print '<item label="' + i + '">'
  82. print '<action name="Execute">'
  83. print '<execute>'
  84. print manager + " " + i
  85. print '</execute>'
  86. print '</action>'
  87. print '</item>'
  88. print '</openbox_pipe_menu>'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement