Project

General

Profile

1
#!/usr/bin/env python
2
# Concatenates spreadsheets, removing any duplicated headers
3
# Usage: self [sheet...] >out_sheet
4

    
5
import os.path
6
import sys
7

    
8
sys.path.append(os.path.dirname(__file__)+"/../lib")
9

    
10
import csvs
11
import util
12

    
13
def main():
14
    paths = sys.argv[1:]
15
    
16
    first_path = None
17
    first_info = None
18
    for path in paths:
19
        if path == '-': stream = sys.stdin
20
        else: stream = open(path, 'rb')
21
        
22
        # Get dialect and process first line
23
        info = csvs.stream_info(stream)
24
        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
        
45
        stream.close()
46

    
47
main()
(1-1/38)