]> git.decadent.org.uk Git - dak.git/commitdiff
Add get_format_from_string utility method
authorChris Lamb <lamby@debian.org>
Wed, 28 Oct 2009 13:32:40 +0000 (13:32 +0000)
committerChris Lamb <lamby@debian.org>
Wed, 28 Oct 2009 13:39:03 +0000 (13:39 +0000)
Signed-off-by: Chris Lamb <lamby@debian.org>
daklib/srcformats.py
tests/test_srcformats.py

index bcb2e2bbae4e373df2e0c2cb1a130a58d15cfa1e..ade3c45388b6a32d2e27c27d15a4a33a11e98b5a 100644 (file)
@@ -5,6 +5,19 @@ from dak_exceptions import UnknownFormatError
 
 srcformats = []
 
+def get_format_from_string(txt):
+    """
+    Returns the SourceFormat class that corresponds to the specified .changes
+    Format value. If the string does not match any class, UnknownFormatError
+    is raised.
+    """
+
+    for format in srcformats:
+        if format.re_format.match(txt):
+            return format
+
+    raise UnknownFormatError, "Unknown format %r" % txt
+
 def parse_format(txt):
     """
     Parse a .changes Format string into a tuple representation for easy
index d7b84496c86251b264aa93f7ee2c0201441fa743..f6d7215fc8daabcd179543ff59691263ccbb9d23 100755 (executable)
@@ -186,5 +186,27 @@ class ValidateFormatThreeQuiltTestCase(ValidateFormatTestCase):
         self.assertInvalid((0, 0))
         self.assertInvalid((3, 0, 'native'))
 
+class FormatFromStringTestCase(unittest.TestCase):
+    def assertFormat(self, txt, klass):
+        self.assertEqual(srcformats.get_format_from_string(txt), klass)
+
+    def assertInvalid(self, txt):
+        self.assertRaises(
+            UnknownFormatError,
+            lambda: srcformats.get_format_from_string(txt),
+        )
+
+    def testFormats(self):
+        self.assertFormat('1.0', srcformats.FormatOne)
+        self.assertFormat('3.0 (native)', srcformats.FormatThree)
+        self.assertFormat('3.0 (quilt)', srcformats.FormatThreeQuilt)
+
+    def testInvalidFormats(self):
+        self.assertInvalid('')
+        self.assertInvalid('.')
+        self.assertInvalid('3.0 (cvs)')
+        self.assertInvalid(' 1.0 ')
+        self.assertInvalid('8.4 (hardy)')
+
 if __name__ == '__main__':
     unittest.main()