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
|
||||
|
||||
|
||||
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,
|
||||
x=None,
|
||||
y=None,
|
||||
@@ -1104,11 +1106,14 @@ def main(csv_paths, *,
|
||||
legend_below=False,
|
||||
subplot={},
|
||||
subplots=[],
|
||||
head=False,
|
||||
cat=False,
|
||||
keep_open=False,
|
||||
sleep=None,
|
||||
**args):
|
||||
# give f an writeln function
|
||||
def writeln(s=''):
|
||||
f.write(s)
|
||||
f.write('\n')
|
||||
f.writeln = writeln
|
||||
|
||||
# figure out what color should be
|
||||
if color == 'auto':
|
||||
color = sys.stdout.isatty()
|
||||
@@ -1255,11 +1260,7 @@ def main(csv_paths, *,
|
||||
)
|
||||
|
||||
|
||||
def draw(f):
|
||||
def writeln(s=''):
|
||||
f.write(s)
|
||||
f.write('\n')
|
||||
f.writeln = writeln
|
||||
## our main drawing logic
|
||||
|
||||
# first collect results from CSV files
|
||||
fields_, results = collect(csv_paths)
|
||||
@@ -1553,7 +1554,7 @@ def main(csv_paths, *,
|
||||
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
|
||||
xmargin = (
|
||||
@@ -1724,23 +1725,36 @@ def main(csv_paths, *,
|
||||
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:
|
||||
try:
|
||||
while True:
|
||||
# register inotify before running the command, this avoids
|
||||
# modification race conditions
|
||||
if keep_open and Inotify:
|
||||
if Inotify:
|
||||
inotify = Inotify(csv_paths)
|
||||
|
||||
if cat:
|
||||
draw(sys.stdout)
|
||||
main_(sys.stdout, csv_paths,
|
||||
keep_open=False,
|
||||
**args)
|
||||
else:
|
||||
ring = RingIO(head=head)
|
||||
draw(ring)
|
||||
main_(ring, csv_paths,
|
||||
keep_open=True,
|
||||
**args)
|
||||
ring.draw()
|
||||
|
||||
# try to inotifywait
|
||||
if keep_open and Inotify:
|
||||
if Inotify:
|
||||
ptime = time.time()
|
||||
inotify.read()
|
||||
inotify.close()
|
||||
@@ -1753,8 +1767,12 @@ def main(csv_paths, *,
|
||||
|
||||
if not cat:
|
||||
sys.stdout.write('\n')
|
||||
|
||||
# single-pass?
|
||||
else:
|
||||
draw(sys.stdout)
|
||||
main_(sys.stdout, csv_paths,
|
||||
keep_open=False,
|
||||
**args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -1984,6 +2002,10 @@ if __name__ == "__main__":
|
||||
'--subplot',
|
||||
type=AppendSubplot.parse,
|
||||
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(
|
||||
'-^', '--head',
|
||||
action='store_true',
|
||||
@@ -1992,10 +2014,6 @@ if __name__ == "__main__":
|
||||
'-z', '--cat',
|
||||
action='store_true',
|
||||
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(
|
||||
'-s', '--sleep',
|
||||
type=float,
|
||||
|
||||
Reference in New Issue
Block a user