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 98FF9139085 for ; Sat, 24 Dec 2016 09:13:49 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id E71B12340EB; Sat, 24 Dec 2016 09:13:29 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (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 C77B92340EB for ; Sat, 24 Dec 2016 09:13:24 +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 EFC553412CB for ; Sat, 24 Dec 2016 09:13:18 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 604B524F0 for ; Sat, 24 Dec 2016 09:13:16 +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: <1482555254.7db7af132887952418cf2ea0fdcf4513e6f69adf.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: 7db7af132887952418cf2ea0fdcf4513e6f69adf X-VCS-Branch: gsoc-2016 Date: Sat, 24 Dec 2016 09:13:16 +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: e40f8dda-ac9a-49c0-9638-3451ca9b5447 X-Archives-Hash: 50c3336e1c26cdf579b587849b96429b commit: 7db7af132887952418cf2ea0fdcf4513e6f69adf Author: aeroniero33 gmail com> AuthorDate: Sat Aug 27 14:22:28 2016 +0000 Commit: Brian Dolbec gentoo org> CommitDate: Sat Dec 24 04:54:14 2016 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-keys.git/commit/?id=7db7af13 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