configuration.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import json
  2. import os
  3. def singleton(cls):
  4. instances = {}
  5. def wrapper(*args, **kwargs):
  6. if cls not in instances:
  7. instances[cls] = cls(*args, **kwargs)
  8. return instances[cls]
  9. return wrapper
  10. @singleton
  11. class Configuration:
  12. def __init__(self, game_name):
  13. self.game_name = game_name
  14. def save_2_json(self,file_path,output_type,data):
  15. if output_type=="{class_output_data}":
  16. file_path='config/'+self.game_name+'/module_output/'+file_path
  17. os.makedirs(os.path.dirname(file_path), exist_ok=True)
  18. with open(file_path, 'w', encoding='utf-8') as f:
  19. json.dump(data, f, ensure_ascii=False, indent=4)
  20. elif output_type=="{class_output_data_append}":
  21. file_path='config/'+self.game_name+'/module_output/'+file_path
  22. os.makedirs(os.path.dirname(file_path), exist_ok=True)
  23. if os.path.exists(file_path):
  24. with open(file_path, 'r', encoding='utf-8') as f:
  25. existing_data = json.load(f)
  26. else:
  27. existing_data = {"class_output_data":[]}
  28. existing_data["class_output_data"].append(data)
  29. with open(file_path, 'w', encoding='utf-8') as f:
  30. json.dump(existing_data, f, ensure_ascii=False, indent=4)
  31. else:
  32. file_path='config/'+self.game_name+'/module_output/'+file_path
  33. os.makedirs(os.path.dirname(file_path), exist_ok=True)
  34. if os.path.exists(file_path):
  35. with open(file_path, 'r', encoding='utf-8') as f:
  36. existing_data = json.load(f)
  37. else:
  38. existing_data = {"class_output_data":[]}
  39. existing_data["class_output_data"].append(data)
  40. with open(file_path, 'w', encoding='utf-8') as f:
  41. json.dump(data, f, ensure_ascii=False, indent=4)
  42. def prompt_input_process(self,input,prompt_input):
  43. for input_key, input_value in input.items():
  44. with open('config/'+self.game_name+'/'+input_value,encoding='utf-8') as f:
  45. globals()[input_key] = json.load(f)
  46. prompt_input_values = {}
  47. for prompt_input_key, prompt_input_value in prompt_input.items():
  48. prompt_input_values[prompt_input_key] = eval(prompt_input_value.format(**locals()))
  49. return prompt_input_values
  50. def read_prompt(self,prompt_path):
  51. with open('config/'+self.game_name+'/'+prompt_path,encoding='utf-8') as f:
  52. prompt = json.load(f)
  53. return prompt
  54. def read_gpt_config(self,classname,id):
  55. with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f:
  56. data = json.load(f)
  57. gpts=data["GPT"]
  58. for gpt in gpts:
  59. if gpt["id"]==id and gpt["class"]==classname:
  60. return gpt["input"],gpt["prompt_input"],gpt["prompt"],gpt["output"],gpt["next_module"]
  61. def read_judgement_config(self,classname,id):
  62. with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f:
  63. data = json.load(f)
  64. judgements=data["Judgement"]
  65. for judgement in judgements:
  66. if judgement["id"]==id and judgement["class"]==classname:
  67. return judgement["input"],judgement["value"],judgement
  68. def judgement_input_process(self,input,value):
  69. for input_key, input_value in input.items():
  70. with open('config/'+self.game_name+'/'+input_value,encoding='utf-8') as f:
  71. globals()[input_key] = json.load(f)
  72. input_value = eval(value.format(**locals()))
  73. return input_value
  74. def read_interact_config(self,classname,id):
  75. with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f:
  76. data = json.load(f)
  77. interacts=data["Interact"]
  78. for interact in interacts:
  79. if interact["id"]==id and interact["class"]==classname:
  80. return interact["input"],interact["choices"],interact["output"],interact["next_module"]
  81. def interact_input_process(self,input,value):
  82. for input_key, input_value in input.items():
  83. with open('config/'+self.game_name+'/'+input_value,encoding='utf-8') as f:
  84. globals()[input_key] = json.load(f)
  85. input_value = eval(value.format(**locals()))
  86. return input_value
  87. def read_json(self,path):
  88. with open('config/'+path,encoding='utf-8') as f:
  89. data = json.load(f)
  90. return data