Skip to content

Commit

Permalink
[#370] Updated DisplayCAL.config to use f-strings as much as possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
eoyilmaz committed Sep 27, 2024
1 parent d64c9e7 commit 8b8917e
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions DisplayCAL/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def getbitmap(name, display_missing_icon=True, scale=True, use_mask=False):
if not bmp.IsOk():
path = None
if not path:
print("Warning: Missing bitmap '%s'" % name)
print(f"Warning: Missing bitmap '{name}'")
img = wx.Image(w, h)
img.SetMaskColour(0, 0, 0)
img.InitAlpha()
Expand Down Expand Up @@ -557,7 +557,7 @@ def get_argyll_display_number(geometry):
for i, display in enumerate(getcfg("displays")):
if display.find(f"@ {geometry}") > -1:
if debug:
print("[D] Found display %s at index %i" % (geometry, i))
print(f"[D] Found display {geometry} at index {i}")
return i


Expand Down Expand Up @@ -602,7 +602,7 @@ def get_icon_bundle(sizes, name):
iconbundle = wx.IconBundle()
if not sizes:
# Assume ICO format
pth = get_data_path("theme/icons/%s.ico" % name)
pth = get_data_path(f"theme/icons/{name}.ico")
if pth:
ico = wx.Icon(pth)
if ico.IsOk():
Expand Down Expand Up @@ -688,8 +688,7 @@ def get_data_path(relpath, rex=None):
filelist = listdir_re(curpath, rex)
except Exception as exception:
print(
"Error - directory '%s' listing failed: %s"
% (curpath, exception)
f"Error - directory '{curpath}' listing failed: {exception}"
)
else:
for filename in filelist:
Expand Down Expand Up @@ -1060,7 +1059,7 @@ def runtimeconfig(pyfile):
"comport.number": 1,
"comport.number.backup": 1,
# Note: worker.Worker.enumerate_displays_and_ports() overwrites copyright
"copyright": "No copyright. Created with %s %s and ArgyllCMS" % (appname, version),
"copyright": f"No copyright. Created with {appname} {version} and ArgyllCMS",
"dimensions.measureframe": "0.5,0.5,1.0",
"dimensions.measureframe.unzoomed": "0.5,0.5,1.0",
"dimensions.measureframe.whitepoint.visual_editor": "0.5,0.5,1.0",
Expand Down Expand Up @@ -1457,7 +1456,7 @@ def getcfg(name, fallback=True, raw=False, cfg=cfg):
value = "g"
elif name in valid_values and value not in valid_values[name]:
if debug:
print("Invalid config value for %s: %s" % (name, value), end=" ")
print(f"Invalid config value for {name}: {value}", end=" ")
value = None
elif name == "copyright":
# Make sure DisplayCAL and Argyll version are up-to-date
Expand Down Expand Up @@ -1509,7 +1508,7 @@ def getcfg(name, fallback=True, raw=False, cfg=cfg):
# colorimeter_correction_matrix_file is special because it's
# not (only) a path
if debug:
print("%s does not exist: %s" % (name, value), end=" ")
print(f"{name} does not exist: {value}", end=" ")
# Normalize path (important, this turns altsep into sep under Windows)
value = os.path.normpath(value)
# Check if this is a relative path covered by data_dirs
Expand Down Expand Up @@ -1592,7 +1591,7 @@ def get_display_profile(display_no=None):
except Exception:
from DisplayCAL.log import log

print("ICCP.get_display_profile(%s):" % display_no, file=log)
print(f"ICCP.get_display_profile({display_no}):", file=log)


standard_profiles = []
Expand Down Expand Up @@ -1794,8 +1793,8 @@ def makecfgdir(which="user", worker=None):
os.makedirs(confighome)
except Exception as exception:
print(
"Warning - could not create configuration directory '%s': %s"
% (confighome, exception)
"Warning - could not create configuration directory "
f"'{confighome}': {exception}"
)
return False
elif not os.path.exists(config_sys):
Expand All @@ -1816,8 +1815,8 @@ def makecfgdir(which="user", worker=None):
raise result
except Exception as exception:
print(
"Warning - could not create configuration directory '%s': %s"
% (config_sys, exception)
"Warning - could not create configuration directory "
f"'{config_sys}': {exception}"
)
return False
return True
Expand All @@ -1833,7 +1832,7 @@ def initcfg(module=None, cfg=cfg, force_load=False):
settings directory if nonexistent.
"""
if module:
cfgbasename = "%s-%s" % (appbasename, module)
cfgbasename = f"{appbasename}-{module}"
else:
cfgbasename = appbasename
makecfgdir()
Expand All @@ -1849,7 +1848,7 @@ def initcfg(module=None, cfg=cfg, force_load=False):
cfgnames.append(cfgbasename)
else:
cfgnames.extend(
"%s-%s" % (appbasename, othermod) for othermod in ("testchart-editor",)
f"{appbasename}-{othermod}" for othermod in ("testchart-editor",)
)

cfgroots = [confighome]
Expand All @@ -1864,7 +1863,7 @@ def initcfg(module=None, cfg=cfg, force_load=False):
try:
mtime = os.stat(cfgfile).st_mtime
except EnvironmentError as exception:
print("Warning - os.stat('%s') failed: %s" % (cfgfile, exception))
print(f"Warning - os.stat('{cfgfile}') failed: {exception}")
last_checked = cfginited.get(cfgfile)
if force_load or mtime != last_checked:
cfginited[cfgfile] = mtime
Expand All @@ -1890,7 +1889,9 @@ def initcfg(module=None, cfg=cfg, force_load=False):
# if it can't be parsed
except Exception:
print(
"Warning - could not parse configuration files:\n%s" % "\n".join(cfgfiles)
"Warning - could not parse configuration files:\n%s".format(
"\n".join(cfgfiles)
)
)
# Fix Python 2.7 ConfigParser option values being lists instead of
# strings in case of a ParsingError. http://bugs.python.org/issue24142
Expand Down Expand Up @@ -2130,7 +2131,7 @@ def writecfg(which="user", worker=None, module=None, options=(), cfg=cfg):
worker: worker instance if ``which == 'system'``
"""
if module:
cfgbasename = "%s-%s" % (appbasename, module)
cfgbasename = f"{appbasename}-{module}"
else:
cfgbasename = appbasename
# Remove unknown options
Expand All @@ -2156,13 +2157,12 @@ def writecfg(which="user", worker=None, module=None, options=(), cfg=cfg):
optionlines = lines[1:]
# Sorting works as long as config has only one section
lines = lines[:1] + sorted(optionlines)
cfgfile = open(cfgfilename, "wb")
cfgfile.write((os.linesep.join(lines) + os.linesep).encode())
cfgfile.close()
with open(cfgfilename, "wb") as cfgfile:
cfgfile.write((os.linesep.join(lines) + os.linesep).encode())
except Exception as exception:
print(
"Warning - could not write user configuration file "
"'%s': %s" % (cfgfilename, exception)
f"'{cfgfilename}': {exception}"
)
return False
else:
Expand All @@ -2178,12 +2178,11 @@ def writecfg(which="user", worker=None, module=None, options=(), cfg=cfg):
if getcfg("argyll.dir"):
cfgfile.write(
(
"%s%s"
% (
"%s%s".format(
os.linesep.join(
[
"[Default]",
"%s = %s" % ("argyll.dir", getcfg("argyll.dir")),
f"argyll.dir = {getcfg('argyll.dir')}",
]
),
os.linesep,
Expand All @@ -2207,8 +2206,8 @@ def writecfg(which="user", worker=None, module=None, options=(), cfg=cfg):
raise result
except Exception as exception:
print(
"Warning - could not write system-wide configuration file '%s': %s"
% (cfgfilename2, exception)
"Warning - could not write system-wide configuration file "
f"'{cfgfilename2}': {exception}"
)
return False
return True
Expand Down

0 comments on commit 8b8917e

Please sign in to comment.