| Conditions | 11 |
| Total Lines | 52 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 132 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like utils.create-stig-overlay.new_stig_overlay() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #!/usr/bin/env python3 |
||
| 72 | def new_stig_overlay(xccdftree, ssgtree, outfile, quiet): |
||
| 73 | if not ssgtree: |
||
| 74 | ssg_mapping = False |
||
| 75 | else: |
||
| 76 | ssg_mapping = ssg_xccdf_stigid_mapping(ssgtree) |
||
| 77 | |||
| 78 | new_stig_overlay = ET.Element("overlays", xmlns=xccdf_ns) |
||
| 79 | for group in xccdftree.findall("./{%s}Group" % xccdf_ns): |
||
| 80 | vkey = group.get("id").strip('V-') |
||
| 81 | for title in group.findall("./{%s}title" % xccdf_ns): |
||
| 82 | srg = title.text |
||
| 83 | for rule in group.findall("./{%s}Rule" % xccdf_ns): |
||
| 84 | svkey_raw = rule.get("id") |
||
| 85 | svkey = svkey_raw.strip()[3:9] |
||
| 86 | severity = rule.get("severity") |
||
| 87 | release = svkey_raw.strip()[10:-5] |
||
| 88 | version = element_value("version", rule) |
||
| 89 | rule_title = element_value("title", rule) |
||
| 90 | ident = element_value("ident", rule).strip("CCI-").lstrip("0") |
||
| 91 | |||
| 92 | if not ssgtree: |
||
| 93 | mapped_id = "XXXX" |
||
| 94 | else: |
||
| 95 | try: |
||
| 96 | mapped_id = ''.join(ssg_mapping[version].keys()) |
||
| 97 | except KeyError as e: |
||
| 98 | mapped_id = "XXXX" |
||
| 99 | |||
| 100 | overlay = ET.SubElement(new_stig_overlay, "overlay", owner=owner, |
||
| 101 | ruleid=mapped_id, ownerid=version, disa=ident, |
||
| 102 | severity=severity) |
||
| 103 | vmsinfo = ET.SubElement(overlay, "VMSinfo", VKey=vkey, |
||
| 104 | SVKey=svkey, VRelease=release) |
||
| 105 | title = ET.SubElement(overlay, "title", text=rule_title) |
||
| 106 | |||
| 107 | lines = new_stig_overlay.findall("overlay") |
||
| 108 | new_stig_overlay[:] = sorted(lines, key=getkey) |
||
| 109 | |||
| 110 | dom = xml.dom.minidom.parseString(ET.tostring(new_stig_overlay, encoding="UTF-8", xml_declaration=True)) |
||
| 111 | pretty_xml_as_string = dom.toprettyxml(indent=' ', encoding="UTF-8") |
||
| 112 | |||
| 113 | overlay_directory = os.path.dirname(outfile) |
||
| 114 | if not os.path.exists(overlay_directory): |
||
| 115 | os.makedirs(overlay_directory) |
||
| 116 | if not quiet: |
||
| 117 | print("\nOverlay directory created: %s" % overlay_directory) |
||
| 118 | |||
| 119 | with open(outfile, 'wb') as f: |
||
| 120 | f.write(pretty_xml_as_string) |
||
| 121 | |||
| 122 | if not quiet: |
||
| 123 | print("\nGenerated the new STIG overlay file: %s" % outfile) |
||
| 124 | |||
| 181 |