Skip to content

Commit

Permalink
Merge pull request #27 from cdfmlr/26-err-typehint-str-or-none
Browse files Browse the repository at this point in the history
fix(#26): TypeError: unsupported operand type(s) for |: 'type' and 'NoneType' [bug]
  • Loading branch information
cdfmlr authored Aug 27, 2023
2 parents e26402d + d53e781 commit 24017fa
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 60 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# poetry & pyproject.toml based env is not ready & for internal experiment only.
# keeps setup.py for a while.
pyproject.toml
poetry.lock

.idea
.vscode

Expand Down
16 changes: 10 additions & 6 deletions pyflowchart/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
"""

import argparse
from typing import Optional

import chardet

from pyflowchart.flowchart import Flowchart
from pyflowchart.output_html import output_html


def detect_decode(file_content: bytes) -> str:
"""detect_decode detect the encoding of file_content,
then decode file_content on the detected encoding.
If the confidence of detect result is less then 0.9,
If the confidence of detect result is less than 0.9,
the UTF-8 will be used to decode. PyFlowchart is
designed to convert Python 3 codes into flowcharts.
And Python 3 is coding in UTF-8 in default. So only
Expand Down Expand Up @@ -50,16 +52,15 @@ def detect_decode(file_content: bytes) -> str:
return content


def output(flowchart_str: str, file_name: str | None, field: str) -> None:
def output(flowchart_str: str, file_name: Optional[str], field: str) -> None:
"""output convert & write the flowchart into a file.
Args:
flowchart: the generated flowchart to write.
flowchart_str: the generated flowchart to write.
file_name: path to the target file.
- '' or None for stdout.
- '*.html' or '*.htm' for HTML.
field_name: the field of flowchart.
field: the field of flowchart.
"""
if not file_name: # stdout
print(flowchart_str)
Expand Down Expand Up @@ -91,6 +92,7 @@ def main(code_file, field, inner, output_file, simplify, conds_align):
# output='*.html': output_html
output(flowchart.flowchart(), output_file, field)


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Python code to flowchart.')

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

parser.add_argument('-f', '--field', default="", type=str, help="field to draw flowchart. (e.g. Class.method)")
parser.add_argument('-i', '--inner', action="store_true", help="parse the body of field")
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)")
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)")
parser.add_argument('--no-simplify', action="store_false", help="do not simplify the one-line-body If/Loop")
parser.add_argument('--conds-align', action="store_true", help="align consecutive If statements")

Expand Down
107 changes: 54 additions & 53 deletions pyflowchart/output_html.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os


def output_html(output_name: str, field_name: str, flowchart: str) -> None:
"""
This function outputting flowchart as html.
Expand All @@ -17,12 +18,12 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
"""

