SHARE
TWEET

Untitled

a guest Dec 10th, 2018 99 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. files = list(Path(sys.argv[1]).rglob("**/*"))
  27. xml = open(backgrounds_file).readlines()
  28. knownfiles = [line.partition("<filename>")[2].partition("</filename>")[0] for line in xml if "<filename>" in line]
  29. output = ""
  30. count = 0
  31. for filename in files:
  32.     if not str(filename) in knownfiles:
  33.         filetype = mimetypes.guess_type(os.path.abspath(filename))[0]
  34.         if filetype and "image" in str(filetype):
  35.             output += template % (os.path.basename(filename), filename)
  36.             count += 1
  37. if count:
  38.     xml.insert(-1, output)
  39.     with open(backgrounds_file, "w") as f:
  40.         f.write("".join(xml))
  41.     print("%d new background images added successfully" % count)
  42. else:
  43.     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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top