file_utils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. import os
  18. from cachetools import LRUCache, cached
  19. from ruamel import yaml
  20. PROJECT_BASE = os.getenv("FATE_PROJECT_BASE") or os.getenv("FATE_DEPLOY_BASE")
  21. FATE_BASE = os.getenv("FATE_BASE")
  22. READTHEDOC = os.getenv("READTHEDOC")
  23. def get_project_base_directory(*args):
  24. global PROJECT_BASE
  25. global READTHEDOC
  26. if PROJECT_BASE is None:
  27. PROJECT_BASE = os.path.abspath(
  28. os.path.join(
  29. os.path.dirname(os.path.realpath(__file__)),
  30. os.pardir,
  31. os.pardir,
  32. os.pardir,
  33. )
  34. )
  35. if READTHEDOC is None:
  36. PROJECT_BASE = os.path.abspath(
  37. os.path.join(
  38. PROJECT_BASE,
  39. os.pardir,
  40. )
  41. )
  42. if args:
  43. return os.path.join(PROJECT_BASE, *args)
  44. return PROJECT_BASE
  45. def get_fate_directory(*args):
  46. global FATE_BASE
  47. if FATE_BASE is None:
  48. FATE_BASE = os.path.abspath(
  49. os.path.join(
  50. os.path.dirname(os.path.realpath(__file__)),
  51. os.pardir,
  52. os.pardir,
  53. os.pardir,
  54. )
  55. )
  56. if args:
  57. return os.path.join(FATE_BASE, *args)
  58. return FATE_BASE
  59. def get_fate_python_directory(*args):
  60. return get_fate_directory("python", *args)
  61. def get_federatedml_setting_conf_directory():
  62. return os.path.join(get_fate_python_directory(), 'federatedml', 'conf', 'setting_conf')
  63. @cached(cache=LRUCache(maxsize=10))
  64. def load_json_conf(conf_path):
  65. if os.path.isabs(conf_path):
  66. json_conf_path = conf_path
  67. else:
  68. json_conf_path = os.path.join(get_project_base_directory(), conf_path)
  69. try:
  70. with open(json_conf_path) as f:
  71. return json.load(f)
  72. except BaseException:
  73. raise EnvironmentError(
  74. "loading json file config from '{}' failed!".format(json_conf_path)
  75. )
  76. def dump_json_conf(config_data, conf_path):
  77. if os.path.isabs(conf_path):
  78. json_conf_path = conf_path
  79. else:
  80. json_conf_path = os.path.join(get_project_base_directory(), conf_path)
  81. try:
  82. with open(json_conf_path, "w") as f:
  83. json.dump(config_data, f, indent=4)
  84. except BaseException:
  85. raise EnvironmentError(
  86. "loading json file config from '{}' failed!".format(json_conf_path)
  87. )
  88. def load_json_conf_real_time(conf_path):
  89. if os.path.isabs(conf_path):
  90. json_conf_path = conf_path
  91. else:
  92. json_conf_path = os.path.join(get_project_base_directory(), conf_path)
  93. try:
  94. with open(json_conf_path) as f:
  95. return json.load(f)
  96. except BaseException:
  97. raise EnvironmentError(
  98. "loading json file config from '{}' failed!".format(json_conf_path)
  99. )
  100. def load_yaml_conf(conf_path):
  101. if not os.path.isabs(conf_path):
  102. conf_path = os.path.join(get_project_base_directory(), conf_path)
  103. try:
  104. with open(conf_path) as f:
  105. return yaml.safe_load(f)
  106. except Exception as e:
  107. raise EnvironmentError(
  108. "loading yaml file config from {} failed:".format(conf_path), e
  109. )
  110. def rewrite_yaml_conf(conf_path, config):
  111. if not os.path.isabs(conf_path):
  112. conf_path = os.path.join(get_project_base_directory(), conf_path)
  113. try:
  114. with open(conf_path, "w") as f:
  115. yaml.dump(config, f, Dumper=yaml.RoundTripDumper)
  116. except Exception as e:
  117. raise EnvironmentError(
  118. "rewrite yaml file config {} failed:".format(conf_path), e
  119. )
  120. def rewrite_json_file(filepath, json_data):
  121. with open(filepath, "w") as f:
  122. json.dump(json_data, f, indent=4, separators=(",", ": "))
  123. f.close()