# Three raw html code sections encapsulated in triple quotes """
html_part_before_title="""<!DOCTYPE html>
html_part_before_title = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>"""
html_part_after_title_before_code_block="""</title>
html_part_after_title_before_code_block = """</title>
<style type="text/css">
.end-element { fill : #FFCCFF; }
</style>
Expand All @@ -36,7 +37,7 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
var btn = document.getElementById("run"),
cd = document.getElementById("code"),
chart;
(btn.onclick = function () {
var code = cd.value;
Expand Down Expand Up @@ -66,11 +67,11 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
'scale': 1,
'symbols': {
'start': {
'font-size': 14,
'font-size': 14,
'font-color': 'yellow',
'element-color': 'blue',
'fill': 'green',
'class': 'start-element'
'class': 'start-element'
},
'inputoutput': {
'font-color': 'black',
Expand All @@ -89,11 +90,11 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
},
'condition': {
'font-color': 'red',
'element-color': 'black',
'element-color': 'black',
'fill': 'yellow'
},
'end':{
'font-size': 20,
'font-size': 20,
'class': 'end-element'
}
},
Expand All @@ -107,42 +108,42 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
'rejected' : { 'fill' : '#C45879', 'font-size' : 12, 'yes-text' : 'n/a', 'no-text' : 'REJECTED' }
}
});
//create base64 encoding of SVG to generate download link for title(without html or htm).SVG
var currentCanvasDIV = document.getElementById('canvas')
var currentDrawSVG = currentCanvasDIV.innerHTML.replaceAll('ë','e');
//create base64 encoding of SVG to generate download link for title(without html or htm).SVG
var currentCanvasDIV = document.getElementById('canvas')
var currentDrawSVG = currentCanvasDIV.innerHTML.replaceAll('ë','e');
const OUTsvgBASE64 = btoa(currentDrawSVG)
doctitle = document.title.replace('.html','');
doctitle = doctitle.replace('.htm','');
const OUTsvgBASE64 = btoa(currentDrawSVG)
doctitle = document.title.replace('.html','');
doctitle = doctitle.replace('.htm','');
var currentCanvasDIV = document.getElementById('canvas')
var currentDrawSVG = currentCanvasDIV.innerHTML.replaceAll('ë','e');
svgSource = currentDrawSVG
svgXML = currentDrawSVG;
// Use SVG Height and Width from the SVG XML to set canvas size
svgXMLsubstringHeight = svgXML.substring(svgXML.indexOf('height='), svgXML.indexOf('version='));
svgXMLsubstringWidth = svgXML.substring(svgXML.indexOf('width='), svgXML.indexOf('xmlns='));
HeightValue = svgXMLsubstringHeight.substring(svgXMLsubstringHeight.indexOf('"')+1,svgXMLsubstringHeight.lastIndexOf('"'));
WidthValue = svgXMLsubstringWidth.substring(svgXMLsubstringWidth.indexOf('"')+1,svgXMLsubstringWidth.lastIndexOf('"'));
HeightValueInt = Math.round(HeightValue)
WidthValueInt = Math.round(WidthValue)
// setup input for base64SvgToBase64Png
let svgSrc = "data:image/svg+xml;base64,"+OUTsvgBASE64;
var pngBase
imageUtil.base64SvgToBase64Png(svgSrc, WidthValueInt, HeightValueInt).then(pngSrc => {
pngBase = pngSrc
// output download link for base64 PNG converted on download from base64
var pngOutHtml = `<a href="${pngBase}" download="${doctitle}.png">PNG - Click here to download current rendered flowchart as ${doctitle}.png</a>`
document.getElementById("pngbase64").innerHTML=pngOutHtml;
});
// output download link for base64 SVG converted on download from base64
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> `
document.getElementById("svgbase64").innerHTML=svgOutHtml;
})();
var currentCanvasDIV = document.getElementById('canvas')
var currentDrawSVG = currentCanvasDIV.innerHTML.replaceAll('ë','e');
svgSource = currentDrawSVG
svgXML = currentDrawSVG;
// Use SVG Height and Width from the SVG XML to set canvas size
svgXMLsubstringHeight = svgXML.substring(svgXML.indexOf('height='), svgXML.indexOf('version='));
svgXMLsubstringWidth = svgXML.substring(svgXML.indexOf('width='), svgXML.indexOf('xmlns='));
HeightValue = svgXMLsubstringHeight.substring(svgXMLsubstringHeight.indexOf('"')+1,svgXMLsubstringHeight.lastIndexOf('"'));
WidthValue = svgXMLsubstringWidth.substring(svgXMLsubstringWidth.indexOf('"')+1,svgXMLsubstringWidth.lastIndexOf('"'));
HeightValueInt = Math.round(HeightValue)
WidthValueInt = Math.round(WidthValue)
// setup input for base64SvgToBase64Png
let svgSrc = "data:image/svg+xml;base64,"+OUTsvgBASE64;
var pngBase
imageUtil.base64SvgToBase64Png(svgSrc, WidthValueInt, HeightValueInt).then(pngSrc => {
pngBase = pngSrc
// output download link for base64 PNG converted on download from base64
var pngOutHtml = `<a href="${pngBase}" download="${doctitle}.png">PNG - Click here to download current rendered flowchart as ${doctitle}.png</a>`
document.getElementById("pngbase64").innerHTML=pngOutHtml;
});
// output download link for base64 SVG converted on download from base64
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> `
document.getElementById("svgbase64").innerHTML=svgOutHtml;
})();
};
};
// derived from https://stackoverflow.com/a/64800570
// we need to use web browser canvas to generate a image. In this case png
Expand All @@ -156,7 +157,7 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
*/
imageUtil.base64SvgToBase64Png = function (originalBase64, width, height, secondTry) {
return new Promise(resolve => {
let img = document.createElement('img');
let img = document.createElement('img');
img.onload = function () {
if (!secondTry && (img.naturalWidth === 0 || img.naturalHeight === 0)) {
let svgDoc = base64ToSvgDocument(originalBase64);
Expand Down Expand Up @@ -213,28 +214,28 @@ def output_html(output_name: str, field_name: str, flowchart: str) -> None:
}
</script>
<script>
function HelpText() {
var x = document.getElementById("HelpTextBlock");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
<script>
function HelpText() {
var x = document.getElementById("HelpTextBlock");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
</head>
<body>
<div><textarea id="code" style="width: 100%;" rows="11">"""

html_part_after_code_block_remaining_html="""</textarea></div>
html_part_after_code_block_remaining_html = """</textarea></div>
<div><button id="run" type="button">Run</button> <button onclick="HelpText()">Format Help</button></div>
<div id="HelpTextBlock" style="display:none"><br/>Conditions can also be redirected like cond(yes, bottom) or cond(yes, right)
<div id="HelpTextBlock" style="display:none"><br/>Conditions can also be redirected like cond(yes, bottom) or cond(yes, right)
... and the other symbols too... like sub1(right)<br/>
You can also tweak the <b>diagram.drawSVG('diagram', {});</b> script in this file for more changes<br/>
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.
</div><br/><div id="svgbase64"></div>
<div id="pngbase64"></div>
<div id="pngbase64"></div>
<div id="canvas"></div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='pyflowchart',
version='0.3.0',
version='0.3.1',
url='https://github.com/cdfmlr/pyflowchart',
license='MIT',
author='CDFMLR',
Expand Down

0 comments on commit 24017fa

Please sign in to comment.