mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-06 20:40:38 +00:00
add ability to have more methods available from within the templates
This commit is contained in:
13
dotdrop/jhelpers.py
Normal file
13
dotdrop/jhelpers.py
Normal file
@@ -0,0 +1,13 @@
|
||||
"""
|
||||
author: deadc0de6 (https://github.com/deadc0de6)
|
||||
Copyright (c) 2018, deadc0de6
|
||||
|
||||
jinja2 helper methods
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
def exists(path):
|
||||
'''return true when path exists'''
|
||||
return os.path.exists(path)
|
||||
@@ -11,6 +11,7 @@ from jinja2 import Environment, FileSystemLoader
|
||||
# local imports
|
||||
import dotdrop.utils as utils
|
||||
from dotdrop.logger import Logger
|
||||
import dotdrop.jhelpers as jhelpers
|
||||
|
||||
BLOCK_START = '{%@@'
|
||||
BLOCK_END = '@@%}'
|
||||
@@ -25,6 +26,7 @@ class Templategen:
|
||||
def __init__(self, profile, base='.', variables={}, debug=False):
|
||||
self.base = base.rstrip(os.sep)
|
||||
self.debug = debug
|
||||
self.log = Logger()
|
||||
loader = FileSystemLoader(self.base)
|
||||
self.env = Environment(loader=loader,
|
||||
trim_blocks=True, lstrip_blocks=True,
|
||||
@@ -35,11 +37,14 @@ class Templategen:
|
||||
variable_end_string=VAR_END,
|
||||
comment_start_string=COMMENT_START,
|
||||
comment_end_string=COMMENT_END)
|
||||
self.env.globals['header'] = self._header
|
||||
# adding variables
|
||||
self.env.globals['env'] = os.environ
|
||||
self.env.globals['profile'] = profile
|
||||
self.env.globals.update(variables)
|
||||
self.log = Logger()
|
||||
# adding header method
|
||||
self.env.globals['header'] = self._header
|
||||
# adding helper methods
|
||||
self.env.globals['exists'] = jhelpers.exists
|
||||
|
||||
def generate(self, src):
|
||||
if not os.path.exists(src):
|
||||
|
||||
Reference in New Issue
Block a user