utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 read_file(file: str):
  20. """
  21. Read a file, support both .yaml and .json format.
  22. Return empty dict if file not exists.
  23. Args:
  24. file (str):
  25. Returns:
  26. dict, list:
  27. """
  28. folder = os.path.dirname(file)
  29. if not os.path.exists(folder):
  30. os.mkdir(folder)
  31. if not os.path.exists(file):
  32. return {}
  33. _, ext = os.path.splitext(file)
  34. lock = FileLock(f"{file}.lock")
  35. with lock:
  36. print(f'read: {file}')
  37. if ext == '.yaml':
  38. with open(file, mode='r', encoding='utf-8') as f:
  39. s = f.read()
  40. data = list(yaml.safe_load_all(s))
  41. if len(data) == 1:
  42. data = data[0]
  43. if not data:
  44. data = {}
  45. return data
  46. elif ext == '.json':
  47. with open(file, mode='r', encoding='utf-8') as f:
  48. s = f.read()
  49. return json.loads(s)
  50. else:
  51. print(f'Unsupported config file extension: {ext}')
  52. return {}
  53. def write_file(file: str, data):
  54. """
  55. Write data into a file, supports both .yaml and .json format.
  56. Args:
  57. file (str):
  58. data (dict, list):
  59. """
  60. folder = os.path.dirname(file)
  61. if not os.path.exists(folder):
  62. os.mkdir(folder)
  63. _, ext = os.path.splitext(file)
  64. lock = FileLock(f"{file}.lock")
  65. with lock:
  66. print(f'write: {file}')
  67. if ext == '.yaml':
  68. with atomic_write(file, overwrite=True, encoding='utf-8', newline='') as f:
  69. if isinstance(data, list):
  70. yaml.safe_dump_all(data, f, default_flow_style=False, encoding='utf-8', allow_unicode=True,
  71. sort_keys=False)
  72. else:
  73. yaml.safe_dump(data, f, default_flow_style=False, encoding='utf-8', allow_unicode=True,
  74. sort_keys=False)
  75. elif ext == '.json':
  76. with atomic_write(file, overwrite=True, encoding='utf-8', newline='') as f:
  77. s = json.dumps(data, indent=2, ensure_ascii=False, sort_keys=False, default=str)
  78. f.write(s)
  79. else:
  80. print(f'Unsupported config file extension: {ext}')
  81. def deep_iter(data, depth=0, current_depth=1):
  82. """
  83. Iter a dictionary safely.
  84. Args:
  85. data (dict):
  86. depth (int): Maximum depth to iter
  87. current_depth (int):
  88. Returns:
  89. list: Key path
  90. Any:
  91. """
  92. if isinstance(data, dict) \
  93. and (depth and current_depth <= depth):
  94. for key, value in data.items():
  95. for child_path, child_value in deep_iter(value, depth=depth, current_depth=current_depth + 1):
  96. yield [key] + child_path, child_value
  97. else:
  98. yield [], data