loglan_db.model_db.addons.addon_word_linker   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 87
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A AddonWordLinker.add_child() 0 15 2
A AddonWordLinker.add_author() 0 12 2
A AddonWordLinker.add_authors() 0 11 2
A AddonWordLinker.add_children() 0 14 2
A AddonWordLinker._is_parented() 0 11 1
1
# -*- coding: utf-8 -*-
2 1
"""
3
This module contains an addon for basic Word Model,
4
which makes it possible to specify the authors and derivatives of words
5
"""
6 1
from typing import List
7
8 1
from flask_sqlalchemy import BaseQuery
9
10 1
from loglan_db.model_db.base_author import BaseAuthor
11 1
from loglan_db.model_db.base_connect_tables import t_connect_words
12 1
from loglan_db.model_db.base_word import BaseWord
13
14
15 1
class AddonWordLinker:
16
    """AddonWordLinker Model"""
17 1
    _derivatives: BaseQuery = None
18 1
    authors: BaseQuery = None
19
20 1
    def _is_parented(self, child: BaseWord) -> bool:
21
        """
22
        Check, if this word is already added as a parent for this 'child'
23
24
        Args:
25
            child: BaseWord: BaseWord object to check
26
27
        Returns: bool:
28
29
        """
30 1
        return bool(self._derivatives.filter(t_connect_words.c.child_id == child.id).count() > 0)
31
32 1
    def add_child(self, child: BaseWord) -> str:
33
        """Add derivative for the source word
34
        Get words from Used In and add relationship in database
35
36
        Args:
37
          child: BaseWord: Object to add
38
39
        Returns:
40
            String with the name of the added child (BaseWord.name)
41
42
        """
43
        # TODO add check if type of child is allowed to add to this word
44 1
        if not self._is_parented(child):
45 1
            self._derivatives.append(child)
46 1
        return child.name
47
48 1
    def add_children(self, children: List[BaseWord]):
49
        """Add derivatives for the source word
50
        Get words from Used In and add relationship in database
51
52
        Args:
53
          children: List[BaseWord]:
54
55
        Returns:
56
          None
57
58
        """
59
        # TODO add check if type of child is allowed to add to this word
60 1
        new_children = list(set(children) - set(self._derivatives))
61 1
        _ = self._derivatives.extend(new_children) if new_children else None
62
63 1
    def add_author(self, author: BaseAuthor) -> str:
64
        """Connect Author object with BaseWord object
65
66
        Args:
67
          author: BaseAuthor:
68
69
        Returns:
70
71
        """
72 1
        if not self.authors.filter(BaseAuthor.abbreviation == author.abbreviation).count() > 0:
73 1
            self.authors.append(author)
74 1
        return author.abbreviation
75
76 1
    def add_authors(self, authors: List[BaseAuthor]):
77
        """Connect Author objects with BaseWord object
78
79
        Args:
80
          authors: List[BaseAuthor]:
81
82
        Returns:
83
84
        """
85 1
        new_authors = list(set(authors) - set(self.authors))
86
        _ = self.authors.extend(new_authors) if new_authors else None
87