Completed
Push — master ( 291ee9...9d841e )
by Matěj
20s queued 12s
created

ssg._yaml._identify_special_macro_mapping()   B

Complexity

Conditions 4

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
nop 1
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
1
import codecs
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
import yaml
0 ignored issues
show
introduced by
Unable to import 'yaml'
Loading history...
3
4
from ssg._jinja import _extract_substitutions_dict_from_template
5
from ssg._jinja import _rename_items
6
from ssg._jinja import *
0 ignored issues
show
Coding Style introduced by
The usage of wildcard imports like ssg._jinja should generally be avoided.
Loading history...
Unused Code introduced by
jinja2 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
os was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
AbsolutePathFileSystemLoader was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
required_key was imported with wildcard, but is not used.
Loading history...
7
from ssg._constants import *
0 ignored issues
show
Coding Style introduced by
The usage of wildcard imports like ssg._constants should generally be avoided.
Loading history...
Unused Code introduced by
datetime was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
xml_version was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
datastream_namespace was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ocil_namespace was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
oval_footer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
oval_namespace was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ocil_cs was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
xccdf_header was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
xccdf_footer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
bash_system was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ansible_system was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
puppet_system was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
anaconda_system was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
cce_uri was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
stig_ns was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
disa_cciuri was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
disa_srguri was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ssg_version_uri was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
OSCAP_VENDOR was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
OSCAP_DS_STRING was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
OSCAP_GROUP was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
OSCAP_GROUP_PCIDSS was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
OSCAP_GROUP_VAL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
OSCAP_GROUP_NON_PCI was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
XCCDF11_NS was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
XCCDF12_NS was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
min_ansible_version was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ansible_version_requirement_pre_task_name was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
oval_header was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
timestamp was imported with wildcard, but is not used.
Loading history...
8
9
try:
10
    from yaml import CSafeLoader as yaml_SafeLoader
0 ignored issues
show
introduced by
Imports from package yaml are not grouped
Loading history...
11
except ImportError:
12
    from yaml import SafeLoader as yaml_SafeLoader
13
14
15
def _bool_constructor(self, node):
16
    return self.construct_scalar(node)
17
18
19
# Don't follow python bool case
20
yaml_SafeLoader.add_constructor(u'tag:yaml.org,2002:bool', _bool_constructor)
21
22
23
def _save_rename(result, stem, prefix):
24
    result["{0}_{1}".format(prefix, stem)] = stem
25
26
27
def _open_yaml(stream):
28
    """
29
    Open given file-like object and parse it as YAML
30
    Return None if it contains "documentation_complete" key set to "false".
31
    """
32
    yaml_contents = yaml.load(stream, Loader=yaml_SafeLoader)
33
34
    if yaml_contents.pop("documentation_complete", "true") == "false":
35
        return None
36
37
    return yaml_contents
38
39
40
def _identify_special_macro_mapping(existing_properties):
41
    result = dict()
42
43
    pkg_manager = existing_properties.get("pkg_manager")
44
    if pkg_manager is not None:
45
        _save_rename(result, "describe_package_install", pkg_manager)
46
        _save_rename(result, "describe_package_remove", pkg_manager)
47
48
    pkg_system = existing_properties.get("pkg_system")
49
    if pkg_system is not None:
50
        _save_rename(result, "ocil_package", pkg_system)
51
        _save_rename(result, "complete_ocil_entry_package", pkg_system)
52
53
    init_system = existing_properties.get("init_system")
54
    if init_system is not None:
55
        _save_rename(result, "describe_service_enable", init_system)
56
        _save_rename(result, "describe_service_disable", init_system)
57
        _save_rename(result, "ocil_service_enabled", init_system)
58
        _save_rename(result, "ocil_service_disabled", init_system)
59
        _save_rename(result, "describe_socket_enable", init_system)
60
        _save_rename(result, "describe_socket_disable", init_system)
61
        _save_rename(result, "complete_ocil_entry_socket_and_service_disabled",
62
                     init_system)
63
64
    return result
65
66
67
def _get_implied_properties(existing_properties):
68
    result = dict()
69
    if ("pkg_manager" in existing_properties and
70
            "pkg_system" not in existing_properties):
71
        pkg_manager = existing_properties["pkg_manager"]
72
        result["pkg_system"] = PKG_MANAGER_TO_SYSTEM[pkg_manager]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable PKG_MANAGER_TO_SYSTEM does not seem to be defined.
Loading history...
73
    return result
74
75
76
def open_and_expand(yaml_file, substitutions_dict=None):
77
    """
78
    Process the file as a template, using substitutions_dict to perform
79
    expansion. Then, process the expansion result as a YAML content.
80
81
    See also: _open_yaml
82
    """
83
    if substitutions_dict is None:
84
        substitutions_dict = dict()
85
86
    expanded_template = process_file(yaml_file, substitutions_dict)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable process_file does not seem to be defined.
Loading history...
87
    yaml_contents = _open_yaml(expanded_template)
88
    return yaml_contents
89
90
91
def open_and_macro_expand(yaml_file, substitutions_dict=None):
92
    """
93
    Do the same as open_and_expand, but load definitions of macros
94
    so they can be expanded in the template.
95
    """
96
    if substitutions_dict is None:
97
        substitutions_dict = dict()
98
99
    try:
100
        macro_definitions = _extract_substitutions_dict_from_template(
101
            JINJA_MACROS_DEFINITIONS, substitutions_dict)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable JINJA_MACROS_DEFINITIONS does not seem to be defined.
Loading history...
102
    except Exception as exc:
103
        msg = ("Error extracting macro definitions from {0}: {1}"
104
               .format(JINJA_MACROS_DEFINITIONS, str(exc)))
105
        raise RuntimeError(msg)
106
    mapping = _identify_special_macro_mapping(substitutions_dict)
107
    special_macros = _rename_items(macro_definitions, mapping)
108
    substitutions_dict.update(macro_definitions)
109
    substitutions_dict.update(special_macros)
110
    return open_and_expand(yaml_file, substitutions_dict)
111
112
113
def open_raw(yaml_file):
114
    """
115
    Open given file-like object and parse it as YAML
116
    without performing any kind of template processing
117
118
    See also: _open_yaml
119
    """
120
    with codecs.open(yaml_file, "r", "utf8") as stream:
121
        yaml_contents = _open_yaml(stream)
122
    return yaml_contents
123
124
125
def open_environment(build_config_yaml, product_yaml):
0 ignored issues
show
Coding Style introduced by
This function 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...
126
    contents = open_raw(build_config_yaml)
127
    contents.update(open_raw(product_yaml))
128
    contents.update(_get_implied_properties(contents))
129
    return contents
130