-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagen.py
301 lines (238 loc) · 7.9 KB
/
agen.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# coding=utf8
__version__ = '0.1.1'
__author__ = 'Yufei Li <[email protected]>'
__all__ = [
'string_render', 'render',
'generate', 'generate_dir'
]
"""
Code Generator
~~~~~~~~~~~~~~
:copyright: (c) 2016 Yufei Li
"""
import os
import re
import six
import json
import yaml
import click
import jinja2
import chardet
import logging
import subprocess
# Initialize globals
logging.basicConfig()
logger = logging.getLogger('agen')
app_folder = click.get_app_dir("agen", force_posix=True)
class Coderes(object):
@classmethod
def read(cls, obj, as_encoding=None):
"""
Read as unicode
"""
content = obj.read()
if six.PY3:
return content
confidences = chardet.detect(content)
_confidence, _encoding = [0, 'utf-8']
for confidence, encoding in six.iteritems(confidences):
if confidence > _confidence:
_confidence, _encoding = confidence, encoding
if as_encoding:
return content.decode(_encoding).encode(as_encoding)
return content.decode(_encoding)
class AbsLoader(jinja2.loaders.BaseLoader):
"""
A loader use absolute path for jinja2.
see _`http://jinja.pocoo.org/docs/dev/api/#loaders`
"""
def __init__(self):
pass
def get_source(self, environment, template):
path = template
if not os.path.exists(path):
raise jinja2.exceptions.TemplateNotFound(template)
mtime = os.path.getmtime(path)
with open(path) as f:
source = Coderes.read(f)
return source, path, lambda: mtime == os.path.getmtime(path)
class agenError(Exception):
"""
agen exception.
"""
pass
TEMPLATED_PARTTERN = re.compile(r'{{[^}]+}}')
def string_render(string, context, jinja_env=None):
"""
Straight to render a template string.
:param string: a template string
:param context: context variable will be render
:return: string be rendered
"""
_env = jinja_env or jinja2.Environment(loader=AbsLoader())
_context = context or {}
if TEMPLATED_PARTTERN.search(string):
return _env.from_string(string).render(_context)
return string
def render(template_file, context=None, jinja_env=None):
"""
Render string from :mod:`jinja2` template file.
:param template_file: path of template file.
:param context: context dictionary
:param jinja_env: :class:`jinja2.Environment` object.
:return: rendered text
"""
_env = jinja_env or jinja2.Environment(loader=AbsLoader())
_context = context or {}
template = _env.get_template(template_file)
return template.render(_context)
def generate(template_file, out, context=None, jinja_env=None):
"""
Generate a pure text file from :mod:`jinja2` template file.
:param template_file: path of template file.
:param out: path of output file
:param context: context dictionary
:param jinja_env: :class:`jinja2.Environment` object.
"""
_env = jinja_env or jinja2.Environment(loader=AbsLoader())
_context = context or {}
outdir = os.path.dirname(out)
if not os.path.exists(outdir):
os.makedirs(outdir)
with open(out, 'w') as f:
text = render(template_file, context=_context, jinja_env=_env)
if six.PY2:
text = text.encode('utf-8')
f.write(text)
def generate_dir(directory, out, context=None, jinja_env=None):
"""
Generate a directory from :mod:`jinja2` template directory.
:param template_file: path of template file.
:param out: path of output file
:param context: context dictionary
:param jinja_env: :class:`jinja2.Environment` object.
"""
loader = jinja2.FileSystemLoader(directory)
_env = jinja_env or jinja2.Environment(loader=loader)
_context = context or {}
for dirname, _, fnames in os.walk(directory):
reldir = os.path.relpath(dirname, directory)
for name in fnames:
relpath = os.path.join(reldir, name)
relout = string_render(relpath, _context)
relout = os.path.join(out, relout)
generate(relpath, relout, context=_context, jinja_env=_env)
def choice(directory, files):
"""
:param directory: directory of files in.
:param files: list of file be choice
:return: first existed path
"""
for name in files:
fpath = os.path.join(directory, name)
if os.path.exists(fpath):
return fpath
return None
def load_context(path):
"""
load context file
"""
with open(path) as f:
if path.endswith('.json'):
return json.load(f)
if path.endswith('.yaml'):
return yaml.load(f)
return {}
def prompt(dictionary):
"""
Interactive input for dictionary sample data
:param dictionary: key-value pair that will be prompted
"""
_dictionary = {}
for key, value in six.iteritems(dictionary):
_dictionary[key] = click.prompt(key, default=value,
type=type(value))
return _dictionary
class Path(object):
"""
Path validator
"""
def __init__(self, path):
self.path = path
assert os.path.exists(path)
@property
def is_single(self):
return os.path.isfile(self.path)
@property
def is_repo(self):
try:
return bool(self.inner) and (self.context is not None)
except AssertionError:
return False
@property
def is_dir(self):
return os.path.isdir(self.path) and not self.is_repo
@property
def inner(self):
assert os.path.isdir(self.path)
for name in os.listdir(self.path):
inner = os.path.join(self.path, name)
if os.path.isdir(inner):
return inner
return None
@property
def context(self):
assert os.path.isdir(self.path)
conf = choice(self.path, ['agen.json', 'agen.yaml'])
if not conf:
return None
return load_context(conf)
def _prompt_app_folder(folder):
folder_prompt = " see -> {tf}".format(tf=folder)
click.secho("-" * (len(folder_prompt) + 2), fg='blue')
click.secho(" agen Library", fg='blue')
click.secho(folder_prompt, fg='blue')
click.secho("-" * (len(folder_prompt) + 2), fg='blue')
@click.command()
@click.option('-o', '--out', type=click.Path(), help="Output path or directory")
@click.option('-s', '--source', type=click.Path(exists=True), help="Source path or directory")
@click.option('-c', '--context', type=click.Path(exists=True), help="Path of context file")
@click.argument('name', nargs=-1)
def cli(out, source, context, name):
_context = {}
tf = os.path.join(app_folder, "templates")
if not os.path.exists(app_folder):
os.mkdir(app_folder)
os.mkdir(tf)
click.secho("Creating {af} ...".format(af=app_folder), fg="green")
if not (source or name):
_prompt_app_folder(tf)
subprocess.check_call(
'ls ' + os.path.join(app_folder, "templates"),
shell=True)
exit(0)
if not out:
out = click.prompt('Output directory', default=os.path.curdir)
try:
path = Path(source or os.path.join(tf, name[0]))
except AssertionError:
click.secho("\nThis path does not exist", fg='red')
return
if context:
_context.update(load_context(context))
if path.is_repo:
if not context:
_context.update(prompt(path.context))
inner = path.inner
out_name = string_render(os.path.split(inner)[-1], _context)
out = os.path.join(out, out_name)
generate_dir(inner, out, context=_context)
if path.is_dir:
generate_dir(path.path, out, context=_context)
if path.is_single:
if os.path.isdir(out):
out = os.path.join(out, path.path)
generate(path.path, out, context=_context)
click.secho("Generate success ~ !!", fg='green')
if __name__ == '__main__':
cli()