Passed
Push — master ( f9ed9c...2417ae )
by Matěj
02:01
created

ssg.id_translate.IDTranslator.__init__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
ccs 1
cts 2
cp 0.5
crap 1.125
rs 10
c 0
b 0
f 0
1 1
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 1
from __future__ import print_function
3
4 1
from collections import namedtuple
5
6 1
from .xml import ElementTree
7 1
from .constants import oval_namespace as oval_ns
8 1
from .constants import ocil_namespace as ocil_ns
9 1
from .constants import OVALTAG_TO_ABBREV, OCILTAG_TO_ABBREV
10 1
from .constants import OVALREFATTR_TO_TAG, OCILREFATTR_TO_TAG
11
12
13 1
def _split_namespace(tag):
14
    """
15
    returns a namedtuple of (namespace, tag), removing any
16
    fragment id from namespace
17
    """
18
19 1
    element = namedtuple('element', ['namespace', 'tag'])
20
21 1
    if tag[0] == "{":
22 1
        namespace, name = tag[1:].split("}", 1)
23 1
        return element(namespace.split("#")[0], name)
24
25 1
    return element(None, tag)
26
27
28 1
def _namespace_to_prefix(tag):
29
    namespace, _ = _split_namespace(tag)
30
    if namespace == ocil_ns:
31
        return "ocil"
32
    if namespace == oval_ns:
33
        return "oval"
34
35
    raise RuntimeError(
36
        "Error: unknown checksystem referenced in tag : %s" % tag
37
    )
38
39
40 1
def _tagname_to_abbrev(tag):
41
    namespace, tag = _split_namespace(tag)
42
    if tag == "extend_definition":
43
        return tag
44
    # grab the last part of the tag name to determine its type
45
    tag = tag.rsplit("_", 1)[-1]
46
    if namespace == ocil_ns:
47
        return OCILTAG_TO_ABBREV[tag]
48
    if namespace == oval_ns:
49
        return OVALTAG_TO_ABBREV[tag]
50
51
    raise RuntimeError(
52
        "Error: unknown checksystem referenced in tag : %s" % tag
53
    )
54
55
56 1
class IDTranslator(object):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
57
    """This class is designed to handle the mapping of meaningful, human-readable
58
    names to IDs in the formats required by the SCAP checking systems, such as
59
    OVAL and OCIL."""
60
61 1
    def __init__(self, content_id):
62
        self.content_id = content_id
63
64 1
    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...
65
        return "%s:%s-%s:%s:1" % (
66
            _namespace_to_prefix(tagname),
67
            self.content_id, name,
68
            _tagname_to_abbrev(tagname)
69
        )
70
71 1
    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...
72
        for element in tree.getiterator():
73
            idname = element.get("id")
74
            if idname:
75
                # store the old name if requested (for OVAL definitions)
76
                if store_defname and \
77
                        element.tag == "{%s}definition" % oval_ns:
78
                    metadata = element.find("{%s}metadata" % oval_ns)
79
                    if metadata is None:
80
                        metadata = ElementTree.SubElement(element, "metadata")
81
                    defnam = ElementTree.Element(
82
                        "reference", ref_id=idname, source=self.content_id)
83
                    metadata.append(defnam)
84
85
                # set the element to the new identifier
86
                element.set("id", self.generate_id(element.tag, idname))
87
                # continue
88
            if element.tag == "{%s}filter" % oval_ns:
89
                element.text = self.generate_id("{%s}state" % oval_ns,
90
                                                element.text)
91
                continue
92
            if element.tag == "{%s#independent}var_ref" % oval_ns:
93
                element.text = self.generate_id("{%s}variable" % oval_ns,
94
                                                element.text)
95
                continue
96
            for attr in element.keys():
97
                if attr in OVALREFATTR_TO_TAG.keys():
98
                    element.set(attr, self.generate_id(
99
                        "{%s}%s" % (oval_ns, OVALREFATTR_TO_TAG[attr]),
100
                        element.get(attr)))
101
                if attr in OCILREFATTR_TO_TAG.keys():
102
                    element.set(attr, self.generate_id(
103
                        "{%s}%s" % (ocil_ns, OCILREFATTR_TO_TAG[attr]),
104
                        element.get(attr)))
105
            if element.tag == "{%s}test_action_ref" % ocil_ns:
106
                element.text = self.generate_id("{%s}action" % ocil_ns,
107
                                                element.text)
108
109
        return tree
110