[tor-commits] [arm/release] Input field validation for monthly limit

atagar at torproject.org atagar at torproject.org
Sun Jul 17 06:08:30 UTC 2011


commit 5625ac4baf6f333e024ce4537031ef434416a0ec
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jul 3 17:51:49 2011 -0700

    Input field validation for monthly limit
---
 src/cli/wizard.py |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/src/cli/wizard.py b/src/cli/wizard.py
index 43d107b..16752e7 100644
--- a/src/cli/wizard.py
+++ b/src/cli/wizard.py
@@ -211,6 +211,7 @@ def showWizard():
   
   # sets input validators
   config[Options.BANDWIDTH].setValidator(_relayRateValidator)
+  config[Options.LIMIT].setValidator(_monthlyLimitValidator)
   config[Options.BRIDGE1].setValidator(_bridgeDestinationValidator)
   config[Options.BRIDGE2].setValidator(_bridgeDestinationValidator)
   config[Options.BRIDGE3].setValidator(_bridgeDestinationValidator)
@@ -444,6 +445,19 @@ def _relayRateValidator(option, value):
   elif (int(rate) < 20 and units == "KB/s") or int(rate) < 1:
     raise ValueError("To be usable as a relay the rate must be at least 20 KB/s")
 
+def _monthlyLimitValidator(option, value):
+  if value.count(" ") != 1:
+    msg = "This should be a measurement followed by the units (for instance, \"5 MB\")"
+    raise ValueError(msg)
+  
+  rate, units = value.split(" ", 1)
+  acceptedUnits = ("MB", "GB", "TB")
+  if not rate.isdigit():
+    raise ValueError("'%s' isn't an integer" % rate)
+  elif not units in acceptedUnits:
+    msg = "'%s' is an invalid unit, options include \"%s\"" % (units, "\", \"".join(acceptedUnits))
+    raise ValueError(msg)
+
 def _bridgeDestinationValidator(option, value):
   if value.count(":") != 1:
     raise ValueError("Bridges are of the form '<ip address>:<port>'")





More information about the tor-commits mailing list