loglan_db.model_export.ExportDefinition.export()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nop 1
crap 1
1
# -*- coding: utf-8 -*-
2 1
"""
3
This module contains an "Export extensions" for LOD dictionary SQL model.
4
Add export() function to db object for returning its text string presentation.
5
"""
6
7 1
from datetime import datetime
8 1
from typing import Dict
9 1
from sqlalchemy.orm import Query
10 1
from loglan_db.model_db.base_word import BaseWord
11 1
from loglan_db.model_db.base_word_spell import BaseWordSpell
12 1
from loglan_db.model_db.base_definition import BaseDefinition
13 1
from loglan_db.model_db.base_type import BaseType
14 1
from loglan_db.model_db.base_syllable import BaseSyllable
15 1
from loglan_db.model_db.base_setting import BaseSetting
16 1
from loglan_db.model_db.base_event import BaseEvent
17 1
from loglan_db.model_db.base_author import BaseAuthor
18
19
20 1
class ExportAuthor(BaseAuthor):
21
    """
22
    ExportAuthor Class
23
    """
24 1
    def export(self) -> str:
25
        """
26
        Prepare Author data for exporting to text file
27
        Returns:
28
            Formatted basic string
29
        """
30 1
        return f"{self.abbreviation}@{self.full_name}@{self.notes}"
31
32
33 1
class ExportEvent(BaseEvent):
34
    """
35
    ExportEvent Class
36
    """
37 1
    def export(self) -> str:
38
        """
39
        Prepare Event data for exporting to text file
40
        Returns:
41
            Formatted basic string
42
        """
43 1
        return f"{self.id}@{self.name}" \
44
               f"@{self.date.strftime('%m/%d/%Y')}@{self.definition}" \
45
               f"@{self.annotation}@{self.suffix}"
46
47
48 1
class ExportSyllable(BaseSyllable):
49
    """
50
    ExportSyllable Class
51
    """
52 1
    def export(self) -> str:
53
        """
54
        Prepare Syllable data for exporting to text file
55
        Returns:
56
            Formatted basic string
57
        """
58 1
        return f"{self.name}@{self.type}@{self.allowed}"
59
60
61 1
class ExportSetting(BaseSetting):
62
    """
63
    ExportSetting Class
64
    """
65 1
    def export(self) -> str:
66
        """
67
        Prepare Setting data for exporting to text file
68
        Returns:
69
            Formatted basic string
70
        """
71 1
        return f"{self.date.strftime('%d.%m.%Y %H:%M:%S')}" \
72
               f"@{self.db_version}" \
73
               f"@{self.last_word_id}" \
74
               f"@{self.db_release}"
75
76
77 1
class ExportType(BaseType):
78
    """
79
    ExportType Class
80
    """
81 1
    def export(self) -> str:
82
        """
83
        Prepare Type data for exporting to text file
84
        Returns:
85
            Formatted basic string
86
        """
87 1
        return f"{self.type}@{self.type_x}@{self.group}@{self.parentable}" \
88
               f"@{self.description if self.description else ''}"
89
90
91 1
class AddonExportWordConverter:
92
    """
93
    Addon for ExportWord class with converters for properties
94
    """
95 1
    notes: Dict[str, str]
96 1
    authors: Query
97 1
    year: datetime
98 1
    complexes: Query
99 1
    affixes: Query
100 1
    rank: str
101
102 1
    @property
103 1
    def e_source(self) -> str:
104
        """
105
        Returns:
106
        """
107 1
        source = '/'.join(sorted([author.abbreviation for author in self.authors.all()]))
108 1
        notes: Dict[str, str] = self.notes if self.notes else {}
109
110 1
        return f"{source} {notes.get('author', str())}".strip()
111
112 1
    @property
113 1
    def e_year(self) -> str:
114
        """
115
        Returns:
116
        """
117 1
        notes: Dict[str, str] = self.notes if self.notes else {}
118 1
        return f"{self.year.year} {notes.get('year', str())}".strip()
119
120 1
    @property
121 1
    def e_usedin(self) -> str:
122
        """
123
        Returns:
124
        """
125 1
        return ' | '.join(cpx.name for cpx in self.complexes)
126
127 1
    @property
128 1
    def e_affixes(self) -> str:
129
        """
130
        Returns:
131
        """
132 1
        return ' '.join(afx.name.replace("-", "") for afx in self.affixes).strip()
133
134 1
    @property
135 1
    def e_rank(self) -> str:
136
        """
137
        Returns:
138
        """
139 1
        notes: Dict[str, str] = self.notes if self.notes else {}
140 1
        return f"{self.rank} {notes.get('rank', str())}".strip()
141
142
143 1
class ExportWord(BaseWord, AddonExportWordConverter):
144
    """
145
    ExportWord Class
146
    """
147
148 1
    def export(self) -> str:
149
        """
150
        Prepare Word data for exporting to text file
151
        Returns:
152
            Formatted basic string
153
        """
154
155 1
        match = self.stringer(self.match)
156 1
        tid_old = self.stringer(self.TID_old)
157 1
        origin_x = self.stringer(self.origin_x)
158 1
        origin = self.stringer(self.origin)
159
160 1
        return f"{self.id_old}@{self.type.type}@{self.type.type_x}@{self.e_affixes}" \
161
               f"@{match}@{self.e_source}@{self.e_year}@{self.e_rank}" \
162
               f"@{origin}@{origin_x}@{self.e_usedin}@{tid_old}"
163
164
165 1
class ExportDefinition(BaseDefinition):
166
    """
167
    ExportDefinition Class
168
    """
169 1
    @property
170 1
    def e_grammar(self) -> str:
171
        """
172
        Prepare grammar data for export
173
        Returns:
174
            Formatted string
175
        """
176 1
        return f"{self.slots if self.slots else ''}" \
177
            f"{self.grammar_code if self.grammar_code else ''}"
178
179 1
    def export(self) -> str:
180
        """
181
        Prepare Definition data for exporting to text file
182
        Returns:
183
            Formatted basic string
184
        """
185 1
        return f"{self.source_word.id_old}@{self.position}@{self.usage if self.usage else ''}" \
186
               f"@{self.e_grammar}@{self.body}@@{self.case_tags if self.case_tags else ''}"
187
188
189 1
class ExportWordSpell(BaseWordSpell, BaseWord):
190
    """
191
    ExportWordSpell Class
192
    """
193 1
    def export(self) -> str:
194
        """
195
        Prepare WordSpell data for exporting to text file
196
        Returns:
197
            Formatted basic string
198
        """
199 1
        code_name = "".join("0" if symbol.isupper() else "5" for symbol in str(self.name))
200
201 1
        return f"{self.id_old}@{self.name}@{self.name.lower()}@{code_name}" \
202
               f"@{self.event_start_id}@{self.event_end_id if self.event_end else 9999}@"
203
204
205 1
export_models_pg = (
206
    ExportAuthor, ExportDefinition, ExportEvent, ExportSetting,
207
    ExportSyllable, ExportType, ExportWord, ExportWordSpell, )
208