From 762dc07a165fc430f808744bb0a565273dad9d70 Mon Sep 17 00:00:00 2001 From: Fred Pauchet Date: Thu, 5 Jan 2023 20:46:30 +0100 Subject: [PATCH] Add a class that represents a taxonomy of categories --- LICENCE | 9 +++++++++ jack/models.py | 17 ++++++++++++++++- tests/test_models.py | 22 +++++++++++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/LICENCE b/LICENCE index e69de29..67fe97b 100644 --- a/LICENCE +++ b/LICENCE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2023 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/jack/models.py b/jack/models.py index c433cce..bcc79c0 100644 --- a/jack/models.py +++ b/jack/models.py @@ -48,13 +48,28 @@ class Article: return self.html +class Section: + def __init__(self, key, path=None): + self.key = key + self.path = path + self.parent = None + self.sections = {} + + def add(self, section): + if section.key not in self.sections.keys(): + section.parent = self + self.sections[section.key] = section + + return self.sections[section.key] + + class Site: """A site contains articles, categories and all related data. """ def __init__(self, root_directory: str): self.root_directory = root_directory self.articles = [] - self.categories = {} + self.sections = {} self.tags = {} def add(self, file_content: str): diff --git a/tests/test_models.py b/tests/test_models.py index c9ef54b..58950a0 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,6 +1,6 @@ """Tests associated to the models""" -from jack.models import Article, Site +from jack.models import Article, Section, Site content = """--- title: This is a test article @@ -42,6 +42,26 @@ def test_article_fenced_code(): assert """
""" in article.to_prose()
 
 
+def test_section_add():
+	parent = Section("dev")
+	child = Section("code")
+	parent.add(child)
+
+	assert child.parent == parent
+	assert child in parent.sections.values()
+	assert child == parent.sections.get("code")
+
+
+def test_add_existing_section():
+	parent = Section("dev")
+	child = Section("code")
+	parent.add(child)
+	other_child = Section("code")
+	parent.add(other_child)
+
+	assert len(parent.sections) == 1
+
+
 def test_site_append_article():
 	site = Site(".")
 	article = site.add(content)