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
        stream = open(path, 'rb')
20
        
21
        # Get dialect and process first line
22
        info = csvs.stream_info(stream)
23
        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
        
44
        stream.close()
45

    
46
main()
(1-1/29)