Project

General

Profile

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 1661 aaronmk
            if info.dialect == None: continue # dialect of None = empty stream
26
27 1448 aaronmk
            def write_header(): sys.stdout.write(info.header_line)
28
            if first_info == None:
29
                first_path = path
30
                first_info = info
31
                write_header()
32
            else:
33
                if not util.classes_eq(info.dialect, first_info.dialect):
34
                    raise SystemExit('Spreadsheet error: "'+path
35
                        +'" dialect doesn\'t match "'+first_path+'" dialect')
36
                if info.header_line != first_info.header_line: write_header()
37
                    # not a duplicated header
38
39
            # Copy remaining lines
40
            while True:
41
                line = stream.readline()
42
                if line == '': break
43
                sys.stdout.write(line)
44
        except IOError, e: # abort if output stream closed
45
            if str(e) != '[Errno 32] Broken pipe': raise # other IOErrors
46 1661 aaronmk
        finally: stream.close() # still run if continue is called
47 355 aaronmk
48
main()