commit eb5b522430bc32acb0f8f18c1e92e8ae7782df62 Author: Damian Johnson atagar@torproject.org Date: Sun Nov 6 11:24:21 2011 -0800
Adding optional key arg to is_next_mapping checks
Option to simplify testing for the next key being parsed. --- stem/types.py | 20 +++++++++++++------- test/unit/types/control_line.py | 9 ++++++--- 2 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/stem/types.py b/stem/types.py index d152542..17d812c 100644 --- a/stem/types.py +++ b/stem/types.py @@ -275,11 +275,13 @@ class ControlLine(str): start_quote, end_quote = _get_quote_indeces(self._remainder, escaped) return start_quote == 0 and end_quote != -1
- def is_next_mapping(self, quoted = False, escaped = False): + def is_next_mapping(self, key = None, quoted = False, escaped = False): """ Checks if our next entry is a KEY=VALUE mapping or not.
Arguments: + key (str) - checks that the key matches this value, skipping the + check if None quoted (bool) - checks that the mapping is to a quoted value escaped (bool) - unescapes the CONTROL_ESCAPES escape sequences
@@ -291,12 +293,16 @@ class ControlLine(str): remainder = self._remainder # temp copy to avoid locking key_match = KEY_ARG.match(remainder)
- if key_match and quoted: - # checks that we have a quoted value and that it comes after the 'key=' - start_quote, end_quote = _get_quote_indeces(remainder, escaped) - return start_quote == key_match.end() and end_quote != -1 - elif key_match: - return True # we just needed to check for the key + if key_match: + if key and key != key_match.groups()[0]: + return False + + if quoted: + # checks that we have a quoted value and that it comes after the 'key=' + start_quote, end_quote = _get_quote_indeces(remainder, escaped) + return start_quote == key_match.end() and end_quote != -1 + else: + return True # we just needed to check for the key else: return False # doesn't start with a key
diff --git a/test/unit/types/control_line.py b/test/unit/types/control_line.py index 7f7349c..bacda76 100644 --- a/test/unit/types/control_line.py +++ b/test/unit/types/control_line.py @@ -91,7 +91,9 @@ class TestControlLine(unittest.TestCase): self.assertFalse(line.is_empty()) self.assertFalse(line.is_next_quoted()) self.assertTrue(line.is_next_mapping()) - self.assertTrue(line.is_next_mapping(True)) + self.assertTrue(line.is_next_mapping(key = "Tor")) + self.assertTrue(line.is_next_mapping(key = "Tor", quoted = True)) + self.assertTrue(line.is_next_mapping(quoted = True))
# try popping this as a non-quoted mapping self.assertEquals(line.pop_mapping(), ('Tor', '"0.2.1.30')) @@ -126,8 +128,9 @@ class TestControlLine(unittest.TestCase):
self.assertEquals(line.remainder(), r'COOKIEFILE="/tmp/my data\"dir//control_auth_cookie"') self.assertTrue(line.is_next_mapping()) - self.assertTrue(line.is_next_mapping(True)) - self.assertTrue(line.is_next_mapping(True, True)) + self.assertTrue(line.is_next_mapping(key = "COOKIEFILE")) + self.assertTrue(line.is_next_mapping(quoted = True)) + self.assertTrue(line.is_next_mapping(quoted = True, escaped = True)) cookie_file_entry = line.remainder()
# try a general pop
tor-commits@lists.torproject.org