Skip to content

Commit 24017fa

Browse files
authored
Merge pull request #27 from cdfmlr/26-err-typehint-str-or-none
fix(#26): TypeError: unsupported operand type(s) for |: 'type' and 'NoneType' [bug]
2 parents e26402d + d53e781 commit 24017fa

File tree

4 files changed

+70
-60
lines changed

4 files changed

+70
-60
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# poetry & pyproject.toml based env is not ready & for internal experiment only.
2+
# keeps setup.py for a while.
3+
pyproject.toml
4+
poetry.lock
5+
16
.idea
27
.vscode
38

pyflowchart/__main__.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
"""
88

99
import argparse
10+
from typing import Optional
1011

1112
import chardet
1213

1314
from pyflowchart.flowchart import Flowchart
1415
from pyflowchart.output_html import output_html
1516

17+
1618
def detect_decode(file_content: bytes) -> str:
1719
"""detect_decode detect the encoding of file_content,
1820
then decode file_content on the detected encoding.
1921
20-
If the confidence of detect result is less then 0.9,
22+
If the confidence of detect result is less than 0.9,
2123
the UTF-8 will be used to decode. PyFlowchart is
2224
designed to convert Python 3 codes into flowcharts.
2325
And Python 3 is coding in UTF-8 in default. So only
@@ -50,16 +52,15 @@ def detect_decode(file_content: bytes) -> str:
5052
return content
5153

5254

53-
def output(flowchart_str: str, file_name: str | None, field: str) -> None:
55+
def output(flowchart_str: str, file_name: Optional[str], field: str) -> None:
5456
"""output convert & write the flowchart into a file.
5557
5658
Args:
57-
flowchart: the generated flowchart to write.
59+
flowchart_str: the generated flowchart to write.
5860
file_name: path to the target file.
5961
- '' or None for stdout.
6062
- '*.html' or '*.htm' for HTML.
61-
field_name: the field of flowchart.
62-
63+
field: the field of flowchart.
6364
"""
6465
if not file_name: # stdout
6566
print(flowchart_str)
@@ -91,6 +92,7 @@ def main(code_file, field, inner, output_file, simplify, conds_align):
9192
# output='*.html': output_html
9293
output(flowchart.flowchart(), output_file, field)
9394

95+
9496
if __name__ == '__main__':
9597
parser = argparse.ArgumentParser(description='Python code to flowchart.')
9698

@@ -99,7 +101,9 @@ def main(code_file, field, inner, output_file, simplify, conds_align):
99101

100102
parser.add_argument('-f', '--field', default="", type=str, help="field to draw flowchart. (e.g. Class.method)")
101103
parser.add_argument('-i', '--inner', action="store_true", help="parse the body of field")
102-
parser.add_argument('-o', '--output', default="", type=str, help="Output the flowchart to specific file with a format indicating by the extension name. (available: *.html)")
104+
parser.add_argument('-o', '--output', default="", type=str,
105+
help="Output the flowchart to specific file with a format indicating by the extension name. ("
106+
"available: *.html)")
103107
parser.add_argument('--no-simplify', action="store_false", help="do not simplify the one-line-body If/Loop")
104108
parser.add_argument('--conds-align', action="store_true", help="align consecutive If statements")
105109

pyflowchart/output_html.py

+54-53
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22

3+
34
def output_html(output_name: str, field_name: str, flowchart: str) -> None:
45
"""
56
This function outputting flowchart as html.
@@ -17,12 +18,12 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
1718
"""
1819

1920
# Three raw html code sections encapsulated in triple quotes """
20-
html_part_before_title="""<!DOCTYPE html>
21+
html_part_before_title = """<!DOCTYPE html>
2122
<html lang="en">
2223
<head>
2324
<meta charset="utf-8">
2425
<title>"""
25-
html_part_after_title_before_code_block="""</title>
26+
html_part_after_title_before_code_block = """</title>
2627
<style type="text/css">
2728
.end-element { fill : #FFCCFF; }
2829
</style>
@@ -36,7 +37,7 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
3637
var btn = document.getElementById("run"),
3738
cd = document.getElementById("code"),
3839
chart;
39-
40+
4041
(btn.onclick = function () {
4142
var code = cd.value;
4243
@@ -66,11 +67,11 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
6667
'scale': 1,
6768
'symbols': {
6869
'start': {
69-
'font-size': 14,
70+
'font-size': 14,
7071
'font-color': 'yellow',
7172
'element-color': 'blue',
7273
'fill': 'green',
73-
'class': 'start-element'
74+
'class': 'start-element'
7475
},
7576
'inputoutput': {
7677
'font-color': 'black',
@@ -89,11 +90,11 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
8990
},
9091
'condition': {
9192
'font-color': 'red',
92-
'element-color': 'black',
93+
'element-color': 'black',
9394
'fill': 'yellow'
9495
},
9596
'end':{
96-
'font-size': 20,
97+
'font-size': 20,
9798
'class': 'end-element'
9899
}
99100
},
@@ -107,42 +108,42 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
107108
'rejected' : { 'fill' : '#C45879', 'font-size' : 12, 'yes-text' : 'n/a', 'no-text' : 'REJECTED' }
108109
}
109110
});
110-
//create base64 encoding of SVG to generate download link for title(without html or htm).SVG
111-
var currentCanvasDIV = document.getElementById('canvas')
112-
var currentDrawSVG = currentCanvasDIV.innerHTML.replaceAll('ë','e');
111+
//create base64 encoding of SVG to generate download link for title(without html or htm).SVG
112+
var currentCanvasDIV = document.getElementById('canvas')
113+
var currentDrawSVG = currentCanvasDIV.innerHTML.replaceAll('ë','e');
113114
114-
const OUTsvgBASE64 = btoa(currentDrawSVG)
115-
doctitle = document.title.replace('.html','');
116-
doctitle = doctitle.replace('.htm','');
115+
const OUTsvgBASE64 = btoa(currentDrawSVG)
116+
doctitle = document.title.replace('.html','');
117+
doctitle = doctitle.replace('.htm','');
117118
118119
119-
var currentCanvasDIV = document.getElementById('canvas')
120-
var currentDrawSVG = currentCanvasDIV.innerHTML.replaceAll('ë','e');
121-
svgSource = currentDrawSVG
122-
svgXML = currentDrawSVG;
123-
// Use SVG Height and Width from the SVG XML to set canvas size
124-
svgXMLsubstringHeight = svgXML.substring(svgXML.indexOf('height='), svgXML.indexOf('version='));
125-
svgXMLsubstringWidth = svgXML.substring(svgXML.indexOf('width='), svgXML.indexOf('xmlns='));
126-
HeightValue = svgXMLsubstringHeight.substring(svgXMLsubstringHeight.indexOf('"')+1,svgXMLsubstringHeight.lastIndexOf('"'));
127-
WidthValue = svgXMLsubstringWidth.substring(svgXMLsubstringWidth.indexOf('"')+1,svgXMLsubstringWidth.lastIndexOf('"'));
128-
HeightValueInt = Math.round(HeightValue)
129-
WidthValueInt = Math.round(WidthValue)
130-
// setup input for base64SvgToBase64Png
131-
let svgSrc = "data:image/svg+xml;base64,"+OUTsvgBASE64;
132-
var pngBase
133-
imageUtil.base64SvgToBase64Png(svgSrc, WidthValueInt, HeightValueInt).then(pngSrc => {
134-
pngBase = pngSrc
135-
// output download link for base64 PNG converted on download from base64
136-
var pngOutHtml = `<a href="${pngBase}" download="${doctitle}.png">PNG - Click here to download current rendered flowchart as ${doctitle}.png</a>`
137-
document.getElementById("pngbase64").innerHTML=pngOutHtml;
138-
});
139-
// output download link for base64 SVG converted on download from base64
140-
var svgOutHtml = `<a href="data:image/svg+xml;base64,${OUTsvgBASE64}" download=${doctitle}.svg>SVG - Click here to download current rendered flowchart as ${doctitle}.svg</a> `
141-
document.getElementById("svgbase64").innerHTML=svgOutHtml;
142-
})();
120+
var currentCanvasDIV = document.getElementById('canvas')
121+
var currentDrawSVG = currentCanvasDIV.innerHTML.replaceAll('ë','e');
122+
svgSource = currentDrawSVG
123+
svgXML = currentDrawSVG;
124+
// Use SVG Height and Width from the SVG XML to set canvas size
125+
svgXMLsubstringHeight = svgXML.substring(svgXML.indexOf('height='), svgXML.indexOf('version='));
126+
svgXMLsubstringWidth = svgXML.substring(svgXML.indexOf('width='), svgXML.indexOf('xmlns='));
127+
HeightValue = svgXMLsubstringHeight.substring(svgXMLsubstringHeight.indexOf('"')+1,svgXMLsubstringHeight.lastIndexOf('"'));
128+
WidthValue = svgXMLsubstringWidth.substring(svgXMLsubstringWidth.indexOf('"')+1,svgXMLsubstringWidth.lastIndexOf('"'));
129+
HeightValueInt = Math.round(HeightValue)
130+
WidthValueInt = Math.round(WidthValue)
131+
// setup input for base64SvgToBase64Png
132+
let svgSrc = "data:image/svg+xml;base64,"+OUTsvgBASE64;
133+
var pngBase
134+
imageUtil.base64SvgToBase64Png(svgSrc, WidthValueInt, HeightValueInt).then(pngSrc => {
135+
pngBase = pngSrc
136+
// output download link for base64 PNG converted on download from base64
137+
var pngOutHtml = `<a href="${pngBase}" download="${doctitle}.png">PNG - Click here to download current rendered flowchart as ${doctitle}.png</a>`
138+
document.getElementById("pngbase64").innerHTML=pngOutHtml;
139+
});
140+
// output download link for base64 SVG converted on download from base64
141+
var svgOutHtml = `<a href="data:image/svg+xml;base64,${OUTsvgBASE64}" download=${doctitle}.svg>SVG - Click here to download current rendered flowchart as ${doctitle}.svg</a> `
142+
document.getElementById("svgbase64").innerHTML=svgOutHtml;
143+
})();
143144
144-
};
145-
145+
};
146+
146147
147148
// derived from https://stackoverflow.com/a/64800570
148149
// we need to use web browser canvas to generate a image. In this case png
@@ -156,7 +157,7 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
156157
*/
157158
imageUtil.base64SvgToBase64Png = function (originalBase64, width, height, secondTry) {
158159
return new Promise(resolve => {
159-
let img = document.createElement('img');
160+
let img = document.createElement('img');
160161
img.onload = function () {
161162
if (!secondTry && (img.naturalWidth === 0 || img.naturalHeight === 0)) {
162163
let svgDoc = base64ToSvgDocument(originalBase64);
@@ -213,28 +214,28 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
213214
}
214215
</script>
215216
216-
<script>
217-
function HelpText() {
218-
var x = document.getElementById("HelpTextBlock");
219-
if (x.style.display === "none") {
220-
x.style.display = "block";
221-
} else {
222-
x.style.display = "none";
223-
}
224-
}
225-
</script>
217+
<script>
218+
function HelpText() {
219+
var x = document.getElementById("HelpTextBlock");
220+
if (x.style.display === "none") {
221+
x.style.display = "block";
222+
} else {
223+
x.style.display = "none";
224+
}
225+
}
226+
</script>
226227
</head>
227228
<body>
228229
<div><textarea id="code" style="width: 100%;" rows="11">"""
229230

230-
html_part_after_code_block_remaining_html="""</textarea></div>
231+
html_part_after_code_block_remaining_html = """</textarea></div>
231232
<div><button id="run" type="button">Run</button> <button onclick="HelpText()">Format Help</button></div>
232-
<div id="HelpTextBlock" style="display:none"><br/>Conditions can also be redirected like cond(yes, bottom) or cond(yes, right)
233+
<div id="HelpTextBlock" style="display:none"><br/>Conditions can also be redirected like cond(yes, bottom) or cond(yes, right)
233234
... and the other symbols too... like sub1(right)<br/>
234235
You can also tweak the <b>diagram.drawSVG('diagram', {});</b> script in this file for more changes<br/>
235236
This is based on <a href="https://github.com/adrai/flowchart.js">flowchart.js on github</a> and <a href="http://flowchart.js.org">http://flowchart.js.org</a> more documentation can be found over there.
236237
</div><br/><div id="svgbase64"></div>
237-
<div id="pngbase64"></div>
238+
<div id="pngbase64"></div>
238239
239240
<div id="canvas"></div>
240241
</body>

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='pyflowchart',
8-
version='0.3.0',
8+
version='0.3.1',
99
url='https://github.com/cdfmlr/pyflowchart',
1010
license='MIT',
1111
author='CDFMLR',

0 commit comments

Comments
 (0)