Skip to content

Instantly share code, notes, and snippets.

@martin0258
Created November 2, 2012 05:45
Show Gist options
  • Save martin0258/3998939 to your computer and use it in GitHub Desktop.
Save martin0258/3998939 to your computer and use it in GitHub Desktop.
Insert not null information to EA generated HTML Documentation for Data model using DDL
# -*- coding: utf-8 -*-
import sys
import re
import glob
import os
from HTMLParser import HTMLParser
class FileIndicator:
def __init__(self):
self.hasNotNull = False
self.hasTable = False
# create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
#print "Encountered a start tag:", tag
for attr in attrs:
if attr[0]=="class" and attr[1]=="TableRow":
self.hasFoundColumn = True
def handle_endtag(self, tag):
pass
#print "Encountered an end tag :", tag
def handle_data(self, data):
#print "Encountered some data :", data
if self.get_starttag_text()=="<title>" and len(data.strip())>0:
self.hasFoundTitle = True
self.title = data
elif self.get_starttag_text()=="<strong>" and len(data.strip())>0:
self.columnName = data
# main
DDLRootDir = "B-TableStructure"
EARootDir = "DataModel-html/EARoot/"
# ["table"]["column"] = true or false
nullable = {}
# Iterate *.sql under DDLRootDir to find table definition
for dirpath, dirnames, filenames in os.walk(DDLRootDir):
for filename in filenames:
if not filename.endswith(".sql"):
continue
sqlFile = open(os.path.join(dirpath, filename))
# Find [CREATE TABLE XXX] syntax in the file
state = "FindingTable"
for line in sqlFile:
words = line.strip().split()
if state in "FindingTable":
if len(words)>2 and words[0]=="CREATE" and words[1]=="TABLE":
tableName = words[2]
nullable[tableName] = {}
state = "FindingColumn"
elif state in "FindingColumn":
# Assumption: We assume that there will be only one column definition per line
# The syntax ends with a semicolon or no syntax of [ColumnName type]
if ";" in line or len(words)<2:
state = "FindingTable"
continue
columnName = words[0]
nullable[tableName][columnName] = bool(not "NOT NULL" in line)
sqlFile.close()
parser = MyHTMLParser()
parser.hasFoundTitle = False
parser.hasFoundColumn = False
# Iterate *.htm, *.html under EARootDir to find table definition
for dirpath, dirnames, filenames in os.walk(EARootDir):
for filename in filenames:
if not (filename.endswith(".htm") or filename.endswith(".html")):
continue
filePath = os.path.join(dirpath, filename)
htmlFile = open(filePath, "r")
lines = htmlFile.readlines()
htmlFile.close()
htmlFileIndicator = FileIndicator()
state = "FindingTitle"
for n, line in enumerate(lines):
parser.feed(line)
if state in "FindingTitle":
if parser.hasFoundTitle:
htmlFileIndicator.hasTable = "::" in parser.title
tableName = parser.title.replace("::", ".")
parser.hasFoundTitle = False
state = "FindingColumn" if htmlFileIndicator.hasTable else "Exit"
elif state in "FindingColumn":
if parser.hasFoundColumn:
# Assumption: CoulmnBody would be at the next line
state = "FindingColumnBody"
parser.hasFoundColumn = False
elif state in "FindingColumnBody":
state = "FindingColumn"
columnName = parser.columnName
if columnName in nullable[tableName]:
if not nullable[tableName][columnName]:
print "%s in %s is NOT NULL" % (tableName, columnName)
lines[n] = lines[n].rstrip() + "<i>, NOT NULL</i>\n"
htmlFileIndicator.hasNotNull = True
elif state in "Exit":
break
if htmlFileIndicator.hasNotNull:
htmlFile = open(filePath, "w")
htmlFile.writelines(lines)
htmlFile.close()
raw_input("Pause...")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment