From 0f7eb871fd216d317252b88e75e50e4b3ea3d9fb Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 21 Jun 2016 21:52:20 +0200 Subject: [PATCH] slightly refactor placeholder related code - move from instance method to global function, so it can be used in other contexts also - rename preformat_text -> replace_placeholders --- borg/helpers.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/borg/helpers.py b/borg/helpers.py index fad401a52..90f8f3a34 100644 --- a/borg/helpers.py +++ b/borg/helpers.py @@ -566,6 +566,20 @@ def format_line(format, data): return '' +def replace_placeholders(text): + """Replace placeholders in text with their values.""" + current_time = datetime.now() + data = { + 'pid': os.getpid(), + 'fqdn': socket.getfqdn(), + 'hostname': socket.gethostname(), + 'now': current_time.now(), + 'utcnow': current_time.utcnow(), + 'user': uid2user(os.getuid(), os.getuid()) + } + return format_line(text, data) + + def safe_timestamp(item_timestamp_ns): try: return datetime.fromtimestamp(bigint_to_int(item_timestamp_ns) / 1e9) @@ -720,21 +734,8 @@ class Location: if not self.parse(self.orig): raise ValueError - def preformat_text(self, text): - """Format repository and archive path with common tags""" - current_time = datetime.now() - data = { - 'pid': os.getpid(), - 'fqdn': socket.getfqdn(), - 'hostname': socket.gethostname(), - 'now': current_time.now(), - 'utcnow': current_time.utcnow(), - 'user': uid2user(os.getuid(), os.getuid()) - } - return format_line(text, data) - def parse(self, text): - text = self.preformat_text(text) + text = replace_placeholders(text) valid = self._parse(text) if valid: return True