aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2020-10-02 09:19:34 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2020-10-02 09:19:34 +0200
commit67493bb361e738f376c26b443be4175746b008ec (patch)
tree707acf572eeca5be0b27a2ddcfcd51cdf8100d97
parentb75c00bf5a7f650ff53a407b47a96407ac277d8e (diff)
More types
-rw-r--r--lesana/types.py30
-rw-r--r--tests/test_types.py44
2 files changed, 74 insertions, 0 deletions
diff --git a/lesana/types.py b/lesana/types.py
index e5c556c..565d1b2 100644
--- a/lesana/types.py
+++ b/lesana/types.py
@@ -191,6 +191,36 @@ class LesanaBoolean(LesanaType):
return None
+class LesanaFile(LesanaString):
+ """
+ A path to a local file.
+
+ Relative paths are assumed to be relative to the base lesana
+ directory (i.e. where .lesana lives)
+ """
+ name = 'file'
+
+
+class LesanaURL(LesanaString):
+ """
+ An URL
+ """
+ name = 'url'
+
+
+class LesanaYAML(LesanaType):
+ """
+ Free YAML contents (no structure is enforced)
+ """
+ name = 'yaml'
+
+ def load(self, data):
+ return data
+
+ def empty(self):
+ return None
+
+
class LesanaValueError(ValueError):
"""
Raised in case of validation errors.
diff --git a/tests/test_types.py b/tests/test_types.py
index 2d6b744..f27089d 100644
--- a/tests/test_types.py
+++ b/tests/test_types.py
@@ -209,6 +209,50 @@ class testTypes(unittest.TestCase):
v = checker.load(None)
self.assertEqual(v, None)
+ def test_file(self):
+ checker = types.LesanaFile()
+
+ v = checker.empty()
+ self.assertEqual(v, "")
+
+ v = checker.load("relative/path/to/file")
+ self.assertEqual(v, "relative/path/to/file")
+
+ v = checker.load(None)
+ self.assertEqual(v, None)
+
+ # TODO: check for invalid file paths
+
+ def test_url(self):
+ checker = types.LesanaURL()
+
+ v = checker.empty()
+ self.assertEqual(v, "")
+
+ v = checker.load("http://example.org")
+ self.assertEqual(v, "http://example.org")
+
+ v = checker.load(None)
+ self.assertEqual(v, None)
+
+ # TODO: check for invalid URLs
+
+ def test_yaml(self):
+ checker = types.LesanaYAML()
+
+ v = checker.empty()
+ self.assertEqual(v, None)
+
+ some_data = {
+ 'anything': 'goes',
+ 'everything': 42
+ }
+ v = checker.load(some_data)
+ self.assertEqual(v, some_data)
+
+ v = checker.load(None)
+ self.assertEqual(v, None)
+
if __name__ == '__main__':
unittest.main()