file_1.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import json
  2. import os
  3. import random
  4. from os import getcwd
  5. from os.path import isfile
  6. from typing import Optional
  7. import pandas as pd
  8. from PyQt5.QtWidgets import QFileDialog, QInputDialog, QMessageBox
  9. def dict_from_json(filename: str) -> dict:
  10. if isfile(filename):
  11. with open(filename) as f:
  12. return json.load(f)
  13. else:
  14. return {}
  15. def mass_from_xlsx(path: str) -> dict:
  16. logs = {}
  17. xl = pd.ExcelFile(path)
  18. df = xl.parse(xl.sheet_names[0])
  19. for name in df.keys():
  20. logs[name] = list(df[name].values)
  21. return logs
  22. def save_dict_as_json(data: dict, path: str = os.getcwd(),
  23. filename: str = f'/temp_files/lay_name{random.randint(1, 1000)}') -> str:
  24. if path.__contains__('.json'):
  25. path_save = path
  26. elif filename.__contains__('.json'):
  27. path_save = filename
  28. elif filename.__contains__('.'):
  29. path_save = path + '/' + filename.split('/')[-1]
  30. else:
  31. path_save = path + f'/{filename}.json'
  32. path_save.replace("\\", '/')
  33. path_save.replace('//', '/')
  34. try:
  35. json_file = open(path_save, mode='x')
  36. except FileNotFoundError:
  37. os.mkdir(path_save.split('lay_name')[0])
  38. json_file = open(path_save, mode='x')
  39. except FileExistsError:
  40. json_file = open(path_save, mode='w')
  41. json.dump(data, json_file)
  42. json_file.close()
  43. return path_save
  44. class FileEdit:
  45. # Messages
  46. create_project_default = 'Введите название проекта:'
  47. create_file_default = 'Введите название файла:'
  48. create_file_error = 'Не удалось создать файл, \nфайл с таким именем уже существует'
  49. data_log = 'log.log'
  50. data_model_name = 'data.model'
  51. data_polygon_model = 'polygon.model'
  52. def __init__(self, project_path=''):
  53. self.project_path = project_path
  54. @property
  55. def model_path(self):
  56. if os.path.isdir(self.project_path):
  57. if os.path.isfile(self.project_path + '/' + FileEdit.data_model_name):
  58. pass
  59. else:
  60. save_dict_as_json({}, self.project_path, FileEdit.data_model_name)
  61. return self.project_path + '/' + FileEdit.data_model_name
  62. return ''
  63. @property
  64. def polygon_model_path(self):
  65. if os.path.isdir(self.project_path):
  66. return self.project_path + '/' + FileEdit.data_polygon_model
  67. @property
  68. def log_path(self):
  69. if os.path.isdir(self.project_path):
  70. return self.project_path + '/' + FileEdit.data_log
  71. def save_log(self, data):
  72. if not self.log_path:
  73. self.open_project()
  74. if self.log_path:
  75. save_dict_as_json(data=data, path=self.project_path, filename=self.log_path)
  76. def save_model_file(self, data) -> str:
  77. if not self.model_path:
  78. self.create_project()
  79. if self.model_path:
  80. return save_dict_as_json(data=data, path=self.project_path, filename=self.model_path)
  81. else:
  82. return ''
  83. def save_polygon_model(self, data) -> str:
  84. if not self.polygon_model_path:
  85. self.create_project()
  86. if self.polygon_model_path:
  87. return save_dict_as_json(data=data, path=self.project_path,
  88. filename=self.polygon_model_path)
  89. else:
  90. return ''
  91. def save_file(self, data: dict):
  92. if not self.project_path:
  93. self.create_file()
  94. save_dict_as_json(data=data, filename=self.project_path)
  95. def open_file(self, file_extension='json'):
  96. message = f'{file_extension} Files (*.{file_extension})'
  97. self.project_path, _ = QFileDialog.getOpenFileName(None, '', getcwd(), message)
  98. return self.project_path
  99. def create_file(self, message=None, extension: str = '', filename: str = '') -> Optional[str]:
  100. if not message:
  101. message = self.create_file_default
  102. filename, ok = QInputDialog.getText(None, 'Input Dialog', str(message)) \
  103. if not filename else (filename, True)
  104. if ok and filename and filename != '':
  105. path = QFileDialog.getExistingDirectory(None, getcwd())
  106. if filename.__contains__('.') or extension is None:
  107. return f'{path}/{filename}'
  108. else:
  109. return f'{path}/{filename}{"." + extension.replace(".", "")}'
  110. return None
  111. def open_project(self):
  112. self.project_path = QFileDialog.getExistingDirectory(None, '', getcwd())
  113. if self.project_path:
  114. return self.project_path
  115. def create_project(self) -> Optional[str]:
  116. name, ok = QInputDialog.getText(None, 'Input Dialog', str(FileEdit.create_project_default))
  117. if not ok or name == '':
  118. return
  119. project_name = f'/{name.replace(" ", "_")}.oilcase'
  120. self.project_path = QFileDialog.getExistingDirectory(None, getcwd()) + project_name
  121. try:
  122. os.mkdir(self.project_path)
  123. save_dict_as_json({}, self.project_path, 'data.model')
  124. return self.project_path
  125. except FileExistsError as e:
  126. msg = QMessageBox()
  127. msg.setWindowTitle("Project create error")
  128. msg.setText(str(e))
  129. msg.exec_()