MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / save_configuration

Method save_configuration

utils.py:735–786  ·  view source on GitHub ↗
(self, handler)

Source from the content-addressed store, hash-verified

733 subprocess.Popen(['sudo', 'nmcli', 'connection', 'reload']).communicate()
734
735 def save_configuration(self, handler):
736 try:
737 content_length = int(handler.headers['Content-Length'])
738 post_data = handler.rfile.read(content_length).decode('utf-8')
739 params = json.loads(post_data)
740 fichier = self.shared_data.shared_config_json
741 self.logger.info(f"Received params: {params}")
742
743 with open(fichier, 'r') as f:
744 current_config = json.load(f)
745
746 for key, value in params.items():
747 if isinstance(value, bool):
748 current_config[key] = value
749 elif isinstance(value, str) and value.lower() in ['true', 'false']:
750 current_config[key] = value.lower() == 'true'
751 elif isinstance(value, (int, float)):
752 current_config[key] = value
753 elif isinstance(value, list):
754 # Lets boot any values in a list that are just empty strings
755 for val in value[:]:
756 if val == "" :
757 value.remove(val)
758 current_config[key] = value
759 elif isinstance(value, str):
760 if value.replace('.', '', 1).isdigit():
761 current_config[key] = float(value) if '.' in value else int(value)
762 else:
763 current_config[key] = value
764 else:
765 current_config[key] = value
766
767 with open(fichier, 'w') as f:
768 json.dump(current_config, f, indent=4)
769 self.logger.info("Configuration saved to file")
770
771 handler.send_response(200)
772 handler.send_header('Content-type', 'application/json')
773 handler.end_headers()
774 handler.wfile.write(json.dumps({"status": "success", "message": "Configuration saved"}).encode('utf-8'))
775 self.logger.info("Configuration saved (web)")
776
777 self.shared_data.load_config()
778 self.logger.info("Configuration reloaded (web)")
779
780 except Exception as e:
781 handler.send_response(500)
782 handler.send_header('Content-type', 'application/json')
783 handler.end_headers()
784 error_message = {"status": "error", "message": str(e)}
785 handler.wfile.write(json.dumps(error_message).encode('utf-8'))
786 self.logger.error(f"Error saving configuration: {e}")
787
788 def list_files(self, directory):
789 files = []

Callers

nothing calls this directly

Calls 9

infoMethod · 0.80
dumpMethod · 0.80
load_configMethod · 0.80
errorMethod · 0.80
decodeMethod · 0.45
readMethod · 0.45
itemsMethod · 0.45
writeMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected