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 |
1493
|
aaronmk
|
if path == '-': stream = sys.stdin
|
20 |
|
|
else: stream = open(path, 'rb')
|
21 |
1447
|
aaronmk
|
|
22 |
|
|
# Get dialect and process first line
|
23 |
|
|
info = csvs.stream_info(stream)
|
24 |
1448
|
aaronmk
|
try:
|
25 |
|
|
def write_header(): sys.stdout.write(info.header_line)
|
26 |
|
|
if first_info == None:
|
27 |
|
|
first_path = path
|
28 |
|
|
first_info = info
|
29 |
|
|
write_header()
|
30 |
|
|
else:
|
31 |
|
|
if not util.classes_eq(info.dialect, first_info.dialect):
|
32 |
|
|
raise SystemExit('Spreadsheet error: "'+path
|
33 |
|
|
+'" dialect doesn\'t match "'+first_path+'" dialect')
|
34 |
|
|
if info.header_line != first_info.header_line: write_header()
|
35 |
|
|
# not a duplicated header
|
36 |
|
|
|
37 |
|
|
# Copy remaining lines
|
38 |
|
|
while True:
|
39 |
|
|
line = stream.readline()
|
40 |
|
|
if line == '': break
|
41 |
|
|
sys.stdout.write(line)
|
42 |
|
|
except IOError, e: # abort if output stream closed
|
43 |
|
|
if str(e) != '[Errno 32] Broken pipe': raise # other IOErrors
|
44 |
1447
|
aaronmk
|
|
45 |
|
|
stream.close()
|
46 |
355
|
aaronmk
|
|
47 |
|
|
main()
|