daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3
  2. """
  3. Recursively adds all new images in a given path to ~/.config/mate/backgrounds.xml
  4. """
  5.  
  6. import os, sys
  7. from pathlib import Path
  8. import mimetypes
  9.  
  10. if not len(sys.argv) == 2 or not os.path.exists(sys.argv[1]):
  11.     print("Argument must be a valid path")
  12.     sys.exit(1)
  13.  
  14. template = """\
  15.   <wallpaper deleted="false">
  16.     <name>%s</name>
  17.     <filename>%s</filename>
  18.     <options>zoom</options>
  19.     <shade_type>solid</shade_type>
  20.     <pcolor>rgb(0,0,0)</pcolor>
  21.     <scolor>rgb(0,0,0)</scolor>
  22.     <artist>(unknown)</artist>
  23.   </wallpaper>
  24. """
  25. backgrounds_file = os.path.join(Path.home(), ".config/mate/backgrounds.xml")
  26. if os.path.exists(backgrounds_file):
  27.     xml = open(backgrounds_file).readlines()
  28. else:
  29.     xml = ['<?xml version="1.0"?>\n','<!DOCTYPE wallpapers SYSTEM "mate-wp-list.dtd">\n','<wallpapers>\n','</wallpapers>\n']
  30. files = list(Path(sys.argv[1]).rglob("**/*"))
  31. knownfiles = [line.partition("<filename>")[2].partition("</filename>")[0] for line in xml if "<filename>" in line]
  32. output = ""
  33. count = 0
  34. for filename in files:
  35.     if not str(filename) in knownfiles:
  36.         filetype = mimetypes.guess_type(os.path.abspath(filename))[0]
  37.         if filetype and "image" in str(filetype):
  38.             output += template % (os.path.basename(filename), filename)
  39.             count += 1
  40. if count:
  41.     for i, line in enumerate(xml):
  42.         if line.strip() == "</wallpapers>":
  43.             xml.insert(i, output)
  44.             break
  45.     with open(backgrounds_file, "w") as f:
  46.         f.write("".join(xml))
  47.     print("%d new background images added successfully" % count)
  48. else:
  49.     print("No new background images found")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top