-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.py
More file actions
executable file
·165 lines (152 loc) · 5.32 KB
/
render.py
File metadata and controls
executable file
·165 lines (152 loc) · 5.32 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 linuor. All Rights Reserved.
# @file render.py
# @brief render a set of files from jinja2 template. Template never includes
# sub-directory. And it will replace the file name with the one given by user.
# @author linuor
# @version 0.1
# @date 2016-02-05
import os, sys
import argparse
import time
import codecs
from jinja2 import Environment, FileSystemLoader
class Render(object):
"""Render files from jinja2 template. """
def __init__(self):
"""Init"""
self.args = None
self.tmpldir = None
self.srcdir = None
self.desdir = None
self.render = None
self.parser = self.initParser()
def initParser(self):
"""Init a argparse
:returns: instance of a argparse
"""
parser = argparse.ArgumentParser(description="Render files from jinja template.")
parser.add_argument(
'dest',
help = 'Specify the location directory, also the name of the destination.',
metavar = 'Destination',
)
parser.add_argument(
'-t',
'--template',
help = 'Specify the source template used to render, also the name of the sub-directory under the template directory.',
metavar = 'Template',
dest = 'tmpl',
default = 'cpp/class'
)
parser.add_argument(
'-n',
'--namespace',
help = 'Specify the namespace for files to render.',
metavar = 'Namespace',
dest = 'ns',
default = 'name_space'
)
parser.add_argument(
'-a',
'--author',
help = 'Specify the author name.',
metavar = 'Author',
dest = 'author',
default = os.getenv('USER', '')
)
parser.add_argument(
'-d',
'--date',
help = 'Specify the date.',
metavar = 'Date',
dest = 'date',
default = time.strftime("%Y-%m-%d %H:%M:%S")
)
parser.add_argument(
'-v',
'--version',
help = 'Specify the version.',
metavar = 'Version',
dest = 'ver',
default = '0.1'
)
return parser
def getTmplDir(self):
"""Get the directory of the template files
:returns: string of the directory
"""
if self.tmpldir == None:
self.tmpldir = sys.path[0]
if os.path.isfile(self.tmpldir):
self.tmpldir = os.path.dirname(self.tmpldir)
return self.tmpldir
def getSrcDir(self):
"""Get the source template files directory
:returns: string of the directory
"""
if self.srcdir == None:
tmpl = self.getTmplDir()
self.srcdir = os.path.join(tmpl, 'template', self.args.tmpl)
return self.srcdir
def getDesDir(self):
"""Get the destination directory
:returns: string of the directory
"""
if self.desdir == None:
cwd = os.getcwd()
(h,t) = os.path.split(self.args.dest)
self.desdir = os.path.join(cwd, h)
self.args.dest = t
return self.desdir
def printInfo(self):
"""Print all the information about the destination to be rendered.
"""
print("Rendering %s with the following info:" % self.args.dest)
print("From: %s" % self.srcdir)
print("To: %s" % self.desdir)
print("Template: %s" % self.args.tmpl)
print("Author: %s" % self.args.author)
print("Version: %s" % self.args.ver)
print("Date: %s" % self.args.date)
print("\n")
def initJinja(self):
self.render = Environment(loader = FileSystemLoader(self.getSrcDir()))
def main():
render = Render()
render.args = render.parser.parse_args()
destDir = render.getDesDir()
rootDir = render.getSrcDir()
render.initJinja()
render.printInfo()
if not os.path.exists(destDir):
t = raw_input("%s does not exist, create it or not? [y/n] " % destDir)
if t.lower() in ['y', 'yes']:
print("Creating directory %s ... " % destDir)
os.makedirs(destDir)
else:
print("Abort!\n")
return
for dirName, subdirList, fileList in os.walk(rootDir):
for fname in fileList:
fs = os.path.join(dirName, fname)
fs = os.path.relpath(fs, rootDir)
(h,t) = os.path.splitext(fname)
fd = os.path.join(destDir, render.args.dest.lower() + t)
if os.path.exists(fd):
t = raw_input("\t%s exists, replace, skip or abort? [r/s/a] " % fd)
t = t.lower()
if t in ['a', 'abort']:
return
if t in ['r', 'replace']:
pass
else:
continue
print("\tRendering %s" % fd)
template = render.render.get_template(fs)
with codecs.open(fd, 'w', 'utf-8') as f:
f.write(template.render(info=render.args))
if __name__ == "__main__":
main()