aboutsummaryrefslogtreecommitdiff
path: root/wal.py
diff options
context:
space:
mode:
authorDylan Araps <[email protected]>2017-06-17 11:14:32 +1000
committerDylan Araps <[email protected]>2017-06-17 11:14:32 +1000
commit429e3c9f10d4c4b10802bb16030a955928172e3f (patch)
treef84088ed37308716a21134cb1997082e075c8e95 /wal.py
parentGeneral: Generate scheme (diff)
downloadpywal-429e3c9f10d4c4b10802bb16030a955928172e3f.tar.xz
pywal-429e3c9f10d4c4b10802bb16030a955928172e3f.zip
General: Fix all syntax errors
Diffstat (limited to 'wal.py')
-rw-r--r--wal.py141
1 files changed, 90 insertions, 51 deletions
diff --git a/wal.py b/wal.py
index d2dc75f..8dc744f 100644
--- a/wal.py
+++ b/wal.py
@@ -1,85 +1,124 @@
-#!/usr/bin/env python
-#
-# wal - Generate and change colorschemes on the fly.
-#
-# Created by Dylan Araps
-
+"""
+wal - Generate and change colorschemes on the fly.
+Created by Dylan Araps
+"""
import argparse
+import re
+import subprocess
+import random
+
import os
+from os.path import expanduser
+
import pathlib
-import subprocess
-import re
from pathlib import Path
-from os.path import expanduser
+
# Internal variables.
-cache_dir = expanduser("~") + "/.cache/wal"
-color_count = 16
-os = os.uname
+CACHE_DIR = expanduser("~") + "/.cache/wal"
+COLOR_COUNT = 16
+OS = os.uname
def get_args():
- parser = argparse.ArgumentParser(description='wal - Generate colorschemes on the fly')
+ """Get the script arguments."""
+ description = "wal - Generate colorschemes on the fly"
+ arg = argparse.ArgumentParser(description=description)
- parser.add_argument('-a', help='Set terminal background transparency. *Only works in URxvt*', metavar='0-100', type=int)
- parser.add_argument('-c', help='Delete all cached colorschemes.', action='store_true')
- parser.add_argument('-f', help='Load colors directly from a colorscheme file.', metavar='"/path/to/colors"')
- parser.add_argument('-i', help='Which image or directory to use.', metavar='"/path/to/img.jpg"')
- parser.add_argument('-n', help='Skip setting the wallpaper.', action='store_true')
- parser.add_argument('-o', help='External script to run after "wal".', metavar='script_name')
- parser.add_argument('-q', help='Quiet mode, don\'t print anything.', action='store_true')
- parser.add_argument('-r', help='Reload current colorscheme.', action='store_true')
- parser.add_argument('-t', help='Fix artifacts in VTE Terminals. (Termite, xfce4-terminal)', action='store_true')
- parser.add_argument('-x', help='Use extended 16-color palette.', action='store_true')
+ # Add the args.
+ arg.add_argument('-a', metavar='0-100', type=int,
+ help='Set terminal background transparency. \
+ *Only works in URxvt*')
- return parser.parse_args()
+ arg.add_argument('-c', action='store_true',
+ help='Delete all cached colorschemes.')
+ arg.add_argument('-f', metavar='"/path/to/colors"',
+ help='Load colors directly from a colorscheme file.')
-def get_colors(img):
+ arg.add_argument('-i', metavar='"/path/to/img.jpg"', required=True,
+ help='Which image or directory to use.')
+
+ arg.add_argument('-n', action='store_true',
+ help='Skip setting the wallpaper.')
+
+ arg.add_argument('-o', metavar='script_name',
+ help='External script to run after "wal".')
+
+ arg.add_argument('-q', action='store_true',
+ help='Quiet mode, don\'t print anything.')
+
+ arg.add_argument('-r', action='store_true',
+ help='Reload current colorscheme.')
+
+ arg.add_argument('-t', action='store_true',
+ help='Fix artifacts in VTE Terminals. \
+ (Termite, xfce4-terminal)')
+
+ arg.add_argument('-x', action='store_true',
+ help='Use extended 16-color palette.')
+
+ return arg.parse_args()
+
+
+def get_image(img):
+ """Validate image input."""
image = Path(img)
if image.is_file():
- colors = []
+ return image
- # Create colorscheme dir.
- pathlib.Path(cache_dir + "/schemes").mkdir(parents=True, exist_ok=True)
- cache_file = cache_dir + "/schemes/" + img.replace('/', '_')
+ elif image.is_dir():
+ rand = random.choice(os.listdir(image))
+ rand_img = Path(str(image) + "/" + rand)
- # Cache the wallpaper name.
- wal = open(cache_dir + "/wal", 'w')
- wal.write(img)
- wal.close()
+ if rand_img.is_file():
+ return rand_img
- # Long-ass imagemagick command.
- magic = subprocess.Popen(["convert", img, "+dither", "-colors",
- str(color_count), "-unique-colors", "txt:-"],
- stdout=subprocess.PIPE,stderr=subprocess.PIPE)
+def get_colors(img):
+ """Generate a colorscheme using imagemagick."""
+ colors = []
+
+ # Create colorscheme dir.
+ pathlib.Path(CACHE_DIR + "/schemes").mkdir(parents=True, exist_ok=True)
- # Create a list of hex colors.
- for color in magic.stdout:
- print(color)
- hex = re.search('#.{6}', str(color))
+ # Cache file.
+ cache_file = CACHE_DIR + "/schemes/" + img.replace('/', '_')
- if hex:
- colors.append(hex.group(0))
+ # Cache the wallpaper name.
+ wal = open(CACHE_DIR + "/wal", 'w')
+ wal.write(img + "\n")
+ wal.close()
+ # Long-ass imagemagick command.
+ magic = subprocess.Popen(["convert", img, "+dither", "-colors",
+ str(COLOR_COUNT), "-unique-colors", "txt:-"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
- # Remove the first element which isn't a color.
- del colors[0]
+ # Create a list of hex colors.
+ for color in magic.stdout:
+ hex_color = re.search('#.{6}', str(color))
- # Cache the colorscheme.
- scheme = open(cache_file, 'w')
+ if hex_color:
+ colors.append(hex_color.group(0))
- for color in colors:
- scheme.write(color + "\n")
+ # Remove the first element, which isn't a color.
+ del colors[0]
- scheme.close()
+ # Cache the colorscheme.
+ scheme = open(cache_file, 'w')
+ for color in colors:
+ scheme.write(color + "\n")
+ scheme.close()
def main():
+ """Main script function."""
args = get_args()
- get_colors(args.i)
+ image = str(get_image(args.i))
+ get_colors(image)
return 0