Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- """
- Recursively adds all new images in a given path to ~/.config/mate/backgrounds.xml
- """
- import os, sys
- from pathlib import Path
- import mimetypes
- if not len(sys.argv) == 2 or not os.path.exists(sys.argv[1]):
- print("Argument must be a valid path")
- sys.exit(1)
- template = """\
- <wallpaper deleted="false">
- <name>%s</name>
- <filename>%s</filename>
- <options>zoom</options>
- <shade_type>solid</shade_type>
- <pcolor>rgb(0,0,0)</pcolor>
- <scolor>rgb(0,0,0)</scolor>
- <artist>(unknown)</artist>
- </wallpaper>
- """
- backgrounds_file = os.path.join(Path.home(), ".config/mate/backgrounds.xml")
- files = list(Path(sys.argv[1]).rglob("**/*"))
- xml = open(backgrounds_file).readlines()
- knownfiles = [line.partition("<filename>")[2].partition("</filename>")[0] for line in xml if "<filename>" in line]
- output = ""
- count = 0
- for filename in files:
- if not str(filename) in knownfiles:
- filetype = mimetypes.guess_type(os.path.abspath(filename))[0]
- if filetype and "image" in str(filetype):
- output += template % (os.path.basename(filename), filename)
- count += 1
- if count:
- xml.insert(-1, output)
- with open(backgrounds_file, "w") as f:
- f.write("".join(xml))
- print("%d new background images added successfully" % count)
- else:
- print("No new background images found")
Add Comment
Please, Sign In to add comment