debugging restore handler

This commit is contained in:
sanjayk03-dev
2024-06-07 18:27:38 +05:30
parent dd994d6229
commit f0bbe03874

View File

@@ -295,53 +295,56 @@ class ConfigDownloadHandler(bbctrl.APIHandler):
class ConfigRestoreHandler(bbctrl.APIHandler): class ConfigRestoreHandler(bbctrl.APIHandler):
def put(self): def put(self):
if 'zipfile' not in self.request.files['zipfile']:
raise HTTPError(400,'No file uploaded')
zip_file = self.request.files['zipfile'][0] zip_file = self.request.files['zipfile'][0]
temp_dir = './config-temp'; self.get_log('ConfigRestoreHandler').info('Request Hit')
# if 'zipfile' not in self.request.files:
# raise HTTPError(400, 'No file uploaded')
if not os.path.exists(temp_dir): # zip_file = self.request.files['zipfile'][0]
os.mkdir(temp_dir) # temp_dir = './config-temp';
files_path = os.path.join(temp_dir, zip_file['filename']) # if not os.path.exists(temp_dir):
# os.mkdir(temp_dir)
try: # files_path = os.path.join(temp_dir, zip_file['filename'])
with open(files_path, 'wb') as f:
f.write(zip_file['body'])
except Exception as e:
raise HTTPError(500, f"Error handling zip file: {str(e)}")
if not os.path.exists(self.get_upload()): # try:
os.mkdir(self.get_upload()) # with open(files_path, 'wb') as f:
# f.write(zip_file['body'])
# except Exception as e:
# raise HTTPError(500, f"Error handling zip file: {str(e)}")
with zipfile.ZipFile(files_path, 'r') as zip_ref: # if not os.path.exists(self.get_upload()):
zip_ref.extractall(temp_dir+'/extracted') # os.mkdir(self.get_upload())
extension = (".nc", ".ngc", ".gcode", ".gc") # with zipfile.ZipFile(files_path, 'r') as zip_ref:
# zip_ref.extractall(temp_dir+'/extracted')
try: # extension = (".nc", ".ngc", ".gcode", ".gc")
for root, dirs, files in os.walk(temp_dir+'/extracted'):
for file in files:
file_path = os.path.join(root, file)
#Updating the config.json # try:
if file =="config.json": # for root, dirs, files in os.walk(temp_dir+'/extracted'):
with open(file_path, 'r') as json_file: # for file in files:
json_data = json.load(json_file) # file_path = os.path.join(root, file)
keys_to_remove = ['non_macros_list','gcode_list']
for key in keys_to_remove:
if key in json_data:
del json_data[key]
self.get_ctrl().config.save(json_data) # #Updating the config.json
#moving the gcodes from temp to uploads # if file =="config.json":
elif file.endswith(extension): # with open(file_path, 'r') as json_file:
shutil.move(file_path,self.get_upload(file)) # json_data = json.load(json_file)
except Exception as e: # keys_to_remove = ['non_macros_list','gcode_list']
raise HTTPError(500, f"Error restoring: {str(e)}") # for key in keys_to_remove:
# if key in json_data:
# del json_data[key]
shutil.rmtree(temp_dir) # self.get_ctrl().config.save(json_data)
# #moving the gcodes from temp to uploads
# elif file.endswith(extension):
# shutil.move(file_path,self.get_upload(file))
# except Exception as e:
# raise HTTPError(500, f"Error restoring: {str(e)}")
# shutil.rmtree(temp_dir)
class ConfigSaveHandler(bbctrl.APIHandler): class ConfigSaveHandler(bbctrl.APIHandler):