restoring the config and macros gcodes
This commit is contained in:
@@ -76,33 +76,6 @@ module.exports = {
|
|||||||
console.error("Restore Failed: ", error);
|
console.error("Restore Failed: ", error);
|
||||||
alert("Restore failed");
|
alert("Restore failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
// const fileReader = new FileReader();
|
|
||||||
// fileReader.onload = async ({ target }) => {
|
|
||||||
// let config;
|
|
||||||
// try {
|
|
||||||
// config = JSON.parse(target.result);
|
|
||||||
// } catch (error) {
|
|
||||||
// console.error("Invalid config file:", error);
|
|
||||||
// alert("Invalid config file");
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// await api.put("config/save", config);
|
|
||||||
// this.$dispatch("update");
|
|
||||||
// SvelteComponents.showDialog("Message", {
|
|
||||||
// title: "Success",
|
|
||||||
// message: "Configuration restored"
|
|
||||||
// });
|
|
||||||
// this.confirmReset= false
|
|
||||||
// } catch (error) {
|
|
||||||
// console.error("Restore failed:", error);
|
|
||||||
// alert("Restore failed");
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// fileReader.readAsText(files[0]);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
next: async function () {
|
next: async function () {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ from urllib.request import urlopen
|
|||||||
import iw_parse
|
import iw_parse
|
||||||
import io
|
import io
|
||||||
import zipfile
|
import zipfile
|
||||||
|
import shutil
|
||||||
|
|
||||||
def call_get_output(cmd):
|
def call_get_output(cmd):
|
||||||
p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
|
p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
|
||||||
@@ -294,44 +294,55 @@ class ConfigDownloadHandler(bbctrl.APIHandler):
|
|||||||
self.finish()
|
self.finish()
|
||||||
|
|
||||||
class ConfigRestoreHandler(bbctrl.APIHandler):
|
class ConfigRestoreHandler(bbctrl.APIHandler):
|
||||||
def put_ok(self):
|
def put(self):
|
||||||
if 'zipfile' not in self.request.files:
|
if 'zipfile' not in self.request.files['zipfile']:
|
||||||
raise HTTPError(401,'No file uploaded')
|
raise HTTPError(400,'No file uploaded')
|
||||||
|
|
||||||
self.get_log('ConfigRestoreHandler').info('self.request: {}'.format(str(self.request)))
|
|
||||||
|
|
||||||
zip_file = self.request.files['zipfile'][0]
|
zip_file = self.request.files['zipfile'][0]
|
||||||
|
|
||||||
temp_dir = './config-temp';
|
temp_dir = './config-temp';
|
||||||
|
|
||||||
if not os.path.exists(temp_dir):
|
if not os.path.exists(temp_dir):
|
||||||
os.mkdir(temp_dir)
|
os.mkdir(temp_dir)
|
||||||
|
|
||||||
fmt = socket.gethostname() + '-%Y%m%d.zip'
|
files_path = os.path.join(temp_dir, zip_file['filename'])
|
||||||
filename = datetime.date.today().strftime(fmt)
|
|
||||||
file_path = os.path.join(temp_dir, filename)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(file_path, 'wb') as f:
|
with open(files_path, 'wb') as f:
|
||||||
f.write(zip_file['body'])
|
f.write(zip_file['body'])
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPError(500, f"Unexpected error: {str(e)}")
|
raise HTTPError(500, f"Error handling zip file: {str(e)}")
|
||||||
# if not os.path.exists(self.get_upload()):
|
|
||||||
# os.mkdir(self.get_upload())
|
|
||||||
|
|
||||||
|
if not os.path.exists(self.get_upload()):
|
||||||
|
os.mkdir(self.get_upload())
|
||||||
|
|
||||||
# zip_path = os.path.join("./temp", zip_file['filename'])
|
with zipfile.ZipFile(files_path, 'r') as zip_ref:
|
||||||
# print(zip_path)
|
zip_ref.extractall(temp_dir+'/extracted')
|
||||||
# with open(zip_path, 'wb') as f:
|
|
||||||
# f.write(zip_file['body'])
|
|
||||||
|
|
||||||
# with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
|
||||||
# zip_ref.extractall("./temp")
|
|
||||||
|
|
||||||
# for root, dirs, files in os.walk("./temp"):
|
|
||||||
# for files in files:
|
|
||||||
|
|
||||||
|
extension = (".nc", ".ngc", ".gcode", ".gc")
|
||||||
|
|
||||||
|
try:
|
||||||
|
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
|
||||||
|
if file =="config.json":
|
||||||
|
with open(file_path, 'r') as json_file:
|
||||||
|
json_data = json.load(json_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)
|
||||||
|
#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):
|
||||||
def put_ok(self): self.get_ctrl().config.save(self.json)
|
def put_ok(self): self.get_ctrl().config.save(self.json)
|
||||||
|
|||||||
Reference in New Issue
Block a user