sdoc.helper.SDoc.SDoc.escape()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 19
Code Lines 5

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
eloc 5
dl 19
loc 19
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 1
nop 1
crap 1.008
1 1
import re
2
3
4 1 View Code Duplication
class SDoc:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5
    """
6
    Utility class with functions for generating SDoc code.
7
    """
8
9
    # ------------------------------------------------------------------------------------------------------------------
10 1
    @staticmethod
11 1
    def escape(text: str) -> str:
12
        """
13
        Returns an escaped string that is safe to use in SDoc.
14
15
        :param str text: The escaped string.
16
        """
17
18 1
        def replace(match_obj):
19
            """
20
            Returns the match text prefixed with backslash
21
22
            :param re.match match_obj: The match.
23
24
            :rtype: str
25
            """
26
            return '\\' + match_obj.group(0)
27
28 1
        return re.sub(r'[\\{}]', replace, text)
29
30
    # ------------------------------------------------------------------------------------------------------------------
31 1
    @staticmethod
32 1
    def unescape(text: str) -> str:
33
        """
34
        Returns an unescaped SDoc escaped string. I.e. removes back slashes.
35
36
        :param str text: The SDoc escaped string.
37
        """
38
39
        def replace(match_obj):
40
            """
41
            Returns the match text without prefixed backslash.
42
43
            :param re.match match_obj: The match.
44
45
            :rtype: str
46
            """
47
            return match_obj.group(0)[1:]
48
49
        return re.sub(r'\\.', replace, text)
50
51
# ----------------------------------------------------------------------------------------------------------------------
52