Last active
October 4, 2017 02:20
-
-
Save alpiepho/40cc24a074d4cdfa130ab215dff0b7ff to your computer and use it in GitHub Desktop.
Python - tool to a template of THE ONE FILE and insert data from a CSV file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# NOTE | |
# Need to back burner this. Proving too time consuming to re-create | |
# the various attributes from the CSV strings (not enough information) | |
# Either need helper from .Net or write this tool in .NET | |
import getopt | |
import os | |
import sys | |
def Usage(): | |
print("Usage: %s -t <file> -i <file> -o <file>" % sys.argv[0]) | |
print(" -t <file> Template HTM file") | |
print(" -i <file> Input CSV file") | |
print(" -o <file> Output HTM file") | |
# TODO | |
def computeTimeStamp(val): | |
return "TBD" | |
# TODO | |
def computeGroupMemberAttributes(str1, str2, str3): | |
str = "TBD" | |
if str2 == "Notes": | |
str = "isi-group-member=\"{}\" isi-property=\"{}\" isi-text-node=\"\"".format(str1, str2) | |
else: | |
str = "isi-group-member=\"{}\" isi-property=\"{}\" isi-text-node=\"\"".format(str1, str2) | |
return str | |
TABLE_OPENTAG = "<table id=\"isi-report\">" | |
TABLE_CLOSETAG = "</table>" | |
CSV_STARTTABLE = "\"Date Time\"" | |
CSV_ENDTABLE = "\n\"\"" | |
CSV_BLANK = "\"\"" | |
def getDataIndexes(contents): | |
dataOpenIndex = contents.find(CSV_STARTTABLE) | |
dataCloseIndex = contents.find(CSV_ENDTABLE, dataOpenIndex) | |
return dataOpenIndex, dataCloseIndex | |
CSV_START = "\"" | |
CSV_NEWLINE = "\n" | |
CSV_BLANK = "\"\"" | |
def getGroupInfo(outFp, contents, startIndex, endIndex): | |
# assume group data is in form: | |
# "Section Header" | |
# "Section Member" | |
# ... | |
# "" | |
index = startIndex | |
currentGroup = "" | |
while index < endIndex: | |
if contents.startswith(CSV_BLANK, index): | |
outFp.write("<tr><td></td></tr>\n") | |
index += len(CSV_BLANK) | |
currentGroup = "" | |
elif currentGroup == "" and contents.startswith(CSV_START, index): | |
a = index | |
index = contents.find(CSV_NEWLINE, index) | |
b = index | |
str1 = contents[a:b].replace("\"", "") | |
str2 = str1.replace(" ", "") | |
outFp.write("<tr class=\"sectionHeader\"><td isi-group=\"{}\"\">{}</td></tr>\n".format(str2, str1)) | |
currentGroup = str2 | |
elif currentGroup != "" and contents.startswith(CSV_START, index): | |
a = index | |
index = contents.find(CSV_NEWLINE, index) | |
b = index | |
str = contents[a:b].replace("\"", "") | |
if str.find("=") >= 0: | |
parts = contents[a:b].replace("\"", "").split("=") | |
str1 = ''.join(parts[0]).strip().replace(" ", "") | |
str2 = ''.join(parts[0]).strip() | |
str3 = ''.join(parts[1]).strip() | |
outFp.write("<tr class=\"sectionMember\"><td isi-group-member=\"{}\" isi-property=\"{}\" isi-text-node=\"\"><span isi-label=\"\">{}</span> = <span isi-value=\"\">{}</span></td></tr>\n".format(currentGroup, str1, str2, str3)) | |
else: | |
parts = contents[a:b].replace("\"", "").split(":") | |
str1 = ''.join(parts[0]).strip().replace(" ", "") | |
str2 = ''.join(parts[0]).strip() | |
str3 = ''.join(parts[1]).strip() | |
attrStr = computeGroupMemberAttributes(str1, str2, str3) | |
outFp.write("<tr class=\"sectionMember\"><td isi-group-member=\"{}\" {}><span isi-label=\"\">{}</span>: <span isi-value=\"\">{}</span></td></tr>\n".format(currentGroup, attrStr, str1, str2, str3)) | |
index += 1 | |
return index | |
TD_DATA_HEADER = "\"Date Time\"" | |
def getDataAndValues(outFp, contents, startIndex, endIndex): | |
# assume first line is headers row | |
# and subsequent lines are data rows | |
index = startIndex | |
while index < endIndex: | |
if contents.startswith(TD_DATA_HEADER, index): | |
outFp.write("<tr class=\"dataHeader\" isi-data-table=\"\">\n") | |
index = contents.find(TD_DATA_HEADER, index) | |
a = index | |
index = contents.find(CSV_NEWLINE, index) | |
b = index | |
parts = contents[a:b].replace("\"", "").split(",") | |
for part in parts: | |
str1 = part.replace(" ", "") # TODO use lookup | |
str2 = part | |
outFp.write("<td isi-data-column-header=\"{}\">{}</td></tr>\n".format(str1, str2)) | |
outFp.write("</tr>\n") | |
elif contents.startswith(CSV_START, index): | |
index = contents.find(CSV_START, index) | |
a = index | |
index = contents.find(CSV_NEWLINE, index) | |
b = index | |
parts = contents[a:b].replace("\"", "").split(",") | |
ts = computeTimeStamp(parts[0]) | |
str = "<tr class=\"data\" isi-data-row=\"\" isi-timestamp=\"{}\"><td class=\"dateTime\">".format(ts) | |
idx = 0 | |
for part in parts: | |
str1 = part.replace(" ", "") # TODO use lookup | |
str2 = part | |
if idx > 0: | |
str += "<td>" | |
idx += 1 | |
str += "{}</td>".format(str2) | |
outFp.write(str + "</tr>\n") | |
index += 1 | |
tptFilename = '' | |
inFilename = '' | |
outFilename = '' | |
try: | |
# process command arguments | |
ouropts, args = getopt.getopt(sys.argv[1:],"t:i:o:h") | |
for o, a in ouropts: | |
if o == '-t': | |
tptFilename = a | |
elif o == '-i': | |
inFilename = a | |
elif o == '-o': | |
outFilename = a | |
elif o == '-h': | |
Usage() | |
sys.exit(0) | |
except getopt.GetoptError as e: | |
print(str(e)) | |
Usage() | |
sys.exit(2) | |
if type(tptFilename) != str or len(tptFilename) <= 0: | |
print("please use -t for input HTM template file") | |
Usage() | |
sys.exit(0) | |
if type(inFilename) != str or len(inFilename) <= 0: | |
print("please use -i for input CSV log file") | |
Usage() | |
sys.exit(0) | |
if type(outFilename) != str or len(outFilename) <= 0: | |
print("please use -o for output HTM log file") | |
Usage() | |
sys.exit(0) | |
with open(outFilename, 'wb') as outFp: | |
with open(tptFilename, 'rb') as tptFp: | |
with open(inFilename, 'rb') as inFp: | |
tptContents = tptFp.readlines() | |
tptContents = ''.join(tptContents) | |
contents = inFp.readlines() | |
contents = ''.join(contents) | |
# get top temlate | |
index = tptContents.find(TABLE_OPENTAG) | |
outFp.write(tptContents[0:index]) | |
outFp.write("<table id=\"isi-report\">") | |
dataOpenIndex, dataCloseIndex = getDataIndexes(contents) | |
index = getGroupInfo( outFp, contents, 0, dataOpenIndex) | |
index = getDataAndValues(outFp, contents, dataOpenIndex, dataCloseIndex) | |
index = getGroupInfo( outFp, contents, dataCloseIndex, len(contents)-1) | |
outFp.write("</table>") | |
# get bottom template | |
index = tptContents.find(TABLE_CLOSETAG, index) | |
index += len(TABLE_CLOSETAG) | |
outFp.write(tptContents[index:-1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment