__init__.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. from .ChangeLog import ChangeLog
  3. class CloneXls:
  4. def __init__(self, source_file, clone_file_postfix="_baangt"):
  5. """
  6. This class is used to make a clone of an xlsx file and than maintain a change log
  7. :param source_file: Path to source file
  8. :param clone_file_postfix: (optional) (default="_baangt") Postfix to be added in the name of clone file
  9. """
  10. self.source_file = source_file
  11. self.source_file_name = ".".join(source_file.split(".")[:-1])
  12. self.source_file_extension = source_file.split(".")[-1]
  13. self.clone_file = self.source_file_name + clone_file_postfix + "." + self.source_file_extension
  14. self.check_source_file()
  15. def check_source_file(self):
  16. if not os.path.exists(self.source_file):
  17. raise BaseException(f"{self.source_file} doesn't exists, please verify the path entered!")
  18. def update_or_make_clone(self, log_sheet_name="Change Logs", ignore_headers=[], ignore_sheets=[],
  19. case_sensitive_ignore=False, clone=True):
  20. """
  21. This method is used to update the clone file with the change log and if their is no clone file then make one
  22. :return:
  23. """
  24. if not os.path.exists(self.clone_file):
  25. # Will make a clone file if it doesn't exists, the cloned file will be exact copy of source
  26. # Change log sheets are not added here
  27. with open(self.source_file, "rb") as file:
  28. data = file.read()
  29. with open(self.clone_file, "wb") as file:
  30. file.write(data)
  31. elif clone:
  32. changeLog = ChangeLog(self.source_file, self.clone_file,
  33. log_sheet_name=log_sheet_name, ignore_headers=ignore_headers,
  34. ignore_sheets=ignore_sheets, case_sensitive_ignore=case_sensitive_ignore)
  35. changeLog.xlsxChangeLog()
  36. # Returning filename with absolute path of clone file
  37. return self.clone_file