import csv

from sch.sch import Schematic
import argparse


parser = argparse.ArgumentParser(description='')
parser.add_argument('--csv', dest='csv_file', required=True,
                    help='CSV file as input')
parser.add_argument('--sch', dest='sch_file', required=True,
                    help='Schematic file as output')
parser.add_argument('--col-ref', dest='column_reference', required=False, default="Ref",
                    help='Reference column name')

args = parser.parse_args()

csvf = open(args.csv_file, 'r')
reader = csv.reader(csvf)
header_ok = False

for row in reader:
    # Try to locate the header row
    if args.column_reference in row:
        header_ok = True
        ref_col = row.index(args.column_reference)

    print row

if not header_ok:
    sys.stderr.write('Cannot find a header with reference field: "%s"\n' % (args.column_reference))
    sys.exit()


@@ -4,16 +4,41 @@
 ort csv%0A
+import pprint%0A%0Aimport sys
 %0Afrom sc
@@ -597,66 +597,84 @@
 se%0A%0A
-for row in reader:%0A    # Try to locate the header row%0A    
+components = %7B%7D%0A%0A%0Afields = reader.next()%0A#fields = map(str.strip, fields)%0A%0A%0A
 if a
@@ -701,59 +701,37 @@
  in 
-row
+fields
 :%0A    
- 
-   header_ok = True%0A        ref_col = row
+refIndex = fields
 .ind
@@ -760,40 +760,120 @@
 ce)%0A
-%0A
     
-print row%0A%0Aif not header_ok
+if refIndex != 0:%0A        sys.stderr.write('Reference field must be first column' )%0A        sys.exit()%0A%0Aelse
 :%0A  
@@ -906,22 +906,25 @@
  find a 
-header
+title row
  with re
@@ -973,24 +973,397 @@
 rence))%0A    sys.exit()%0A%0A
+for row in reader:%0A    ref = row%5B0%5D.strip()%0A    if ref in components.keys():%0A        sys.stderr.write('Component with reference %22%25s%22 already exist on previous row%5Cn' %25 (ref))%0A        sys.exit()%0A%0A    components%5Bref%5D = %7B%7D%0A%0A    for i in range(len(row))%5B1:%5D:%0A        if row%5Bi%5D != '':%0A            components%5Bref%5D%5Bfields%5Bi%5D.strip()%5D = row%5Bi%5D.strip()%0A%0Apprint.pprint(components)%0A%0A%0A

Read CSV