1 |
commit: 7db7af132887952418cf2ea0fdcf4513e6f69adf |
2 |
Author: aeroniero33 <justthisthing <AT> gmail <DOT> com> |
3 |
AuthorDate: Sat Aug 27 14:22:28 2016 +0000 |
4 |
Commit: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Dec 24 04:54:14 2016 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/gentoo-keys.git/commit/?id=7db7af13 |
7 |
|
8 |
Added some util methods in keyhandler |
9 |
|
10 |
The methods are: |
11 |
is_expiring that checks if a key is expiring or has recently expired |
12 |
set_template that reads the template file and returns it as a string |
13 |
generate_template that substitutes the key prints in the template |
14 |
find_email that extracts the correct email address from the key uid |
15 |
|
16 |
gkeys/gkeys/keyhandler.py | 42 ++++++++++++++++++++++++++++++++++++++++++ |
17 |
1 file changed, 42 insertions(+) |
18 |
|
19 |
diff --git a/gkeys/gkeys/keyhandler.py b/gkeys/gkeys/keyhandler.py |
20 |
index 9043fcd..0a02c22 100644 |
21 |
--- a/gkeys/gkeys/keyhandler.py |
22 |
+++ b/gkeys/gkeys/keyhandler.py |
23 |
@@ -11,6 +11,9 @@ |
24 |
""" |
25 |
import os |
26 |
import sys |
27 |
+import re |
28 |
+ |
29 |
+from string import Template |
30 |
|
31 |
from snakeoil.demandload import demandload |
32 |
|
33 |
@@ -108,3 +111,42 @@ class KeyHandler(object): |
34 |
|
35 |
self.logger.debug(_unicode("KeyHandler: key_search; keys = %s") % str(keys)) |
36 |
return keys |
37 |
+ |
38 |
+ @staticmethod |
39 |
+ def is_expiring(keys, days_limit=30): |
40 |
+ '''Check if any of the keys is within the days_limit''' |
41 |
+ is_exp = False |
42 |
+ for key in keys: |
43 |
+ for specs in keys[key]: |
44 |
+ if specs.days > days_limit*(-1) and specs.days < days_limit: |
45 |
+ is_exp = True |
46 |
+ break |
47 |
+ return is_exp |
48 |
+ |
49 |
+ @staticmethod |
50 |
+ def set_template(template_path): |
51 |
+ '''Read the template file and returns the template message''' |
52 |
+ with open(template_path, 'r') as file_contents: |
53 |
+ content = file_contents.read() |
54 |
+ message_template = Template(content) |
55 |
+ return message_template |
56 |
+ |
57 |
+ @staticmethod |
58 |
+ def generate_template(message_template, keyprints, specprint): |
59 |
+ '''Substitute the print variables in the template''' |
60 |
+ message = message_template.substitute(key_print=keyprints, spec_print=specprint) |
61 |
+ return message |
62 |
+ |
63 |
+ @staticmethod |
64 |
+ def find_email(uids, prefered_address=None): |
65 |
+ '''Find the email address from the uid by prioritizing the prefered address''' |
66 |
+ if type(prefered_address) is not str: |
67 |
+ uids = [uids[0]] |
68 |
+ for uid in uids: |
69 |
+ match = re.findall(r'[\w\.-]+@[\w\.-]+', uid) |
70 |
+ uid = '' |
71 |
+ if match: |
72 |
+ uid = match[0] |
73 |
+ if prefered_address and uid.endswith(prefered_address): |
74 |
+ return uid |
75 |
+ return uid |