summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-12-25 16:44:11 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-12-25 16:44:11 +0000
commit6372d03a0cf93e6062be1136cb0a2e52286e2fc8 (patch)
treeafebe0f2e1c78db7c3fc09660d3b006dca1b8731
parent335c9f22efc319ea0c3232a6dfd69c425153c89b (diff)
downloadz80count-0.8.2.tar.gz
z80count-0.8.2.zip
New home out of GH0.8.2
-rw-r--r--z80count/z80count.py160
1 files changed, 103 insertions, 57 deletions
diff --git a/z80count/z80count.py b/z80count/z80count.py
index da5668f..d1ac199 100644
--- a/z80count/z80count.py
+++ b/z80count/z80count.py
@@ -30,7 +30,7 @@ import re
import argparse
from os import path
-version = "0.8.1"
+version = "0.8.2"
OUR_COMMENT = re.compile(r"(\[[0-9.\s/]+\])")
DEF_COLUMN = 50
@@ -55,6 +55,7 @@ def perror(message, *args, **kwargs):
# (when the value comes from the defaults). If the value is invalid
# for its domain they must raise a ValueError or TypeError exception.
+
def boolean(x):
if x in (True, "1", "on", "yes", "true"):
return True
@@ -70,12 +71,12 @@ Option = collections.namedtuple(
DEFAULTS = [
- Option("column", "column", DEF_COLUMN, int),
- Option("debug", "debug", False, boolean),
- Option("subtotals", "subt", False, boolean),
+ Option("column", "column", DEF_COLUMN, int),
+ Option("debug", "debug", False, boolean),
+ Option("subtotals", "subt", False, boolean),
Option("tab width", "tab_width", DEF_TABSTOP, int),
- Option("keep cycles", "no_update", False, boolean),
- Option("use tabs", "use_tabs", False, boolean),
+ Option("keep cycles", "no_update", False, boolean),
+ Option("use tabs", "use_tabs", False, boolean),
]
@@ -100,9 +101,7 @@ def get_program_args():
else:
config = {i.config_name: i.default for i in DEFAULTS}
- args = parse_command_line(
- {i.arg_name: config[i.config_name] for i in DEFAULTS}
- )
+ args = parse_command_line({i.arg_name: config[i.config_name] for i in DEFAULTS})
return args
@@ -133,7 +132,6 @@ def load_config_file(config_file, schema):
def locate_config_file():
-
# TODO: check on windows
z80count_rc = os.environ.get("Z80COUNT_RC")
@@ -168,36 +166,70 @@ def locate_config_file():
def parse_command_line(defaults):
parser = argparse.ArgumentParser(
- description='Z80 Cycle Count',
- epilog="Copyright (C) 2019 Juan J Martinez <jjm@usebox.net>")
+ description="Z80 Cycle Count",
+ epilog="Copyright (C) 2019 Juan J Martinez <jjm@usebox.net>",
+ )
+ parser.add_argument("--version", action="version", version="%(prog)s " + version)
+ parser.add_argument(
+ "-d",
+ dest="debug",
+ action="store_true",
+ help="Enable debug (show the matched case)",
+ default=defaults["debug"],
+ )
+ parser.add_argument(
+ "-s",
+ dest="subt",
+ action="store_true",
+ help="Include subtotal",
+ default=defaults["subt"],
+ )
+ parser.add_argument(
+ "-n",
+ dest="no_update",
+ action="store_true",
+ help="Do not update existing count if available",
+ default=defaults["no_update"],
+ )
parser.add_argument(
- "--version", action="version", version="%(prog)s " + version)
- parser.add_argument('-d', dest='debug', action='store_true',
- help="Enable debug (show the matched case)",
- default=defaults["debug"])
- parser.add_argument('-s', dest='subt', action='store_true',
- help="Include subtotal",
- default=defaults["subt"])
- parser.add_argument('-n', dest='no_update', action='store_true',
- help="Do not update existing count if available",
- default=defaults["no_update"])
- parser.add_argument('-T', dest='tab_width', type=int,
- help="Number of spaces for each tab (default: %d)" % DEF_TABSTOP,
- default=defaults["tab_width"])
- parser.add_argument('-t', '--use-tabs', dest='use_tabs', action='store_true',
- help="Use tabs to align newly added comments (default: use spaces)",
- default=defaults["use_tabs"])
- parser.add_argument('-c', '--column', dest='column', type=int,
- help="Column to align newly added comments (default: %d)" % DEF_COLUMN,
- default=defaults["column"])
+ "-T",
+ dest="tab_width",
+ type=int,
+ help="Number of spaces for each tab (default: %d)" % DEF_TABSTOP,
+ default=defaults["tab_width"],
+ )
+ parser.add_argument(
+ "-t",
+ "--use-tabs",
+ dest="use_tabs",
+ action="store_true",
+ help="Use tabs to align newly added comments (default: use spaces)",
+ default=defaults["use_tabs"],
+ )
+ parser.add_argument(
+ "-c",
+ "--column",
+ dest="column",
+ type=int,
+ help="Column to align newly added comments (default: %d)" % DEF_COLUMN,
+ default=defaults["column"],
+ )
parser.add_argument(
- "infile", nargs="?", type=argparse.FileType('r'), default=sys.stdin,
- help="Input file")
+ "infile",
+ nargs="?",
+ type=argparse.FileType("r"),
+ default=sys.stdin,
+ help="Input file",
+ )
parser.add_argument(
- "outfile", nargs="?", type=argparse.FileType('w'), default=sys.stdout,
- help="Output file")
+ "outfile",
+ nargs="?",
+ type=argparse.FileType("w"),
+ default=sys.stdout,
+ help="Output file",
+ )
return parser.parse_args()
@@ -206,23 +238,32 @@ def parse_command_line(defaults):
# z80count
#
-def z80count(line,
- parser,
- total,
- subt,
- no_update,
- column=50,
- use_tabs=False,
- tab_width=8,
- debug=False,
- ):
+
+def z80count(
+ line,
+ parser,
+ total,
+ subt,
+ no_update,
+ column=50,
+ use_tabs=False,
+ tab_width=8,
+ debug=False,
+):
out = line.rstrip() + "\n"
entry = parser.lookup(line)
if entry:
total, total_cond = update_counters(entry, total)
out = format_line(
- line, entry, total, total_cond, subt, update=not no_update,
- column=column, debug=debug, use_tabs=use_tabs,
+ line,
+ entry,
+ total,
+ total_cond,
+ subt,
+ update=not no_update,
+ column=column,
+ debug=debug,
+ use_tabs=use_tabs,
tab_width=tab_width,
)
return (out, total)
@@ -238,8 +279,9 @@ def update_counters(entry, total):
return (total, total_cond)
-def format_line(line, entry, total, total_cond, subt, update, column,
- debug, use_tabs, tab_width):
+def format_line(
+ line, entry, total, total_cond, subt, update, column, debug, use_tabs, tab_width
+):
cycles = entry["cycles"]
line = line.rstrip().rsplit(";", 1)
comment = "; [%s" % cycles
@@ -254,8 +296,7 @@ def format_line(line, entry, total, total_cond, subt, update, column,
comment += " case{%s}" % entry["case"]
if len(line) == 1:
- comment = comment_alignment(
- line[0], column, use_tabs, tab_width) + comment
+ comment = comment_alignment(line[0], column, use_tabs, tab_width) + comment
out = line[0] + comment
if len(line) > 1:
if update:
@@ -347,8 +388,7 @@ class Parser(object):
@classmethod
def _load_table(cls):
- table_file = path.join(
- path.dirname(path.realpath(__file__)), "z80table.json")
+ table_file = path.join(path.dirname(path.realpath(__file__)), "z80table.json")
with open(table_file, "rt") as fd:
table = json.load(fd)
@@ -379,8 +419,7 @@ class Parser(object):
@staticmethod
def _init_entry(entry):
- entry["cregex"] = re.compile(
- r"^\s*" + entry["regex"] + r"\s*(;.*)?$", re.I)
+ entry["cregex"] = re.compile(r"^\s*" + entry["regex"] + r"\s*(;.*)?$", re.I)
cycles = entry["cycles"]
if "/" in cycles:
c = cycles.split("/")
@@ -402,11 +441,18 @@ def main():
total = 0
for line in in_f:
output, total = z80count(
- line, parser, total, args.subt, args.no_update,
- args.column, args.use_tabs, args.tab_width,
+ line,
+ parser,
+ total,
+ args.subt,
+ args.no_update,
+ args.column,
+ args.use_tabs,
+ args.tab_width,
args.debug,
)
out_f.write(output)
+
if __name__ == "__main__":
main()