Skip to content

Instantly share code, notes, and snippets.

@khaosans
Created July 25, 2016 23:32
Show Gist options
  • Save khaosans/d75041b2d54c875340823b436a731f8d to your computer and use it in GitHub Desktop.
Save khaosans/d75041b2d54c875340823b436a731f8d to your computer and use it in GitHub Desktop.
parser
import xml.etree.ElementTree as ET
def main():
configs = {'in-memory-format': 'BINARY',
'backup-count': '1',
'eviction-policy': 'LRU',
'eviction-percentage': '25',
'min-eviction-check-millis': '100',
'merge-policy': 'com.hazelcast.map.merge.PutIfAbsentMapMergePolicy'}
tree = ET.parse('hazelcast.xml')
root = tree.getroot()
elem = root.find('map/indexes')
caches = ['backingRelationshipStatsCache']
indexes = ['tenant2', 'userId2', 'projectId2', 'orgId2']
map_xml = root
add_map('backingrel', map_xml, indexes, configs)
indent(root)
ET.dump(root)
def add_map(cache_name, map_xml, indexes, configs):
map_element = ET.Element('map', attrib={'name': cache_name})
for k, v in configs.items():
add_map_configs(map_element, k, v)
add_map_configs(map_element, 'max-size', '90', attrib={'policy': 'USED_HEAP_PERCENTAGE'})
add_map_configs(map_element, 'max-idle-seconds', '300')
map_xml.append(map_element)
for index in indexes:
index_element = ET.Element('index', attrib={'ordered': 'false'})
index_element.text = index
map_element.append(index_element)
# print ET.dump(root)
def add_map_configs(parent, key, value, attrib={}):
config = ET.SubElement(parent, key, attrib=attrib)
config.text = value
def indent(elem, level=0):
i = "\n" + level * " "
j = "\n" + (level - 1) * " "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for subelem in elem:
indent(subelem, level + 1)
if not elem.tail or not elem.tail.strip():
elem.tail = j
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = j
return elem
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment