file_manager: refactor config file handling

Remove the "primary_config" option from the upload handler, as we no longer allow writes to printer.cfg unless it is located in the config path.  We now assume that the config path is the main config, so all files there are located at  /server/files/config/*.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-08-05 06:24:54 -04:00
parent e93e4521a4
commit d572a13655
1 changed files with 15 additions and 31 deletions

View File

@ -56,22 +56,14 @@ class FileManager:
self._update_file_list() self._update_file_list()
except Exception: except Exception:
logging.exception("Unable to initialize gcode file list") logging.exception("Unable to initialize gcode file list")
# Main configuration file # Configuration files in the optional config path
main_cfg = config.get('printer_config_main', None) cfg_path = config.get('printer_config_path', None)
if main_cfg is not None: if cfg_path is not None:
main_cfg = os.path.normpath(os.path.expanduser(main_cfg)) cfg_path = os.path.normpath(os.path.expanduser(cfg_path))
if main_cfg != self.file_paths.get("printer.cfg", ""): if cfg_path != self.file_paths.get('config', ""):
self.file_paths['printer.cfg'] = main_cfg self.file_paths['config'] = cfg_path
self.server.register_static_file_handler( self.server.register_static_file_handler(
'/server/files/config/printer.cfg', main_cfg) "/server/files/config/", cfg_path,
# "Included" configuration files
included_cfg = config.get('printer_config_path', None)
if included_cfg is not None:
included_cfg = os.path.normpath(os.path.expanduser(included_cfg))
if included_cfg != self.file_paths.get('config', ""):
self.file_paths['config'] = included_cfg
self.server.register_static_file_handler(
"/server/files/config/include/", included_cfg,
can_delete=True) can_delete=True)
try: try:
self._update_file_list(base='config') self._update_file_list(base='config')
@ -84,7 +76,7 @@ class FileManager:
if example_cfg_path != self.file_paths.get("config_examples", ""): if example_cfg_path != self.file_paths.get("config_examples", ""):
self.file_paths['config_examples'] = example_cfg_path self.file_paths['config_examples'] = example_cfg_path
self.server.register_static_file_handler( self.server.register_static_file_handler(
"/server/files/config/examples/", example_cfg_path) "/server/files/config_examples/", example_cfg_path)
try: try:
self._update_file_list(base='config_examples') self._update_file_list(base='config_examples')
except Exception: except Exception:
@ -317,10 +309,8 @@ class FileManager:
root = self._get_argument(request, 'root', "gcodes") root = self._get_argument(request, 'root', "gcodes")
if root == "gcodes": if root == "gcodes":
result = await self._do_gcode_upload(request) result = await self._do_gcode_upload(request)
elif root == "config":
result = self._do_config_upload(request)
else: else:
raise self.server.error(400, "Unknown root path") result = self._do_standard_upload(request, root)
return result return result
async def _do_gcode_upload(self, request): async def _do_gcode_upload(self, request):
@ -357,19 +347,13 @@ class FileManager:
self.notify_filelist_changed(upload['filename'], 'added', "gcodes") self.notify_filelist_changed(upload['filename'], 'added', "gcodes")
return {'result': upload['filename'], 'print_started': start_print} return {'result': upload['filename'], 'print_started': start_print}
def _do_config_upload(self, request): def _do_standard_upload(self, request, root):
req_arg = self._get_argument(request, 'primary_config', "false") path = self.file_paths.get(root, None)
is_main_config = req_arg.lower() == "true" if path is None:
cfg_base = "printer.cfg" if is_main_config else "config" raise self.server.error("Unknown root path: %s" % (root))
cfg_path = self.file_paths.get(cfg_base, None) upload = self._get_upload_info(request, path)
if cfg_path is None:
raise self.server.error(
"Printer configuration location on disk not set")
upload = self._get_upload_info(request, cfg_path)
self._write_file(upload) self._write_file(upload)
if cfg_base == "config": self.notify_filelist_changed(upload['filename'], 'added', root)
self.notify_filelist_changed(
upload['filename'], 'added', "config")
return {'result': upload['filename']} return {'result': upload['filename']}
def _get_argument(self, request, name, default=None): def _get_argument(self, request, name, default=None):