1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-12 23:55:20 +00:00

Merge pull request #292 from deadc0de6/chmod

Chmod
This commit is contained in:
deadc0de
2020-11-21 13:52:03 +01:00
committed by GitHub
51 changed files with 2967 additions and 919 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
@@ -154,7 +157,7 @@ class Templategen:
except ImportError:
# fallback
_, filetype = utils.run(['file', '-b', '--mime-type', src],
raw=False, debug=self.debug)
debug=self.debug)
if self.debug:
self.log.dbg('using \"file\" for filetype identification')
filetype = filetype.strip()
@@ -245,16 +248,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):