|
# Copyright (c) 2024 Janne Kujanpää |
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
# of this software and associated documentation files (the "Software"), to deal |
|
# in the Software without restriction, including without limitation the rights |
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
# copies of the Software, and to permit persons to whom the Software is |
|
# furnished to do so, subject to the following conditions: |
|
|
|
# The above copyright notice and this permission notice shall be included in all |
|
# copies or substantial portions of the Software. |
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
# SOFTWARE. |
|
|
|
import base64 |
|
import glob |
|
import json |
|
import sys |
|
import zlib |
|
|
|
mode = sys.argv[1] |
|
globStr = sys.argv[2] |
|
outputFile = sys.argv[3] |
|
|
|
def create_data_shape(filename): |
|
with open(filename, "rb") as image_file: |
|
encoded_string = base64.b64encode(image_file.read()).decode("ascii") |
|
shape = { |
|
"data": "data:image/svg+xml;base64," + encoded_string, |
|
"w": 40, |
|
"h": 40, |
|
"title": filename |
|
} |
|
return shape |
|
|
|
def create_xml_shape(filename): |
|
with open(filename, "rb") as image_file: |
|
encoded_string = base64.b64encode(image_file.read()).decode("ascii") |
|
image = encoded_string |
|
style = "shape=image;verticalLabelPosition=bottom;verticalAlign=top;imageAspect=0;aspect=fixed;image=data:image/svg+xml," + image |
|
mxGraphModel = '<mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="2" value="" style="' + style + '" vertex="1" parent="1"><mxGeometry width="40" height="40" as="geometry"/></mxCell></root></mxGraphModel>' |
|
mxGraphModel2 = zlib.compress(mxGraphModel.encode(), wbits=-15) |
|
mxGraphModel3 = base64.b64encode(mxGraphModel2).decode("ascii") |
|
shape = { |
|
"xml": mxGraphModel3, |
|
"w": 40, |
|
"h": 40, |
|
"title": filename |
|
} |
|
return shape |
|
|
|
result = [] |
|
for filename in glob.iglob(globStr, recursive=True): |
|
if mode == "data": |
|
shape = create_data_shape(filename) |
|
else: |
|
shape = create_xml_shape(filename) |
|
result.append(shape) |
|
|
|
outputStr = "<mxlibrary>" + json.dumps(result) + "</mxlibrary>" |
|
with open(outputFile, "w") as text_file: |
|
text_file.write(outputStr) |