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