transformation.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/python
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. """This module transform properties into different format"""
  20. def render_yaml(yaml_root, prefix=""):
  21. """render yaml"""
  22. result = ""
  23. if isinstance(yaml_root, dict):
  24. if prefix:
  25. result += "\n"
  26. for key in yaml_root:
  27. result += "{}{}: {}".format(prefix, key, render_yaml(
  28. yaml_root[key], prefix + " "))
  29. elif isinstance(yaml_root, list):
  30. result += "\n"
  31. for item in yaml_root:
  32. result += prefix + " - " + render_yaml(item, prefix + " ")
  33. else:
  34. result += "{}\n".format(yaml_root)
  35. return result
  36. def to_yaml(content):
  37. """transform to yaml"""
  38. props = process_properties(content)
  39. keys = props.keys()
  40. yaml_props = {}
  41. for key in keys:
  42. parts = key.split(".")
  43. node = yaml_props
  44. prev_part = None
  45. parent_node = {}
  46. for part in parts[:-1]:
  47. if part.isdigit():
  48. if isinstance(node, dict):
  49. parent_node[prev_part] = []
  50. node = parent_node[prev_part]
  51. while len(node) <= int(part):
  52. node.append({})
  53. parent_node = node
  54. node = node[int(node)]
  55. else:
  56. if part not in node:
  57. node[part] = {}
  58. parent_node = node
  59. node = node[part]
  60. prev_part = part
  61. if parts[-1].isdigit():
  62. if isinstance(node, dict):
  63. parent_node[prev_part] = []
  64. node = parent_node[prev_part]
  65. node.append(props[key])
  66. else:
  67. node[parts[-1]] = props[key]
  68. return render_yaml(yaml_props)
  69. def to_yml(content):
  70. """transform to yml"""
  71. return to_yaml(content)
  72. def to_properties(content):
  73. """transform to properties"""
  74. result = ""
  75. props = process_properties(content)
  76. for key, val in props.items():
  77. result += "{}: {}\n".format(key, val)
  78. return result
  79. def to_env(content):
  80. """transform to environment variables"""
  81. result = ""
  82. props = process_properties(content)
  83. for key, val in props:
  84. result += "{}={}\n".format(key, val)
  85. return result
  86. def to_sh(content):
  87. """transform to shell"""
  88. result = ""
  89. props = process_properties(content)
  90. for key, val in props:
  91. result += "export {}=\"{}\"\n".format(key, val)
  92. return result
  93. def to_cfg(content):
  94. """transform to config"""
  95. result = ""
  96. props = process_properties(content)
  97. for key, val in props:
  98. result += "{}={}\n".format(key, val)
  99. return result
  100. def to_conf(content):
  101. """transform to configuration"""
  102. result = ""
  103. props = process_properties(content)
  104. for key, val in props:
  105. result += "export {}={}\n".format(key, val)
  106. return result
  107. def to_xml(content):
  108. """transform to xml"""
  109. result = "<configuration>\n"
  110. props = process_properties(content)
  111. for key in props:
  112. result += "<property><name>{0}</name><value>{1}</value></property>\n". \
  113. format(key, props[key])
  114. result += "</configuration>"
  115. return result
  116. def process_properties(content, sep=': ', comment_char='#'):
  117. """
  118. Read the file passed as parameter as a properties file.
  119. """
  120. props = {}
  121. for line in content.split("\n"):
  122. sline = line.strip()
  123. if sline and not sline.startswith(comment_char):
  124. key_value = sline.split(sep)
  125. key = key_value[0].strip()
  126. value = sep.join(key_value[1:]).strip().strip('"')
  127. props[key] = value
  128. return props