<commit_before>"""
wal - Generate and change colorschemes on the fly.
Created by <NAME>
"""
import argparse
import re
import random
import glob
import shutil

import subprocess
from subprocess import call
from subprocess import Popen

import os
from os.path import expanduser

import pathlib
from pathlib import Path


# wal files.
CACHE_DIR = "%s%s" % (expanduser("~"), "/.cache/wal/")
SCHEME_DIR = "%s%s" % (CACHE_DIR, "schemes/")
SEQUENCE_FILE = "%s%s" % (CACHE_DIR, "sequences")
WAL_FILE = "%s%s" % (CACHE_DIR, "wal")
PLAIN_FILE = "%s%s" % (CACHE_DIR, "colors")
XRDB_FILE = "%s%s" % (CACHE_DIR, "xcolors")

# Internal variables.
COLOR_COUNT = 16
OS = os.uname


# ARGS {{{


def get_args():
    """Get the script arguments."""
    description = "wal - Generate colorschemes on the fly"
    arg = argparse.ArgumentParser(description=description)

    # Add the args.
    # arg.add_argument('-a', metavar='0-100', type=int,
    #                  help='Set terminal background transparency. \
    #                        *Only works in URxvt*')

    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.')

    arg.add_argument('-i', metavar='"/path/to/img.jpg"',
                     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 process_args(args):
    """Process the arguments."""

    # -c
    if args.c:
        shutil.rmtree(SCHEME_DIR)
        quit()

    # -r
    if args.r:
        reload_colors(args.t)


# }}}


# RELOAD COLORS {{{

def reload_colors(vte):
    """Reload colors."""
    with open(SEQUENCE_FILE) as file:
        sequences = file.read()

    # If vte mode was used, remove the problem sequence.
    if vte:
        sequences = re.sub(r'\]708;\#.{6}', '', sequences)

    # Decode the string.
    sequences = bytes(sequences, "utf-8").decode("unicode_escape")

    print(sequences, end='')
    quit()


# }}}


# COLORSCHEME GENERATION {{{


def get_image(img):
    """Validate image input."""
    image = Path(img)

    # Check if the user has Imagemagick installed.
    if not shutil.which("convert"):
        print("error: imagemagick not found, exiting...")
        print("error: wal requires imagemagick to function.")
        exit(1)

    if image.is_file():
        return image

    elif image.is_dir():
        rand = random.choice(os.listdir(image))
        rand_img = "%s/%s" % (str(image), rand)
        rand_img = Path(rand_img)

        if rand_img.is_file():
            return rand_img


def magic(color_count, img):
    """Call Imagemagick to generate a scheme."""
    colors = Popen(["convert", img, "+dither", "-colors",
                    str(color_count), "-unique-colors", "txt:-"],
                   stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)

    return colors.stdout


def gen_colors(img):
    """Generate a color palette using imagemagick."""
    colors = []

    # Generate initial scheme.
    magic_output = magic(COLOR_COUNT, img).readlines()

    # If imagemagick finds less than 16 colors, use a larger source number
    # of colors.
    index = 0
    while len(magic_output) - 1 <= 15:
        index += 1
        magic_output = magic(COLOR_COUNT + index, img).readlines()

        print("colors: Imagemagick couldn't generate a", COLOR_COUNT,
              "color palette, trying a larger palette size",
              COLOR_COUNT + index)

    # Create a list of hex colors.
    search = re.search
    append = colors.append
    for color in magic_output:
        hex_color = search('#.{6}', str(color))

        if hex_color:
            append(hex_color.group(0))

    # Remove the first element, which isn't a color.
    del colors[0]

    return colors


def get_colors(img):
    """Generate a colorscheme using imagemagick."""
    # Cache file.
    cache_file = "%s%s" % (SCHEME_DIR, img.replace('/', '_'))
    cache_file = Path(cache_file)

    # Cache the wallpaper name.
    with open(WAL_FILE, 'w') as file:
        file.write("%s\n" % (img))

    if cache_file.is_file():
        with open(cache_file) as file:<commit_msg>Optimization: Optimize for loop.<commit_after>"""
wal - Generate and change colorschemes on the fly.
Created by <NAME>
"""
import argparse
import re
import random
import glob
import shutil

import subprocess
from subprocess import call
from subprocess import Popen

import os
from os.path import expanduser

import pathlib
from pathlib import Path


# wal files.
CACHE_DIR = "%s%s" % (expanduser("~"), "/.cache/wal/")
SCHEME_DIR = "%s%s" % (CACHE_DIR, "schemes/")
SEQUENCE_FILE = "%s%s" % (CACHE_DIR, "sequences")
WAL_FILE = "%s%s" % (CACHE_DIR, "wal")
PLAIN_FILE = "%s%s" % (CACHE_DIR, "colors")
XRDB_FILE = "%s%s" % (CACHE_DIR, "xcolors")

# Internal variables.
COLOR_COUNT = 16
OS = os.uname


# ARGS {{{


def get_args():
    """Get the script arguments."""
    description = "wal - Generate colorschemes on the fly"
    arg = argparse.ArgumentParser(description=description)

    # Add the args.
    # arg.add_argument('-a', metavar='0-100', type=int,
    #                  help='Set terminal background transparency. \
    #                        *Only works in URxvt*')

    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.')

    arg.add_argument('-i', metavar='"/path/to/img.jpg"',
                     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 process_args(args):
    """Process the arguments."""

    # -c
    if args.c:
        shutil.rmtree(SCHEME_DIR)
        quit()

    # -r
    if args.r:
        reload_colors(args.t)


# }}}


# RELOAD COLORS {{{

def reload_colors(vte):
    """Reload colors."""
    with open(SEQUENCE_FILE) as file:
        sequences = file.read()

    # If vte mode was used, remove the problem sequence.
    if vte:
        sequences = re.sub(r'\]708;\#.{6}', '', sequences)

    # Decode the string.
    sequences = bytes(sequences, "utf-8").decode("unicode_escape")

    print(sequences, end='')
    quit()


# }}}


# COLORSCHEME GENERATION {{{


def get_image(img):
    """Validate image input."""
    image = Path(img)

    # Check if the user has Imagemagick installed.
    if not shutil.which("convert"):
        print("error: imagemagick not found, exiting...")
        print("error: wal requires imagemagick to function.")
        exit(1)

    if image.is_file():
        return image

    elif image.is_dir():
        rand = random.choice(os.listdir(image))
        rand_img = "%s/%s" % (str(image), rand)
        rand_img = Path(rand_img)

        if rand_img.is_file():
            return rand_img


def magic(color_count, img):
    """Call Imagemagick to generate a scheme."""
    colors = Popen(["convert", img, "+dither", "-colors",
                    str(color_count), "-unique-colors", "txt:-"],
                   stdout=subprocess.PIPE)

    return colors.stdout.readlines()


def gen_colors(img):
    """Generate a color palette using imagemagick."""
    # Generate initial scheme.
    magic_output = magic(COLOR_COUNT, img)

    # If imagemagick finds less than 16 colors, use a larger source number
    # of colors.
    index = 0
    while len(magic_output) - 1 <= 15:
        index += 1
        magic_output = magic(COLOR_COUNT + index, img)

        print("colors: Imagemagick couldn't generate a", COLOR_COUNT,
              "color palette, trying a larger palette size",
              COLOR_COUNT + index)

    # Create a list of hex colors.
    colors = [re.search('#.{6}', str(col)).group(0) for col in magic_output]

    # Remove the first element, which isn't a color.
    del colors[0]

    return colors


def get_colors(img):
    """Generate a colorscheme using imagemagick."""
    # Cache file.
    cache_file = "%s%s" % (SCHEME_DIR, img.replace('/', '_'))
    cache_file = Path(cache_file)

    # Cache the wallpaper name.
    with open(WAL_FILE, 'w') as file:
        file.write("%s\n" % (img))

    if cache_file.is_file():
        with open(cache_file) as file:
{'fraction_non_alphanumeric': 0.13906810035842293, 'fraction_numerical': 0.005120327700972862, 'mean_word_length': 2.470504619758351, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3758077', 'n_tokens_mistral': 3205, 'n_tokens_neox': 3029, 'n_words': 815}