From 186d398d1abc18e87188359c8e353fd179e19066 Mon Sep 17 00:00:00 2001 From: aszlig Date: Tue, 17 Jan 2023 01:00:15 +0100 Subject: aacolorize: Convert to Python 3 Python 2 was sunset a long time ago and recently got marked as insecure, so evaluation fails. Since this program is very little in size, I decided to quickly switch to Python 3 in a low-effort way. This means that I didn't switch to using pathlib or using context managers for files, only the things necessary so that it still works. Signed-off-by: aszlig --- pkgs/aszlig/aacolorize/aacolorize.py | 43 ++++++++++++++++++++++++------------ pkgs/aszlig/aacolorize/default.nix | 4 ++-- 2 files changed, 31 insertions(+), 16 deletions(-) (limited to 'pkgs') diff --git a/pkgs/aszlig/aacolorize/aacolorize.py b/pkgs/aszlig/aacolorize/aacolorize.py index ff19b687..bb92f805 100755 --- a/pkgs/aszlig/aacolorize/aacolorize.py +++ b/pkgs/aszlig/aacolorize/aacolorize.py @@ -1,8 +1,8 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import os import sys -from optparse import Option, OptionParser +from argparse import ArgumentParser COLORS = { "k": (30, "Black"), @@ -17,9 +17,11 @@ COLORS = { ESC = chr(27) + class ColorizeError(Exception): pass + class Color(object): def __init__(self, ident=None): """ @@ -131,12 +133,14 @@ class Color(object): else: return "" % self.name.lower() + def print_colortable(): - for ident in COLORS.iterkeys(): + for ident in COLORS.keys(): normal = Color(ident).describe() bold = Color(ident.upper()).describe() sys.stdout.write("%-35s%s\n" % (normal, bold)) + def colorize_art(art, colmap): if len(art) != len(colmap): raise ColorizeError("Art and colormap differ in size!") @@ -154,6 +158,7 @@ def colorize_art(art, colmap): return out + def colorize_file(artfile, mapfile=None): if mapfile is None: mapfile = os.path.splitext(artfile)[0]+'.colmap' @@ -163,20 +168,30 @@ def colorize_file(artfile, mapfile=None): return colorize_art(asciiart, colormap) -if __name__ == "__main__": - parser = OptionParser(usage="%prog [options] artfile [mapfile]") - parser.add_option("-t", "--table", action="store_true", dest="table", - help="Show color table and exit.") - (options, args) = parser.parse_args() +if __name__ == "__main__": + parser = ArgumentParser() + + table_or_merge = parser.add_mutually_exclusive_group(required=True) + table_or_merge.add_argument( + '-t', '--table', action='store_true', dest='table', + help="Show color table and exit." + ) + table_or_merge.add_argument( + 'artfile', nargs='?', + help="ASCII art file to colorize" + ) + + parser.add_argument( + 'mapfile', nargs='?', + help="The file with the color information." + " (Default: artfile with .colmap suffix)" + ) + + options = parser.parse_args() if options.table: print_colortable() - parser.exit() - - if not len(args) in (1, 2): - parser.print_help() - parser.exit() else: - colorized = colorize_file(*args) + colorized = colorize_file(options.artfile, options.mapfile) sys.stdout.write(colorized) diff --git a/pkgs/aszlig/aacolorize/default.nix b/pkgs/aszlig/aacolorize/default.nix index ef36f4e0..fe027606 100644 --- a/pkgs/aszlig/aacolorize/default.nix +++ b/pkgs/aszlig/aacolorize/default.nix @@ -1,6 +1,6 @@ -{ pythonPackages, runCommand }: +{ python3Packages, runCommand }: -pythonPackages.buildPythonPackage { +python3Packages.buildPythonPackage { name = "aacolorize"; src = runCommand "aacolorize-src" {} '' mkdir -p "$out" -- cgit 1.4.1