@@ -1152,12 +1152,9 @@ class AddCSVRowInputSpec(TraitedSpec):
1152
1152
cols = traits .Int (desc = 'Number of columns' )
1153
1153
field_headings = traits .List (traits .Str (), mandatory = True ,
1154
1154
desc = 'Heading list of available field to be added.' )
1155
-
1156
- float_trait = traits .Float ( 0.0 , argstr = '%.5f' )
1157
- int_trait = traits .Int ( 0 , argstr = '%d' )
1158
- str_trait = traits .Str ( '' , argstr = '"%s"' )
1159
- row_trait = traits .Either ( int_trait , float_trait , str_trait )
1160
- new_fields = traits .List ( row_trait , mandatory = True , desc = 'List of new values in row' )
1155
+ new_fields = traits .List ( traits .Any (), mandatory = True , desc = 'List of new values in row' , separator = ',' )
1156
+ col_width = traits .Int ( 7 , mandatory = True , usedefault = True , desc = 'column width' )
1157
+ float_dec = traits .Int ( 4 , mandatory = True , usedefault = True , desc = 'decimals' )
1161
1158
1162
1159
class AddCSVRowOutputSpec (TraitedSpec ):
1163
1160
csv_file = File (desc = 'Output CSV file containing rows ' )
@@ -1200,36 +1197,50 @@ def _run_interface(self, runtime):
1200
1197
1201
1198
if not isdefined ( self .inputs .cols ) and isdefined ( self .inputs .field_headings ):
1202
1199
cols = len ( self .inputs .field_headings )
1200
+ headings = self .inputs .field_headings
1203
1201
1204
1202
if cols == 0 :
1205
1203
iflogger .error ( 'Number of cols and length of field headings must be > 0' )
1206
1204
1207
1205
if len ( self .inputs .new_fields ) != cols :
1208
1206
iflogger .error ( 'Wrong length of fields, does not match number of cols' )
1209
1207
1210
- with open (self .inputs .in_file , 'r+' ) as in_file :
1208
+ col_width = self .inputs .col_width
1209
+ float_dec = self .inputs .float_dec
1210
+
1211
+ with open (self .inputs .in_file , 'a+' ) as in_file :
1211
1212
lines = in_file .readlines ()
1213
+
1214
+ if len (lines )> 0 and lines [0 ]== '\n ' :
1215
+ lines .pop ()
1216
+
1217
+ print len (lines )
1212
1218
1213
- if len (headings )> 0 and (len (lines ) == 0 or lines [ 0 ] == '' ):
1219
+ if ( len (headings )> 0 ) and (len (lines )== 0 ):
1214
1220
hdr = [ '"%s"' % h for h in self .inputs .field_headings ]
1215
1221
hdrstr = "," .join (hdr )
1216
- lines .insert ( 0 , hdrstr )
1222
+ lines = [ hdrstr + '\n ' ]
1223
+
1224
+ if not lines [- 1 ] or lines [- 1 ]== '\n ' :
1225
+ lines .pop ()
1217
1226
1218
- print self .inputs .new_fields .items ()
1219
-
1220
- metadata = dict (argstr = lambda t : t is not None )
1227
+ row_data = []
1228
+ metadata = dict (separator = lambda t : t is not None )
1221
1229
for name , spec in sorted (self .inputs .traits (** metadata ).items ()):
1222
- print name
1223
- value = getattr (self .inputs , name )
1224
- if not value is None :
1225
- print value
1226
- #arg = self._format_row( name, spec, value )
1230
+ values = getattr (self .inputs , name )
1231
+
1232
+ for v in values :
1233
+ argstr = '{:>%d}' % col_width
1234
+ if type (v )== 'float' :
1235
+ argstr = '{:>%d.%df}' % ( col_width , float_dec )
1227
1236
1237
+ row_data .append ( argstr .format (v ) )
1228
1238
1239
+ newrow = "," .join ( row_data )
1240
+ lines .append ( newrow + '\n ' )
1229
1241
1230
- #newrow = ",".join( self.inputs.new_fields )
1231
- #lines.append( newrow )
1232
- #in_file.writelines( lines )
1242
+ print "" .join ( lines )
1243
+ in_file .write ( "" .join (lines ) )
1233
1244
1234
1245
return runtime
1235
1246
0 commit comments