utils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import os
  5. import json
  6. import yaml
  7. from filelock import FileLock
  8. from datetime import datetime, timedelta, timezone
  9. from module.config.atomicwrites import atomic_write
  10. DEFAULT_TIME = datetime(2023, 1, 1, 0, 0)
  11. def filepath_config(filename, mod_name='script') -> str:
  12. """
  13. 返回配置文件的路径
  14. """
  15. if mod_name == 'script':
  16. return os.path.join('./config', f'{filename}.json')
  17. else:
  18. return os.path.join('./config', f'{filename}.{mod_name}.json')
  19. def filepath_args(filename='args', mod_name='alas'):
  20. return f'./module/config/argument/{filename}.json'
  21. def filepath_argument(filename):
  22. return f'./module/config/argument/{filename}.yaml'
  23. def read_file(file: str):
  24. """
  25. Read a file, support both .yaml and .json format.
  26. Return empty dict if file not exists.
  27. Args:
  28. file (str):
  29. Returns:
  30. dict, list:
  31. """
  32. folder = os.path.dirname(file)
  33. if not os.path.exists(folder):
  34. os.mkdir(folder)
  35. if not os.path.exists(file):
  36. return {}
  37. _, ext = os.path.splitext(file)
  38. lock = FileLock(f"{file}.lock")
  39. with lock:
  40. print(f'read: {file}')
  41. if ext == '.yaml':
  42. with open(file, mode='r', encoding='utf-8') as f:
  43. s = f.read()
  44. data = list(yaml.safe_load_all(s))
  45. if len(data) == 1:
  46. data = data[0]
  47. if not data:
  48. data = {}
  49. return data
  50. elif ext == '.json':
  51. with open(file, mode='r', encoding='utf-8') as f:
  52. s = f.read()
  53. return json.loads(s)
  54. else:
  55. print(f'Unsupported config file extension: {ext}')
  56. return {}
  57. def write_file(file: str, data):
  58. """
  59. Write data into a file, supports both .yaml and .json format.
  60. Args:
  61. file (str):
  62. data (dict, list):
  63. """
  64. folder = os.path.dirname(file)
  65. if not os.path.exists(folder):
  66. os.mkdir(folder)
  67. _, ext = os.path.splitext(file)
  68. lock = FileLock(f"{file}.lock")
  69. with lock:
  70. print(f'write: {file}')
  71. if ext == '.yaml':
  72. with atomic_write(file, overwrite=True, encoding='utf-8', newline='') as f:
  73. if isinstance(data, list):
  74. yaml.safe_dump_all(data, f, default_flow_style=False, encoding='utf-8', allow_unicode=True,
  75. sort_keys=False)
  76. else:
  77. yaml.safe_dump(data, f, default_flow_style=False, encoding='utf-8', allow_unicode=True,
  78. sort_keys=False)
  79. elif ext == '.json':
  80. with atomic_write(file, overwrite=True, encoding='utf-8', newline='') as f:
  81. s = json.dumps(data, indent=2, ensure_ascii=False, sort_keys=False, default=str)
  82. f.write(s)
  83. else:
  84. print(f'Unsupported config file extension: {ext}')
  85. def deep_iter(data, depth=0, current_depth=1):
  86. """
  87. Iter a dictionary safely.
  88. Args:
  89. data (dict):
  90. depth (int): Maximum depth to iter
  91. current_depth (int):
  92. Returns:
  93. list: Key path
  94. Any:
  95. """
  96. if isinstance(data, dict) \
  97. and (depth and current_depth <= depth):
  98. for key, value in data.items():
  99. for child_path, child_value in deep_iter(value, depth=depth, current_depth=current_depth + 1):
  100. yield [key] + child_path, child_value
  101. else:
  102. yield [], data