forked from particleKIT/StaticKIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjinja.py
More file actions
executable file
·57 lines (47 loc) · 1.92 KB
/
Copy pathjinja.py
File metadata and controls
executable file
·57 lines (47 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import codecs
import fnmatch
from jinja2 import Environment, FileSystemLoader, Template
from difflib import unified_diff
from datetime import datetime
def datetimeformat(value, format='%d.%m.%Y %H:%M'):
return value.strftime(format)
def todatetime(value, format='%d.%m.%Y'):
return datetime.strptime(value, format)
class template:
def __init__(self):
self.env = Environment(loader=FileSystemLoader("/"),trim_blocks=True)
self.substitutions = {}
self.env.filters['datetimeformat'] = datetimeformat
self.env.filters['todatetime'] = todatetime
def add_subst(self, new):
self.substitutions.update(new)
def add_glob(self, new):
self.env.globals.update(new)
def clear(self):
self.substitutions.clear()
self.env.globals.clear()
def save(self, filein, fileout, mode='w', diff=False):
if os.path.isdir(filein):
if not os.path.isdir(fileout):
os.makedirs(fileout)
for file in os.listdir(filein):
if fnmatch.fnmatch(file, '*~'): continue
if fnmatch.fnmatch(file, '.*.sw?'): continue
next_fileout = fileout + '/' + file
next_filein = filein + '/' + file
self.save(next_filein, next_fileout, mode, diff)
else:
content = self.env.get_template(filein).render(self.substitutions)
if diff:
if os.path.isfile(fileout):
before=open(fileout).read().splitlines()
after=content.splitlines()
print('\n'.join(unified_diff(before, after, fromfile=fileout, tofile=fileout)))
else:
print("created: " + fileout)
if mode == "w":
f = codecs.open(fileout, mode, encoding='utf-8', errors='ignore')
f.write(content)