import csv
import pprint

import sys
from sch.sch import Schematic
from sch.editor import ComponentEditor
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="Reference",
                    help='Reference column name')

args = parser.parse_args()

sch = Schematic(args.sch_file)
sch_components = sch.components
map_components = {}
for c in sch_components:
    for f in c.fields:
        if int(f['id']) == 0:
            map_components[f['ref'].strip('"')] = c
            continue


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

components = {}


fields = reader.next()

if args.column_reference in fields:
    refIndex = fields.index(args.column_reference)
    if refIndex != 0:
        sys.stderr.write('Reference field must be first column' )
        sys.exit()

else:
    sys.stderr.write('Cannot find a title row with reference field: "%s"\n' % (args.column_reference))
    sys.exit()

for row in reader:
    ref = row[0].strip()
    try:
        component = map_components[ref]
        ed = ComponentEditor(component)
        for i in range(len(row))[1:]:
            if row[i] != '':
                ed.set_field_value(fields[i].strip(), row[i].strip())

    except KeyError:
        print 'Can\'t find component %s \n' % ref

sch.save()



@@ -277,122 +277,8 @@
 t')%0A
-parser.add_argument('--sch', dest='sch_file', required=True,%0A                    help='Schematic file as output')%0A
 pars
@@ -422,68 +422,206 @@
 e')%0A
-%0Aargs = parser.parse_args()%0A%0Asch = Schematic(args.sch_file)%0A
+parser.add_argument('sch_filename', help='Schematic file as output', nargs='+')%0A%0Aargs = parser.parse_args()%0A%0Afor fname in args.sch_filename:%0A%0A    sch = Schematic(fname)%0A    sheets = sch.sheets%0A%0A    
 sch_
@@ -648,16 +648,20 @@
 ponents%0A
+    
 map_comp
@@ -672,16 +672,20 @@
 ts = %7B%7D%0A
+    
 for c in
@@ -701,16 +701,20 @@
 onents:%0A
+    
     for 
@@ -736,16 +736,20 @@
         
+   
+ 
 if int(f
@@ -758,24 +758,28 @@
 id'%5D) == 0:%0A
+    
             
@@ -830,16 +830,20 @@
         
+  
+  
 continue
@@ -845,16 +845,20 @@
 tinue%0A%0A%0A
+    
 csvf = o
@@ -881,16 +881,20 @@
 e, 'r')%0A
+    
 reader =
@@ -911,16 +911,20 @@
 r(csvf)%0A
+    
 header_o
@@ -934,16 +934,20 @@
  False%0A%0A
+    
 componen
@@ -956,16 +956,20 @@
  = %7B%7D%0A%0A%0A
+    
 fields =
@@ -984,16 +984,20 @@
 next()%0A%0A
+    
 if args.
@@ -1020,24 +1020,28 @@
  in fields:%0A
+    
     refIndex
@@ -1079,24 +1079,28 @@
 erence)%0A    
+ 
+   
 if refIndex 
@@ -1101,24 +1101,28 @@
 Index != 0:%0A
+    
         sys.
@@ -1179,24 +1179,28 @@
 ' )%0A        
+    
 sys.exit()%0A%0A
@@ -1203,14 +1203,22 @@
 ()%0A%0A
+    
 else:%0A
+    
     
@@ -1316,16 +1316,20 @@
 rence))%0A
+    
     sys.
@@ -1332,24 +1332,28 @@
 sys.exit()%0A%0A
+    
 for row in r
@@ -1363,16 +1363,20 @@
 er:%0A    
+   
+ 
 ref = ro
@@ -1388,16 +1388,20 @@
 strip()%0A
+    
     try:
@@ -1405,24 +1405,28 @@
 ry:%0A        
+   
+ 
 component = 
@@ -1445,16 +1445,20 @@
 ts%5Bref%5D%0A
+    
         
@@ -1493,24 +1493,28 @@
 nt)%0A        
+  
+  
 for i in ran
@@ -1539,24 +1539,29 @@
             
+    #
 if row%5Bi%5D !=
@@ -1639,30 +1639,12 @@
 ())%0A
-%0A
     
-except KeyError:%0A
     
@@ -1658,18 +1658,14 @@
 nt '
-Can%5C't fin
+Update
 d co
@@ -1679,19 +1679,104 @@
  %25s 
+from CSV in schematic file %25s 
 %5Cn' %25 
+(
 ref
-%0A%0A
+, fname)%0A        except KeyError:%0A            pass%0A%0A    
 sch.

Support multiple sch files, support empty values in csv