Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR
  2. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
  3. # FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS.
  4.  
  5. from java.util import HashSet
  6. from java.util import Properties
  7. from java.io import FileReader
  8. from java.io import FileWriter
  9.  
  10. def loadProps(propertiesFile):
  11. props = Properties()
  12. reader = FileReader(propertiesFile)
  13. try:
  14. props.load(reader)
  15. except:
  16. print 'ERROR: Unable to load properties from', propertiesFile
  17. finally:
  18. reader.close()
  19. return props
  20.  
  21. def saveProps(props, targetFile, comment=""):
  22. writer = FileWriter(targetFile)
  23. try:
  24. props.store(writer, comment)
  25. except IOError, Arg:
  26. print Arg.getMessage()
  27. print 'ERROR: Unable to save properties to', targetFile
  28. finally:
  29. writer.close()
  30. return
  31.  
  32. def importIntoDict(propertiesFile, dictName, overwriteExisting=False):
  33. try:
  34. dict = repository.read('Environments/' + dictName)
  35. except:
  36. print "ERROR: Unknown Dictionary '%s'. Please verify that an item with ID 'Environments/%s' exists in the repository" % (dictName, dictName)
  37. return
  38. props = loadProps(propertiesFile)
  39. if not props:
  40. print "ERROR: No properties loaded"
  41. return
  42. # copy, no live view
  43. existingKeys = HashSet(dict.entries.keySet())
  44. for prop in props.entrySet():
  45. key = prop.key
  46. if key in existingKeys:
  47. if overwriteExisting:
  48. print "Overwriting existing value for key '%s'." % (key)
  49. dict.entries[key] = prop.value
  50. else:
  51. print "Dictionary already contains key '%s'. Skipping." % (key)
  52. else:
  53. dict.entries[key] = prop.value
  54. print "Saving updated dictionary", dictName
  55. repository.update(dict)
  56. return dict
  57.  
  58. def exportFromDict(dictName, targetFile):
  59. try:
  60. dict = repository.read('Environments/' + dictName)
  61. except:
  62. print "ERROR: Unknown Dictionary '%s'. Please verify that an item with ID 'Environments/%s' exists in the repository" % (dictName, dictName)
  63. return
  64. props = Properties()
  65. for entry in dict.entries.entrySet():
  66. props.put(entry.key, entry.value)
  67. saveProps(props, targetFile, "Export of " + dictName)
  68. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement