From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 30AB2139088 for ; Fri, 23 Dec 2016 08:37:19 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 83592E0DD7; Fri, 23 Dec 2016 08:37:17 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 5DFA5E0DD9 for ; Fri, 23 Dec 2016 08:37:17 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id C7BD534112A for ; Fri, 23 Dec 2016 08:37:15 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id BC79624E7 for ; Fri, 23 Dec 2016 08:37:13 +0000 (UTC) From: "Brian Dolbec" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" Message-ID: <1472326992.693f78919985b86620bc76ba80926edeeae0b91b.dolsen@gentoo> Subject: [gentoo-commits] proj/gentoo-keys:gsoc-2016 commit in: gkeys/gkeys/ X-VCS-Repository: proj/gentoo-keys X-VCS-Files: gkeys/gkeys/keyhandler.py X-VCS-Directories: gkeys/gkeys/ X-VCS-Committer: dolsen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: 693f78919985b86620bc76ba80926edeeae0b91b X-VCS-Branch: gsoc-2016 Date: Fri, 23 Dec 2016 08:37:13 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 29d71e0e-6bd9-45db-8b3f-a725ee0fa4c7 X-Archives-Hash: 3c89cf4c42f961afbd65f5c0415eb34d commit: 693f78919985b86620bc76ba80926edeeae0b91b Author: aeroniero33 gmail com> AuthorDate: Sat Aug 27 14:22:28 2016 +0000 Commit: Brian Dolbec gentoo 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