settings_registry_example.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import azlmbr.settingsregistry as SettingsRegistry
  9. import os
  10. ExampleTestFileSetreg = 'AutomatedTesting/Editor/Scripts/SettingsRegistry/example.file.setreg'
  11. ExampleTestFolderSetreg = 'AutomatedTesting/Editor/Scripts/SettingsRegistry'
  12. def test_settings_registry():
  13. # Access the Global Settings Registry and dump it to a string
  14. if SettingsRegistry.g_SettingsRegistry.IsValid():
  15. dumpedSettings = SettingsRegistry.g_SettingsRegistry.DumpSettings("")
  16. if dumpedSettings:
  17. print("Full Settings Registry dumped successfully\n{}", dumpedSettings.value())
  18. # Making a script local settings registry
  19. localSettingsRegistry = SettingsRegistry.SettingsRegistry()
  20. localSettingsRegistry.MergeSettings('''
  21. {
  22. "TestObject": {
  23. "boolValue": false,
  24. "intValue": 17,
  25. "floatValue": 32.0,
  26. "stringValue": "Hello World"
  27. }
  28. }''')
  29. registryVal = localSettingsRegistry.GetBool('/TestObject/boolValue')
  30. if registryVal:
  31. print(f"Bool value '{registryVal.value()}' found")
  32. registryVal = localSettingsRegistry.GetInt('/TestObject/intValue')
  33. if registryVal:
  34. print(f"Int value '{registryVal.value()}' found")
  35. registryVal = localSettingsRegistry.GetFloat('/TestObject/floatValue')
  36. if registryVal:
  37. print(f"Float value '{registryVal.value()}' found")
  38. registryVal = localSettingsRegistry.GetString('/TestObject/stringValue')
  39. if registryVal:
  40. print(f"String value '{registryVal.value()}' found")
  41. if localSettingsRegistry.SetBool('/TestObject/boolValue', True):
  42. registryVal = localSettingsRegistry.GetBool('/TestObject/boolValue')
  43. print(f"Bool value '{registryVal.value()}' set")
  44. if localSettingsRegistry.SetInt('/TestObject/intValue', 22):
  45. registryVal = localSettingsRegistry.GetInt('/TestObject/intValue')
  46. print(f"Int value '{registryVal.value()}' set")
  47. if localSettingsRegistry.SetFloat('/TestObject/floatValue', 16.0):
  48. registryVal = localSettingsRegistry.GetFloat('/TestObject/floatValue')
  49. print(f"Float value '{registryVal.value()}' set")
  50. if localSettingsRegistry.SetString('/TestObject/stringValue', 'Goodbye World'):
  51. registryVal = localSettingsRegistry.GetString('/TestObject/stringValue')
  52. print(f"String value '{registryVal.value()}' found")
  53. if localSettingsRegistry.RemoveKey('/TestObject/stringValue'):
  54. print("Key '/TestObject/stringValue' has been successfully removed")
  55. print("current working directory is {}".format(os.getcwd()))
  56. # Merge a Settings File using the JsonPatch format
  57. jsonPatchMerged = localSettingsRegistry.MergeSettings('''
  58. [
  59. { "op": "add", "path": "/TestObject", "value": {} },
  60. { "op": "add", "path": "/TestObject/boolValue", "value": false },
  61. { "op": "add", "path": "/TestObject/intValue", "value": 17 },
  62. { "op": "add", "path": "/TestObject/floatValue", "value": 32.0 },
  63. { "op": "add", "path": "/TestObject/stringValue", "value": "Hello World" },
  64. { "op": "add", "path": "/TestArray", "value": [] },
  65. { "op": "add", "path": "/TestArray/0", "value": { "intIndex": 3 } },
  66. { "op": "add", "path": "/TestArray/1", "value": { "intIndex": -55 } }
  67. ]''', SettingsRegistry.JsonPatch)
  68. if jsonPatchMerged:
  69. print("JSON in JSON Patch format has been merged successfully to the local settings registry")
  70. # Below is how the the MergeSettingsFile and MergeSettingsFolder could be used
  71. if localSettingsRegistry.MergeSettingsFile(ExampleTestFileSetreg):
  72. print(f"Successfully merged setreg file '{ExampleTestFileSetreg}' to local settings registry")
  73. registryVal = localSettingsRegistry.GetString('/AutomatedTesting/ScriptingTestArray/3')
  74. if registryVal:
  75. print(f"Settings Registry contains '/AutomatedTesting/ScriptingTestArray/3'='{registryVal.value()}' merged from the {ExampleTestFileSetreg}")
  76. # Add the 'folder' to the Settings Registry so that only non-specialized .setreg
  77. # and .setreg files which contains only a 'folder' tag are merged into the Setting Registry
  78. filetags = SettingsRegistry.Specializations()
  79. filetags.Append('folder')
  80. if localSettingsRegistry.MergeSettingsFolder(ExampleTestFolderSetreg, filetags):
  81. print(f"Successfully merged setreg folder '{ExampleTestFolderSetreg}' to local settings registry")
  82. registryVal = localSettingsRegistry.GetBool('/AutomatedTesting/Spectra/IsFolder')
  83. if registryVal:
  84. print(f"Settings Registry contains '/AutomatedTesting/Spectra/IsFolder'='{registryVal.value()}' merged from the {ExampleTestFolderSetreg} folder")
  85. # Invoke main function
  86. if __name__ == '__main__':
  87. test_settings_registry()