scripts: plot.py: Reorganized main -> main_ to isolate -k/--keep-open logic
This simplifies plot.py's -k/--keep-open logic into a self-contained loop that just calls main_ on an update. This is a compromise on getting rid of -k/--keep-open completely, since we _could_ just rely on watch.py. But plot.py knowing which argument is the file to watch is convenient. The eventual plan is to adopt this small bit of copy-pastable-code in the other ascii-art scripts (treemap.py, dbgbmap.py, etc).
This commit is contained in:
+37
-19
@@ -1070,7 +1070,9 @@ class Grid:
|
|||||||
return grid
|
return grid
|
||||||
|
|
||||||
|
|
||||||
def main(csv_paths, *,
|
# TODO adopt main_ in all result scripts? (vs report)
|
||||||
|
# TODO adopt this keep_open pattern in all ascii-art scripts?
|
||||||
|
def main_(f, csv_paths, *,
|
||||||
by=None,
|
by=None,
|
||||||
x=None,
|
x=None,
|
||||||
y=None,
|
y=None,
|
||||||
@@ -1104,11 +1106,14 @@ def main(csv_paths, *,
|
|||||||
legend_below=False,
|
legend_below=False,
|
||||||
subplot={},
|
subplot={},
|
||||||
subplots=[],
|
subplots=[],
|
||||||
head=False,
|
|
||||||
cat=False,
|
|
||||||
keep_open=False,
|
keep_open=False,
|
||||||
sleep=None,
|
|
||||||
**args):
|
**args):
|
||||||
|
# give f an writeln function
|
||||||
|
def writeln(s=''):
|
||||||
|
f.write(s)
|
||||||
|
f.write('\n')
|
||||||
|
f.writeln = writeln
|
||||||
|
|
||||||
# figure out what color should be
|
# figure out what color should be
|
||||||
if color == 'auto':
|
if color == 'auto':
|
||||||
color = sys.stdout.isatty()
|
color = sys.stdout.isatty()
|
||||||
@@ -1255,11 +1260,7 @@ def main(csv_paths, *,
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def draw(f):
|
## our main drawing logic
|
||||||
def writeln(s=''):
|
|
||||||
f.write(s)
|
|
||||||
f.write('\n')
|
|
||||||
f.writeln = writeln
|
|
||||||
|
|
||||||
# first collect results from CSV files
|
# first collect results from CSV files
|
||||||
fields_, results = collect(csv_paths)
|
fields_, results = collect(csv_paths)
|
||||||
@@ -1553,7 +1554,7 @@ def main(csv_paths, *,
|
|||||||
s.yticklabels_ = subyticklabels
|
s.yticklabels_ = subyticklabels
|
||||||
|
|
||||||
|
|
||||||
# now that everything's plotted, let's render things to the terminal
|
## now that everything's plotted, let's render things to the terminal
|
||||||
|
|
||||||
# figure out margin
|
# figure out margin
|
||||||
xmargin = (
|
xmargin = (
|
||||||
@@ -1724,23 +1725,36 @@ def main(csv_paths, *,
|
|||||||
for j in range(min(legend_cols, len(legend_)-i)))))
|
for j in range(min(legend_cols, len(legend_)-i)))))
|
||||||
|
|
||||||
|
|
||||||
|
def main(csv_paths, *,
|
||||||
|
keep_open=False,
|
||||||
|
head=False,
|
||||||
|
cat=False,
|
||||||
|
sleep=False,
|
||||||
|
**args):
|
||||||
|
# note main_ still wants keep-open for header padding
|
||||||
|
|
||||||
|
# keep-open?
|
||||||
if keep_open:
|
if keep_open:
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
# register inotify before running the command, this avoids
|
# register inotify before running the command, this avoids
|
||||||
# modification race conditions
|
# modification race conditions
|
||||||
if keep_open and Inotify:
|
if Inotify:
|
||||||
inotify = Inotify(csv_paths)
|
inotify = Inotify(csv_paths)
|
||||||
|
|
||||||
if cat:
|
if cat:
|
||||||
draw(sys.stdout)
|
main_(sys.stdout, csv_paths,
|
||||||
|
keep_open=False,
|
||||||
|
**args)
|
||||||
else:
|
else:
|
||||||
ring = RingIO(head=head)
|
ring = RingIO(head=head)
|
||||||
draw(ring)
|
main_(ring, csv_paths,
|
||||||
|
keep_open=True,
|
||||||
|
**args)
|
||||||
ring.draw()
|
ring.draw()
|
||||||
|
|
||||||
# try to inotifywait
|
# try to inotifywait
|
||||||
if keep_open and Inotify:
|
if Inotify:
|
||||||
ptime = time.time()
|
ptime = time.time()
|
||||||
inotify.read()
|
inotify.read()
|
||||||
inotify.close()
|
inotify.close()
|
||||||
@@ -1753,8 +1767,12 @@ def main(csv_paths, *,
|
|||||||
|
|
||||||
if not cat:
|
if not cat:
|
||||||
sys.stdout.write('\n')
|
sys.stdout.write('\n')
|
||||||
|
|
||||||
|
# single-pass?
|
||||||
else:
|
else:
|
||||||
draw(sys.stdout)
|
main_(sys.stdout, csv_paths,
|
||||||
|
keep_open=False,
|
||||||
|
**args)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@@ -1984,6 +2002,10 @@ if __name__ == "__main__":
|
|||||||
'--subplot',
|
'--subplot',
|
||||||
type=AppendSubplot.parse,
|
type=AppendSubplot.parse,
|
||||||
help="Add subplot-specific arguments to the main plot.")
|
help="Add subplot-specific arguments to the main plot.")
|
||||||
|
parser.add_argument(
|
||||||
|
'-k', '--keep-open',
|
||||||
|
action='store_true',
|
||||||
|
help="Continue to open and redraw the CSV files in a loop.")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-^', '--head',
|
'-^', '--head',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
@@ -1992,10 +2014,6 @@ if __name__ == "__main__":
|
|||||||
'-z', '--cat',
|
'-z', '--cat',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help="Pipe directly to stdout.")
|
help="Pipe directly to stdout.")
|
||||||
parser.add_argument(
|
|
||||||
'-k', '--keep-open',
|
|
||||||
action='store_true',
|
|
||||||
help="Continue to open and redraw the CSV files in a loop.")
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-s', '--sleep',
|
'-s', '--sleep',
|
||||||
type=float,
|
type=float,
|
||||||
|
|||||||
Reference in New Issue
Block a user