Commit fe7b8ba7 authored by heemayl's avatar heemayl

Modified CLI output formatting; closes #1

parent d039a762
......@@ -28,51 +28,30 @@ Benefits of `hat`:
## Example workflow:
```bash
% hatc -l
[]
Job queue is empty
% hatc -c
0
% hatc --add free 'tomorrow 15:47:57'
% hatc --add free 'now + 5 min'
{'msg': 'Done'}
% hatc -l
[
[
"1",
{
"command": "free",
"job_run_at": 1518040077,
"use_shell": false
}
]
]
ID Time Shell Command
1 2018-02-08T16:47:29 - free
% hatc -c
1
% hatc -a 'echo $PATH' 'now + 3 mins' bash
% hatc -a 'echo $PATH' 'tomorrow 14:40:30' bash
{'msg': 'Done'}
% hatc -l
[
[
"1",
{
"command": "free",
"job_run_at": 1518040077,
"use_shell": false
}
],
[
"2",
{
"command": "bash -c \"echo $PATH\"",
"job_run_at": 1517999620,
"use_shell": "bash"
}
]
]
ID Time Shell Command
1 2018-02-08T16:47:29 - free
2 2018-02-09T14:40:30 bash echo $PATH
% hatc -c
2
......@@ -81,16 +60,8 @@ Benefits of `hat`:
{'msg': 'Queued'}
% hatc -l
[
[
"2",
{
"command": "bash -c \"echo $PATH\"",
"job_run_at": 1517999620,
"use_shell": "bash"
}
]
]
ID Time Shell Command
2 2018-02-09T14:40:30 bash echo $PATH
% hatc -c
1
......
'''The client.'''
import argparse
import json
import os
import re
import shlex
import subprocess
import sys
......@@ -110,7 +112,33 @@ def check_daemon_process(pid_file):
if not ret or b'hat/daemon_front.py' in proc.communicate()[0]:
return True
return False
def json_to_table_print(json_str):
'''Takes a str (e.g. from `json_dumps`)
and converts to a table for printing.
'''
# re.sub is to replace the nested double quotes, for `json.loads`
data = json.loads(re.sub(
r'("command":\s+")[^"]+"([^"]+)"(",)', r'\1\2\3',
json_str.replace('\\', '')))
if data:
# Header
to_print = '\t\t'.join(('ID', 'Time', 'Shell', 'Command'))
# Sorting list according to the Epoch, then ID
data.sort(key=lambda x: (x[1]['job_run_at'], x[0]))
for job in data:
job_id = job[0]
command = job[1]['command']
shell = job[1]['use_shell'] or ' -'
time_ = time.strftime('%Y-%m-%dT%H:%M:%S',
time.localtime(job[1]['job_run_at']))
to_print = '{}\n{}'.format(to_print,
'\t'.join((job_id, time_, shell,
'\t{}'.format(command))))
return to_print
return 'Job queue is empty'
class SendReceiveData:
'''Takes and parses input, based on key creates and
......@@ -221,7 +249,10 @@ def main():
data.check_get_send()
received = data.receive_from_daemon()
if received is not None:
print(received)
if not isinstance(received, str):
print(received)
else:
print(json_to_table_print(received))
if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment