source: stamper/bin/stamp2json@ 15:c343a1ed55c2

Last change on this file since 15:c343a1ed55c2 was 11:30241f3a03eb, checked in by Óscar M. Lage <info@…>, 10 years ago

Added stamp2json: a format convert tool (from ~/.workstamps.txt to ~/.workstamps.json)

  • Property exe set to *
File size: 1.6 KB
Line 
1#!/usr/bin/env python
2
3import json
4from argparse import ArgumentParser
5from os.path import expanduser
6
7
8def 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
20def 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
28if __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)
Note: See TracBrowser for help on using the repository browser.