import sublime
import sublime_plugin

import os
from os import path

from .src.build import build_yaml_macros

class BuildYamlMacrosCommand(sublime_plugin.WindowCommand):
    def run(self, working_dir=None):
        if working_dir:
            os.chdir(working_dir)

        view = self.window.active_view();
        source_path = view.file_name()

        output_path, extension = path.splitext(source_path)

        if extension != '.yaml-macros': raise "Not a .yaml-macros file!"

        output_file = open(output_path, 'w')

        build_yaml_macros(
            view.substr( sublime.Region(0, view.size()) ),
            output_file,
            {
                "file_path": source_path
            },
        )

import sublime
import sublime_plugin

import os
from os import path

from .src.build import build_yaml_macros

class BuildYamlMacrosCommand(sublime_plugin.WindowCommand):
    def run(self, working_dir=None):
        if working_dir:
            os.chdir(working_dir)

        view = self.window.active_view();
        source_path = view.file_name()

        output_path, extension = path.splitext(source_path)

        if extension != '.yaml-macros': raise "Not a .yaml-macros file!"

        with open(output_path, 'w') as output_file:

            build_yaml_macros(
                view.substr( sublime.Region(0, view.size()) ),
                output_file,
                {
                    "file_path": source_path
                },
            )

Use with context manager to handle file access
Use with context manager to handle file access

Currently, after the build completes the output file is not closed, and so it remains locked and is unable to be edited by other processes.
  import sublime
  import sublime_plugin
  
  import os
  from os import path
  
  from .src.build import build_yaml_macros
  
  class BuildYamlMacrosCommand(sublime_plugin.WindowCommand):
      def run(self, working_dir=None):
          if working_dir:
              os.chdir(working_dir)
  
          view = self.window.active_view();
          source_path = view.file_name()
  
          output_path, extension = path.splitext(source_path)
  
          if extension != '.yaml-macros': raise "Not a .yaml-macros file!"
  
-         output_file = open(output_path, 'w')
+         with open(output_path, 'w') as output_file:
  
-         build_yaml_macros(
+             build_yaml_macros(
-             view.substr( sublime.Region(0, view.size()) ),
+                 view.substr( sublime.Region(0, view.size()) ),
-             output_file,
+                 output_file,
-             {
+                 {
-                 "file_path": source_path
+                     "file_path": source_path
+                 },
-             },
+             )
-         )
  
Use with context manager to handle file access
## Code Before:
import sublime
import sublime_plugin

import os
from os import path

from .src.build import build_yaml_macros

class BuildYamlMacrosCommand(sublime_plugin.WindowCommand):
    def run(self, working_dir=None):
        if working_dir:
            os.chdir(working_dir)

        view = self.window.active_view();
        source_path = view.file_name()

        output_path, extension = path.splitext(source_path)

        if extension != '.yaml-macros': raise "Not a .yaml-macros file!"

        output_file = open(output_path, 'w')

        build_yaml_macros(
            view.substr( sublime.Region(0, view.size()) ),
            output_file,
            {
                "file_path": source_path
            },
        )

## Instruction:
Use with context manager to handle file access
## Code After:
import sublime
import sublime_plugin

import os
from os import path

from .src.build import build_yaml_macros

class BuildYamlMacrosCommand(sublime_plugin.WindowCommand):
    def run(self, working_dir=None):
        if working_dir:
            os.chdir(working_dir)

        view = self.window.active_view();
        source_path = view.file_name()

        output_path, extension = path.splitext(source_path)

        if extension != '.yaml-macros': raise "Not a .yaml-macros file!"

        with open(output_path, 'w') as output_file:

            build_yaml_macros(
                view.substr( sublime.Region(0, view.size()) ),
                output_file,
                {
                    "file_path": source_path
                },
            )
