moonraker: Use file_manager to update mutable endpoints

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-08-06 14:24:10 -04:00
parent a4638f6c21
commit b92000dd46
2 changed files with 48 additions and 37 deletions

View File

@ -245,16 +245,16 @@ class Server:
static_paths = result.get('static_paths', {}) static_paths = result.get('static_paths', {})
for ep in endpoints: for ep in endpoints:
self.moonraker_app.register_remote_handler(ep) self.moonraker_app.register_remote_handler(ep)
for sp in static_paths: mutable_paths = {sp['resource_id']: sp['file_path']
self.moonraker_app.register_static_file_handler( for sp in static_paths}
sp['resource_id'], sp['file_path']) file_manager = self.lookup_plugin('file_manager')
file_manager.update_mutable_paths(mutable_paths)
async def _check_available(self): async def _check_available(self):
request = self.make_request( request = self.make_request(
"moonraker/check_available", "GET", {}) "moonraker/check_available", "GET", {})
result = await request.wait() result = await request.wait()
if not isinstance(result, ServerError): if not isinstance(result, ServerError):
self.send_event("server:moonraker_available", result)
self.moonraker_available = True self.moonraker_available = True
else: else:
logging.info( logging.info(

View File

@ -24,9 +24,7 @@ class FileManager:
self.file_lists = {} self.file_lists = {}
self.gcode_metadata = {} self.gcode_metadata = {}
self.metadata_lock = Lock() self.metadata_lock = Lock()
self.mutable_path_args = {}
self.server.register_event_handler(
"server:moonraker_available", self.update_mutable_paths)
# Register file management endpoints # Register file management endpoints
self.server.register_endpoint( self.server.register_endpoint(
@ -51,52 +49,65 @@ class FileManager:
# Register Klippy Configuration Path # Register Klippy Configuration Path
config_path = config.get('config_path', None) config_path = config.get('config_path', None)
if config_path is not None: if config_path is not None:
cfg_path = os.path.normpath(os.path.expanduser(config_path)) ret = self._register_directory(
if not os.path.isdir(cfg_path): 'config', config_path, can_delete=True)
if not ret:
raise config.error( raise config.error(
"Option 'config_path' is not a valid directory") "Option 'config_path' is not a valid directory")
self.file_paths['config'] = cfg_path
self.server.register_static_file_handler(
"/server/files/config/", cfg_path,
can_delete=True)
try:
self._update_file_list(base='config')
except Exception:
logging.exception("Unable to initialize config file list")
def update_mutable_paths(self, paths): def update_mutable_paths(self, paths):
# Update paths from Klippy. The sd_path can potentially change # Update paths from Klippy. The sd_path can potentially change
# location on restart. # location on restart.
logging.debug("Updating Mutable Paths: %s" % (str(paths))) if paths == self.mutable_path_args:
sd = paths.get('sd_path', None) # No change in mutable paths
if sd is not None: return
sd = os.path.normpath(os.path.expanduser(sd)) self.mutable_path_args = dict(paths)
if sd != self.file_paths.get('gcodes', ""): str_paths = "\n".join(["%s: %s" % (k, v) for k, v in paths.items()])
self.file_paths['gcodes'] = sd logging.debug("\nUpdating Mutable Paths:\n%s" % (str_paths))
self.server.register_static_file_handler(
'/server/files/gcodes/', sd, can_delete=True, # Register directories
op_check_cb=self._handle_operation_check) sd = paths.pop('sd_path', None)
try: self._register_directory("gcodes", sd, can_delete=True)
self._update_file_list()
except Exception:
logging.exception("Unable to initialize gcode file list")
# Register path for example configs # Register path for example configs
klipper_path = paths.get('klipper_path', None) klipper_path = paths.pop('klipper_path', None)
if klipper_path is not None: if klipper_path is not None:
example_cfg_path = os.path.join(klipper_path, "config") example_cfg_path = os.path.join(klipper_path, "config")
if example_cfg_path != self.file_paths.get("config_examples", ""): self._register_directory("config_examples", example_cfg_path)
self.file_paths['config_examples'] = example_cfg_path paths.pop('klippy_env', None)
self.server.register_static_file_handler( paths.pop('printer.cfg', None)
"/server/files/config_examples/", example_cfg_path)
# register remaining static files
for pattern, path in paths.items():
if path is not None:
path = os.path.normpath(os.path.expanduser(path))
self.server.register_static_file_handler(pattern, path)
def _register_directory(self, base, path, can_delete=False):
op_check_cb = None
if base == 'gcodes':
op_check_cb = self._handle_operation_check
if path is None:
return False
path = os.path.normpath(os.path.expanduser(path))
if not os.path.isdir(path):
return False
if path != self.file_paths.get(base, ""):
self.file_paths[base] = path
self.server.register_static_file_handler(
base, path, can_delete=can_delete, op_check_cb=op_check_cb)
try: try:
self._update_file_list(base='config_examples') self._update_file_list(base=base)
except Exception: except Exception:
logging.exception( logging.exception(
"Unable to initialize config_examples file list") "Unable to initialize file list: <%s>" % (base))
return True
def get_sd_directory(self): def get_sd_directory(self):
return self.file_paths.get('gcodes', "") return self.file_paths.get('gcodes', "")
def get_mutable_path_args(self):
return dict(self.mutable_path_args)
async def _handle_filelist_request(self, path, method, args): async def _handle_filelist_request(self, path, method, args):
root = args.get('root', "gcodes") root = args.get('root', "gcodes")
return self.get_file_list(format_list=True, base=root) return self.get_file_list(format_list=True, base=root)