transformation.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. def render_yaml(yaml_root, prefix=""):
  20. result = ""
  21. if isinstance(yaml_root, dict):
  22. if len(prefix)>0:
  23. result +="\n"
  24. for key in yaml_root:
  25. result += "{}{}: {}".format(prefix, key, render_yaml(yaml_root[key], prefix + " "))
  26. elif isinstance(yaml_root, list):
  27. result += "\n"
  28. for item in yaml_root:
  29. result += prefix + " - " + render_yaml(item, prefix + " ")
  30. else:
  31. result += "{}\n".format(yaml_root)
  32. return result
  33. def to_yaml(content):
  34. props = process_properties(content)
  35. keys = props.keys()
  36. yaml_props = {}
  37. for key in keys:
  38. parts = key.split(".")
  39. node = yaml_props
  40. prev_part = None
  41. parent_node = None
  42. for part in parts[:-1]:
  43. if part.isdigit():
  44. if isinstance(node, dict):
  45. parent_node[prev_part] = []
  46. node = parent_node[prev_part]
  47. while len(node) <= int(part):
  48. node.append({})
  49. parent_node = node
  50. node = node[int(node)]
  51. else:
  52. if part not in node:
  53. node[part] = {}
  54. parent_node = node
  55. node = node[part]
  56. prev_part = part
  57. if parts[-1].isdigit():
  58. if isinstance(node, dict):
  59. parent_node[prev_part] = []
  60. node = parent_node[prev_part]
  61. node.append(props[key])
  62. else:
  63. node[parts[-1]] = props[key]
  64. return render_yaml(yaml_props)
  65. def to_yml(content):
  66. return to_yaml(content)
  67. def to_properties(content):
  68. result = ""
  69. props = process_properties(content)
  70. for key in props.keys():
  71. result += "{}: {}\n".format(key, props[key])
  72. return result
  73. def to_env(content):
  74. result = ""
  75. props = process_properties(content)
  76. for key in props.keys():
  77. result += "{}={}\n".format(key, props[key])
  78. return result
  79. def to_sh(content):
  80. result = ""
  81. props = process_properties(content)
  82. for key in props.keys():
  83. result += "export {}=\"{}\"\n".format(key, props[key])
  84. return result
  85. def to_cfg(content):
  86. result = ""
  87. props = process_properties(content)
  88. for key in props.keys():
  89. result += "{}={}\n".format(key, props[key])
  90. return result
  91. def to_conf(content):
  92. result = ""
  93. props = process_properties(content)
  94. for key in props.keys():
  95. result += "export {}={}\n".format(key, props[key])
  96. return result
  97. def to_xml(content):
  98. result = "<configuration>\n"
  99. props = process_properties(content)
  100. for key in props.keys():
  101. result += "<property><name>{0}</name><value>{1}</value></property>\n".format(key, props[key])
  102. result += "</configuration>"
  103. return result
  104. def process_properties(content, sep=': ', comment_char='#'):
  105. """
  106. Read the file passed as parameter as a properties file.
  107. """
  108. props = {}
  109. for line in content.split("\n"):
  110. l = line.strip()
  111. if l and not l.startswith(comment_char):
  112. key_value = l.split(sep)
  113. key = key_value[0].strip()
  114. value = sep.join(key_value[1:]).strip().strip('"')
  115. props[key] = value
  116. return props