[or-cvs] r18295: {torctl} Add __str__ support to restrictions to catch a bizzare restr (torctl/trunk/python/TorCtl)

mikeperry at seul.org mikeperry at seul.org
Wed Jan 28 14:30:28 UTC 2009


Author: mikeperry
Date: 2009-01-28 09:30:28 -0500 (Wed, 28 Jan 2009)
New Revision: 18295

Modified:
   torctl/trunk/python/TorCtl/PathSupport.py
Log:

Add __str__ support to restrictions to catch a bizzare
restriction-related error.



Modified: torctl/trunk/python/TorCtl/PathSupport.py
===================================================================
--- torctl/trunk/python/TorCtl/PathSupport.py	2009-01-28 13:58:02 UTC (rev 18294)
+++ torctl/trunk/python/TorCtl/PathSupport.py	2009-01-28 14:30:28 UTC (rev 18295)
@@ -108,6 +108,9 @@
         lambda r: not isinstance(r, RestrictionClass),
           self.restrictions)
 
+  def __str__(self):
+    return str(self.restrictions)
+
 class PathRestriction:
   "Interface for path restriction policies"
   def path_is_ok(self, path):
@@ -137,6 +140,9 @@
         lambda r: not isinstance(r, RestrictionClass),
           self.restrictions)
 
+  def __str__(self):
+    return str(self.restrictions)
+
 class NodeGenerator:
   "Interface for node generation"
   def __init__(self, sorted_r, rstr_list):
@@ -154,7 +160,7 @@
     "Rewind the generator to the 'beginning'"
     self.routers = copy.copy(self.rstr_routers)
     if not self.routers:
-      plog("ERROR", "No routers left after restrictions applied!")
+      plog("ERROR", "No routers left after restrictions applied: "+str(self.rstr_list))
       raise NoNodesRemain()
  
   def rebuild(self, sorted_r=None):
@@ -164,7 +170,7 @@
       self.sorted_r = sorted_r
     self.rstr_routers = filter(lambda r: self.rstr_list.r_is_ok(r), self.sorted_r)
     if not self.rstr_routers:
-      plog("ERROR", "No routers left after restrictions applied!")
+      plog("ERROR", "No routers left after restrictions applied: "+str(self.rstr_list))
       raise NoNodesRemain()
 
   def mark_chosen(self, r):
@@ -227,6 +233,9 @@
     elif r.list_rank > len(self.sorted_r)*self.pct_fast/100: return False
     
     return True
+
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.pct_skip)+","+str(self.pct_fast)+")"
     
 class OSRestriction(NodeRestriction):
   "Restriction based on operating system"
@@ -247,10 +256,16 @@
     if self.ok: return False
     if self.bad: return True
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.ok)+","+str(self.bad)+")"
+
 class ConserveExitsRestriction(NodeRestriction):
   "Restriction to reject exits from selection"
   def r_is_ok(self, r): return not "Exit" in r.flags
 
+  def __str__(self):
+    return self.__class__.__name__+"()"
+
 class FlagsRestriction(NodeRestriction):
   "Restriction for mandatory and forbidden router flags"
   def __init__(self, mandatory, forbidden=[]):
@@ -266,6 +281,9 @@
       if f in router.flags: return False
     return True
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.mandatory)+","+str(self.forbidden)+")"
+
 class NickRestriction(NodeRestriction):
   """Require that the node nickname is as specified"""
   def __init__(self, nickname):
@@ -274,6 +292,9 @@
   def r_is_ok(self, router):
     return router.nickname == self.nickname
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.nickname)+")"
+
 class IdHexRestriction(NodeRestriction):
   """Require that the node idhash is as specified"""
   def __init__(self, idhex):
@@ -284,13 +305,19 @@
 
   def r_is_ok(self, router):
     return router.idhex == self.idhex
-  
+
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.idhex)+")"
+ 
 class MinBWRestriction(NodeRestriction):
   """Require a minimum bandwidth"""
   def __init__(self, minbw):
     self.min_bw = minbw
 
   def r_is_ok(self, router): return router.bw >= self.min_bw
+
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.min_bw)+")"
    
 class VersionIncludeRestriction(NodeRestriction):
   """Require that the version match one in the list"""
@@ -306,6 +333,9 @@
         return True
     return False
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.eq)+")"
+
 class VersionExcludeRestriction(NodeRestriction):
   """Require that the version not match one in the list"""
   def __init__(self, exclude):
@@ -320,6 +350,9 @@
         return False
     return True
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.exclude)+")"
+
 class VersionRangeRestriction(NodeRestriction):
   """Require that the versions be inside a specified range""" 
   def __init__(self, gr_eq, less_eq=None):
