Last active
August 29, 2015 14:16
-
-
Save xlcommunity/418b4fd7ada6e8849551 to your computer and use it in GitHub Desktop.
Importing a properties file into a dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS | |
# FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS. | |
from java.util import HashSet | |
from java.util import Properties | |
from java.io import FileReader | |
def loadProps(propertiesFile): | |
props = Properties() | |
reader = FileReader(propertiesFile) | |
try: | |
props.load(reader) | |
except: | |
print 'Unable to load properties from', propertiesFile | |
finally: | |
reader.close() | |
return props | |
def loadIntoDict(propertiesFile, dictName, overwriteExisting=False): | |
try: | |
dict = repository.read('Environments/' + dictName) | |
except: | |
print "Unknown Dictionary '%s'. Please verify that an item with ID 'Environments/%s' exists in the repository" % (dictName, dictName) | |
return | |
props = loadProps(propertiesFile) | |
if not props: | |
print "No properties loaded" | |
return | |
# copy, no live view | |
existingKeys = HashSet(dict.values['entries'].keySet()) | |
for prop in props.entrySet(): | |
key = prop.key | |
if key in existingKeys: | |
if overwriteExisting: | |
print "Overwriting existing value for key '%s'." % (key) | |
dict.values['entries'][key] = prop.value | |
else: | |
print "Dictionary already contains key '%s'. Skipping." % (key) | |
else: | |
dict.values['entries'][key] = prop.value | |
print "Saving updated dictionary", dictName | |
repository.update(dict) | |
return dict | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment