commit:     693f78919985b86620bc76ba80926edeeae0b91b
Author:     aeroniero33 <justthisthing <AT> gmail <DOT> com>
AuthorDate: Sat Aug 27 14:22:28 2016 +0000
Commit:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
CommitDate: Sat Aug 27 19:43:12 2016 +0000
URL:        https://gitweb.gentoo.org/proj/gentoo-keys.git/commit/?id=693f7891

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

Reply via email to