Passed
Branch master (2b673d)
by Matěj
03:01
created

ssg.id_translate   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 30.51%

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 105
ccs 18
cts 59
cp 0.3051
rs 10
c 0
b 0
f 0
wmc 23

3 Functions

Rating   Name   Duplication   Size   Complexity  
A _split_namespace() 0 10 2
A _tagname_to_abbrev() 0 13 4
A _namespace_to_prefix() 0 9 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A IDTranslator.generate_id() 0 5 1
D IDTranslator.translate() 0 39 12
A IDTranslator.__init__() 0 2 1
1 2
from __future__ import absolute_import
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2 2
from __future__ import print_function
3
4 2
from .xml import ElementTree
5 2
from .constants import oval_namespace as oval_ns
6 2
from .constants import ocil_namespace as ocil_ns
7 2
from .constants import OVALTAG_TO_ABBREV, OCILTAG_TO_ABBREV
8 2
from .constants import OVALREFATTR_TO_TAG, OCILREFATTR_TO_TAG
9
10
11 2
def _split_namespace(tag):
12
    """returns a tuple of (namespace, tag),
13
    removing any fragment id from namespace
14
    """
15
16 2
    if tag[0] == "{":
17 2
        namespace, name = tag[1:].split("}", 1)
18 2
        return (namespace.split("#", 1)[0], name)
19
20 2
    return (None, tag)
21
22
23 2
def _namespace_to_prefix(tag):
24
    namespace, _ = _split_namespace(tag)
25
    if namespace == ocil_ns:
26
        return "ocil"
27
    if namespace == oval_ns:
28
        return "oval"
29
30
    raise RuntimeError(
31
        "Error: unknown checksystem referenced in tag : %s" % tag
32
    )
33
34
35 2
def _tagname_to_abbrev(tag):
36
    namespace, tag = _split_namespace(tag)
37
    if tag == "extend_definition":
38
        return tag
39
    # grab the last part of the tag name to determine its type
40
    tag = tag.rsplit("_", 1)[-1]
41
    if namespace == ocil_ns:
42
        return OCILTAG_TO_ABBREV[tag]
43
    if namespace == oval_ns:
44
        return OVALTAG_TO_ABBREV[tag]
45
46
    raise RuntimeError(
47
        "Error: unknown checksystem referenced in tag : %s" % tag
48
    )
49
50
51 2
class IDTranslator(object):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
52
    """This class is designed to handle the mapping of meaningful, human-readable
53
    names to IDs in the formats required by the SCAP checking systems, such as
54
    OVAL and OCIL."""
55
56 2
    def __init__(self, content_id):
57
        self.content_id = content_id
58
59 2
    def generate_id(self, tagname, name):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
60
        return "%s:%s-%s:%s:1" % (
61
            _namespace_to_prefix(tagname),
62
            self.content_id, name,
63
            _tagname_to_abbrev(tagname)
64
        )
65
66 2
    def translate(self, tree, store_defname=False):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
67
        for element in tree.getiterator():
68
            idname = element.get("id")
69
            if idname:
70
                # store the old name if requested (for OVAL definitions)
71
                if store_defname and \
72
                        element.tag == "{%s}definition" % oval_ns:
73
                    metadata = element.find("{%s}metadata" % oval_ns)
74
                    if metadata is None:
75
                        metadata = ElementTree.SubElement(element, "metadata")
76
                    defnam = ElementTree.Element(
77
                        "reference", ref_id=idname, source=self.content_id)
78
                    metadata.append(defnam)
79
80
                # set the element to the new identifier
81
                element.set("id", self.generate_id(element.tag, idname))
82
                # continue
83
            if element.tag == "{%s}filter" % oval_ns:
84
                element.text = self.generate_id("{%s}state" % oval_ns,
85
                                                element.text)
86
                continue
87
            if element.tag == "{%s#independent}var_ref" % oval_ns:
88
                element.text = self.generate_id("{%s}variable" % oval_ns,
89
                                                element.text)
90
                continue
91
            for attr in element.keys():
92
                if attr in OVALREFATTR_TO_TAG.keys():
93
                    element.set(attr, self.generate_id(
94
                        "{%s}%s" % (oval_ns, OVALREFATTR_TO_TAG[attr]),
95
                        element.get(attr)))
96
                if attr in OCILREFATTR_TO_TAG.keys():
97
                    element.set(attr, self.generate_id(
98
                        "{%s}%s" % (ocil_ns, OCILREFATTR_TO_TAG[attr]),
99
                        element.get(attr)))
100
            if element.tag == "{%s}test_action_ref" % ocil_ns:
101
                element.text = self.generate_id("{%s}action" % ocil_ns,
102
                                                element.text)
103
104
        return tree
105