From f2764e8aaf3d19ee694e77aae5b20c904440d211 Mon Sep 17 00:00:00 2001 From: aeroniero33 Date: Sat, 27 Aug 2016 14:22:28 +0000 Subject: Added some util methods in keyhandler The methods are: is_expiring that checks if a key is expiring or has recently expired set_template that reads the template file and returns it as a string generate_template that substitutes the key prints in the template find_email that extracts the correct email address from the key uid --- gkeys/gkeys/keyhandler.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/gkeys/gkeys/keyhandler.py b/gkeys/gkeys/keyhandler.py index 9043fcd..0a02c22 100644 --- a/gkeys/gkeys/keyhandler.py +++ b/gkeys/gkeys/keyhandler.py @@ -11,6 +11,9 @@ """ import os import sys +import re + +from string import Template from snakeoil.demandload import demandload @@ -108,3 +111,42 @@ class KeyHandler(object): self.logger.debug(_unicode("KeyHandler: key_search; keys = %s") % str(keys)) return keys + + @staticmethod + def is_expiring(keys, days_limit=30): + '''Check if any of the keys is within the days_limit''' + is_exp = False + for key in keys: + for specs in keys[key]: + if specs.days > days_limit*(-1) and specs.days < days_limit: + is_exp = True + break + return is_exp + + @staticmethod + def set_template(template_path): + '''Read the template file and returns the template message''' + with open(template_path, 'r') as file_contents: + content = file_contents.read() + message_template = Template(content) + return message_template + + @staticmethod + def generate_template(message_template, keyprints, specprint): + '''Substitute the print variables in the template''' + message = message_template.substitute(key_print=keyprints, spec_print=specprint) + return message + + @staticmethod + def find_email(uids, prefered_address=None): + '''Find the email address from the uid by prioritizing the prefered address''' + if type(prefered_address) is not str: + uids = [uids[0]] + for uid in uids: + match = re.findall(r'[\w\.-]+@[\w\.-]+', uid) + uid = '' + if match: + uid = match[0] + if prefered_address and uid.endswith(prefered_address): + return uid + return uid -- cgit v1.2.3-65-gdbad