| Conditions | 21 |
| Total Lines | 69 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| 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 sopel.loader.clean_callable() 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 | # coding=utf-8 |
||
| 39 | def clean_callable(func, config): |
||
| 40 | """Compiles the regexes, moves commands into func.rule, fixes up docs and |
||
| 41 | puts them in func._docs, and sets defaults""" |
||
| 42 | nick = config.core.nick |
||
| 43 | alias_nicks = config.core.alias_nicks |
||
| 44 | prefix = config.core.prefix |
||
| 45 | help_prefix = config.core.help_prefix |
||
| 46 | func._docs = {} |
||
| 47 | doc = trim_docstring(func.__doc__) |
||
| 48 | examples = [] |
||
| 49 | |||
| 50 | func.unblockable = getattr(func, 'unblockable', False) |
||
| 51 | func.echo = getattr(func, 'echo', False) |
||
| 52 | func.priority = getattr(func, 'priority', 'medium') |
||
| 53 | func.thread = getattr(func, 'thread', True) |
||
| 54 | func.rate = getattr(func, 'rate', 0) |
||
| 55 | func.channel_rate = getattr(func, 'channel_rate', 0) |
||
| 56 | func.global_rate = getattr(func, 'global_rate', 0) |
||
| 57 | |||
| 58 | if not hasattr(func, 'event'): |
||
| 59 | func.event = ['PRIVMSG'] |
||
| 60 | else: |
||
| 61 | if isinstance(func.event, basestring): |
||
|
|
|||
| 62 | func.event = [func.event.upper()] |
||
| 63 | else: |
||
| 64 | func.event = [event.upper() for event in func.event] |
||
| 65 | |||
| 66 | if hasattr(func, 'rule'): |
||
| 67 | if isinstance(func.rule, basestring): |
||
| 68 | func.rule = [func.rule] |
||
| 69 | func.rule = [compile_rule(nick, rule, alias_nicks) for rule in func.rule] |
||
| 70 | |||
| 71 | if any(hasattr(func, attr) for attr in ['commands', 'nickname_commands', 'action_commands']): |
||
| 72 | func.rule = getattr(func, 'rule', []) |
||
| 73 | for command in getattr(func, 'commands', []): |
||
| 74 | regexp = get_command_regexp(prefix, command) |
||
| 75 | if regexp not in func.rule: |
||
| 76 | func.rule.append(regexp) |
||
| 77 | for command in getattr(func, 'nickname_commands', []): |
||
| 78 | regexp = get_nickname_command_regexp(nick, command, alias_nicks) |
||
| 79 | if regexp not in func.rule: |
||
| 80 | func.rule.append(regexp) |
||
| 81 | for command in getattr(func, 'action_commands', []): |
||
| 82 | regexp = get_action_command_regexp(command) |
||
| 83 | if regexp not in func.rule: |
||
| 84 | func.rule.append(regexp) |
||
| 85 | if hasattr(func, 'example'): |
||
| 86 | # If no examples are flagged as user-facing, just show the first one like Sopel<7 did |
||
| 87 | examples = [rec["example"] for rec in func.example if rec["help"]] or [func.example[0]["example"]] |
||
| 88 | for i, example in enumerate(examples): |
||
| 89 | example = example.replace('$nickname', nick) |
||
| 90 | if example[0] != help_prefix and not example.startswith(nick): |
||
| 91 | example = example.replace(default_prefix, help_prefix, 1) |
||
| 92 | examples[i] = example |
||
| 93 | if doc or examples: |
||
| 94 | cmds = [] |
||
| 95 | cmds.extend(getattr(func, 'commands', [])) |
||
| 96 | cmds.extend(getattr(func, 'nickname_commands', [])) |
||
| 97 | for command in cmds: |
||
| 98 | func._docs[command] = (doc, examples) |
||
| 99 | |||
| 100 | if hasattr(func, 'intents'): |
||
| 101 | # Can be implementation-dependent |
||
| 102 | _regex_type = type(re.compile('')) |
||
| 103 | func.intents = [ |
||
| 104 | (intent |
||
| 105 | if isinstance(intent, _regex_type) |
||
| 106 | else re.compile(intent, re.IGNORECASE)) |
||
| 107 | for intent in func.intents |
||
| 108 | ] |
||
| 164 |