General: simplify imports.

This commit is contained in:
Dylan Araps 2017-06-19 17:20:17 +10:00
parent f485d06b45
commit eac365eff5

40
wal.py
View File

@ -7,19 +7,13 @@ import re
import random import random
import glob import glob
import shutil import shutil
import subprocess import subprocess
from subprocess import call, Popen
import os import os
from os.path import expanduser
import pathlib import pathlib
from pathlib import Path
# wal files. # wal files.
CACHE_DIR = "%s%s" % (expanduser("~"), "/.cache/wal/") CACHE_DIR = "%s%s" % (os.path.expanduser("~"), "/.cache/wal/")
SCHEME_DIR = "%s%s" % (CACHE_DIR, "schemes/") SCHEME_DIR = "%s%s" % (CACHE_DIR, "schemes/")
SEQUENCE_FILE = "%s%s" % (CACHE_DIR, "sequences") SEQUENCE_FILE = "%s%s" % (CACHE_DIR, "sequences")
WAL_FILE = "%s%s" % (CACHE_DIR, "wal") WAL_FILE = "%s%s" % (CACHE_DIR, "wal")
@ -117,7 +111,7 @@ def reload_colors(vte):
def get_image(img): def get_image(img):
"""Validate image input.""" """Validate image input."""
image = Path(img) image = pathlib.Path(img)
# Check if the user has Imagemagick installed. # Check if the user has Imagemagick installed.
if not shutil.which("convert"): if not shutil.which("convert"):
@ -131,7 +125,7 @@ def get_image(img):
elif image.is_dir(): elif image.is_dir():
rand = random.choice(os.listdir(image)) rand = random.choice(os.listdir(image))
rand_img = "%s/%s" % (str(image), rand) rand_img = "%s/%s" % (str(image), rand)
rand_img = Path(rand_img) rand_img = pathlib.Path(rand_img)
if rand_img.is_file(): if rand_img.is_file():
wal_img = rand_img wal_img = rand_img
@ -142,9 +136,9 @@ def get_image(img):
def magic(color_count, img): def magic(color_count, img):
"""Call Imagemagick to generate a scheme.""" """Call Imagemagick to generate a scheme."""
colors = Popen(["convert", img, "+dither", "-colors", colors = subprocess.Popen(["convert", img, "+dither", "-colors",
str(color_count), "-unique-colors", "txt:-"], str(color_count), "-unique-colors", "txt:-"],
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
return colors.stdout.readlines() return colors.stdout.readlines()
@ -178,7 +172,7 @@ def get_colors(img):
"""Generate a colorscheme using imagemagick.""" """Generate a colorscheme using imagemagick."""
# Cache file. # Cache file.
cache_file = "%s%s" % (SCHEME_DIR, img.replace('/', '_')) cache_file = "%s%s" % (SCHEME_DIR, img.replace('/', '_'))
cache_file = Path(cache_file) cache_file = pathlib.Path(cache_file)
# Cache the wallpaper name. # Cache the wallpaper name.
with open(WAL_FILE, 'w') as file: with open(WAL_FILE, 'w') as file:
@ -299,27 +293,27 @@ def send_sequences(colors, vte):
def set_wallpaper(img): def set_wallpaper(img):
"""Set the wallpaper.""" """Set the wallpaper."""
if shutil.which("feh"): if shutil.which("feh"):
Popen(["feh", "--bg-fill", img]) subprocess.Popen(["feh", "--bg-fill", img])
elif shutil.which("nitrogen"): elif shutil.which("nitrogen"):
Popen(["nitrogen", "--set-zoom-fill", img]) subprocess.Popen(["nitrogen", "--set-zoom-fill", img])
elif shutil.which("bgs"): elif shutil.which("bgs"):
Popen(["bgs", img]) subprocess.Popen(["bgs", img])
elif shutil.which("hsetroot"): elif shutil.which("hsetroot"):
Popen(["hsetroot", "-fill", img]) subprocess.Popen(["hsetroot", "-fill", img])
elif shutil.which("habak"): elif shutil.which("habak"):
Popen(["habak", "-mS", img]) subprocess.Popen(["habak", "-mS", img])
elif OS == "Darwin": elif OS == "Darwin":
Popen(["osascript", "-e", "'tell application \"Finder\" to set \ subprocess.Popen(["osascript", "-e", "'tell application \"Finder\" to set \
desktop picture to POSIX file\'" + img + "\'"]) desktop picture to POSIX file\'" + img + "\'"])
else: else:
Popen(["gsettings", "set", "org.gnome.desktop.background", subprocess.Popen(["gsettings", "set", "org.gnome.desktop.background",
"picture-uri", img]) "picture-uri", img])
print("wallpaper: Set the new wallpaper") print("wallpaper: Set the new wallpaper")
return 0 return 0
@ -390,7 +384,7 @@ def export_xrdb(colors):
file.write(x_colors) file.write(x_colors)
# Merge the colors into the X db so new terminals use them. # Merge the colors into the X db so new terminals use them.
call(["xrdb", "-merge", XRDB_FILE]) subprocess.call(["xrdb", "-merge", XRDB_FILE])
print("export: Exported xrdb colors.") print("export: Exported xrdb colors.")