[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [bridgedb/master] Change isValidRouterNickname() to only return booleans.



commit 192a3e876015fd6bc1414af2df55945bb79471a2
Author: Isis Lovecruft <isis@xxxxxxxxxxxxxx>
Date:   Thu Dec 11 04:28:04 2014 +0000

    Change isValidRouterNickname() to only return booleans.
---
 lib/bridgedb/parse/networkstatus.py |    2 ++
 lib/bridgedb/parse/nickname.py      |   25 +++++++++++++++++--------
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/lib/bridgedb/parse/networkstatus.py b/lib/bridgedb/parse/networkstatus.py
index 106edd9..8140fe0 100644
--- a/lib/bridgedb/parse/networkstatus.py
+++ b/lib/bridgedb/parse/networkstatus.py
@@ -39,6 +39,8 @@ from twisted.python.log import showwarning
 from bridgedb.parse import addr
 from bridgedb.parse import parseUnpaddedBase64
 from bridgedb.parse import InvalidBase64
+from bridgedb.parse.nickname import InvalidRouterNickname
+from bridgedb.parse.nickname import isValidRouterNickname
 
 
 class NetworkstatusParsingError(Exception):
diff --git a/lib/bridgedb/parse/nickname.py b/lib/bridgedb/parse/nickname.py
index b38bdb8..9c3a559 100644
--- a/lib/bridgedb/parse/nickname.py
+++ b/lib/bridgedb/parse/nickname.py
@@ -33,16 +33,25 @@ class InvalidRouterNickname(ValueError):
 def isValidRouterNickname(nickname):
     """Determine if a router's given nickname meets the specification.
 
-    :raises InvalidRouterNickname: if the nickname is invalid.
     :param string nickname: An OR's nickname.
+    :rtype: bool
+    :returns: ``True`` if the nickname is valid, ``False`` otherwise.
     """
     ALPHANUMERIC = string.letters + string.digits
 
-    if not (1 <= len(nickname) <= 19):
-        raise InvalidRouterNickname(
-            "Nicknames must be between 1 and 19 characters: %r" % nickname)
-    for letter in nickname:
-        if not letter in ALPHANUMERIC:
+    try:
+        if not (1 <= len(nickname) <= 19):
             raise InvalidRouterNickname(
-                "Nicknames must only use [A-Za-z0-9]: %r" % nickname)
-    return True
+                "Nicknames must be between 1 and 19 characters: %r" % nickname)
+        for letter in nickname:
+            if not letter in ALPHANUMERIC:
+                raise InvalidRouterNickname(
+                    "Nicknames must only use [A-Za-z0-9]: %r" % nickname)
+    except InvalidRouterNickname as error:
+        logging.error(str(error))
+    except TypeError:  # The nickname was probably still set to ``None``
+        pass
+    else:
+        return True
+
+    return False



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits