metadata; use python's logging module
Replace calls to `log_to_stderr` with standard logging.<level> calls. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
5691a4f162
commit
d51820f48c
|
@ -17,6 +17,7 @@ import tempfile
|
||||||
import zipfile
|
import zipfile
|
||||||
import shutil
|
import shutil
|
||||||
import uuid
|
import uuid
|
||||||
|
import logging
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
# Annotation imports
|
# Annotation imports
|
||||||
|
@ -35,9 +36,8 @@ if TYPE_CHECKING:
|
||||||
UFP_MODEL_PATH = "/3D/model.gcode"
|
UFP_MODEL_PATH = "/3D/model.gcode"
|
||||||
UFP_THUMB_PATH = "/Metadata/thumbnail.png"
|
UFP_THUMB_PATH = "/Metadata/thumbnail.png"
|
||||||
|
|
||||||
def log_to_stderr(msg: str) -> None:
|
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
|
||||||
sys.stderr.write(f"{msg}\n")
|
logger = logging.getLogger("metadata")
|
||||||
sys.stderr.flush()
|
|
||||||
|
|
||||||
# regex helpers
|
# regex helpers
|
||||||
def _regex_find_floats(pattern: str,
|
def _regex_find_floats(pattern: str,
|
||||||
|
@ -144,12 +144,12 @@ class BaseSlicer(object):
|
||||||
if match is not None:
|
if match is not None:
|
||||||
# Objects already processed
|
# Objects already processed
|
||||||
fname = os.path.basename(self.path)
|
fname = os.path.basename(self.path)
|
||||||
log_to_stderr(
|
logger.info(
|
||||||
f"File '{fname}' currently supports cancellation, "
|
f"File '{fname}' currently supports cancellation, "
|
||||||
"processing aborted"
|
"processing aborted"
|
||||||
)
|
)
|
||||||
if match.group(1).startswith("DEFINE_OBJECT"):
|
if match.group(1).startswith("DEFINE_OBJECT"):
|
||||||
log_to_stderr(
|
logger.info(
|
||||||
"Legacy object processing detected. This is not "
|
"Legacy object processing detected. This is not "
|
||||||
"compatible with official versions of Klipper."
|
"compatible with official versions of Klipper."
|
||||||
)
|
)
|
||||||
|
@ -229,7 +229,7 @@ class BaseSlicer(object):
|
||||||
try:
|
try:
|
||||||
os.mkdir(thumb_dir)
|
os.mkdir(thumb_dir)
|
||||||
except Exception:
|
except Exception:
|
||||||
log_to_stderr(f"Unable to create thumb dir: {thumb_dir}")
|
logger.info(f"Unable to create thumb dir: {thumb_dir}")
|
||||||
return None
|
return None
|
||||||
thumb_base = os.path.splitext(os.path.basename(self.path))[0]
|
thumb_base = os.path.splitext(os.path.basename(self.path))[0]
|
||||||
parsed_matches: List[Dict[str, Any]] = []
|
parsed_matches: List[Dict[str, Any]] = []
|
||||||
|
@ -239,12 +239,12 @@ class BaseSlicer(object):
|
||||||
info = _regex_find_ints(r".*", lines[0])
|
info = _regex_find_ints(r".*", lines[0])
|
||||||
data = "".join(lines[1:-1])
|
data = "".join(lines[1:-1])
|
||||||
if len(info) != 3:
|
if len(info) != 3:
|
||||||
log_to_stderr(
|
logger.info(
|
||||||
f"MetadataError: Error parsing thumbnail"
|
f"MetadataError: Error parsing thumbnail"
|
||||||
f" header: {lines[0]}")
|
f" header: {lines[0]}")
|
||||||
continue
|
continue
|
||||||
if len(data) != info[2]:
|
if len(data) != info[2]:
|
||||||
log_to_stderr(
|
logger.info(
|
||||||
f"MetadataError: Thumbnail Size Mismatch: "
|
f"MetadataError: Thumbnail Size Mismatch: "
|
||||||
f"detected {info[2]}, actual {len(data)}")
|
f"detected {info[2]}, actual {len(data)}")
|
||||||
continue
|
continue
|
||||||
|
@ -283,7 +283,7 @@ class BaseSlicer(object):
|
||||||
'relative_path': rel_path_small
|
'relative_path': rel_path_small
|
||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_to_stderr(str(e))
|
logger.info(str(e))
|
||||||
return parsed_matches
|
return parsed_matches
|
||||||
|
|
||||||
def parse_layer_count(self) -> Optional[int]:
|
def parse_layer_count(self) -> Optional[int]:
|
||||||
|
@ -564,7 +564,7 @@ class Cura(BaseSlicer):
|
||||||
'relative_path': rel_path_small
|
'relative_path': rel_path_small
|
||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_to_stderr(str(e))
|
logger.info(str(e))
|
||||||
return None
|
return None
|
||||||
return thumbs
|
return thumbs
|
||||||
|
|
||||||
|
@ -978,10 +978,10 @@ def process_objects(file_path: str, slicer: BaseSlicer, name: str) -> bool:
|
||||||
preprocess_m486
|
preprocess_m486
|
||||||
)
|
)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
log_to_stderr("Module 'preprocess-cancellation' failed to load")
|
logger.info("Module 'preprocess-cancellation' failed to load")
|
||||||
return False
|
return False
|
||||||
fname = os.path.basename(file_path)
|
fname = os.path.basename(file_path)
|
||||||
log_to_stderr(
|
logger.info(
|
||||||
f"Performing Object Processing on file: {fname}, "
|
f"Performing Object Processing on file: {fname}, "
|
||||||
f"sliced by {name}"
|
f"sliced by {name}"
|
||||||
)
|
)
|
||||||
|
@ -999,7 +999,7 @@ def process_objects(file_path: str, slicer: BaseSlicer, name: str) -> bool:
|
||||||
elif isinstance(slicer, IdeaMaker):
|
elif isinstance(slicer, IdeaMaker):
|
||||||
processor = preprocess_ideamaker
|
processor = preprocess_ideamaker
|
||||||
else:
|
else:
|
||||||
log_to_stderr(
|
logger.info(
|
||||||
f"Object Processing Failed, slicer {name}"
|
f"Object Processing Failed, slicer {name}"
|
||||||
"not supported"
|
"not supported"
|
||||||
)
|
)
|
||||||
|
@ -1007,7 +1007,7 @@ def process_objects(file_path: str, slicer: BaseSlicer, name: str) -> bool:
|
||||||
for line in processor(in_file):
|
for line in processor(in_file):
|
||||||
out_file.write(line)
|
out_file.write(line)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_to_stderr(f"Object processing failed: {e}")
|
logger.info(f"Object processing failed: {e}")
|
||||||
return False
|
return False
|
||||||
if os.path.islink(file_path):
|
if os.path.islink(file_path):
|
||||||
file_path = os.path.realpath(file_path)
|
file_path = os.path.realpath(file_path)
|
||||||
|
@ -1065,7 +1065,7 @@ def extract_metadata(
|
||||||
|
|
||||||
def extract_ufp(ufp_path: str, dest_path: str) -> None:
|
def extract_ufp(ufp_path: str, dest_path: str) -> None:
|
||||||
if not os.path.isfile(ufp_path):
|
if not os.path.isfile(ufp_path):
|
||||||
log_to_stderr(f"UFP file Not Found: {ufp_path}")
|
logger.info(f"UFP file Not Found: {ufp_path}")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
thumb_name = os.path.splitext(
|
thumb_name = os.path.splitext(
|
||||||
os.path.basename(dest_path))[0] + ".png"
|
os.path.basename(dest_path))[0] + ".png"
|
||||||
|
@ -1088,12 +1088,12 @@ def extract_ufp(ufp_path: str, dest_path: str) -> None:
|
||||||
os.mkdir(dest_thumb_dir)
|
os.mkdir(dest_thumb_dir)
|
||||||
shutil.move(tmp_thumb_path, dest_thumb_path)
|
shutil.move(tmp_thumb_path, dest_thumb_path)
|
||||||
except Exception:
|
except Exception:
|
||||||
log_to_stderr(traceback.format_exc())
|
logger.info(traceback.format_exc())
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
try:
|
try:
|
||||||
os.remove(ufp_path)
|
os.remove(ufp_path)
|
||||||
except Exception:
|
except Exception:
|
||||||
log_to_stderr(f"Error removing ufp file: {ufp_path}")
|
logger.info(f"Error removing ufp file: {ufp_path}")
|
||||||
|
|
||||||
def main(path: str,
|
def main(path: str,
|
||||||
filename: str,
|
filename: str,
|
||||||
|
@ -1105,12 +1105,12 @@ def main(path: str,
|
||||||
extract_ufp(ufp, file_path)
|
extract_ufp(ufp, file_path)
|
||||||
metadata: Dict[str, Any] = {}
|
metadata: Dict[str, Any] = {}
|
||||||
if not os.path.isfile(file_path):
|
if not os.path.isfile(file_path):
|
||||||
log_to_stderr(f"File Not Found: {file_path}")
|
logger.info(f"File Not Found: {file_path}")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
try:
|
try:
|
||||||
metadata = extract_metadata(file_path, check_objects)
|
metadata = extract_metadata(file_path, check_objects)
|
||||||
except Exception:
|
except Exception:
|
||||||
log_to_stderr(traceback.format_exc())
|
logger.info(traceback.format_exc())
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
fd = sys.stdout.fileno()
|
fd = sys.stdout.fileno()
|
||||||
data = json.dumps(
|
data = json.dumps(
|
||||||
|
@ -1145,5 +1145,5 @@ if __name__ == "__main__":
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
check_objects = args.check_objects
|
check_objects = args.check_objects
|
||||||
enabled_msg = "enabled" if check_objects else "disabled"
|
enabled_msg = "enabled" if check_objects else "disabled"
|
||||||
log_to_stderr(f"Object Processing is {enabled_msg}")
|
logger.info(f"Object Processing is {enabled_msg}")
|
||||||
main(args.path, args.filename, args.ufp, check_objects)
|
main(args.path, args.filename, args.ufp, check_objects)
|
||||||
|
|
Loading…
Reference in New Issue