about summary refs log tree commit diff
diff options
context:
space:
mode:
authoraszlig <aszlig@nix.build>2023-01-17 01:00:15 +0100
committeraszlig <aszlig@nix.build>2023-01-17 01:03:38 +0100
commit186d398d1abc18e87188359c8e353fd179e19066 (patch)
tree412c2add4986c0d0ec95148022fe2836f9cca745
parent27ff9af4161dde57dcab594f1f513c40ba31d9e3 (diff)
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 <aszlig@nix.build>
-rwxr-xr-xpkgs/aszlig/aacolorize/aacolorize.py43
-rw-r--r--pkgs/aszlig/aacolorize/default.nix4
2 files changed, 31 insertions, 16 deletions
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 "<Color %s>" % 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"