ParseMapping.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Sep 20 15:33:17 2019
  5. @author: tanya
  6. """
  7. import os
  8. import sys
  9. import numpy as np
  10. sys.path.append(os.getcwd())
  11. class ParseMapping:
  12. '''
  13. '''
  14. def __init__(self, mapping_path: str, log_name: str = "ParseMapping",
  15. source: str = "original_name", target: str = "original_name"):
  16. '''
  17. '''
  18. import json
  19. from libraries.log import Log
  20. self._log = Log(log_name)
  21. if not os.path.isfile(mapping_path):
  22. err = "Mapping not found"
  23. self._log.error(err)
  24. raise FileNotFoundError(err)
  25. try:
  26. with open(mapping_path, "r") as f:
  27. self._mapping = json.load(f)
  28. except Exception as e:
  29. err = ("Could not load mapping. "
  30. "Exit with error {}".format(e))
  31. self._log.error(err)
  32. raise Exception(err)
  33. self._source = source
  34. self._target = target
  35. def get_field_mapping(self) -> dict:
  36. '''
  37. '''
  38. assert(all([set([self._source, self._target]) <= set(d)
  39. for d in self._mapping]))
  40. return {d[self._source]: d[self._target] for d in self._mapping}
  41. def _get_fields_satistisfying_condition(self, key: str, value) -> list:
  42. '''
  43. '''
  44. assert(all([self._source in d for d in self._mapping])),\
  45. "Invalid from field"
  46. return [d[self._source] for d in self._mapping
  47. if (key in d) and (d[key] == value)]
  48. def get_required_fields(self) -> list:
  49. '''
  50. '''
  51. return self._get_fields_satistisfying_condition(key="required",
  52. value=1)
  53. def get_date_fields(self) -> list:
  54. '''
  55. '''
  56. return self._get_fields_satistisfying_condition(key="type",
  57. value="Date")
  58. def _get_info(self, key: str, value=None) -> dict:
  59. '''
  60. '''
  61. assert(all([self._source in d for d in self._mapping])),\
  62. "Invalid from field"
  63. return {d[self._source]: d[key] for d in self._mapping
  64. if (key in d) and ((value is not None)
  65. and (d[key] == value)) or (key in d)}
  66. def get_default_values(self) -> dict:
  67. '''
  68. '''
  69. return self._get_info(key="default_values")
  70. def get_date_formats(self) -> dict:
  71. '''
  72. '''
  73. return self._get_info(key="date_format")
  74. def get_types(self) -> dict:
  75. '''
  76. '''
  77. return self._get_info(key="type")
  78. def get_python_types(self) -> dict:
  79. '''
  80. '''
  81. sql_to_python_dtypes = {
  82. "Text": str,
  83. "Date": np.dtype('<M8[ns]'),
  84. "Double": float,
  85. "Integer": int
  86. }
  87. sql_types = self.get_types()
  88. return {k: sql_to_python_dtypes[v] for k, v in sql_types.items()}
  89. def get_value_mappings(self) -> dict:
  90. '''
  91. '''
  92. return self._get_info(key="value_mapping")
  93. def get_column_numbers(self) -> list:
  94. '''
  95. '''
  96. if all(["column_number" in d for d in self._mapping]):
  97. column_numbers = [d["column_number"] for d in self._mapping]
  98. elif all(["column_number" not in d for d in self._mapping]):
  99. column_numbers = list(range(len(self._mapping)))
  100. else:
  101. err = ("Incorrectly filled mapping. Column numbers should ",
  102. "either in all or in neither of the fields")
  103. self.log.err(err)
  104. raise Exception(err)
  105. return column_numbers
  106. if __name__ == "__main__":
  107. mapping_path = os.path.join(".", "migration_mappings", "rs0_mapping.json")
  108. if os.path.isfile(mapping_path):
  109. print("found mapping path")
  110. parser = ParseMapping(mapping_path, source="internal_name",
  111. target="mongo_name")
  112. internal_to_mongo_mapping = parser.get_field_mapping()
  113. original_to_internal_mapping = parser.get_field_mapping()
  114. default_values = parser.get_default_values()
  115. types = parser.get_types()
  116. column_numbers = parser.get_column_numbers()
  117. print("Done testing!")