parameters: (register=True, lib_dir=None)
docstring: Add (or remove) Pythonwin to context menu for python scripts.
      ??? Should probably also add Edit command for pys files also.
      Also need to remove these keys on uninstall, but there's no function
          like file_created to add registry entries to uninstall log ???
docstring_summary: Add (or remove) Pythonwin to context menu for python scripts.
      ??? Should probably also add Edit command for pys files also.
      Also need to remove these keys on uninstall, but there's no function
          like file_created to add registry entries to uninstall log ???
docstring_tokens: Add ( or remove ) Pythonwin to context menu for python scripts . ??? Should probably also add Edit command for pys files also . Also need to remove these keys on uninstall but there s no function like file_created to add registry entries to uninstall log ???
function: def RegisterPythonwin(register=True, lib_dir=None):
      """Add (or remove) Pythonwin to context menu for python scripts.
      ??? Should probably also add Edit command for pys files also.
      Also need to remove these keys on uninstall, but there's no function
          like file_created to add registry entries to uninstall log ???
      """
      import os
  
      if lib_dir is None:
          lib_dir = sysconfig.get_paths()["platlib"]
      classes_root = get_root_hkey()
      ## Installer executable doesn't seem to pass anything to postinstall script indicating if it's a debug build,
      pythonwin_exe = os.path.join(lib_dir, "Pythonwin", "Pythonwin.exe")
      pythonwin_edit_command = pythonwin_exe + ' -edit "%1"'
  
      keys_vals = [
          (
              "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Pythonwin.exe",
              "",
              pythonwin_exe,
          ),
          (
              "Software\\Classes\\Python.File\\shell\\Edit with Pythonwin",
              "command",
              pythonwin_edit_command,
          ),
          (
              "Software\\Classes\\Python.NoConFile\\shell\\Edit with Pythonwin",
              "command",
              pythonwin_edit_command,
          ),
      ]
  
      try:
          if register:
              for key, sub_key, val in keys_vals:
                  ## Since winreg only uses the character Api functions, this can fail if Python
                  ##  is installed to a path containing non-ascii characters
                  hkey = winreg.CreateKey(classes_root, key)
                  if sub_key:
                      hkey = winreg.CreateKey(hkey, sub_key)
                  winreg.SetValueEx(hkey, None, 0, winreg.REG_SZ, val)
                  hkey.Close()
          else:
              for key, sub_key, val in keys_vals:
                  try:
                      if sub_key:
                          hkey = winreg.OpenKey(classes_root, key)
                          winreg.DeleteKey(hkey, sub_key)
                          hkey.Close()
                      winreg.DeleteKey(classes_root, key)
                  except OSError as why:
                      winerror = getattr(why, "winerror", why.errno)
                      if winerror != 2:  # file not found
                          raise
      finally:
          # tell windows about the change
          from win32com.shell import shell, shellcon
  
          shell.SHChangeNotify(
              shellcon.SHCNE_ASSOCCHANGED, shellcon.SHCNF_IDLIST, None, None
          )
function_tokens: def RegisterPythonwin ( register = True , lib_dir = None ) : import os if lib_dir is None : lib_dir = sysconfig . get_paths ( ) [ "platlib" ] classes_root = get_root_hkey ( ) ## Installer executable doesn't seem to pass anything to postinstall script indicating if it's a debug build, pythonwin_exe = os . path . join ( lib_dir , "Pythonwin" , "Pythonwin.exe" ) pythonwin_edit_command = pythonwin_exe + ' -edit "%1"' keys_vals = [ ( "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Pythonwin.exe" , "" , pythonwin_exe , ) , ( "Software\\Classes\\Python.File\\shell\\Edit with Pythonwin" , "command" , pythonwin_edit_command , ) , ( "Software\\Classes\\Python.NoConFile\\shell\\Edit with Pythonwin" , "command" , pythonwin_edit_command , ) , ] try : if register : for key , sub_key , val in keys_vals : ## Since winreg only uses the character Api functions, this can fail if Python ##  is installed to a path containing non-ascii characters hkey = winreg . CreateKey ( classes_root , key ) if sub_key : hkey = winreg . CreateKey ( hkey , sub_key ) winreg . SetValueEx ( hkey , None , 0 , winreg . REG_SZ , val ) hkey . Close ( ) else : for key , sub_key , val in keys_vals : try : if sub_key : hkey = winreg . OpenKey ( classes_root , key ) winreg . DeleteKey ( hkey , sub_key ) hkey . Close ( ) winreg . DeleteKey ( classes_root , key ) except OSError as why : winerror = getattr ( why , "winerror" , why . errno ) if winerror != 2 : # file not found raise finally : # tell windows about the change from win32com . shell import shell , shellcon shell . SHChangeNotify ( shellcon . SHCNE_ASSOCCHANGED , shellcon . SHCNF_IDLIST , None , None )