Conditions | 12 |
Total Lines | 118 |
Lines | 0 |
Ratio | 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 generate_files() 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 python |
||
241 | def generate_files(repo_dir, context=None, output_dir='.', |
||
242 | overwrite_if_exists=False): |
||
243 | """ |
||
244 | Renders the templates and saves them to files. |
||
245 | |||
246 | :param repo_dir: Project template input directory. |
||
247 | :param context: Dict for populating the template's variables. |
||
248 | :param output_dir: Where to output the generated project dir into. |
||
249 | :param overwrite_if_exists: Overwrite the contents of the output directory |
||
250 | if it exists |
||
251 | """ |
||
252 | |||
253 | template_dir = find_template(repo_dir) |
||
254 | logging.debug('Generating project from {0}...'.format(template_dir)) |
||
255 | context = context or {} |
||
256 | |||
257 | unrendered_dir = os.path.split(template_dir)[1] |
||
258 | ensure_dir_is_templated(unrendered_dir) |
||
259 | env = StrictEnvironment( |
||
260 | context=context, |
||
261 | keep_trailing_newline=True, |
||
262 | ) |
||
263 | try: |
||
264 | project_dir = render_and_create_dir( |
||
265 | unrendered_dir, |
||
266 | context, |
||
267 | output_dir, |
||
268 | env, |
||
269 | overwrite_if_exists |
||
270 | ) |
||
271 | except UndefinedError as err: |
||
272 | msg = "Unable to create project directory '{}'".format(unrendered_dir) |
||
273 | raise UndefinedVariableInTemplate(msg, err, context) |
||
274 | |||
275 | # We want the Jinja path and the OS paths to match. Consequently, we'll: |
||
276 | # + CD to the template folder |
||
277 | # + Set Jinja's path to '.' |
||
278 | # |
||
279 | # In order to build our files to the correct folder(s), we'll use an |
||
280 | # absolute path for the target folder (project_dir) |
||
281 | |||
282 | project_dir = os.path.abspath(project_dir) |
||
283 | logging.debug('project_dir is {0}'.format(project_dir)) |
||
284 | |||
285 | _run_hook_from_repo_dir(repo_dir, 'pre_gen_project', project_dir, context) |
||
286 | |||
287 | with work_in(template_dir): |
||
288 | env.loader = FileSystemLoader('.') |
||
289 | |||
290 | for root, dirs, files in os.walk('.'): |
||
291 | # We must separate the two types of dirs into different lists. |
||
292 | # The reason is that we don't want ``os.walk`` to go through the |
||
293 | # unrendered directories, since they will just be copied. |
||
294 | copy_dirs = [] |
||
295 | render_dirs = [] |
||
296 | |||
297 | for d in dirs: |
||
298 | d_ = os.path.normpath(os.path.join(root, d)) |
||
299 | # We check the full path, because that's how it can be |
||
300 | # specified in the ``_copy_without_render`` setting, but |
||
301 | # we store just the dir name |
||
302 | if copy_without_render(d_, context): |
||
303 | copy_dirs.append(d) |
||
304 | else: |
||
305 | render_dirs.append(d) |
||
306 | |||
307 | for copy_dir in copy_dirs: |
||
308 | indir = os.path.normpath(os.path.join(root, copy_dir)) |
||
309 | outdir = os.path.normpath(os.path.join(project_dir, indir)) |
||
310 | logging.debug( |
||
311 | 'Copying dir {0} to {1} without rendering' |
||
312 | ''.format(indir, outdir) |
||
313 | ) |
||
314 | shutil.copytree(indir, outdir) |
||
315 | |||
316 | # We mutate ``dirs``, because we only want to go through these dirs |
||
317 | # recursively |
||
318 | dirs[:] = render_dirs |
||
319 | for d in dirs: |
||
320 | unrendered_dir = os.path.join(project_dir, root, d) |
||
321 | try: |
||
322 | render_and_create_dir( |
||
323 | unrendered_dir, |
||
324 | context, |
||
325 | output_dir, |
||
326 | env, |
||
327 | overwrite_if_exists |
||
328 | ) |
||
329 | except UndefinedError as err: |
||
330 | rmtree(project_dir) |
||
331 | _dir = os.path.relpath(unrendered_dir, output_dir) |
||
332 | msg = "Unable to create directory '{}'".format(_dir) |
||
333 | raise UndefinedVariableInTemplate(msg, err, context) |
||
334 | |||
335 | for f in files: |
||
336 | infile = os.path.normpath(os.path.join(root, f)) |
||
337 | if copy_without_render(infile, context): |
||
338 | outfile_tmpl = env.from_string(infile) |
||
339 | outfile_rendered = outfile_tmpl.render(**context) |
||
340 | outfile = os.path.join(project_dir, outfile_rendered) |
||
341 | logging.debug( |
||
342 | 'Copying file {0} to {1} without rendering' |
||
343 | ''.format(infile, outfile) |
||
344 | ) |
||
345 | shutil.copyfile(infile, outfile) |
||
346 | shutil.copymode(infile, outfile) |
||
347 | continue |
||
348 | logging.debug('f is {0}'.format(f)) |
||
349 | try: |
||
350 | generate_file(project_dir, infile, context, env) |
||
351 | except UndefinedError as err: |
||
352 | rmtree(project_dir) |
||
353 | msg = "Unable to create file '{}'".format(infile) |
||
354 | raise UndefinedVariableInTemplate(msg, err, context) |
||
355 | |||
356 | _run_hook_from_repo_dir(repo_dir, 'post_gen_project', project_dir, context) |
||
357 | |||
358 | return project_dir |
||
359 |