1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 19:44:45 +00:00

speed up template detection

This commit is contained in:
deadc0de6
2020-11-18 15:23:28 +01:00
parent ef6d78ed45
commit 578fb86466
2 changed files with 14 additions and 8 deletions

View File

@@ -6,6 +6,9 @@ jinja2 template generator
"""
import os
import io
import re
import mmap
from jinja2 import Environment, FileSystemLoader, \
ChoiceLoader, FunctionLoader, TemplateNotFound, \
StrictUndefined
@@ -244,16 +247,19 @@ class Templategen:
"""test if file pointed by path is a template"""
if not os.path.isfile(path):
return False
if os.stat(path).st_size == 0:
return False
markers = [BLOCK_START, VAR_START, COMMENT_START]
patterns = [re.compile(marker.encode()) for marker in markers]
try:
with open(path, 'r') as f:
data = f.read()
with io.open(path, "r", encoding="utf-8") as f:
m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
for pattern in patterns:
if pattern.search(m):
return True
except UnicodeDecodeError:
# is binary so surely no template
return False
markers = [BLOCK_START, VAR_START, COMMENT_START]
for marker in markers:
if marker in data:
return True
return False
def _debug_dict(self, title, elems):

View File

@@ -67,8 +67,8 @@ config:
actions:
pre:
first: 'echo first > ${tmpa}/cookie'
second: 'echo second >> ${tmpa}/cookie'
third: 'echo third >> ${tmpa}/cookie'
second: 'sleep 1; echo second >> ${tmpa}/cookie'
third: 'sleep 1; echo third >> ${tmpa}/cookie'
dotfiles:
f_first:
dst: ${tmpd}/first