Refactor: make "sys_info" an internal module, rename for clarity
- Move sys_info into an internal module to avoid having so many top level modules for Blender's internal functionality. - Rename system_info sub-modules that pre-fill URL's for clarity. - Move top-level exception handling into the operator. - Report an error if an unexpected exception occurs. - Use `Exception` instead of `BaseException` as there is no reason to catch the additional exceptions. - Remove use of sys_info from the command line example, replace with in-lined system info.
This commit is contained in:
@@ -5,25 +5,74 @@ Registering commands makes it possible to conveniently expose command line
|
||||
functionality via commands passed to (``-c`` / ``--command``).
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
import bpy
|
||||
|
||||
|
||||
def sysinfo_print():
|
||||
"""
|
||||
Report basic system information.
|
||||
"""
|
||||
|
||||
import pprint
|
||||
import platform
|
||||
import textwrap
|
||||
|
||||
width = 80
|
||||
indent = 2
|
||||
|
||||
print("Blender {:s}".format(bpy.app.version_string))
|
||||
print("Running on: {:s}-{:s}".format(platform.platform(), platform.machine()))
|
||||
print("Processors: {!r}".format(os.cpu_count()))
|
||||
print()
|
||||
|
||||
# Dump `bpy.app`.
|
||||
for attr in dir(bpy.app):
|
||||
if attr.startswith("_"):
|
||||
continue
|
||||
# Overly verbose.
|
||||
if attr in {"handlers", "build_cflags", "build_cxxflags"}:
|
||||
continue
|
||||
|
||||
value = getattr(bpy.app, attr)
|
||||
if attr.startswith("build_"):
|
||||
pass
|
||||
elif isinstance(value, tuple):
|
||||
pass
|
||||
else:
|
||||
# Otherwise ignore.
|
||||
continue
|
||||
|
||||
if isinstance(value, bytes):
|
||||
value = value.decode("utf-8", errors="ignore")
|
||||
|
||||
if isinstance(value, str):
|
||||
pass
|
||||
elif isinstance(value, tuple) and hasattr(value, "__dir__"):
|
||||
value = {
|
||||
attr_sub: value_sub
|
||||
for attr_sub in dir(value)
|
||||
# Exclude built-ins.
|
||||
if not attr_sub.startswith(("_", "n_"))
|
||||
# Exclude methods.
|
||||
if not callable(value_sub := getattr(value, attr_sub))
|
||||
}
|
||||
value = pprint.pformat(value, indent=0, width=width)
|
||||
else:
|
||||
value = pprint.pformat(value, indent=0, width=width)
|
||||
|
||||
print("{:s}:\n{:s}\n".format(attr, textwrap.indent(value, " " * indent)))
|
||||
|
||||
|
||||
def sysinfo_command(argv):
|
||||
import tempfile
|
||||
import sys_info
|
||||
|
||||
if argv and argv[0] == "--help":
|
||||
print("Print system information & exit!")
|
||||
return 0
|
||||
|
||||
with tempfile.TemporaryDirectory() as tempdir:
|
||||
filepath = os.path.join(tempdir, "system_info.txt")
|
||||
sys_info.write_sysinfo(filepath)
|
||||
with open(filepath, "r", encoding="utf-8") as fh:
|
||||
sys.stdout.write(fh.read())
|
||||
sysinfo_print()
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user