@@ -118,137 +118,142 @@ def run(self, edit=None, source_format=None, target_format=None, ext=None,
118118 if source_format and target_format == source_format :
119119 return self .status ("Target and source file format are identical. (%s)" % target_format )
120120
121- if source_format and not source_format in loaders .get :
121+ if source_format and source_format not in loaders .get :
122122 return self .status ("%s for '%s' not supported/implemented." % ("Loader" , source_format ))
123123
124- if target_format and not target_format in dumpers .get :
124+ if target_format and target_format not in dumpers .get :
125125 return self .status ("%s for '%s' not supported/implemented." % ("Dumper" , target_format ))
126126
127127 # Now the actual "building" starts (collecting remaining parameters)
128- output = _output or OutputPanel (self .window , "package_dev" )
129- output .show ()
130-
131- # Auto-detect the file type if it's not specified
132- if not source_format :
133- output .write ("Input type not specified, auto-detecting..." )
134- for Loader in loaders .get .values ():
135- if Loader .file_is_valid (self .view ):
136- source_format = Loader .ext
137- output .write_line (' %s\n ' % Loader .name )
138- break
128+ with OutputPanel (self .window , "package_dev" ) as output :
129+ output .show ()
139130
131+ # Auto-detect the file type if it's not specified
140132 if not source_format :
141- return output .write_line ("\n Unable to detect file type." )
142- elif target_format == source_format :
143- return output .write_line ("File already is %s." % loaders .get [source_format ].name )
144-
145- # Load inline options
146- Loader = loaders .get [source_format ]
147- opts = Loader .load_options (self .view )
148-
149- # Function to determine the new file extension depending on the target format
150- def get_new_ext (target_format ):
151- if ext :
152- return '.' + ext
153- if opts and 'ext' in opts :
154- return '.' + opts ['ext' ]
155- else :
156- new_ext , prepend_target_format = Loader .get_new_file_ext (self .view )
157- if prepend_target_format :
158- new_ext = ".%s-%s" % (target_format .upper (), new_ext [1 :])
159-
160- return (new_ext or '.' + target_format )
161-
162- path_tuple = file_path_tuple (file_path ) # This is the latest point possible
163-
164- if not target_format :
165- output .write ("No target format specified, searching in file..." )
166-
167- # No information about a target format; ask for it
168- if not opts or not 'target_format' in opts :
169- output .write (" Could not detect target format.\n "
170- "Please select or define a target format..." )
171-
172- # Show overlay with all dumping options except for the current type
173- # Save stripped-down `items` for later
174- options , items = [], []
175- for itm in self .target_list :
176- # To not clash with function-local "target_format"
177- target_format_ = itm ['kwargs' ]['target_format' ]
178- if target_format_ != source_format :
179- options .append (["Convert to: %s" % itm ['name' ],
180- path_tuple .base_name + get_new_ext (target_format_ )])
181- items .append (itm )
182-
183- def on_select (index ):
184- if index < 0 or index >= len (items ):
185- # canceled or other magic
186- output .write_line ("\n \n Build canceled." )
187- return
188-
189- target = items [index ]
190- output .write_line (' %s\n ' % target ['name' ])
191-
192- kwargs .update (target ['kwargs' ])
193- kwargs .update (dict (source_format = source_format , _output = output ))
194- self .run (* args , ** kwargs )
195-
196- # Forward all params to the new command call
197- self .window .show_quick_panel (options , on_select )
133+ output .write ("Input type not specified, auto-detecting..." )
134+ for Loader in loaders .get .values ():
135+ if Loader .file_is_valid (self .view ):
136+ source_format = Loader .ext
137+ output .write_line (' %s\n ' % Loader .name )
138+ break
139+
140+ if not source_format :
141+ return output .write_line ("\n Unable to detect file type." )
142+ elif target_format == source_format :
143+ return output .write_line ("File already is %s." % loaders .get [source_format ].name )
144+
145+ # Load inline options
146+ Loader = loaders .get [source_format ]
147+ opts = Loader .load_options (self .view )
148+
149+ # Function to determine the new file extension depending on the target format
150+ def get_new_ext (target_format ):
151+ if ext :
152+ return '.' + ext
153+ if opts and 'ext' in opts :
154+ return '.' + opts ['ext' ]
155+ else :
156+ new_ext , prepend_target_format = Loader .get_new_file_ext (self .view )
157+ if prepend_target_format :
158+ new_ext = ".%s-%s" % (target_format .upper (), new_ext [1 :])
159+
160+ return (new_ext or '.' + target_format )
161+
162+ path_tuple = file_path_tuple (file_path ) # This is the latest point possible
163+
164+ if not target_format :
165+ output .write ("No target format specified, searching in file..." )
166+
167+ # No information about a target format; ask for it
168+ if not opts or 'target_format' not in opts :
169+ output .write (" Could not detect target format.\n "
170+ "Please select or define a target format..." )
171+
172+ # Show overlay with all dumping options except for the current type
173+ # Save stripped-down `items` for later
174+ options , items = [], []
175+ for itm in self .target_list :
176+ # To not clash with function-local "target_format"
177+ target_format_ = itm ['kwargs' ]['target_format' ]
178+ if target_format_ != source_format :
179+ options .append (["Convert to: %s" % itm ['name' ],
180+ path_tuple .base_name + get_new_ext (target_format_ )])
181+ items .append (itm )
182+
183+ def on_select (index ):
184+ if index < 0 or index >= len (items ):
185+ # canceled or other magic
186+ output .write_line ("\n \n Build canceled." )
187+ return
188+
189+ target = items [index ]
190+ output .write_line (' %s\n ' % target ['name' ])
191+
192+ kwargs .update (target ['kwargs' ])
193+ kwargs .update (dict (source_format = source_format , _output = output ))
194+ self .run (* args , ** kwargs )
195+
196+ # Forward all params to the new command call
197+ self .window .show_quick_panel (options , on_select )
198+ return
199+
200+ target_format = opts ['target_format' ]
201+ # Validate the shit again, but this time print to output panel
202+ if source_format is not None and target_format == source_format :
203+ return output .write_line ("\n Target and source file format are identical. (%s)" % target_format )
204+
205+ if target_format not in dumpers .get :
206+ return output .write_line ("\n %s for '%s' not supported/implemented." % ("Dumper" , target_format ))
207+
208+ output .write_line (' %s\n ' % dumpers .get [target_format ].name )
209+
210+ start_time = time .time ()
211+
212+ # Okay, THIS is where the building really starts
213+ # Note: loader or dumper errors are not caught in order to receive a nice traceback in the console
214+ loader_ = Loader (self .window , self .view , output = output )
215+ try :
216+ data = loader_ .load (* args , ** kwargs )
217+ except :
218+ output .write_line ("Unexpected error occured while parsing, "
219+ "please see the console for details." )
220+ raise
221+ if not data :
198222 return
199223
200- target_format = opts ['target_format' ]
201- # Validate the shit again, but this time print to output panel
202- if source_format is not None and target_format == source_format :
203- return output .write_line ("\n Target and source file format are identical. (%s)" % target_format )
204-
205- if not target_format in dumpers .get :
206- return output .write_line ("\n %s for '%s' not supported/implemented." % ("Dumper" , target_format ))
207-
208- output .write_line (' %s\n ' % dumpers .get [target_format ].name )
209-
210- start_time = time .time ()
211-
212- # Okay, THIS is where the building really starts
213- # Note: loader or dumper errors are not caught in order to receive a nice traceback in the console
214- loader = Loader (self .window , self .view , output = output )
215-
216- data = None
217- try :
218- data = loader .load (* args , ** kwargs )
219- except NotImplementedError as e :
220- # use NotImplementedError to make the handler report the message as it pleases
221- output .write_line (str (e ))
222- self .status (str (e ), file_path )
224+ # Determine new file name
225+ new_file_path = path_tuple .no_ext + get_new_ext (target_format )
226+ new_dir = os .path .dirname (new_file_path )
227+ if not os .path .exists (new_dir ):
228+ try :
229+ os .makedirs (new_dir )
230+ except OSError :
231+ output .write_line ("Could not create folder '%s'" % new_dir )
232+ return
223233
224- # Determine new file name
225- new_file_path = path_tuple .no_ext + get_new_ext (target_format )
226- new_dir = os .path .dirname (new_file_path )
227- if not os .path .exists (new_dir ):
228- try :
229- os .makedirs (new_dir )
230- except OSError :
231- output .write_line ("Could not create folder '%s'" % new_dir )
232-
233- if data and os .path .exists (new_dir ):
234234 # Now dump to new file
235-
236- # Init the Dumper
237235 dumper = dumpers .get [target_format ](self .window , self .view , new_file_path , output = output )
238- if dumper .dump (data , * args , ** kwargs ):
239- self .status ("File conversion successful. (%s -> %s)" % (source_format , target_format ))
236+ try :
237+ success = dumper .dump (data , * args , ** kwargs )
238+ except :
239+ output .write_line ("Unexpected error occured while dumping, "
240+ "please see the console for details." )
241+ raise
242+ self .status ("File conversion %ssuccessful. (%s -> %s)"
243+ % ("un" if success else "" , source_format , target_format ))
244+ if not success :
245+ return
240246
241- # Finish
242- output .write ("[Finished in %.3fs]" % (time .time () - start_time ))
243- output .finish ()
247+ # Finish
248+ output .write ("[Finished in %.3fs]" % (time .time () - start_time ))
244249
245- if open_new_file or rearrange_yaml_syntax_def :
246- new_view = self .window .open_file (new_file_path )
250+ if open_new_file or rearrange_yaml_syntax_def :
251+ new_view = self .window .open_file (new_file_path )
247252
248- if rearrange_yaml_syntax_def :
249- # For some reason, ST would still indicate the new buffer having "usaved changes"
250- # even though there aren't any (calling "save" command here).
251- new_view .run_command ("rearrange_yaml_syntax_def" , {"save" : True })
253+ if rearrange_yaml_syntax_def :
254+ # For some reason, ST would still indicate the new buffer having "usaved changes"
255+ # even though there aren't any (calling "save" command here).
256+ new_view .run_command ("rearrange_yaml_syntax_def" , {"save" : True })
252257
253258 def status (self , msg , file_path = None ):
254259 sublime .status_message (msg )
0 commit comments