Skip to content

Commit

Permalink
str.format() => fstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeCarrier committed Apr 17, 2024
1 parent a54d24c commit 4ae35c4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
26 changes: 12 additions & 14 deletions mkdocs_drawio_exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def __init__(self, key, value, message):
Exception.__init__(self, self.message)

def __str__(self):
return 'drawio-exporter: value "{}" for key "{}" is invalid: {}'.format(
self.value, self.key, self.message)
return f'drawio-exporter: value "{self.value}" for key "{self.key}" is invalid: {self.message}'

def drawio_executable(value, message):
"""Raise an error for a misconfigured Draw.io path.
Expand Down Expand Up @@ -159,7 +158,7 @@ def drawio_executable_paths(self, platform):
program_files.append(os.environ['ProgramFiles(x86)'])
return [os.path.join(dir, 'draw.io', 'draw.io.exe') for dir in program_files]
else:
self.log.warning('Draw.io executable paths not known for platform "{}"'.format(platform))
self.log.warning(f'Draw.io executable paths not known for platform "{platform}"')

def prepare_cache_dir(self, cache_dir, docs_dir):
"""Ensure the cache path is set, absolute and exists.
Expand Down Expand Up @@ -191,15 +190,14 @@ def prepare_drawio_executable(self, executable, executable_names, platform_execu
for name in executable_names:
executable = shutil.which(name)
if executable:
self.log.debug('Found Draw.io executable "{}" at "{}"'.format(name, executable))
self.log.debug(f'Found Draw.io executable "{name}" at "{executable}"')
return executable

candidates = platform_executable_paths
self.log.debug('Trying paths {} for platform "{}"'.format(candidates, sys.platform))
self.log.debug(f'Trying paths {candidates} for platform "{sys.platform}"')
for candidate in candidates:
if os.path.isfile(candidate):
self.log.debug('Found Draw.io executable for platform "{}" at "{}"'.format(
sys.platform, candidate))
self.log.debug(f'Found Draw.io executable for platform "{sys.platform}" at "{candidate}"')
return candidate

raise ConfigurationError.drawio_executable(
Expand All @@ -225,7 +223,7 @@ def replace(match):

if fnmatch.fnmatch(filename, sources):
content_sources.append(Source(filename, page_index))
img_src = "{}-{}.{}".format(filename, page_index, format)
img_src = f"{filename}-{page_index}.{format}"

return embed_format.format(
img_open=match.group(1), img_close=match.group(3),
Expand Down Expand Up @@ -261,13 +259,13 @@ def ensure_file_cached(self, source, source_rel, page_index, drawio_executable,
exit_status = None

if self.use_cached_file(source, cache_filename):
self.log.debug('Source file appears unchanged; using cached copy from "{}"'.format(cache_filename))
self.log.debug(f'Source file appears unchanged; using cached copy from "{cache_filename}"')
else:
if not drawio_executable:
self.log.warning('Skipping export of "{}" as Draw.io executable not available'.format(source))
self.log.warning(f'Skipping export of "{source}" as Draw.io executable not available')
return (None, exit_status)

self.log.debug('Exporting "{}" to "{}"'.format(source, cache_filename))
self.log.debug(f'Exporting "{source}" to "{cache_filename}"')
exit_status = self.export_file(
source, page_index, cache_filename,
drawio_executable, drawio_args, format)
Expand All @@ -282,8 +280,8 @@ def make_cache_filename(self, source, page_index, cache_dir):
:param str cache_dir: Export cache directory.
:return str: Resulting filename.
"""
basename = '{}-{}'.format(
hashlib.sha1(source.encode('utf-8')).hexdigest(), page_index)
filename_hash = hashlib.sha1(source.encode('utf-8')).hexdigest()
basename = f'{filename_hash}-{page_index}'
return os.path.join(cache_dir, basename)

def use_cached_file(self, source, cache_filename):
Expand Down Expand Up @@ -317,7 +315,7 @@ def export_file(self, source, page_index, dest, drawio_executable, drawio_args,
cmd += drawio_args

try:
self.log.debug('Using export command {}'.format(cmd))
self.log.debug(f'Using export command {cmd}')
return subprocess.call(cmd)
except:
self.log.exception('Subprocess raised exception')
15 changes: 7 additions & 8 deletions mkdocs_drawio_exporter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def on_config(self, config):

os.makedirs(self.config['cache_dir'], exist_ok=True)

log.debug('Using Draw.io executable "{}", arguments {} and cache directory "{}"'.format(
self.config['drawio_executable'], self.config['drawio_args'],
self.config['cache_dir']))
log.debug(f'Using Draw.io executable "{self.config['drawio_executable']}", '
f'arguments {self.config['drawio_args']} and '
f'cache directory "{self.config['cache_dir']}"')

def on_post_page(self, output_content, page, **kwargs):
output_content, content_sources = self.exporter.rewrite_image_embeds(
Expand All @@ -65,18 +65,17 @@ def on_post_page(self, output_content, page, **kwargs):

def on_files(self, files, config):
keep = self.exporter.filter_cache_files(files, self.config['cache_dir'])
log.debug('{} files left after excluding cache'.format(len(keep)))
log.debug(f'{len(keep)} files left after excluding cache')

return Files(keep)

def on_post_build(self, config):
sources = set(self.sources)
log.debug('Found {} unique sources in {} total embeds'.format(len(sources), len(self.sources)))
log.debug(f'Found {len(sources)} unique sources in {len(self.sources)} total embeds')
self.sources = []

for source in sources:
dest_rel_path = '{}-{}.{}'.format(
source.source_rel, source.page_index, self.config['format'])
dest_rel_path = f'{source.source_rel}-{source.page_index}.{self.config['format']}'
abs_src_path = os.path.join(config['docs_dir'], source.source_rel)
abs_dest_path = os.path.join(config['site_dir'], dest_rel_path)
cache_filename, exit_status = self.exporter.ensure_file_cached(
Expand All @@ -85,7 +84,7 @@ def on_post_build(self, config):
self.config['cache_dir'], self.config['format'])

if exit_status not in (None, 0):
log.error('Export failed with exit status {}; skipping copy'.format(exit_status))
log.error(f'Export failed with exit status {exit_status}; skipping copy')
continue

try:
Expand Down
2 changes: 1 addition & 1 deletion mkdocs_drawio_exporter/tests/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def getmtime_return_value(path):
elif path == source:
return 1577133635.3318102
else:
raise ValueError('didn\'t expect path "{}"'.format(path))
raise ValueError(f'didn\'t expect path "{path}"')
getmtime_mock.side_effect = getmtime_return_value

result = self.exporter.use_cached_file(source, cache_filename)
Expand Down

0 comments on commit 4ae35c4

Please sign in to comment.