aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-02-20 11:18:13 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-02-20 11:18:13 +0100
commited8b6ad28bacca1db642fa1855ebb7c5f882307e (patch)
tree0de92d5f13424ec5a0dc31711c73b0863318c1fe
parent85df20f3644349aa2eaa0b9c7748d960561098a2 (diff)
New data type: geo (for Geo URIs)
-rw-r--r--CHANGELOG.rst2
-rw-r--r--docs/field_types.rst2
-rw-r--r--lesana/types.py15
-rw-r--r--tests/test_types.py20
4 files changed, 39 insertions, 0 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index c566e16..04a8463 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -5,6 +5,8 @@
Unreleased
==========
+* New data type: geo (for Geo URIs)
+
0.8.1
=====
diff --git a/docs/field_types.rst b/docs/field_types.rst
index 81e86ab..91eda44 100644
--- a/docs/field_types.rst
+++ b/docs/field_types.rst
@@ -24,6 +24,8 @@ file:
.
url:
.
+geo:
+ A Geo URI.
list:
.
yaml:
diff --git a/lesana/types.py b/lesana/types.py
index 3e9450a..83d12ba 100644
--- a/lesana/types.py
+++ b/lesana/types.py
@@ -341,6 +341,21 @@ class LesanaURL(LesanaString):
name = 'url'
+class LesanaGeo(LesanaString):
+ """
+ A Geo URI
+ """
+ name = 'geo'
+
+ def load(self, data):
+ data = super().load(data)
+ if data and not data.startswith("geo:"):
+ raise LesanaValueError("{} does not look like a geo URI".format(
+ data
+ ))
+ return data
+
+
class LesanaYAML(LesanaType):
"""
Free YAML contents (no structure is enforced)
diff --git a/tests/test_types.py b/tests/test_types.py
index 32340d5..21a6d52 100644
--- a/tests/test_types.py
+++ b/tests/test_types.py
@@ -415,6 +415,26 @@ class testTypes(unittest.TestCase):
v = checker.auto("http://example.org")
self.assertEqual(v, "http://example.org")
+ def test_geo(self):
+ checker = types.LesanaGeo(self._get_field_def('geo'), {})
+
+ v = checker.empty()
+ self.assertEqual(v, "")
+
+ v = checker.load("geo:45.81483,9.07524?z=17")
+ self.assertEqual(v, "geo:45.81483,9.07524?z=17")
+
+ v = checker.load(None)
+ self.assertEqual(v, None)
+
+ # TODO: improve check for invalid Geo URIs
+ for u in ("http://example.org",):
+ with self.assertRaises(types.LesanaValueError):
+ checker.load(u)
+
+ v = checker.auto("geo:45.81483,9.07524?z=17")
+ self.assertEqual(v, "geo:45.81483,9.07524?z=17")
+
def test_yaml(self):
checker = types.LesanaYAML(self._get_field_def('yaml'), {})