| Conditions | 15 |
| Total Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 240 |
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 NaxsiRules.parse_rule() 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 | 1 | from time import strftime, localtime |
|
| 137 | def parse_rule(self, x): |
||
| 138 | """ |
||
| 139 | Parse and validate a full naxsi rule |
||
| 140 | :param x: raw rule |
||
| 141 | :return: [True|False, dict] |
||
| 142 | """ |
||
| 143 | xfrag_kw = {"id:": self.p_id, "str:": self.p_genericstr, |
||
| 144 | "rx:": self.p_genericstr, "msg:": self.p_dummy, "mz:": self.p_mz, |
||
| 145 | "negative": self.p_dummy, "s:": self.p_dummy} |
||
| 146 | |||
| 147 | split = self.splitter(x) # parse string |
||
| 148 | |||
| 149 | |||
| 150 | sect = set(self.mr_kw) & set(split) # check if it's a MainRule/BasicRule, store&delete kw |
||
| 151 | |||
| 152 | if len(sect) != 1: |
||
| 153 | return self.fail("no (or multiple) mainrule/basicrule keyword.") |
||
| 154 | |||
| 155 | split.remove(sect.pop()) |
||
| 156 | |||
| 157 | if ";" in split: |
||
| 158 | split.remove(";") |
||
| 159 | |||
| 160 | while True: # iterate while there is data, as handlers can defer |
||
| 161 | |||
| 162 | if not split: # we are done |
||
| 163 | break |
||
| 164 | |||
| 165 | for kw in split: |
||
| 166 | okw = kw |
||
| 167 | kw = kw.strip() |
||
| 168 | |||
| 169 | # clean-up quotes or semicolon |
||
| 170 | if kw.endswith(";"): |
||
| 171 | kw = kw[:-1] |
||
| 172 | if kw.startswith(('"', "'")) and (kw[0] == kw[-1]): |
||
| 173 | kw = kw[1:-1] |
||
| 174 | for frag_kw in xfrag_kw: |
||
| 175 | ret = False |
||
| 176 | if kw.startswith(frag_kw): |
||
| 177 | # parser funcs returns True/False |
||
| 178 | ret = xfrag_kw[frag_kw](kw[len(frag_kw):]) |
||
| 179 | if ret is False: |
||
| 180 | return self.fail("parsing of element '{0}' failed.".format(kw)) |
||
| 181 | if ret is True: |
||
| 182 | split.remove(okw) |
||
| 183 | break |
||
| 184 | # we have an item that wasn't successfully parsed |
||
| 185 | if okw in split and ret is not None: |
||
| 186 | return False |
||
| 187 | return True |
||
| 188 |
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.