| Conditions | 11 |
| Total Lines | 54 |
| 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 prompt_for_config() 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 -*- |
||
| 143 | def prompt_for_config(context, no_input=False): |
||
| 144 | """ |
||
| 145 | Prompts the user to enter new config, using context as a source for the |
||
| 146 | field names and sample values. |
||
| 147 | |||
| 148 | :param no_input: Prompt the user at command line for manual configuration? |
||
| 149 | """ |
||
| 150 | cookiecutter_dict = {} |
||
| 151 | env = StrictEnvironment(context=context) |
||
| 152 | |||
| 153 | # First pass: Handle simple and raw variables, plus choices. |
||
| 154 | # These must be done first because the dictionaries keys and |
||
| 155 | # values might refer to them. |
||
| 156 | for key, raw in iteritems(context[u'cookiecutter']): |
||
| 157 | if key.startswith(u'_'): |
||
| 158 | cookiecutter_dict[key] = raw |
||
| 159 | continue |
||
| 160 | |||
| 161 | try: |
||
| 162 | if isinstance(raw, list): |
||
| 163 | # We are dealing with a choice variable |
||
| 164 | val = prompt_choice_for_config( |
||
| 165 | cookiecutter_dict, env, key, raw, no_input |
||
| 166 | ) |
||
| 167 | cookiecutter_dict[key] = val |
||
| 168 | elif not isinstance(raw, dict): |
||
| 169 | # We are dealing with a regular variable |
||
| 170 | val = render_variable(env, raw, cookiecutter_dict) |
||
| 171 | |||
| 172 | if not no_input: |
||
| 173 | val = read_user_variable(key, val) |
||
| 174 | |||
| 175 | cookiecutter_dict[key] = val |
||
| 176 | except UndefinedError as err: |
||
| 177 | msg = "Unable to render variable '{}'".format(key) |
||
| 178 | raise UndefinedVariableInTemplate(msg, err, context) |
||
| 179 | |||
| 180 | # Second pass; handle the dictionaries. |
||
| 181 | for key, raw in iteritems(context[u'cookiecutter']): |
||
| 182 | |||
| 183 | try: |
||
| 184 | if isinstance(raw, dict): |
||
| 185 | # We are dealing with a dict variable |
||
| 186 | val = render_variable(env, raw, cookiecutter_dict) |
||
| 187 | |||
| 188 | if not no_input: |
||
| 189 | val = read_user_dict(key, val) |
||
| 190 | |||
| 191 | cookiecutter_dict[key] = val |
||
| 192 | except UndefinedError as err: |
||
| 193 | msg = "Unable to render variable '{}'".format(key) |
||
| 194 | raise UndefinedVariableInTemplate(msg, err, context) |
||
| 195 | |||
| 196 | return cookiecutter_dict |
||
| 197 |