/
template.py
60 lines (48 loc) · 1.42 KB
/
template.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
# -*- coding: UTF-8 -*-
import re
__all__ = ["new"]
TARGETS = {
'pybots.HTTPBot': """
with HTTPBot("...", verbose=args.verbose) as bot:
bot.get()
""",
'pybots.JSONBot': """
with JSONBot("...", verbose=args.verbose) as bot:
bot.get()
""",
'pybots.TCPBot': """
with TCPBot("...", 1234, verbose=args.verbose) as bot:
bot.send_receive("...")
"""
}
TEMPLATE = """#!/usr/bin/python3
# -*- coding: UTF-8 -*-
{target}from tinyscript import *
# TODO: fill in imports
__author__ = "John Doe"
__email__ = "john.doe@example.com"
__version__ = "1.0"
__copyright__ = "J. Doe"
__license__ = "agpl-3.0"
#__reference__ = ""
#__source__ = ""
#__training__ = ""
# TODO: fill in the docstring
__doc__ = \"\"\"
This tool ...
\"\"\"
# TODO: fill in examples
__examples__ = [""]
if __name__ == '__main__':
# TODO: add arguments
initialize()
# TODO: write logic here{base}
"""
def new(name, target=None):
""" Function for creating a template script.
:param name: name of the new script/tool
:param target: type of script to be created, a value among TARGETS' keys
"""
with open("{}.py".format(name), 'w') as f:
target_imp = "" if target is None else "from {} import {}\n".format(*target.split('.'))
f.write(TEMPLATE.format(base=TARGETS.get(target) or "", target=target_imp))