summaryrefslogtreecommitdiff
path: root/src/leap/keymanager/validation.py
diff options
context:
space:
mode:
authorRuben Pollan <meskio@sindominio.net>2015-06-26 18:12:22 +0200
committerRuben Pollan <meskio@sindominio.net>2015-06-27 00:39:36 +0200
commitd7acaf356d16c795f1481fefc7a75ffc8a36b1d0 (patch)
treed7767fca75892cfa7b2a80b4adbe3928f8f6c07e /src/leap/keymanager/validation.py
parentb1ac5bdbed955cf5a88c76dd90691266e45d277c (diff)
[bug] remove the dependency on enum34
* Resolves: #7188
Diffstat (limited to 'src/leap/keymanager/validation.py')
-rw-r--r--src/leap/keymanager/validation.py73
1 files changed, 51 insertions, 22 deletions
diff --git a/src/leap/keymanager/validation.py b/src/leap/keymanager/validation.py
index dfe64329..3bb40325 100644
--- a/src/leap/keymanager/validation.py
+++ b/src/leap/keymanager/validation.py
@@ -24,34 +24,63 @@ See:
from datetime import datetime
-from enum import IntEnum
-ValidationLevel = IntEnum("ValidationLevel",
- "Weak_Chain "
- "Provider_Trust "
- "Provider_Endorsement "
- "Third_Party_Endorsement "
- "Third_Party_Consensus "
- "Historically_Auditing "
- "Known_Key "
- "Fingerprint")
+class ValidationLevel(object):
+ """
+ A validation level
+
+ Meant to be used to compare levels or get it's string representation.
+ """
+ def __init__(self, name, value):
+ self.name = name
+ self.value = value
+
+ def __cmp__(self, other):
+ return cmp(self.value, other.value)
+
+ def __str__(self):
+ return self.name
+
+ def __repr__(self):
+ return "<ValidationLevel: %s (%d)>" % (self.name, self.value)
-def toValidationLevel(value):
+class _ValidationLevels(object):
"""
- Convert a string representation of a validation level into
- C{ValidationLevel}
+ Handler class to manage validation levels. It should have only one global
+ instance 'ValidationLevels'.
- :param value: validation level
- :type value: str
- :rtype: ValidationLevel
- :raises ValueError: if C{value} is not a validation level
+ The levels are attributes of the instance and can be used like:
+ ValidationLevels.Weak_Chain
+ ValidationLevels.get("Weak_Chain")
"""
- for level in ValidationLevel:
- if value == level.name:
- return level
- raise ValueError("Not valid validation level: %s" % (value,))
+ _level_names = ("Weak_Chain",
+ "Provider_Trust",
+ "Provider_Endorsement",
+ "Third_Party_Endorsement",
+ "Third_Party_Consensus",
+ "Historically_Auditing",
+ "Known_Key",
+ "Fingerprint")
+
+ def __init__(self):
+ for name in self._level_names:
+ setattr(self, name,
+ ValidationLevel(name, self._level_names.index(name)))
+
+ def get(self, name):
+ """
+ Get the ValidationLevel of a name
+
+ :param name: name of the level
+ :type name: str
+ :rtype: ValidationLevel
+ """
+ return getattr(self, name)
+
+
+ValidationLevels = _ValidationLevels()
def can_upgrade(new_key, old_key):
@@ -69,7 +98,7 @@ def can_upgrade(new_key, old_key):
return True
# Manually verified fingerprint
- if new_key.validation == ValidationLevel.Fingerprint:
+ if new_key.validation == ValidationLevels.Fingerprint:
return True
# Expired key and higher validation level