diff options
author | 2016-08-27 14:22:28 +0000 | |
---|---|---|
committer | 2018-07-06 22:22:08 -0700 | |
commit | f2764e8aaf3d19ee694e77aae5b20c904440d211 (patch) | |
tree | b0c5e5494ff19efc9512695f049eb3dfe7969083 | |
parent | Added a mail script that handles the emailing proccess (diff) | |
download | gentoo-keys-f2764e8aaf3d19ee694e77aae5b20c904440d211.tar.gz gentoo-keys-f2764e8aaf3d19ee694e77aae5b20c904440d211.tar.bz2 gentoo-keys-f2764e8aaf3d19ee694e77aae5b20c904440d211.zip |
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
-rw-r--r-- | gkeys/gkeys/keyhandler.py | 42 |
1 files changed, 42 insertions, 0 deletions
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 |