| 1 |  |  | import subprocess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from coalib.results.Diff import Diff | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from coalib.results.Result import Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from coalib.results.result_actions.ResultAction import ResultAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | EDITOR_ARGS = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     "subl": "--wait", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     "gedit": "-s", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     "atom": "--wait" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | GUI_EDITORS = ["kate", "gedit", "subl", "atom"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class OpenEditorAction(ResultAction): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     SUCCESS_MESSAGE = "Changes saved successfully." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     def is_applicable(result, original_file_dict, file_diff_dict): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         return isinstance(result, Result) and len(result.affected_code) > 0 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |     def apply(self, result, original_file_dict, file_diff_dict, editor: str): | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |         ''' | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |         Open the affected file(s) in an editor. | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |         :param editor: The editor to open the file with. | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         ''' | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         # Use set to remove duplicates | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         filenames = set(src.file for src in result.affected_code) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |         editor_args = [editor] + list(filenames) | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |         arg = EDITOR_ARGS.get(editor.strip(), None) | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |         if arg: | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |             editor_args.append(arg) | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |         # Dear user, you wanted an editor, so you get it. But do you really | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |         # think you can do better than we? | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |         if editor in GUI_EDITORS: | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |             subprocess.call(editor_args, stdout=subprocess.PIPE) | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |         else: | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |             subprocess.call(editor_args) | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         for filename in filenames: | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |             with open(filename, encoding='utf-8') as file: | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |                 file_diff_dict[filename] = Diff.from_string_arrays( | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |                     original_file_dict[filename], file.readlines()) | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         return file_diff_dict | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  |