From e5ac938486edd6edf3b0ec21b14cb16cf095debd Mon Sep 17 00:00:00 2001 From: Isis Lovecruft Date: Sat, 11 May 2013 18:30:22 +0000 Subject: [PATCH] Add hastily written check_preferences(). * TODO I sleepily realised two lines before the end of bashing this out that it should just be a set difference. --- gnupg/parsers.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/gnupg/parsers.py b/gnupg/parsers.py index 3707191..ab10a0c 100644 --- a/gnupg/parsers.py +++ b/gnupg/parsers.py @@ -44,6 +44,33 @@ class UsageError(Exception): """Raised when incorrect usage of the API occurs..""" +def _check_preferences(prefs, pref_type=None): + cipher = frozenset(['AES256', 'AES192', 'CAMELLIA256', 'CAMELLIA192', + 'TWOFISH',]) + digest = frozenset(['SHA512', 'SHA384', 'SHA256', 'SHA224']) + compress = frozenset(['ZLIB', 'ZIP', 'Uncompressed']) + all = frozenset([ciphers, hashes, compress]) + + if isinstance(prefs, str): + prefs = prefs.split(' ') + if not pref_type: + pref_type = all + + ## xxx we should use set differences + if pref_type == 'cipher': + for pref in prefs: + if not cipher.contains(pref): return + if pref_type == 'digest': + for pref in prefs: + if not digest.contains(pref): return + if pref_type == 'compress': + for pref in prefs: + if not compress.contains(pref): return + if pref_type == 'all': + for pref in prefs: + if not all.contains(pref): return + return ' '.join([pref for pref in prefs]) + def _fix_unsafe(shell_input): """Find characters used to escape from a string into a shell, and wrap them in quotes if they exist. Regex pilfered from python-3.x shlex module.