python3inizing scripts

This commit is contained in:
Ville Rantanen
2020-10-12 11:37:33 +03:00
parent 877acc0d35
commit 52db6b9bf4
8 changed files with 1328 additions and 934 deletions

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# coding=utf-8
#
# Copyright 2016 Ville Rantanen
@@ -17,61 +17,83 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
'''Color by ${X} notation.'''
"""Color by ${X} notation."""
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
__version__ = "0.1"
import sys,os,argparse,re
import sys, os, argparse, re
from argparse import ArgumentParser
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import ansicodes as ansi
def setup_options():
''' Create command line options '''
usage='''
""" Create command line options """
usage = (
"""
Color notation renderer in ANSI codes
Special syntaxes:
* Colors: insert string ${X}, where X values in the table below.
''' + ansi.demo()
"""
+ ansi.demo()
)
parser=ArgumentParser(description=usage,
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__author__)
parser = ArgumentParser(
description=usage,
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__author__,
)
parser.add_argument("-v","--version",action="version",version=__version__)
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True,
help="Disable color.")
parser.add_argument("-z",action="store_true",dest="zero",default=False,
help="Reset coloring at the end of each line.")
parser.add_argument("-Z",action="store_false",dest="zero_final",default=True,
help="Disable reset of colors at the end of file.")
parser.add_argument("filename",type=str,
help="File to show, - for stdin")
opts=parser.parse_args()
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument(
"--no-color",
"-n",
action="store_false",
dest="color",
default=True,
help="Disable color.",
)
parser.add_argument(
"-z",
action="store_true",
dest="zero",
default=False,
help="Reset coloring at the end of each line.",
)
parser.add_argument(
"-Z",
action="store_false",
dest="zero_final",
default=True,
help="Disable reset of colors at the end of file.",
)
parser.add_argument("filename", type=str, help="File to show, - for stdin")
opts = parser.parse_args()
return opts
bc=ansi.code()
opts=setup_options()
if opts.filename=="-":
f=sys.stdin
bc = ansi.code()
opts = setup_options()
if opts.filename == "-":
f = sys.stdin
else:
f=open(opts.filename,'r')
f = open(opts.filename, "r")
for row in f:
if not row:
continue
if type(row) == bytes:
row = row.decode('utf-8')
row=row.rstrip("\n\r ")
row = row.decode("utf-8")
row = row.rstrip("\n\r ")
if opts.color:
colored=bc.color_string(row)
colored = bc.color_string(row)
else:
colored=bc.nocolor_string(row)
colored = bc.nocolor_string(row)
sys.stdout.write(colored)
if opts.zero and opts.color:
sys.stdout.write(bc.Z)
sys.stdout.write("\n")
if opts.zero_final and opts.color:
sys.stdout.write(bc.Z)