[11] | 1 | #!/usr/bin/env python
|
---|
| 2 |
|
---|
| 3 | import json
|
---|
| 4 | from argparse import ArgumentParser
|
---|
| 5 | from os.path import expanduser
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | def args_cmd():
|
---|
| 9 | """Parse the command line arguments via argparse"""
|
---|
| 10 | parser = ArgumentParser(description='Convert tool from txt to json format')
|
---|
| 11 | parser.add_argument(
|
---|
| 12 | '--input', '-i', default=expanduser('~/.workstamps.txt'),
|
---|
| 13 | help='Input filename (default ~/.workstamps.txt')
|
---|
| 14 | parser.add_argument(
|
---|
| 15 | '--output', '-o', default=expanduser('~/.workstamps.json'),
|
---|
| 16 | help='Input filename (default: ~/.workstamps.json)')
|
---|
| 17 | return parser.parse_args()
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | def itemify(line):
|
---|
| 21 | """Split a line, returning date and the rest of data"""
|
---|
| 22 | line = line.strip()
|
---|
| 23 | end_time = line[:16]
|
---|
| 24 | info = line[17:]
|
---|
| 25 | return(line[:16], line[17:])
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | if __name__ == '__main__':
|
---|
| 29 | args = args_cmd()
|
---|
| 30 |
|
---|
| 31 | stamps = []
|
---|
| 32 |
|
---|
| 33 | with open(args.input, 'r') as infile:
|
---|
| 34 |
|
---|
| 35 | for line in infile:
|
---|
| 36 | time, info = itemify(line)
|
---|
| 37 |
|
---|
| 38 | if info == 'start':
|
---|
| 39 | customer = None
|
---|
| 40 | action = None
|
---|
| 41 | start_time = time
|
---|
| 42 | end_time = None
|
---|
| 43 |
|
---|
| 44 | elif time == 'restarttotals':
|
---|
| 45 | continue
|
---|
| 46 |
|
---|
| 47 | else:
|
---|
| 48 | customer = info.split(' ', 1)[0]
|
---|
| 49 | action = info.split(' ', 1)[1].lstrip('- ')
|
---|
| 50 | end_time = time
|
---|
| 51 |
|
---|
| 52 | stamps.append({
|
---|
| 53 | 'start': start_time,
|
---|
| 54 | 'end': end_time,
|
---|
| 55 | 'customer': customer,
|
---|
| 56 | 'action': action,
|
---|
| 57 | })
|
---|
| 58 | with open(args.output, 'w') as stamps_file:
|
---|
| 59 | json.dump(stamps, stamps_file, indent=4)
|
---|