You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

file_utils.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. import re
  19. from cachetools import LRUCache, cached
  20. from ruamel.yaml import YAML
  21. from web_server.db import FileType
  22. PROJECT_BASE = os.getenv("RAG_PROJECT_BASE") or os.getenv("RAG_DEPLOY_BASE")
  23. FATE_BASE = os.getenv("RAG_BASE")
  24. def get_project_base_directory(*args):
  25. global PROJECT_BASE
  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. )
  33. )
  34. if args:
  35. return os.path.join(PROJECT_BASE, *args)
  36. return PROJECT_BASE
  37. def get_fate_directory(*args):
  38. global FATE_BASE
  39. if FATE_BASE is None:
  40. FATE_BASE = os.path.abspath(
  41. os.path.join(
  42. os.path.dirname(os.path.realpath(__file__)),
  43. os.pardir,
  44. os.pardir,
  45. os.pardir,
  46. )
  47. )
  48. if args:
  49. return os.path.join(FATE_BASE, *args)
  50. return FATE_BASE
  51. def get_fate_python_directory(*args):
  52. return get_fate_directory("python", *args)
  53. @cached(cache=LRUCache(maxsize=10))
  54. def load_json_conf(conf_path):
  55. if os.path.isabs(conf_path):
  56. json_conf_path = conf_path
  57. else:
  58. json_conf_path = os.path.join(get_project_base_directory(), conf_path)
  59. try:
  60. with open(json_conf_path) as f:
  61. return json.load(f)
  62. except BaseException:
  63. raise EnvironmentError(
  64. "loading json file config from '{}' failed!".format(json_conf_path)
  65. )
  66. def dump_json_conf(config_data, conf_path):
  67. if os.path.isabs(conf_path):
  68. json_conf_path = conf_path
  69. else:
  70. json_conf_path = os.path.join(get_project_base_directory(), conf_path)
  71. try:
  72. with open(json_conf_path, "w") as f:
  73. json.dump(config_data, f, indent=4)
  74. except BaseException:
  75. raise EnvironmentError(
  76. "loading json file config from '{}' failed!".format(json_conf_path)
  77. )
  78. def load_json_conf_real_time(conf_path):
  79. if os.path.isabs(conf_path):
  80. json_conf_path = conf_path
  81. else:
  82. json_conf_path = os.path.join(get_project_base_directory(), conf_path)
  83. try:
  84. with open(json_conf_path) as f:
  85. return json.load(f)
  86. except BaseException:
  87. raise EnvironmentError(
  88. "loading json file config from '{}' failed!".format(json_conf_path)
  89. )
  90. def load_yaml_conf(conf_path):
  91. if not os.path.isabs(conf_path):
  92. conf_path = os.path.join(get_project_base_directory(), conf_path)
  93. try:
  94. with open(conf_path) as f:
  95. yaml = YAML(typ='safe', pure=True)
  96. return yaml.load(f)
  97. except Exception as e:
  98. raise EnvironmentError(
  99. "loading yaml file config from {} failed:".format(conf_path), e
  100. )
  101. def rewrite_yaml_conf(conf_path, config):
  102. if not os.path.isabs(conf_path):
  103. conf_path = os.path.join(get_project_base_directory(), conf_path)
  104. try:
  105. with open(conf_path, "w") as f:
  106. yaml = YAML(typ="safe")
  107. yaml.dump(config, f)
  108. except Exception as e:
  109. raise EnvironmentError(
  110. "rewrite yaml file config {} failed:".format(conf_path), e
  111. )
  112. def rewrite_json_file(filepath, json_data):
  113. with open(filepath, "w") as f:
  114. json.dump(json_data, f, indent=4, separators=(",", ": "))
  115. f.close()
  116. def filename_type(filename):
  117. filename = filename.lower()
  118. if re.match(r".*\.pdf$", filename):
  119. return FileType.PDF.value
  120. if re.match(r".*\.(doc|ppt|yml|xml|htm|json|csv|txt|ini|xsl|wps|rtf|hlp|pages|numbers|key|md)$", filename):
  121. return FileType.DOC.value
  122. if re.match(r".*\.(wav|flac|ape|alac|wavpack|wv|mp3|aac|ogg|vorbis|opus|mp3)$", filename):
  123. return FileType.AURAL.value
  124. if re.match(r".*\.(jpg|jpeg|png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|avif|apng|icon|ico|mpg|mpeg|avi|rm|rmvb|mov|wmv|asf|dat|asx|wvx|mpe|mpa|mp4)$", filename):
  125. return FileType.VISUAL