commit 4e773f346f2b442a1726af94b7b2da231b73c1d6 Author: Robert Ransom rransom.8774@gmail.com Date: Mon Mar 23 11:24:33 2015 -0700
Remove clear abuses of str.strip() and str.rstrip() --- lib/bridgedb/Bridges.py | 3 ++- lib/bridgedb/bridges.py | 6 ++++-- lib/bridgedb/test/test_Main.py | 3 ++- lib/bridgedb/test/test_Tests.py | 3 ++- lib/bridgedb/test/test_bridgedb.py | 3 ++- lib/bridgedb/test/test_captcha.py | 7 ++++--- lib/bridgedb/test/test_configure.py | 3 ++- lib/bridgedb/test/test_crypto.py | 3 ++- lib/bridgedb/test/test_email_autoresponder.py | 3 ++- lib/bridgedb/test/test_https.py | 3 ++- lib/bridgedb/test/test_smtp.py | 3 ++- lib/bridgedb/util.py | 9 +++++++++ 12 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/lib/bridgedb/Bridges.py b/lib/bridgedb/Bridges.py index bdb911a..8c05083 100644 --- a/lib/bridgedb/Bridges.py +++ b/lib/bridgedb/Bridges.py @@ -29,6 +29,7 @@ from bridgedb.parse.fingerprint import toHex from bridgedb.parse.fingerprint import fromHex from bridgedb.parse.fingerprint import isValidFingerprint from bridgedb.safelog import logSafely +from bridgedb.util import remove_prefix
try: from cStringIO import StringIO @@ -692,7 +693,7 @@ class FilteredBridgeSplitter(BridgeHolder): subringName = [self.distributorName] for filterName in filterNames: if filterName != 'filterAssignBridgesToRing': - subringName.append(filterName.strip('filterBridgesBy')) + subringName.append(remove_prefix(filterName, 'filterBridgesBy')) subringName = '-'.join([x for x in subringName]) subring.setName(subringName)
diff --git a/lib/bridgedb/bridges.py b/lib/bridgedb/bridges.py index 96f995e..8759473 100644 --- a/lib/bridgedb/bridges.py +++ b/lib/bridgedb/bridges.py @@ -38,6 +38,8 @@ from bridgedb.parse.fingerprint import isValidFingerprint from bridgedb.parse.fingerprint import toHex from bridgedb.parse.fingerprint import fromHex from bridgedb.parse.nickname import isValidRouterNickname +from bridgedb.util import remove_prefix +from bridgedb.util import remove_suffix
class PluggableTransportUnavailable(Exception): @@ -1462,8 +1464,8 @@ class Bridge(BridgeBackwardsCompatibility): sequence = asn1.DerSequence()
key = self.signingKey - key = key.strip(TOR_SIGNING_KEY_HEADER) - key = key.strip(TOR_SIGNING_KEY_FOOTER) + key = remove_prefix(key, TOR_SIGNING_KEY_HEADER) + key = remove_suffix(key, TOR_SIGNING_KEY_FOOTER) key = key.replace('\n', '') key = base64.b64decode(key)
diff --git a/lib/bridgedb/test/test_Main.py b/lib/bridgedb/test/test_Main.py index 0b14319..688c383 100644 --- a/lib/bridgedb/test/test_Main.py +++ b/lib/bridgedb/test/test_Main.py @@ -29,13 +29,14 @@ from twisted.trial import unittest from bridgedb import Main from bridgedb.Bridges import BridgeHolder from bridgedb.parse.options import parseOptions +from bridgedb.util import remove_suffix
logging.getLogger().disabled = True
HERE = os.getcwd() -TOPDIR = HERE.rstrip('_trial_temp') +TOPDIR = remove_suffix(HERE, '_trial_temp') CI_RUNDIR = os.path.join(TOPDIR, 'run')
# A networkstatus descriptor with two invalid ORAddress (127.0.0.1 and ::1) diff --git a/lib/bridgedb/test/test_Tests.py b/lib/bridgedb/test/test_Tests.py index e78a4c4..a46c50f 100644 --- a/lib/bridgedb/test/test_Tests.py +++ b/lib/bridgedb/test/test_Tests.py @@ -29,6 +29,7 @@ from twisted.trial import unittest
from bridgedb.test import legacy_Tests as Tests from bridgedb.test import deprecated +from bridgedb.util import remove_suffix
warnings.filterwarnings('ignore', module="bridgedb.test.legacy_Tests") @@ -191,7 +192,7 @@ class DynamicTestCaseMeta(type): origFunc = origName.split('.')[-2:] origName = '_'.join(origFunc) if origName.endswith('_py'): # this happens with doctests - origName = origName.strip('_py') + origName = remove_suffix(origName, '_py') methName = str(methodPrefix + origName).replace('.', '_') meth = testMethodFactory(test, methName) logging.debug("Set %s.%s=%r" % (cls.__name__, methName, meth)) diff --git a/lib/bridgedb/test/test_bridgedb.py b/lib/bridgedb/test/test_bridgedb.py index 70c71f0..301309a 100644 --- a/lib/bridgedb/test/test_bridgedb.py +++ b/lib/bridgedb/test/test_bridgedb.py @@ -23,6 +23,7 @@ from twisted.trial.unittest import SkipTest
from bridgedb.test.util import processExists from bridgedb.test.util import getBridgeDBPID +from bridgedb.util import remove_suffix
class BridgeDBCliTest(unittest.TestCase): @@ -30,7 +31,7 @@ class BridgeDBCliTest(unittest.TestCase):
def setUp(self): here = os.getcwd() - topdir = here.rstrip('_trial_temp') + topdir = remove_suffix(here, '_trial_temp') self.rundir = os.path.join(topdir, 'run') self.pidfile = os.path.join(self.rundir, 'bridgedb.pid') self.pid = getBridgeDBPID(self.pidfile) diff --git a/lib/bridgedb/test/test_captcha.py b/lib/bridgedb/test/test_captcha.py index a5083d7..ea05749 100644 --- a/lib/bridgedb/test/test_captcha.py +++ b/lib/bridgedb/test/test_captcha.py @@ -23,6 +23,7 @@ from zope.interface import providedBy
from bridgedb import captcha from bridgedb import crypto +from bridgedb.util import remove_suffix
class CaptchaTests(unittest.TestCase): @@ -97,7 +98,7 @@ class GimpCaptchaTests(unittest.TestCase):
def setUp(self): here = os.getcwd() - self.topDir = here.rstrip('_trial_temp') + self.topDir = remove_suffix(here, '_trial_temp') self.cacheDir = os.path.join(self.topDir, 'captchas') self.badCacheDir = os.path.join(here, 'capt')
@@ -131,7 +132,7 @@ class GimpCaptchaTests(unittest.TestCase): """GimpCaptcha with bad cacheDir should raise GimpCaptchaError.""" self.assertRaises(captcha.GimpCaptchaError, captcha.GimpCaptcha, self.publik, self.sekrit, self.hmacKey, - self.cacheDir.rstrip('chas')) + remove_suffix(self.cacheDir, 'chas'))
def test_init(self): """Test that __init__ correctly initialised all the values.""" @@ -238,7 +239,7 @@ class GimpCaptchaTests(unittest.TestCase): c = captcha.GimpCaptcha(self.publik, self.sekrit, self.hmacKey, self.cacheDir) image, challenge = c.get() - challengeBadB64 = challenge.rstrip('==') + "\x42\x42\x42" + challengeBadB64 = remove_suffix(challenge, '==') + "\x42\x42\x42" self.assertEquals( c.check(challenge, c.answer, c.secretKey, c.hmacKey), True) diff --git a/lib/bridgedb/test/test_configure.py b/lib/bridgedb/test/test_configure.py index 501ed1f..5246613 100644 --- a/lib/bridgedb/test/test_configure.py +++ b/lib/bridgedb/test/test_configure.py @@ -16,6 +16,7 @@ import os from twisted.trial import unittest
from bridgedb import configure +from bridgedb.util import remove_suffix
class ConfigureTests(unittest.TestCase): @@ -24,7 +25,7 @@ class ConfigureTests(unittest.TestCase): def setUp(self): """Find the config file in the top directory of this repo.""" here = os.getcwd() - topdir = here.rstrip('_trial_temp') + topdir = remove_suffix(here, '_trial_temp') self.configFilename = os.path.join(topdir, 'bridgedb.conf')
def test_loadConfig_with_file(self): diff --git a/lib/bridgedb/test/test_crypto.py b/lib/bridgedb/test/test_crypto.py index c2010ca..afd63c8 100644 --- a/lib/bridgedb/test/test_crypto.py +++ b/lib/bridgedb/test/test_crypto.py @@ -33,6 +33,7 @@ from bridgedb import txrecaptcha from bridgedb.persistent import Conf from bridgedb.test.util import fileCheckDecorator from bridgedb.test.email_helpers import _createConfig +from bridgedb.util import remove_suffix
logging.disable(50) @@ -98,7 +99,7 @@ class InitializeGnuPGTests(unittest.TestCase): :returns: The full path to the new gnupg home directory. """ here = os.getcwd() - topDir = here.rstrip('_trial_temp') + topDir = remove_suffix(here, '_trial_temp') gnupghome = os.path.join(topDir, '.gnupg') gnupghomeNew = os.path.join(here, '.gnupg')
diff --git a/lib/bridgedb/test/test_email_autoresponder.py b/lib/bridgedb/test/test_email_autoresponder.py index 2895802..4c36be4 100644 --- a/lib/bridgedb/test/test_email_autoresponder.py +++ b/lib/bridgedb/test/test_email_autoresponder.py @@ -29,6 +29,7 @@ from bridgedb.Dist import TooSoonEmail from bridgedb.test.email_helpers import _createConfig from bridgedb.test.email_helpers import _createMailServerContext from bridgedb.test.email_helpers import DummyEmailDistributorWithState +from bridgedb.util import remove_suffix
class CreateResponseBodyTests(unittest.TestCase): @@ -36,7 +37,7 @@ class CreateResponseBodyTests(unittest.TestCase):
def _moveGPGTestKeyfile(self): here = os.getcwd() - topDir = here.rstrip('_trial_temp') + topDir = remove_suffix(here, '_trial_temp') self.gpgFile = os.path.join(topDir, '.gnupg', 'TESTING.subkeys.sec') self.gpgMoved = os.path.join(here, 'TESTING.subkeys.sec') shutil.copy(self.gpgFile, self.gpgMoved) diff --git a/lib/bridgedb/test/test_https.py b/lib/bridgedb/test/test_https.py index 92b07bf..bf229f8 100644 --- a/lib/bridgedb/test/test_https.py +++ b/lib/bridgedb/test/test_https.py @@ -36,6 +36,7 @@ from twisted.trial.unittest import SkipTest
from bridgedb.test.util import processExists from bridgedb.test.util import getBridgeDBPID +from bridgedb.util import remove_suffix
HTTP_ROOT = 'http://127.0.0.1:6788' CAPTCHA_RESPONSE = 'Tvx74Pmy' @@ -44,7 +45,7 @@ CAPTCHA_RESPONSE = 'Tvx74Pmy' class HTTPTests(unittest.TestCase): def setUp(self): here = os.getcwd() - topdir = here.rstrip('_trial_temp') + topdir = remove_suffix(here, '_trial_temp') self.rundir = os.path.join(topdir, 'run') self.pidfile = os.path.join(self.rundir, 'bridgedb.pid') self.pid = getBridgeDBPID(self.pidfile) diff --git a/lib/bridgedb/test/test_smtp.py b/lib/bridgedb/test/test_smtp.py index 1cbafba..b4cf8f9 100644 --- a/lib/bridgedb/test/test_smtp.py +++ b/lib/bridgedb/test/test_smtp.py @@ -17,6 +17,7 @@ from twisted.trial.unittest import SkipTest
from bridgedb.test.util import processExists from bridgedb.test.util import getBridgeDBPID +from bridgedb.util import remove_suffix
# ------------- SMTP Client Config SMTP_DEBUG_LEVEL = 0 # set to 1 to see SMTP message exchange @@ -118,7 +119,7 @@ class SMTPTests(unittest.TestCase): running. ''' here = os.getcwd() - topdir = here.rstrip('_trial_temp') + topdir = remove_suffix(here, '_trial_temp') self.rundir = os.path.join(topdir, 'run') self.pidfile = os.path.join(self.rundir, 'bridgedb.pid') self.pid = getBridgeDBPID(self.pidfile) diff --git a/lib/bridgedb/util.py b/lib/bridgedb/util.py index ca2a88f..3e1b42f 100644 --- a/lib/bridgedb/util.py +++ b/lib/bridgedb/util.py @@ -302,3 +302,12 @@ class mixin: expect mixin classes to end in ``'mixin'``. """ __metaclass__ = abc.ABCMeta + +def remove_prefix(s, f): + assert s.startswith(f) + return s[len(f):] + +def remove_suffix(s, f): + assert s.endswith(f) + return s[:-len(f)] +