-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunJSONrun.py
executable file
·115 lines (90 loc) · 2.51 KB
/
runJSONrun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
# encoding: utf-8
"""
runJSONrun.py
Created by Magnus Wahlberg on 2012-03-15.
Copyright (c) 2012 __RedSwampResarch__. All rights reserved.
"""
from getopt import getopt, GetoptError
from sys import argv, exit, platform
import sys
import os
import csv
import json
VERSION = "0.1"
def getCSVData(csvFile):
try:
rawCSV = csv.DictReader(open(csvFile), delimiter=';')
return rawCSV
except IOError, (errno, strerror):
print "I/O error(%s): %s" % (errno, strerror)
exit(2)
def cleanUpHeaders(csvData):
for idx, fieldname in enumerate(csvData.fieldnames):
csvData.fieldnames[idx] = fieldname.replace('(', '_').replace(' ', '').replace('/', '').replace(')', '')
return csvData
def writeJSON(JSONdata, outputFile):
# No outputfile specified, dump to stdout
if outputFile == None:
print JSONdata
else:
print "Writing JSON data to", outputFile
try:
jsonFile = open(outputFile,"w")
jsonFile.writelines(JSONdata)
jsonFile.close()
except IOError, (errno, strerror):
print "I/O error(%s): %s" % (errno, strerror)
exit(2)
def usage():
print "\nUsage: runJSONrun.py -i file.csv ..."
print "Convert runmeter (http://www.abvio.com/runmeter/) csv files to JSON."
print ""
print "If you start runJSONrun and only specify a csv file, it will dump the JSON data to standard out."
print ""
print "-?, \t\tPrint this help and exit..."
print "-i, --input\tThe runmeter CSV file to read"
print "-o, --output\tThe file you wan't the JSON data to be written to"
print "-V, \t\tPrint the version and exit\n"
def version():
print "runJSONrun v" + VERSION
def main():
try:
opts, args = getopt(argv[1:], "?hi:o:V", ["?", "input=", "output=", "--version"])
except GetoptError, err:
print str(err)
usage()
exit(2)
inputFile = None
outputFile = None
for o, a in opts:
if o in ("-i", "--input"):
inputFile = a
elif o in ("-o", "--output"):
outputFile = a
elif o in ("-V", "--version"):
version()
exit()
elif o == "-?":
usage()
exit()
else:
assert False, "unhandled option " + o
exit(2)
if inputFile != None:
# Parse the CSV file
RawcsvData = getCSVData(inputFile)
csvData = cleanUpHeaders(RawcsvData)
# Add all data points to an array
dataPoints = []
for row in csvData:
dataPoints.append(row)
# Convert the data to JSON
jsonData = json.dumps(dataPoints)
# Output the jsondata, to stdout
writeJSON(jsonData, outputFile)
else:
print "No input file... exiting."
exit(2)
if __name__ == '__main__':
main()