file_manager: process ufp files found when refreshing a directory

This allows users to manaully place ufp files in the gcode directory.  Resolves #81.

Signed-off-by:  Eric Callahan  <arskine.code@gmail.com>
This commit is contained in:
Arksine 2021-01-31 11:45:21 -05:00
parent fe2cec1087
commit 9648630392
1 changed files with 29 additions and 1 deletions

View File

@ -13,7 +13,7 @@ import json
from tornado.ioloop import IOLoop, PeriodicCallback from tornado.ioloop import IOLoop, PeriodicCallback
from tornado.locks import Event from tornado.locks import Event
VALID_GCODE_EXTS = ['.gcode', '.g', '.gco'] VALID_GCODE_EXTS = ['.gcode', '.g', '.gco', '.ufp']
FULL_ACCESS_ROOTS = ["gcodes", "config"] FULL_ACCESS_ROOTS = ["gcodes", "config"]
METADATA_SCRIPT = os.path.abspath(os.path.join( METADATA_SCRIPT = os.path.abspath(os.path.join(
os.path.dirname(__file__), "../../scripts/extract_metadata.py")) os.path.dirname(__file__), "../../scripts/extract_metadata.py"))
@ -285,6 +285,15 @@ class FileManager:
gc_path = self.file_paths.get('gcodes', None) gc_path = self.file_paths.get('gcodes', None)
if gc_path is not None and full_path.startswith(gc_path) and \ if gc_path is not None and full_path.startswith(gc_path) and \
ext in VALID_GCODE_EXTS: ext in VALID_GCODE_EXTS:
if ext == ".ufp":
try:
full_path = self._process_ufp_from_refresh(
full_path)
except Exception:
logging.exception("Error processing ufp file")
continue
path_info = self._get_path_info(full_path)
path_info['filename'] = os.path.split(full_path)[-1]
rel_path = os.path.relpath(full_path, start=gc_path) rel_path = os.path.relpath(full_path, start=gc_path)
self.gcode_metadata.parse_metadata( self.gcode_metadata.parse_metadata(
rel_path, path_info['size'], path_info['modified'], rel_path, path_info['size'], path_info['modified'],
@ -455,6 +464,19 @@ class FileManager:
except Exception: except Exception:
logging.exception("Unable to write Image") logging.exception("Unable to write Image")
def _process_ufp_from_refresh(self, ufp_path):
filename = os.path.split(ufp_path)[-1]
ul = {
'filename': filename,
'full_path': ufp_path
}
with open(ufp_path, 'rb') as ufp:
body = ufp.read()
ul['body'] = body
self._unzip_ufp(ul)
os.remove(ufp_path)
return ul['full_path']
def get_file_list(self, root, list_format=False, notify=False): def get_file_list(self, root, list_format=False, notify=False):
# Use os.walk find files in sd path and subdirs # Use os.walk find files in sd path and subdirs
filelist = {} filelist = {}
@ -482,6 +504,12 @@ class FileManager:
if root == 'gcodes' and ext not in VALID_GCODE_EXTS: if root == 'gcodes' and ext not in VALID_GCODE_EXTS:
continue continue
full_path = os.path.join(dir_path, name) full_path = os.path.join(dir_path, name)
if ext == ".ufp":
try:
full_path = self._process_ufp_from_refresh(full_path)
except Exception:
logging.exception("Error processing ufp file")
continue
fname = full_path[len(path) + 1:] fname = full_path[len(path) + 1:]
finfo = self._get_path_info(full_path) finfo = self._get_path_info(full_path)
filelist[fname] = finfo filelist[fname] = finfo