Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #372 from diraol/fix-370-jinja2-setup-load
Browse files Browse the repository at this point in the history
Fix #370 - jinja2 on setup
  • Loading branch information
beraldoleal committed Apr 25, 2017
2 parents 2520470 + f3bb202 commit 81f85b0
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion setup.py
Expand Up @@ -9,6 +9,7 @@
from abc import abstractmethod
# Disabling checks due to https://github.com/PyCQA/pylint/issues/73
from distutils.command.clean import clean # pylint: disable=E0401,E0611
from pathlib import Path
from subprocess import call

from setuptools import Command, find_packages, setup
Expand Down Expand Up @@ -96,6 +97,26 @@ def run(self):
class CommonInstall:
"""Class with common method used by children classes."""

@staticmethod
def add_jinja2_to_path():
"""Add Jinja2 into sys.path.
As Jinja2 may be installed during this setup, it will not be available
on runtime to be used here. So, we need to look for it and append it on
the sys.path.
"""
#: First we find the 'site_pkg' directory
site_pkg = None
for path in sys.path:
if Path(path).stem == 'site-packages':
site_pkg = Path(path)
break

#: Then we get the 'Jinja2' egg directory and append it into the
#: current sys.path.
jinja2path = next(site_pkg.glob('Jinja2*'))
sys.path.append(str(jinja2path))

@staticmethod
def generate_file_from_template(templates,
destination=os.path.dirname(__file__),
Expand All @@ -110,7 +131,12 @@ def generate_file_from_template(templates,
destination (string): Directory in which the config file will
be placed.
"""
from jinja2 import Template
try:
from jinja2 import Template # pylint: disable=import-error
except ImportError:
CommonInstall.add_jinja2_to_path()
from jinja2 import Template # pylint: disable=import-error

for path in templates:
with open(path, 'r', encoding='utf-8') as src_file:
content = Template(src_file.read()).render(**kwargs)
Expand Down

0 comments on commit 81f85b0

Please sign in to comment.