Last change
on this file since 16:a80165768cd1 was 16:a80165768cd1, checked in by Borja Lopez <borja@…>, 10 years ago |
Keep on converting stamps in case a line in the workstamps.txt file
has no description/action set, for example:
2012-07-16 10:17 customer
|
|
File size:
1.7 KB
|
Rev | Line | |
---|
[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:
|
---|
[16] | 48 | info_items = info.split(' ', 1)
|
---|
| 49 | customer = info_items[0]
|
---|
| 50 | action = ''
|
---|
| 51 | if len(info_items) > 1:
|
---|
| 52 | action = info_items[1].lstrip('- ')
|
---|
[11] | 53 | end_time = time
|
---|
| 54 |
|
---|
| 55 | stamps.append({
|
---|
| 56 | 'start': start_time,
|
---|
| 57 | 'end': end_time,
|
---|
| 58 | 'customer': customer,
|
---|
| 59 | 'action': action,
|
---|
| 60 | })
|
---|
| 61 | with open(args.output, 'w') as stamps_file:
|
---|
| 62 | json.dump(stamps, stamps_file, indent=4)
|
---|
Note:
See
TracBrowser
for help on using the repository browser.