Skip to content

Commit e57d96d

Browse files
committed
Support HTMl output (#20)
1 parent ec30ac3 commit e57d96d

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ In root directory (same directory as `SQLGitHub.py`),
3232
Create and edit `config.py`:
3333
```python
3434
token = "your token here" # can be obtained from https://github.com/settings/tokens
35-
output = "str" # or "csv" for csv output
35+
output = "str" # or "csv", "html"
3636
```
3737

3838
4. Start SQLGitHub

components/table.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@
2222
import itertools
2323

2424

25+
class EscapeHtml:
26+
MAPPING = {u"&": u"&",
27+
u"<": u"&lt;",
28+
u">": u"&gt;",
29+
u"\"": u"&quot;",
30+
u"\'": u"&#39;",
31+
u"\n": u"<br>\n"}
32+
33+
@classmethod
34+
def Escape(cls, ch):
35+
return cls.MAPPING[ch] if cls.MAPPING.has_key(ch) else ch
36+
37+
@classmethod
38+
def EscapeUnicodeStr(cls, unicode_str):
39+
ret = u""
40+
for ch in unicode_str:
41+
ret += cls.Escape(ch)
42+
return ret
43+
44+
2545
class SgTable:
2646
"""A class to store tables."""
2747

@@ -93,6 +113,23 @@ def InCsv(self):
93113
ret += u"\n" + self._GetCsvRepr(row)
94114
return ret
95115

116+
def InHtml(self):
117+
ret = u"<html>\n<head><meta charset=\"utf-8\">\n<title>SQLGitHub Result</title>\n</head>\n<body>\n"
118+
119+
ret += u"<table border=1>"
120+
ret += u"<tr>"
121+
for field in self._fields:
122+
ret += u"<td>" + EscapeHtml.EscapeUnicodeStr(field) + u"</td>"
123+
ret += u"</tr>\n"
124+
for row in self._table:
125+
ret += u"<tr>"
126+
for val in row:
127+
unicode_str = val if isinstance(val, unicode) else unicode(str(val), "utf-8")
128+
ret += u"<td>" + EscapeHtml.EscapeUnicodeStr(unicode_str) + u"</td>"
129+
ret += u"</tr>\n"
130+
ret += u"</table>\n</html>"
131+
return ret
132+
96133
def GetVals(self, field):
97134
idx = [i for i, f in enumerate(self._fields) if f == field][0]
98135
return [row[idx] for row in self._table]

components/top_level.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import definition
1414
import parser
15+
import utilities as util
1516
import tokenizer
1617

1718

@@ -48,10 +49,7 @@ def Execute(self, sql, measure_time=True, display_result=True):
4849
else:
4950
exec_time = time.time() - start_time
5051
if display_result:
51-
if self._output == "str":
52-
print(result)
53-
elif self._output == "csv":
54-
print(result.InCsv())
52+
util.PrintResult(result, self._output)
5553
print("-")
5654
print("Total rows: %d" % (len(result)))
5755
if measure_time:

components/utilities.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
"""Utilities for general operations."""
22

33

4+
def PrintResult(table, output):
5+
if output == "str":
6+
print(table)
7+
elif output == "csv":
8+
print(table.InCsv())
9+
elif output == "html":
10+
print(table.InHtml())
11+
412
def IsNumeric(num_str):
513
try:
614
val = int(num_str)
@@ -40,11 +48,3 @@ def Unescape(ch):
4048
return "\\%"
4149
elif ch == "_":
4250
return "\\_"
43-
44-
def EscapeHtml(ch):
45-
mapping = {u"&": u"&amp;",
46-
u"<": u"&lt;",
47-
u">": u"&gt;",
48-
u"\"": u"&quot;",
49-
u"\'": u"&#39;"}
50-
return mapping[ch] if mapping.has_key(ch) else ch

query.py

100644100755
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import config_loader
66
from components import top_level
7+
from components import utilities as util
78

89
arg_parser = argparse.ArgumentParser()
910
arg_parser.add_argument("sql", type=unicode, help="a line of sql for query")
@@ -14,7 +15,4 @@
1415
token, output = config_loader.Load("config")
1516
sqlserv = top_level.SQLGitHub(token, output)
1617
result, exec_time = sqlserv.Execute(args.sql, display_result=False)
17-
if output == "str":
18-
print(result)
19-
elif output == "csv":
20-
print(result.InCsv())
18+
util.PrintResult(result, output)

0 commit comments

Comments
 (0)