registry_utils.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """
  2. All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. its licensors.
  4. For complete copyright and license terms please see the LICENSE at the root of this
  5. distribution (the "License"). All use of this software is governed by the License,
  6. or, if provided, by the license below or the license accompanying this file. Do not
  7. remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. """
  10. import logging
  11. import winreg
  12. logger = logging.getLogger(__name__)
  13. LUMBERYARD_SETTINGS_PATH = r'Software\Amazon\Lumberyard\Settings'
  14. def set_ly_registry_value(reg_path, value_name, new_value, value_type=winreg.REG_DWORD):
  15. """
  16. Sets the specified value for the specified value_name in the LY registry key.
  17. :param reg_path: A string that identifies the registry path to the desired key (e.g. Software\Amazon\Lumberyard\Settings)
  18. :param value_name: A string that identifies the value name (e.g. UndoLevels, ViewportInteractionModel)
  19. :param new_value: Value to set on the specified value_name
  20. :param value_type: The type of value set. Defaults to a 32-bit number.
  21. :return: None
  22. """
  23. # Open LY Registry key
  24. try:
  25. key = winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, reg_path, 0, access=winreg.KEY_ALL_ACCESS)
  26. except OSError as err:
  27. logger.error(err)
  28. # Set value_name to the specified value
  29. winreg.SetValueEx(key, value_name, 0, value_type, new_value)
  30. # Verify the value set to the specified value_name
  31. value = winreg.QueryValueEx(key, value_name)
  32. if new_value == value[0]:
  33. logger.debug(f'Successfully set {value_name} to {value[0]}')
  34. else:
  35. logger.debug(f'Failed to set {value_name} to {new_value}. Current value is {value[0]}.')
  36. def get_ly_registry_value(reg_path, value_name):
  37. """
  38. Gets the current value for an existing value_name in the LY registry key.
  39. :param reg_path: A string that identifies the registry path to the desired key (e.g. Software\Amazon\Lumberyard\Settings)
  40. :param value_name: A string that identifies the value name (e.g. UndoLevels, ViewportInteractionModel)
  41. :return: Value set for the specified value_name
  42. """
  43. # Open LY Registry key
  44. try:
  45. key = winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, reg_path, 0, access=winreg.KEY_ALL_ACCESS)
  46. except OSError as err:
  47. logger.error(err)
  48. # Check if value name is present and return current value
  49. try:
  50. value_name_value = winreg.QueryValueEx(key, value_name)
  51. logger.debug(f'{value_name} is {value_name_value[0]}')
  52. return value_name_value[0]
  53. except OSError as err:
  54. logger.error(err)
  55. def delete_ly_registry_value(reg_path, value_name):
  56. """
  57. Deletes the specific registry value_name found in the reg_path key.
  58. :param reg_path: A string that identifies the registry path to the desired key (e.g. Software\Amazon\Lumberyard\Settings)
  59. :param value_name: A string that identifies the value name (e.g. UndoLevels, ViewportInteractionModel)
  60. :return: None
  61. """
  62. # Open LY Registry key
  63. try:
  64. key = winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, reg_path, 0, access=winreg.KEY_ALL_ACCESS)
  65. except OSError as err:
  66. logger.error(err)
  67. # Attempt to delete the specified key/value
  68. try:
  69. winreg.DeleteValue(key, value_name)
  70. except OSError as err:
  71. logger.error(err)