| Conditions | 11 |
| Total Lines | 79 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 1 |
| CRAP Score | 120.6497 |
| 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 ssg.oval.parse_affected() 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 | 2 | from __future__ import absolute_import |
|
| 68 | 2 | def parse_affected(oval_contents): |
|
| 69 | """ |
||
| 70 | Returns the tuple (start_affected, end_affected, platform_indents) for |
||
| 71 | the passed oval file contents. start_affected is the line number of |
||
| 72 | starting tag of the <affected> element, end_affected is the line number of |
||
| 73 | the closing tag of the </affected> element, and platform_indents is a |
||
| 74 | string containing the indenting characters before the contents of the |
||
| 75 | <affected> element. |
||
| 76 | """ |
||
| 77 | |||
| 78 | start_affected = list(filter(lambda x: "<affected" in oval_contents[x], |
||
| 79 | range(0, len(oval_contents)))) |
||
| 80 | if len(start_affected) != 1: |
||
| 81 | raise ValueError("OVAL file does not contain a single <affected> " |
||
| 82 | "element; counted %d in:\n%s\n\n" % |
||
| 83 | (len(start_affected), "\n".join(oval_contents))) |
||
| 84 | |||
| 85 | start_affected = start_affected[0] |
||
| 86 | |||
| 87 | end_affected = list(filter(lambda x: "</affected" in oval_contents[x], |
||
| 88 | range(0, len(oval_contents)))) |
||
| 89 | if len(end_affected) != 1: |
||
| 90 | raise ValueError("Malformed OVAL file does not contain a single " |
||
| 91 | "closing </affected>; counted %d in:\n%s\n\n" % |
||
| 92 | (len(start_affected), "\n".join(oval_contents))) |
||
| 93 | end_affected = end_affected[0] |
||
| 94 | |||
| 95 | if start_affected >= end_affected: |
||
| 96 | raise ValueError("Malformed OVAL file: start affected tag begins " |
||
| 97 | "on the same line or after ending affected tag: " |
||
| 98 | "start:%d vs end:%d:\n%s\n\n" % |
||
| 99 | (start_affected, end_affected, oval_contents)) |
||
| 100 | |||
| 101 | # Validate that start_affected contains only a starting <affected> tag; |
||
| 102 | # otherwise, using this information to update the <platform> subelements |
||
| 103 | # would fail. |
||
| 104 | start_line = oval_contents[start_affected] |
||
| 105 | start_line = start_line.strip() |
||
| 106 | |||
| 107 | if not start_line.startswith('<affected'): |
||
| 108 | raise ValueError("Malformed OVAL file: line with starting affected " |
||
| 109 | "tag contains other elements: line:%s\n%s\n\n" % |
||
| 110 | (start_line, oval_contents)) |
||
| 111 | if '<' in start_line[1:]: |
||
| 112 | raise ValueError("Malformed OVAL file: line with starting affected " |
||
| 113 | "tag contains other elements: line:%s\n%s\n\n" % |
||
| 114 | (start_line, oval_contents)) |
||
| 115 | |||
| 116 | # Validate that end_affected contains only an ending </affected> tag; |
||
| 117 | # otherwise, using this information to update the <platform> subelements |
||
| 118 | # would fail. |
||
| 119 | end_line = oval_contents[end_affected] |
||
| 120 | end_line = end_line.strip() |
||
| 121 | |||
| 122 | if not end_line.startswith('</affected>'): |
||
| 123 | raise ValueError("Malformed OVAL file: line with ending affected " |
||
| 124 | "tag contains other elements: line:%s\n%s\n\n" % |
||
| 125 | (end_line, oval_contents)) |
||
| 126 | if '<' in end_line[1:]: |
||
| 127 | raise ValueError("Malformed OVAL file: line with ending affected " |
||
| 128 | "tag contains other elements: line:%s\n%s\n\n" % |
||
| 129 | (end_line, oval_contents)) |
||
| 130 | |||
| 131 | indents = "" |
||
| 132 | if start_affected+1 == end_affected: |
||
| 133 | # Since the affected element is present but empty, the indents should |
||
| 134 | # be two more spaces than that of the starting <affected> element. |
||
| 135 | start_index = oval_contents[start_affected].index('<') |
||
| 136 | indents = oval_contents[start_affected][0:start_index] |
||
| 137 | indents += " " |
||
| 138 | else: |
||
| 139 | # Otherwise, grab the indents off the next line unmodified, as this is |
||
| 140 | # likely a platform element tag. We don't validate here that this is |
||
| 141 | # indeed the case, as other parts of the build infrastructure will |
||
| 142 | # validate this for us. |
||
| 143 | start_index = oval_contents[start_affected+1].index('<') |
||
| 144 | indents = oval_contents[start_affected+1][0:start_index] |
||
| 145 | |||
| 146 | return start_affected, end_affected, indents |
||
| 147 |