utils_30.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. def save_game(self):
  2. save_file = open("saves/main_save.xml", "w+")
  3. level = self.save_level()
  4. self.tree.append(level)
  5. team = self.save_team()
  6. self.tree.append(team)
  7. # Store XML tree in file
  8. save_file.write(etree.tostring(self.tree, pretty_print=True, encoding="unicode"))
  9. save_file.close()
  10. def save_upload_file(
  11. self,
  12. file: UploadFile,
  13. save_dir_path: pathlib.Path,
  14. job_id: str,
  15. dt_string: str,
  16. ) -> pathlib.Path:
  17. """Save `file` under `save_dir_path`.
  18. Args:
  19. file (UploadFile): A file want to save.
  20. save_dir_path (pathlib.Path): A path to directory where file will be saved.
  21. job_id (str): A job id. This will used part of filename.
  22. dt_string (str): A datetime info. This will used part of filename.
  23. Return:
  24. pathlib.Path: A path where file is saved.
  25. """
  26. if not save_dir_path.exists():
  27. save_dir_path.mkdir(parents=True, exist_ok=True)
  28. save_path: Final = save_dir_path / f"{dt_string}_{job_id}_{file.filename}"
  29. try:
  30. with save_path.open("wb") as f:
  31. shutil.copyfileobj(file.file, f)
  32. finally:
  33. file.file.close()
  34. return save_path