from typing import Callable
from typing import Union
import re


def process_paths(
		text: str, pattern: str, process_match: Callable[[str], None], replacement: Union[str, Callable[..., str]]):
	"""
	It searches in a text for strings corresponding to files (maybe including a path), replaces them by another
	string according to some function and, additionally, processes each file according to another function.

	Parameters
	----------
	text : str
		Input text.
	pattern : str
		Regular expression including a capturing group that yields the file.
	process_match : Callable[[str], None]
		Function that will *process* each file.
	replacement : str or Callable[[re.Match], str]
		Regular expression making use of the capturing group or function processing the match delivered by `pattern`

	Returns
	-------
	out: str
		Output text with the replacements performed, after having processed all the files.

	"""

	# all the matching files in the given text
	files = re.findall(pattern, text)

	# breakpoint()

	# every one of them...
	for file in files:
		# ...is processed
		process_match(file)

	# replacement of matches
	return re.sub(pattern, replacement, text)