1 |
355
|
aaronmk
|
#!/usr/bin/env python
|
2 |
1447
|
aaronmk
|
# Concatenates spreadsheets, removing any duplicated headers
|
3 |
|
|
# Usage: self [sheet...] >out_sheet
|
4 |
355
|
aaronmk
|
|
5 |
1447
|
aaronmk
|
import os.path
|
6 |
355
|
aaronmk
|
import sys
|
7 |
|
|
|
8 |
1447
|
aaronmk
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
9 |
|
|
|
10 |
|
|
import csvs
|
11 |
|
|
import util
|
12 |
|
|
|
13 |
355
|
aaronmk
|
def main():
|
14 |
1447
|
aaronmk
|
paths = sys.argv[1:]
|
15 |
355
|
aaronmk
|
|
16 |
1447
|
aaronmk
|
first_path = None
|
17 |
|
|
first_info = None
|
18 |
|
|
for path in paths:
|
19 |
|
|
stream = open(path, 'rb')
|
20 |
|
|
|
21 |
|
|
# Get dialect and process first line
|
22 |
|
|
info = csvs.stream_info(stream)
|
23 |
1448
|
aaronmk
|
try:
|
24 |
|
|
def write_header(): sys.stdout.write(info.header_line)
|
25 |
|
|
if first_info == None:
|
26 |
|
|
first_path = path
|
27 |
|
|
first_info = info
|
28 |
|
|
write_header()
|
29 |
|
|
else:
|
30 |
|
|
if not util.classes_eq(info.dialect, first_info.dialect):
|
31 |
|
|
raise SystemExit('Spreadsheet error: "'+path
|
32 |
|
|
+'" dialect doesn\'t match "'+first_path+'" dialect')
|
33 |
|
|
if info.header_line != first_info.header_line: write_header()
|
34 |
|
|
# not a duplicated header
|
35 |
|
|
|
36 |
|
|
# Copy remaining lines
|
37 |
|
|
while True:
|
38 |
|
|
line = stream.readline()
|
39 |
|
|
if line == '': break
|
40 |
|
|
sys.stdout.write(line)
|
41 |
|
|
except IOError, e: # abort if output stream closed
|
42 |
|
|
if str(e) != '[Errno 32] Broken pipe': raise # other IOErrors
|
43 |
1447
|
aaronmk
|
|
44 |
|
|
stream.close()
|
45 |
355
|
aaronmk
|
|
46 |
|
|
main()
|