General: Fix quoting

This commit is contained in:
Dylan Araps 2017-06-20 16:00:00 +10:00
parent da6cc32699
commit fec7c01b25

78
wal
View File

@ -37,27 +37,27 @@ def get_args():
arg = argparse.ArgumentParser(description=description) arg = argparse.ArgumentParser(description=description)
# Add the args. # Add the args.
arg.add_argument('-c', action='store_true', arg.add_argument("-c", action="store_true",
help='Delete all cached colorschemes.') help="Delete all cached colorschemes.")
arg.add_argument('-i', metavar='"/path/to/img.jpg"', arg.add_argument("-i", metavar="\"/path/to/img.jpg\"",
help='Which image or directory to use.') help="Which image or directory to use.")
arg.add_argument('-n', action='store_true', arg.add_argument("-n", action="store_true",
help='Skip setting the wallpaper.') help="Skip setting the wallpaper.")
arg.add_argument('-o', metavar='"script_name"', arg.add_argument("-o", metavar="\"script_name\"",
help='External script to run after "wal".') help="External script to run after \"wal\".")
arg.add_argument('-q', action='store_true', arg.add_argument("-q", action="store_true",
help='Quiet mode, don\'t print anything.') help="Quiet mode, don\"t print anything.")
arg.add_argument('-r', action='store_true', arg.add_argument("-r", action="store_true",
help='Reload current colorscheme.') help="Reload current colorscheme.")
arg.add_argument('-t', action='store_true', arg.add_argument("-t", action="store_true",
help='Fix artifacts in VTE Terminals. \ help="Fix artifacts in VTE Terminals. \
(Termite, xfce4-terminal)') (Termite, xfce4-terminal)")
return arg.parse_args() return arg.parse_args()
@ -67,13 +67,13 @@ def process_args(args):
# If no args were passed. # If no args were passed.
if not len(sys.argv) > 1: if not len(sys.argv) > 1:
print("error: wal needs to be given arguments to run.") print("error: wal needs to be given arguments to run.")
print(" Refer to 'wal -h' for more info.") print(" Refer to \"wal -h\" for more info.")
exit(1) exit(1)
# -q # -q
if args.q: if args.q:
sys.stdout = open('/dev/null', 'w') sys.stdout = open("/dev/null", "w")
sys.stderr = open('/dev/null', 'w') sys.stderr = open("/dev/null", "w")
# -c # -c
if args.c: if args.c:
@ -118,7 +118,7 @@ def get_image(img):
# Pick a random image from the directory. # Pick a random image from the directory.
elif image.is_dir(): elif image.is_dir():
images = [] images = []
file_types = ('*.png', '*.jpg', '*.jpeg', '*.jpe', '*.gif') file_types = ("*.png", "*.jpg", "*.jpeg", "*.jpe", "*.gif")
for files in file_types: for files in file_types:
images.extend(glob.glob(str(image) + "/" + files)) images.extend(glob.glob(str(image) + "/" + files))
@ -161,22 +161,22 @@ def gen_colors(img):
"color palette, trying a larger palette size", "color palette, trying a larger palette size",
COLOR_COUNT + index) COLOR_COUNT + index)
# Remove the first element, which isn't a color. # Remove the first element, which isn"t a color.
del raw_colors[0] del raw_colors[0]
# Create a list of hex colors. # Create a list of hex colors.
colors = [re.search('#.{6}', str(col)).group(0) for col in raw_colors] colors = [re.search("#.{6}", str(col)).group(0) for col in raw_colors]
return colors return colors
def get_colors(img): def get_colors(img):
"""Generate a colorscheme using imagemagick.""" """Generate a colorscheme using imagemagick."""
# Cache file. # Cache file.
cache_file = CACHE_DIR / "schemes" / img.replace('/', '_') cache_file = CACHE_DIR / "schemes" / img.replace("/", "_")
cache_file = pathlib.Path(cache_file) cache_file = pathlib.Path(cache_file)
# Cache the wallpaper name. # Cache the wallpaper name.
with open(CACHE_DIR / "wal", 'w') as file: with open(CACHE_DIR / "wal", "w") as file:
file.write("%s\n" % (img)) file.write("%s\n" % (img))
if cache_file.is_file(): if cache_file.is_file():
@ -190,7 +190,7 @@ def get_colors(img):
colors = sort_colors(colors) colors = sort_colors(colors)
# Cache the colorscheme. # Cache the colorscheme.
with open(cache_file, 'w') as file: with open(cache_file, "w") as file:
file.write("\n".join(colors)) file.write("\n".join(colors))
print("colors: Generated colorscheme") print("colors: Generated colorscheme")
@ -273,7 +273,7 @@ def send_sequences(colors, vte):
set_special(13, colors[15]) set_special(13, colors[15])
set_special(14, colors[0]) set_special(14, colors[0])
# This escape sequence doesn't work in VTE terminals. # This escape sequence doesn"t work in VTE terminals.
if not vte: if not vte:
set_special(708, colors[0]) set_special(708, colors[0])
@ -281,11 +281,11 @@ def send_sequences(colors, vte):
for num, color in enumerate(colors): for num, color in enumerate(colors):
set_color(num, color) set_color(num, color)
# Set a blank color that isn't affected by bold highlighting. # Set a blank color that isn"t affected by bold highlighting.
set_color(66, colors[0]) set_color(66, colors[0])
# Decode the string. # Decode the string.
sequences = ''.join(ColorFormats.sequences) sequences = "".join(ColorFormats.sequences)
sequences = bytes(sequences, "utf-8").decode("unicode_escape") sequences = bytes(sequences, "utf-8").decode("unicode_escape")
# Send the sequences to all open terminals. # Send the sequences to all open terminals.
@ -293,7 +293,7 @@ def send_sequences(colors, vte):
terminals.append(CACHE_DIR / "sequences") terminals.append(CACHE_DIR / "sequences")
for term in terminals: for term in terminals:
with open(term, 'w') as file: with open(term, "w") as file:
file.write(sequences) file.write(sequences)
print("colors: Set terminal colors") print("colors: Set terminal colors")
@ -325,8 +325,8 @@ def set_wallpaper(img):
subprocess.Popen(["habak", "-mS", img]) subprocess.Popen(["habak", "-mS", img])
elif uname == "Darwin": elif uname == "Darwin":
subprocess.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:
subprocess.Popen(["gsettings", "set", "org.gnome.desktop.background", subprocess.Popen(["gsettings", "set", "org.gnome.desktop.background",
@ -346,14 +346,14 @@ def export_generic(colors, col_format):
"""Export colors to var format.""" """Export colors to var format."""
# Loop over the colors and format them. # Loop over the colors and format them.
colors = [col_format % (num, color) for num, color in enumerate(colors)] colors = [col_format % (num, color) for num, color in enumerate(colors)]
colors = ''.join(colors) colors = "".join(colors)
return colors return colors
def export_plain(colors): def export_plain(colors):
"""Export colors to a plain text file.""" """Export colors to a plain text file."""
with open(CACHE_DIR / "colors", 'w') as file: with open(CACHE_DIR / "colors", "w") as file:
file.write('\n'.join(colors)) file.write("\n".join(colors))
print("export: Exported plain colors") print("export: Exported plain colors")
@ -388,7 +388,7 @@ def export_emacs(colors):
def export_xrdb(colors, export_file): def export_xrdb(colors, export_file):
"""Export colors to xrdb.""" """Export colors to xrdb."""
colors = ''.join(colors) colors = "".join(colors)
save_file(colors, export_file) save_file(colors, export_file)
# Merge the colors into the X db so new terminals use them. # Merge the colors into the X db so new terminals use them.
@ -468,18 +468,18 @@ def reload_colors(vte):
# If vte mode was used, remove the problem sequence. # If vte mode was used, remove the problem sequence.
if vte: if vte:
sequences = re.sub(r'\]708;\#.{6}', '', sequences) sequences = re.sub(r"\]708;\#.{6}", "", sequences)
# Decode the string. # Decode the string.
sequences = bytes(sequences, "utf-8").decode("unicode_escape") sequences = bytes(sequences, "utf-8").decode("unicode_escape")
print(sequences, end='') print(sequences, end="")
quit() quit()
def save_file(colors, export_file): def save_file(colors, export_file):
"""Write the colors to the file.""" """Write the colors to the file."""
with open(export_file, 'w') as file: with open(export_file, "w") as file:
file.write(colors) file.write(colors)
@ -512,8 +512,8 @@ def main():
# -o # -o
if args.o: if args.o:
subprocess.Popen(["nohup", args.o], subprocess.Popen(["nohup", args.o],
stdout=open('/dev/null', 'w'), stdout=open("/dev/null", "w"),
stderr=open('/dev/null', 'w'), stderr=open("/dev/null", "w"),
preexec_fn=os.setpgrp) preexec_fn=os.setpgrp)
return 0 return 0