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
            if info.dialect == None: continue # dialect of None = empty stream
26
            
27
            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
        finally: stream.close() # still run if continue is called
47

    
48
main()
(1-1/47)