Skip to content

Commit

Permalink
[#370] Updated DisplayCAL.worker so that it uses the code page valu…
Browse files Browse the repository at this point in the history
…e returned by the `kernel32` library instead of manually setting it to `utf-8` as `cp1252` seems to be working fine, after #383 has been merged. And, simplified a couple of `if` statements that are checking the `stdout.isatty()` value, and updated code formatting.
  • Loading branch information
eoyilmaz committed Sep 27, 2024
1 parent e395b7b commit 14933b9
Showing 1 changed file with 67 additions and 40 deletions.
107 changes: 67 additions & 40 deletions DisplayCAL/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
from DisplayCAL import imfile
from DisplayCAL import localization as lang
from DisplayCAL import wexpect
# import wexpect
from DisplayCAL.argyll_cgats import (
add_dispcal_options_to_cal,
add_options_to_ti3,
Expand Down Expand Up @@ -1700,7 +1699,6 @@ def set_argyll_bin(parent=None, silent=False, callafter=None, callafter_args=())


class EvalFalse(object):

"""Evaluate to False in boolean comparisons"""

def __init__(self, wrapped_object):
Expand Down Expand Up @@ -1863,7 +1861,9 @@ def __call__(self, *args, **kwargs):
except Exception as exception:
if debug:
messages = traceback.format_exception(exception)
print("[D] Worker raised an unhandled exception: \n" + "\n".join(messages))
print(
"[D] Worker raised an unhandled exception: \n" + "\n".join(messages)
)
raise
if not self.continue_next and self.worker._progress_wnd:
if hasattr(
Expand Down Expand Up @@ -5497,12 +5497,9 @@ def enumerate_displays_and_ports(
if argyll_version_string != self.argyll_version_string:
self.set_argyll_version_from_string(argyll_version_string)
print(f"ArgyllCMS {self.argyll_version_string}")
defaults[
"copyright"
] = "No copyright. Created with %s %s and Argyll CMS %s" % (
appname,
version,
argyll_version_string,
defaults["copyright"] = (
f"No copyright. Created with {appname} {version} and "
f"Argyll CMS {argyll_version_string}"
)

if self.argyll_version > [1, 0, 4]:
Expand Down Expand Up @@ -5955,7 +5952,10 @@ def exec_cmd(
# If dry_run is explicitly set to False, ignore dry_run config value
dry_run = dry_run is not False and (dry_run or getcfg("dry_run"))
if not capture_output:
capture_output = not sys.stdout or not hasattr(sys.stdout, "isatty") or not sys.stdout.isatty()
capture_output = False
if sys.stdout and hasattr(sys.stdout, "isatty") and not sys.stdout.isatty():
capture_output = True

self.clear_cmd_output()
if None in [cmd, args]:
if verbose >= 1 and not silent:
Expand Down Expand Up @@ -6610,7 +6610,7 @@ def exec_cmd(
if os.path.exists(path):
pythonpath[i] = win32api.GetShortPathName(path)
# Write out .wait.py file
scriptfilename = f"{waitfilename}.py"
scriptfilename = f"{waitfilename}.py"
with open(scriptfilename, "w", encoding="utf-8") as scriptfile:
scriptfile.write(pythonscript)
scriptfilename = win32api.GetShortPathName(scriptfilename)
Expand All @@ -6633,7 +6633,7 @@ def exec_cmd(
with open(waitfilename, "w") as waitfile:
waitfile.write(f"#!/usr/bin/env python3\n{pythonscript}")
os.chmod(waitfilename, 0o755)
args[index] += '%s ./%s' % (
args[index] += "%s ./%s" % (
strtr(safe_str(python), {'"': r"\"", "$": r"\$"}),
os.path.basename(waitfilename),
)
Expand Down Expand Up @@ -6977,7 +6977,9 @@ def exec_cmd(
stderr = tempfile.SpooledTemporaryFile()
if capture_output:
stdout = tempfile.SpooledTemporaryFile()
elif sys.stdout and hasattr(sys.stdout, "isatty") and sys.stdout.isatty():
elif (
sys.stdout and hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
):
stdout = sys.stdout
else:
stdout = sp.PIPE
Expand All @@ -6991,9 +6993,9 @@ def exec_cmd(
data_encoding = self.pty_encoding
kwargs = dict(timeout=20, cwd=working_dir, env=os.environ)
if sys.platform == "win32":
# FIX: stdio cp1252 vs utf-8 issue under Windows 10/11+
# FIX: stdio cp1252 vs utf-8 (cp65001) issue under Windows 10/11+
# os.environ["PYTHONLEGACYWINDOWSSTDIO"] = "1"
kwargs["codepage"] = 65001 # windll.kernel32.GetACP()
kwargs["codepage"] = windll.kernel32.GetACP() #1252 # 65001 if capture_output else 1252
# As Windows' console always hard wraps at the
# rightmost column, increase the buffer width
kwargs["columns"] = 160
Expand All @@ -7013,7 +7015,11 @@ def exec_cmd(
)
if log_output:
linebuffered_logfiles = []
if sys.stdout and hasattr(sys.stdout, "isatty") and sys.stdout.isatty():
if (
sys.stdout
and hasattr(sys.stdout, "isatty")
and sys.stdout.isatty()
):
linebuffered_logfiles.append(print)
else:
linebuffered_logfiles.append(log)
Expand Down Expand Up @@ -7217,7 +7223,9 @@ def caffeinate_wait():
):
# Restore madTPG OSD and fullscreen
self.madtpg_restore_settings(False)
self.log(f"{appname}: Sending buffer: {self.send_buffer}")
self.log(
f"{appname}: Sending buffer: {self.send_buffer}"
)
self._safe_send(self.send_buffer)
self.send_buffer = None
if not self.subprocess.isalive():
Expand Down Expand Up @@ -11194,7 +11202,11 @@ def create_profile(
):
# Smooth existing B2A tables
linebuffered_logfiles = []
if sys.stdout and hasattr(sys.stdout, "isatty") and sys.stdout.isatty():
if (
sys.stdout
and hasattr(sys.stdout, "isatty")
and sys.stdout.isatty()
):
linebuffered_logfiles.append(print)
else:
linebuffered_logfiles.append(log)
Expand Down Expand Up @@ -13138,12 +13150,12 @@ def prepare_colprof(
if not os.path.exists(f"{in_out_file}.ti3"):
return (
Error(
lang.getstr("error.measurement.file_missing", f"{in_out_file}.ti3")
lang.getstr("error.measurement.file_missing", f"{in_out_file}.ti3")
),
None,
)
if not os.path.isfile(f"{in_out_file}.ti3"):
return Error(lang.getstr("file_notfile", f"{in_out_file}.ti3")), None
return Error(lang.getstr("file_notfile", f"{in_out_file}.ti3")), None

cmd = get_argyll_util("colprof")
args = ["-v", f"-q{getcfg('profile.quality')}", f"-a{getcfg('profile.type')}"]
Expand Down Expand Up @@ -13960,7 +13972,7 @@ def prepare_dispwin(self, cal=None, profile_path=None, install=True):
# avoid profile install issues
profile_tmp_path = os.path.join(
tmp_dir,
safe_asciize(profile_name).decode("utf-8", "ignore")
safe_asciize(profile_name).decode("utf-8", "ignore"),
)
else:
profile_tmp_path = os.path.join(tmp_dir, profile_name)
Expand Down Expand Up @@ -15034,7 +15046,7 @@ def calculate_gamut(
src = os.path.splitext(os.path.basename(src))[0]
else:
if mods:
src += " " + "".join([f"[{mod.upper()}]" for mod in mods])
src += " " + "".join([f"[{mod.upper()}]" for mod in mods])
src_path = get_data_path(f"ref/{src}.gam")
if not src_path:
continue
Expand Down Expand Up @@ -15085,6 +15097,7 @@ def calculate_gamut(
filename = tmpfilename

try:

def tweak_vrml(vrml):
# Set viewpoint further away
vrml = re.sub(
Expand All @@ -15107,7 +15120,9 @@ def tweak_vrml(vrml):
)
# Add range to axes
vrml = re.sub(
rb'(string\s*\[")(\+?)(L\*)("\])', rb'\1\3", b"\2\0$\4', vrml
rb'(string\s*\[")(\+?)(L\*)("\])',
rb'\1\3", b"\2\0$\4',
vrml,
)
vrml = re.sub(
rb'(string\s*\[")([+\-]?)(a\*)("\])',
Expand Down Expand Up @@ -15193,7 +15208,9 @@ def calibrate(self, remove=False):
result = self.detect_video_levels()
if isinstance(result, Exception) or not result:
return result
capture_output = not sys.stdout or not hasattr(sys.stdout, "isatty") or not sys.stdout.isatty()
capture_output = False
if sys.stdout and hasattr(sys.stdout, "isatty") and not sys.stdout.isatty():
capture_output = True
cmd, args = self.prepare_dispcal()
if not isinstance(cmd, Exception):
print(f"cmd: {cmd}")
Expand Down Expand Up @@ -16004,9 +16021,13 @@ def ti1_lookup_to_ti3(
ofile.write(b"\n")
ofile.write(b"NUMBER_OF_FIELDS ")
if include_sample_name:
ofile.write(bytes(str(2 + len(icolor) + len(color_rep)), "utf-8") + b"\n")
ofile.write(
bytes(str(2 + len(icolor) + len(color_rep)), "utf-8") + b"\n"
)
else:
ofile.write(bytes(str(1 + len(icolor) + len(color_rep)), "utf-8") + b"\n")
ofile.write(
bytes(str(1 + len(icolor) + len(color_rep)), "utf-8") + b"\n"
)
ofile.write(b"BEGIN_DATA_FORMAT\n")
ofile.write(b"SAMPLE_ID ")
if include_sample_name:
Expand All @@ -16015,7 +16036,9 @@ def ti1_lookup_to_ti3(
ofile.write(olabel + b" " + ilabel + b"\n")
ofile.write(b"END_DATA_FORMAT\n")
ofile.write(b"\n")
ofile.write(b"NUMBER_OF_SETS " + bytes(str(len(odata)), "utf-8") + b"\n")
ofile.write(
b"NUMBER_OF_SETS " + bytes(str(len(odata)), "utf-8") + b"\n"
)
ofile.write(b"BEGIN_DATA\n")
if pcs == "x":
# Need to scale XYZ coming from xicclu, Lab is already scaled
Expand Down Expand Up @@ -16225,8 +16248,10 @@ def ti3_lookup_to_ti1(
# add required fields to DATA_FORMAT if not yet present
if (
not bytes(required[0], "utf-8") in list(ti3v.DATA_FORMAT.values())
and not bytes(required[1], "utf-8") in list(ti3v.DATA_FORMAT.values())
and not bytes(required[2], "utf-8") in list(ti3v.DATA_FORMAT.values())
and not bytes(required[1], "utf-8")
in list(ti3v.DATA_FORMAT.values())
and not bytes(required[2], "utf-8")
in list(ti3v.DATA_FORMAT.values())
):
ti3v.DATA_FORMAT.add_data(required)
ti1out.write(b'KEYWORD "COLOR_REP"\n')
Expand Down Expand Up @@ -16269,15 +16294,21 @@ def ti3_lookup_to_ti1(
)
ti1out.write(cie_.encode("utf-8"))
else:
cie_ = b"%s %s %s\n" % (bytes(str(i + 1), "utf-8"), b" ".join(device), b" ".join(cie))
cie_ = b"%s %s %s\n" % (
bytes(str(i + 1), "utf-8"),
b" ".join(device),
b" ".join(cie),
)
ti1out.write(cie_)
if i > len(wp) - 1: # don't include whitepoint patches in ti3
# set device values in ti3
for n, v in enumerate(olabels):
# Assuming 0..100, 4 decimal digits is
# enough for roughly 19 bits integer
# device values
ti3v.DATA[i - len(wp)][v.decode("utf-8")] = round(float(device[n]), 4)
ti3v.DATA[i - len(wp)][v.decode("utf-8")] = round(
float(device[n]), 4
)
# set PCS values in ti3
for n, v in enumerate(cie):
ti3v.DATA[i - len(wp)][required[n]] = float(v)
Expand Down Expand Up @@ -16393,7 +16424,7 @@ def _download(self, uri, force=False, download_dir=None):
uri = response.geturl()
filename = Path(Path(uri).name)
actualhash = sha256()
if hashes and False: # skip this for now
if hashes and False: # skip this for now
# Read max. 64 KB hashes
hashesdata = hashes.read(1024 * 64)
hashes.close()
Expand Down Expand Up @@ -17136,22 +17167,18 @@ def _write(self, txt):
and hasattr(self.patterngenerator, "conn")
)
if use_patterngenerator or self.use_madnet_tpg or self._use_patternwindow:
# sometimes the data is not fully received
# sometimes the data is not fully received
if self.partial_data != "":
# combine the partial data with the currently received one
# combine the partial data with the currently received one
self.log(f"Combining previous data of: {self.partial_data}")
self.log(f"with : {txt}")
txt = self.partial_data + txt
self.log(f"to : {txt}")

rgb = re.search(
r"Current RGB(?:\s+\d+){3}((?:\s+\d+(?:\.\d+)){3})", txt
)
rgb = re.search(r"Current RGB(?:\s+\d+){3}((?:\s+\d+(?:\.\d+)){3})", txt)
# Check if the data is partial
if "Current RGB" in txt and rgb is None:
self.log(
f"Data is not fully received, storing partial data: {txt}"
)
self.log(f"Data is not fully received, storing partial data: {txt}")
self.partial_data = txt

if rgb:
Expand Down

0 comments on commit 14933b9

Please sign in to comment.