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
This commit is contained in:
Thomas Waldmann 2016-06-21 21:52:20 +02:00
parent 195a6b9093
commit 0f7eb871fd
1 changed files with 15 additions and 14 deletions

View File

@ -566,6 +566,20 @@ def format_line(format, data):
return '' 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): def safe_timestamp(item_timestamp_ns):
try: try:
return datetime.fromtimestamp(bigint_to_int(item_timestamp_ns) / 1e9) return datetime.fromtimestamp(bigint_to_int(item_timestamp_ns) / 1e9)
@ -720,21 +734,8 @@ class Location:
if not self.parse(self.orig): if not self.parse(self.orig):
raise ValueError 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): def parse(self, text):
text = self.preformat_text(text) text = replace_placeholders(text)
valid = self._parse(text) valid = self._parse(text)
if valid: if valid:
return True return True