Skip to content

Commit

Permalink
Refactor code for tidyness points
Browse files Browse the repository at this point in the history
  • Loading branch information
ewels committed Mar 22, 2021
1 parent 676cf8f commit 3261493
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions nf_core/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
organization's specification based on a template.
"""
from genericpath import exists
from pathlib import Path
import git
import jinja2
import logging
Expand Down Expand Up @@ -108,31 +107,32 @@ def render_template(self):
output_path = os.path.join(self.outdir, template_fn)
os.makedirs(os.path.dirname(output_path), exist_ok=True)

# Just copy binary files
(ftype, encoding) = mimetypes.guess_type(template_fn_path, strict=False)
if encoding is not None or (ftype is not None and any([ftype.startswith(ft) for ft in binary_ftypes])):
log.debug(f"Copying binary file: '{output_path}'")
shutil.copy(template_fn_path, output_path)
continue
try:
# Just copy certain file extensions
filename, file_extension = os.path.splitext(template_fn_path)
if file_extension in binary_extensions:
raise AttributeError(f"File extension: {file_extension}")

if Path(template_fn_path).suffix in binary_extensions:
log.debug(f"Copying binary file: '{output_path}'")
shutil.copy(template_fn_path, output_path)
continue
# Try to detect binary files
(ftype, encoding) = mimetypes.guess_type(template_fn_path, strict=False)
if encoding is not None or (ftype is not None and any([ftype.startswith(ft) for ft in binary_ftypes])):
raise AttributeError(f"Encoding: {encoding}")

# Render the template
try:
# Got this far - render the template
log.debug(f"Rendering template file: '{template_fn}'")
j_template = env.get_template(template_fn)
rendered_output = j_template.render(object_attrs)
except UnicodeDecodeError as e:
log.error(f"Could not decode file {template_fn_path} {e}")
sys.exit(1)

# Write to the pipeline output file
with open(output_path, "w") as fh:
log.debug(f"Writing to output file: '{output_path}'")
fh.write(rendered_output)
# Write to the pipeline output file
with open(output_path, "w") as fh:
log.debug(f"Writing to output file: '{output_path}'")
fh.write(rendered_output)

# Copy the file directly instead of using Jinja
except (AttributeError, UnicodeDecodeError) as e:
log.debug(f"Copying file without Jinja: '{output_path}' - {e}")
shutil.copy(template_fn_path, output_path)
continue

# Make a logo and save it
self.make_pipeline_logo()
Expand Down

0 comments on commit 3261493

Please sign in to comment.