@@ -331,6 +364,9 @@
     return (not self.gr_eq or router.version >= self.gr_eq) and \
         (not self.less_eq or router.version <= self.less_eq)
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.gr_eq)+","+str(self.less_eq)+")"
+
 class ExitPolicyRestriction(NodeRestriction):
   """Require that a router exit to an ip+port"""
   def __init__(self, to_ip, to_port):
@@ -339,6 +375,9 @@
 
   def r_is_ok(self, r): return r.will_exit_to(self.to_ip, self.to_port)
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.to_ip)+","+str(self.to_port)+")"
+
 class MetaNodeRestriction(NodeRestriction):
   """Interface for a NodeRestriction that is an expression consisting of 
      multiple other NodeRestrictions"""
@@ -361,6 +400,9 @@
         return True
     return False
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.rstrs)+")"
+
 class NotNodeRestriction(MetaNodeRestriction):
   """Negates a single restriction"""
   def __init__(self, a):
@@ -368,6 +410,9 @@
 
   def r_is_ok(self, r): return not self.a.r_is_ok(r)
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.a)+")"
+
 class AtLeastNNodeRestriction(MetaNodeRestriction):
   """MetaNodeRestriction that is true if at least n member 
      restrictions are true."""
@@ -383,7 +428,10 @@
     if cnt < self.n: return False
     else: return True
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.rstrs)+","+str(self.n)+")"
 
+
 #################### Path Restrictions #####################
 
 class Subnet16Restriction(PathRestriction):
@@ -397,6 +445,9 @@
         return False
     return True
 
+  def __str__(self):
+    return self.__class__.__name__+"()"
+
 class UniqueRestriction(PathRestriction):
   """Path restriction that mandates that the same router can't appear more
      than once in a path"""
@@ -406,6 +457,9 @@
         return False
     return True
 
+  def __str__(self):
+    return self.__class__.__name__+"()"
+
 #################### GeoIP Restrictions ###################
 
 class CountryCodeRestriction(NodeRestriction):
@@ -413,6 +467,9 @@
   def r_is_ok(self, r):
     return r.country_code != None
 
+  def __str__(self):
+    return self.__class__.__name__+"()"
+
 class CountryRestriction(NodeRestriction):
   """ Only accept nodes that are in 'country_code' """
   def __init__(self, country_code):
@@ -421,6 +478,9 @@
   def r_is_ok(self, r):
     return r.country_code == self.country_code
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.country_code)+")"
+
 class ExcludeCountriesRestriction(NodeRestriction):
   """ Exclude a list of countries """
   def __init__(self, countries):
@@ -429,6 +489,9 @@
   def r_is_ok(self, r):
     return not (r.country_code in self.countries)
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.countries)+")"
+
 class UniqueCountryRestriction(PathRestriction):
   """ Ensure every router to have a distinct country_code """
   def path_is_ok(self, path):
@@ -438,6 +501,9 @@
           return False;
     return True;
 
+  def __str__(self):
+    return self.__class__.__name__+"()"
+
 class SingleCountryRestriction(PathRestriction):
   """ Ensure every router to have the same country_code """
   def path_is_ok(self, path):
@@ -447,6 +513,9 @@
         return False
     return True
 
+  def __str__(self):
+    return self.__class__.__name__+"()"
+
 class ContinentRestriction(PathRestriction):
   """ Do not more than n continent crossings """
   # TODO: Add src and dest
@@ -466,6 +535,9 @@
     if crossings > self.n: return False
     else: return True
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.n)+")"
+
 class ContinentJumperRestriction(PathRestriction):
   """ Ensure continent crossings between all hops """
   def path_is_ok(self, path):
@@ -478,6 +550,9 @@
       prev = r
     return True
 
+  def __str__(self):
+    return self.__class__.__name__+"()"
+
 class UniqueContinentRestriction(PathRestriction):
   """ Ensure every hop to be on a different continent """
   def path_is_ok(self, path):
@@ -487,6 +562,9 @@
           return False;
     return True;
 
+  def __str__(self):
+    return self.__class__.__name__+"()"
+
 class OceanPhobicRestriction(PathRestriction):
   """ Not more than n ocean crossings """
   # TODO: Add src and dest
@@ -506,6 +584,9 @@
     if crossings > self.n: return False
     else: return True
 
+  def __str__(self):
+    return self.__class__.__name__+"("+str(self.n)+")"
+
 #################### Node Generators ######################
 
 class UniformGenerator(NodeGenerator):



More information about the tor-commits mailing list