Skip to content

Commit

Permalink
Ensuring we have the placeholders for a command
Browse files Browse the repository at this point in the history
  • Loading branch information
WillJRoper committed May 25, 2024
1 parent 00fb503 commit de30114
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/dir_wand/command_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import os
import re
import threading


Expand Down Expand Up @@ -44,6 +45,25 @@ def __init__(self, command):
# complete at a later stage
self.threads = []

# Unpack placeholders
self._placeholders = set()
self.get_placeholders()

def get_placeholders(self):
"""Extract what place holders the command contains."""
# Define the regex pattern to extract the placeholders
# (e.g. {placeholder})
pattern = re.compile(r"\{([a-zA-Z0-9_]+)\}")

# Find all the matches in the command
matches = pattern.findall(self.command)

# If we have matches...
if matches is not None:
# Add the placeholders to the set
for match in matches:
self._placeholders.add(match)

def run_command(self, **swaps):
"""
Run the command.
Expand All @@ -63,6 +83,11 @@ def run_command(self, **swaps):
if not self.command:
return

# Ensure we have all the placeholders before we start swapping
missing = self._placeholders - set(swaps.keys())
if len(missing) > 0:
raise ValueError(f"Missing placeholders: {missing}")

# Replace any swaps in the command
command = self.command.format(**swaps)

Expand Down Expand Up @@ -110,6 +135,8 @@ def run_command_for_all_swaps(self, **swaps):
{swap: swaps[swap][i] for swap in swaps} for i in range(nswaps)
]

# Ensure we have all the swaps we need for the command

# Loop over the swaps we'll have to make
for swap in swaps:
self.run_command(**swap)
Expand Down

0 comments on commit de30114

Please sign in to comment.