-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild_from_template.py
208 lines (186 loc) · 11.7 KB
/
build_from_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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# build_from_template.py
# February 2022
#
# build_from_template.py reads in a file passed as the first parameter
# (e.g. templates/checklist_template.md), strips out custom comments,
# and replaces Table of Contents (TOC) placeholders with sorted and
# filtered tables of tasks in Markdown syntax. It then writes out a
# file to a path passed as the second parameter.
#
# Usage:
# You can either run this script directly, like this:
# build_from_template.py -t templates/checklist_template.md -o checklist.md
# Or, you can import it into another python script and call its
# main function, like this:
# from build_from_template import build_from_template
# build_from_template('templates/checklist_template.md','checklist.md')
#
# Change History
#
# 02FEB2022 Creation
# 04FEB2022 Generalize to build any template .md file, also
# build_from_template.py can now be called as a script or
# build_from_template() can be run as a function from
# another Python script.
# 23MAR2023 Added support for optional Frequency, Topic and Essential columns
#
#
# Copyright © 2022-2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# Import Python modules
import argparse
import sys
import contextlib
import os
import datetime
from checklistsharedfunctions import toc, rtfreq
from distutils import util
debug=True
# Define exception handler so that we only output trace info from errors when in debug mode
def exception_handler(exception_type, exception, traceback, debug_hook=sys.excepthook):
if debug:
debug_hook(exception_type, exception, traceback)
else:
print (exception_type.__name__, exception)
sys.excepthook = exception_handler
def build_from_template(strTemplateFilePathIn,strFilePathOut):
print("Building "+strFilePathOut+" from "+strTemplateFilePathIn)
# Find top level directory of this project, based on an ASSUMPTION
# that this script (whose actual pathname stored in a special variable
# called __FILE__) is in a directory directly below the top level
# directory, e.g. /scripts. Put another way, find the parent directory
# of the directory where this script is. This is independent of the
# current working directory, which can be found by calling (os.getcwd())
# strProjectRootPath="/mnt/c/WorkshopGit/checklist/admin-checklist"
strProjectRootPath, strScriptSubdirectory = os.path.split(os.path.dirname(os.path.realpath(__file__)))
# print('strProjectRootPath: '+strProjectRootPath)
# Tasks subdirectory relative to strProjectRootPath. You shouldn't need to modify this.
strTaskSubdirectory="tasks"
@contextlib.contextmanager
def pushd(strNewDir):
strOldDir = os.getcwd()
os.chdir(strNewDir)
try:
yield
finally:
os.chdir(strOldDir)
# cd to the file root (from wherever we currently are) so that relative file paths work = like pushd in bash
with pushd(strProjectRootPath):
# Code must remain indented to still be in the file root (in this with-statement context)
# When we reach code that is outdented to the same level as the with statement above,
# we effectively popd.
# print(os.getcwd()) # strProjectRootPath
# Delete existing output file
if os.path.exists(strFilePathOut):
os.remove(strFilePathOut)
# Open file reference to the Checklist Template file
with open(strTemplateFilePathIn) as fileIn:
with open(strFilePathOut, 'w') as fileOut:
# Read fileIn line by line
for line in fileIn:
if line.startswith('@_TOC_'):
# Table of contents
# Default sort and filter
strTagFilter="Initial"
intSortColumn=0 # 0:SortString, 1:Title, 2:Description, 3:Tags, 4: relative path to task file from fileOut
boolReverse=False
boolShowTags=False
boolShowFreq=False
boolShowTopic=False
boolShowEssential=False
strTopicFilter=""
# Parameters passed after the '@_TOC_' can override the default sort and filter
strTOCParameters=line[len('@_TOC_ '):].rstrip()
lstTOCParameters=strTOCParameters.split(',')
# print('Found a TOC placeholder with '+str(len(lstTOCParameters))+' parameters: '+strTOCParameters)
for strTOCParameter in lstTOCParameters:
lstTOCParameterNameAndValue=strTOCParameter.split('=')
strParameterName=lstTOCParameterNameAndValue[0]
anyParameterValue=lstTOCParameterNameAndValue[1]
# print('Parameter name: '+strParameterName+', Value: '+anyParameterValue)
# Validate parameter name and value, set sort and filter if valid
if strParameterName == 'strTagFilter':
strTagFilter=anyParameterValue # May be several tags separated by semicolons
# print('strTagFilter: '+strTagFilter)
elif strParameterName == 'intSortColumn' and anyParameterValue.isnumeric():
intSortColumn=int(anyParameterValue)
elif strParameterName == 'boolReverse' and (anyParameterValue=='True' or anyParameterValue=='False'):
boolReverse=bool(util.strtobool(anyParameterValue))
elif strParameterName == 'boolShowTags' and (anyParameterValue=='True' or anyParameterValue=='False'):
boolShowTags=bool(util.strtobool(anyParameterValue))
elif strParameterName == 'boolShowFreq' and (anyParameterValue=='True' or anyParameterValue=='False'):
boolShowFreq=bool(util.strtobool(anyParameterValue))
elif strParameterName == 'boolShowTopic' and (anyParameterValue=='True' or anyParameterValue=='False'):
boolShowTopic=bool(util.strtobool(anyParameterValue))
elif strParameterName == 'boolShowEssential' and (anyParameterValue=='True' or anyParameterValue=='False'):
boolShowEssential=bool(util.strtobool(anyParameterValue))
elif strParameterName == 'strTopicFilter':
strTopicFilter=anyParameterValue # May be several tags separated by semicolons
# print('strTopicFilter: '+strTopicFilter)
# Call the toc function defined in checklistsharedfunctions to actually generate a TOC from the tasks files
# print('strTagFilter:'+strTagFilter+', intSortColumn:'+str(intSortColumn)+', boolReverse:'+str(boolReverse)+' and type of boolReverse is '+str(type(boolReverse)))
toc(fileOut, strProjectRootPath, strTaskSubdirectory, strTagFilter, intSortColumn, boolReverse, boolShowTags,boolShowFreq, boolShowTopic, boolShowEssential,strTopicFilter)
elif line.startswith('@_RTFREQ_'):
# Regular task frequency table
# Default sort and filter
strTagFilter="Regular"
intSortColumn=0 # 0:SortString, 1:Title
boolReverse=False
boolShowEssential=False
strTopicFilter=""
# Parameters passed after the '@_RTFREQ_' can override the default sort and filter
strRTFREQParameters=line[len('@_RTFREQ_ '):].rstrip()
lstRTFREQParameters=strRTFREQParameters.split(',')
# print('Found a RTFREQ placeholder with '+str(len(lstRTFREQParameters))+' parameters: '+strRTFREQParameters)
for strRTFREQParameter in lstRTFREQParameters:
lstRTFREQParameterNameAndValue=strRTFREQParameter.split('=')
strParameterName=lstRTFREQParameterNameAndValue[0]
anyParameterValue=lstRTFREQParameterNameAndValue[1]
# print('Parameter name: '+strParameterName+', Value: '+anyParameterValue)
# Validate parameter name and value, set sort and filter if valid
if strParameterName == 'strTagFilter':
strTagFilter=anyParameterValue # May be several tags separated by semicolons
# print('strTagFilter: '+strTagFilter)
elif strParameterName == 'intSortColumn' and anyParameterValue.isnumeric():
intSortColumn=int(anyParameterValue)
elif strParameterName == 'boolReverse' and (anyParameterValue=='True' or anyParameterValue=='False'):
boolReverse=bool(util.strtobool(anyParameterValue))
elif strParameterName == 'boolShowEssential' and (anyParameterValue=='True' or anyParameterValue=='False'):
boolShowEssential=bool(util.strtobool(anyParameterValue))
elif strParameterName == 'strTopicFilter':
strTopicFilter=anyParameterValue # May be several tags separated by semicolons
# print('strTopicFilter: '+strTopicFilter)
# Call the rtfreq function defined in checklistsharedfunctions to actually generate a Regular Task frequency table from the tasks files
# print('strTagFilter:'+strTagFilter+', intSortColumn:'+str(intSortColumn)+', boolReverse:'+str(boolReverse)+' and type of boolReverse is '+str(type(boolReverse)))
rtfreq(fileOut,strProjectRootPath,strTaskSubdirectory,strTagFilter,intSortColumn,boolReverse,boolShowEssential,strTopicFilter)
elif line.startswith('@_#_'):
# This is a comment in the template - do nothing
pass
else:
# Normal template line - write it to fileOut
fileOut.write(line)
# Write modified datetime and close
fileOut.write('</br>Generated by '+os.path.basename(__file__)+' on: %s.</br>\n' % datetime.datetime.now().strftime('%d %b %Y %H:%M:%S'))
fileOut.close()
fileIn.close()
## Outdent = return to original directory (strOldDir) = like popd in bash
# print(os.getcwd()) # "wherever you started"
if __name__ == '__main__':
# build_from_template.py executed directly
parser = argparse.ArgumentParser()
parser.add_argument("-t","--template", help="Path to template file, relative to project root e.g. templates/checklist_template.md.",required='True')
parser.add_argument("-o","--output", help="Path to output file, relative to project root, e.g. checklist.md.",required='True')
parser.add_argument("-d","--debug", action='store_true', help="Debug")
args = parser.parse_args()
# Path to template file and output file relative to strProjectRootPath.
# strTemplateFilePathIn="templates/checklist_template.md"
strTemplateFilePathIn=args.template
# strFilePathOut="checklist.md"
strFilePathOut=args.output
debug=args.debug
build_from_template(strTemplateFilePathIn,strFilePathOut)
print("build_from_template.py done")
sys.exit()