Merge branch 'dev'

This commit is contained in:
Daniel Rojas 2020-05-27 20:07:15 +02:00
commit 2f42bb41c9
38 changed files with 6298 additions and 2189 deletions

View File

@ -1,20 +0,0 @@
graph {
graph [bgcolor=transparent fontname=arial rankdir=LR ranksep=2]
node [fillcolor=white fontname=arial shape=record style="rounded,filled"]
edge [fontname=arial style=bold]
X1 [label="X1|{D-Sub|female|9-pin}|{{DCD|RX|TX|DTR|GND|DSR|RTS|CTS|RI}|{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8|<p9>9}}"]
X2 [label="X2|{Molex KK 254|female|6-pin}|{{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6}|{GND|RX|TX|NC|OUT|IN}}"]
X2:p5:w -- X2:p6:w
W1 [label="W1|{3x|0.25 mm²|+ S|0.2 m}|{{<w1i>1|<w2i>2|<w3i>3|<wsi>}|{WH|BN|GN|Shield}|{<w1o>1|<w2o>2|<w3o>3|<wso>}}"]
edge [color="#000000:#ffffff:#000000"]
X1:p5 -- W1:w1i
W1:w1o -- X2:p1
edge [color="#000000:#666600:#000000"]
X1:p2 -- W1:w2i
W1:w2o -- X2:p3
edge [color="#000000:#00ff00:#000000"]
X1:p3 -- W1:w3i
W1:w3o -- X2:p2
edge [color="#000000"]
X1:p5 -- W1:wsi
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 57 KiB

View File

@ -6,7 +6,7 @@ h.add_cable('W1', mm2=0.25, length=0.2, show_name=True, show_pinout=True, num_wi
h.add_node('X1', type='D-Sub', gender='female', pinout=('DCD','RX','TX','DTR','GND','DSR','RTS','CTS','RI')) h.add_node('X1', type='D-Sub', gender='female', pinout=('DCD','RX','TX','DTR','GND','DSR','RTS','CTS','RI'))
h.add_node('X2', type='Molex KK 254', gender='female', pinout=('GND','RX','TX','NC','OUT','IN')) h.add_node('X2', type='Molex KK 254', gender='female', pinout=('GND','RX','TX','NC','OUT','IN'))
# Option 1: define wires and shield in one line # Option 1: define wires and shield in one line
h.connect('W1','X1',(5,2,3,5),(1,2,3,'s'),'X2',(1,3,2,None)) h.connect('X1',(5,2,3,5),'W1',(1,2,3,'s'),'X2',(1,3,2,None))
h.loop('X2', 5, 6) h.loop('X2', 5, 6)
# Option 2: define wires and shield separately # Option 2: define wires and shield separately
# Harness.objects['W1'].connect('X1',(5,2,3),'auto','X2',(1,3,2)) # wires # Harness.objects['W1'].connect('X1',(5,2,3),'auto','X2',(1,3,2)) # wires

View File

@ -1,185 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.44.0 (20200408.0750)
-->
<!-- Pages: 1 -->
<svg width="857pt" height="262pt"
viewBox="0.00 0.00 857.00 262.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 258)">
<!-- X1 -->
<g id="node1" class="node">
<title>X1</title>
<path fill="white" stroke="black" d="M12,-0.5C12,-0.5 148,-0.5 148,-0.5 154,-0.5 160,-6.5 160,-12.5 160,-12.5 160,-241.5 160,-241.5 160,-247.5 154,-253.5 148,-253.5 148,-253.5 12,-253.5 12,-253.5 6,-253.5 0,-247.5 0,-241.5 0,-241.5 0,-12.5 0,-12.5 0,-6.5 6,-0.5 12,-0.5"/>
<text text-anchor="middle" x="80" y="-238.3" font-family="arial" font-size="14.00">X1</text>
<polyline fill="none" stroke="black" points="0,-230.5 160,-230.5 "/>
<text text-anchor="middle" x="27.5" y="-215.3" font-family="arial" font-size="14.00">D&#45;Sub</text>
<polyline fill="none" stroke="black" points="55,-207.5 55,-230.5 "/>
<text text-anchor="middle" x="84" y="-215.3" font-family="arial" font-size="14.00">female</text>
<polyline fill="none" stroke="black" points="113,-207.5 113,-230.5 "/>
<text text-anchor="middle" x="136.5" y="-215.3" font-family="arial" font-size="14.00">9&#45;pin</text>
<polyline fill="none" stroke="black" points="0,-207.5 160,-207.5 "/>
<text text-anchor="middle" x="45.5" y="-192.3" font-family="arial" font-size="14.00">DCD</text>
<polyline fill="none" stroke="black" points="0,-184.5 91,-184.5 "/>
<text text-anchor="middle" x="45.5" y="-169.3" font-family="arial" font-size="14.00">RX</text>
<polyline fill="none" stroke="black" points="0,-161.5 91,-161.5 "/>
<text text-anchor="middle" x="45.5" y="-146.3" font-family="arial" font-size="14.00">TX</text>
<polyline fill="none" stroke="black" points="0,-138.5 91,-138.5 "/>
<text text-anchor="middle" x="45.5" y="-123.3" font-family="arial" font-size="14.00">DTR</text>
<polyline fill="none" stroke="black" points="0,-115.5 91,-115.5 "/>
<text text-anchor="middle" x="45.5" y="-100.3" font-family="arial" font-size="14.00">GND</text>
<polyline fill="none" stroke="black" points="0,-92.5 91,-92.5 "/>
<text text-anchor="middle" x="45.5" y="-77.3" font-family="arial" font-size="14.00">DSR</text>
<polyline fill="none" stroke="black" points="0,-69.5 91,-69.5 "/>
<text text-anchor="middle" x="45.5" y="-54.3" font-family="arial" font-size="14.00">RTS</text>
<polyline fill="none" stroke="black" points="0,-46.5 91,-46.5 "/>
<text text-anchor="middle" x="45.5" y="-31.3" font-family="arial" font-size="14.00">CTS</text>
<polyline fill="none" stroke="black" points="0,-23.5 91,-23.5 "/>
<text text-anchor="middle" x="45.5" y="-8.3" font-family="arial" font-size="14.00">RI</text>
<polyline fill="none" stroke="black" points="91,-0.5 91,-207.5 "/>
<text text-anchor="middle" x="125.5" y="-192.3" font-family="arial" font-size="14.00">1</text>
<polyline fill="none" stroke="black" points="91,-184.5 160,-184.5 "/>
<text text-anchor="middle" x="125.5" y="-169.3" font-family="arial" font-size="14.00">2</text>
<polyline fill="none" stroke="black" points="91,-161.5 160,-161.5 "/>
<text text-anchor="middle" x="125.5" y="-146.3" font-family="arial" font-size="14.00">3</text>
<polyline fill="none" stroke="black" points="91,-138.5 160,-138.5 "/>
<text text-anchor="middle" x="125.5" y="-123.3" font-family="arial" font-size="14.00">4</text>
<polyline fill="none" stroke="black" points="91,-115.5 160,-115.5 "/>
<text text-anchor="middle" x="125.5" y="-100.3" font-family="arial" font-size="14.00">5</text>
<polyline fill="none" stroke="black" points="91,-92.5 160,-92.5 "/>
<text text-anchor="middle" x="125.5" y="-77.3" font-family="arial" font-size="14.00">6</text>
<polyline fill="none" stroke="black" points="91,-69.5 160,-69.5 "/>
<text text-anchor="middle" x="125.5" y="-54.3" font-family="arial" font-size="14.00">7</text>
<polyline fill="none" stroke="black" points="91,-46.5 160,-46.5 "/>
<text text-anchor="middle" x="125.5" y="-31.3" font-family="arial" font-size="14.00">8</text>
<polyline fill="none" stroke="black" points="91,-23.5 160,-23.5 "/>
<text text-anchor="middle" x="125.5" y="-8.3" font-family="arial" font-size="14.00">9</text>
</g>
<!-- W1 -->
<g id="node3" class="node">
<title>W1</title>
<path fill="white" stroke="black" d="M316,-104C316,-104 487,-104 487,-104 493,-104 499,-110 499,-116 499,-116 499,-230 499,-230 499,-236 493,-242 487,-242 487,-242 316,-242 316,-242 310,-242 304,-236 304,-230 304,-230 304,-116 304,-116 304,-110 310,-104 316,-104"/>
<text text-anchor="middle" x="401.5" y="-226.8" font-family="arial" font-size="14.00">W1</text>
<polyline fill="none" stroke="black" points="304,-219 499,-219 "/>
<text text-anchor="middle" x="319.5" y="-203.8" font-family="arial" font-size="14.00">3x</text>
<polyline fill="none" stroke="black" points="335,-196 335,-219 "/>
<text text-anchor="middle" x="372.5" y="-203.8" font-family="arial" font-size="14.00">0.25 mm²</text>
<polyline fill="none" stroke="black" points="410,-196 410,-219 "/>
<text text-anchor="middle" x="429" y="-203.8" font-family="arial" font-size="14.00">+ S</text>
<polyline fill="none" stroke="black" points="448,-196 448,-219 "/>
<text text-anchor="middle" x="473.5" y="-203.8" font-family="arial" font-size="14.00">0.2 m</text>
<polyline fill="none" stroke="black" points="304,-196 499,-196 "/>
<text text-anchor="middle" x="331.5" y="-180.8" font-family="arial" font-size="14.00">1</text>
<polyline fill="none" stroke="black" points="304,-173 359,-173 "/>
<text text-anchor="middle" x="331.5" y="-157.8" font-family="arial" font-size="14.00">2</text>
<polyline fill="none" stroke="black" points="304,-150 359,-150 "/>
<text text-anchor="middle" x="331.5" y="-134.8" font-family="arial" font-size="14.00">3</text>
<polyline fill="none" stroke="black" points="304,-127 359,-127 "/>
<text text-anchor="middle" x="331.5" y="-111.8" font-family="arial" font-size="14.00"> </text>
<polyline fill="none" stroke="black" points="359,-104 359,-196 "/>
<text text-anchor="middle" x="401.5" y="-180.8" font-family="arial" font-size="14.00">WH</text>
<polyline fill="none" stroke="black" points="359,-173 444,-173 "/>
<text text-anchor="middle" x="401.5" y="-157.8" font-family="arial" font-size="14.00">BN</text>
<polyline fill="none" stroke="black" points="359,-150 444,-150 "/>
<text text-anchor="middle" x="401.5" y="-134.8" font-family="arial" font-size="14.00">GN</text>
<polyline fill="none" stroke="black" points="359,-127 444,-127 "/>
<text text-anchor="middle" x="401.5" y="-111.8" font-family="arial" font-size="14.00">Shield</text>
<polyline fill="none" stroke="black" points="444,-104 444,-196 "/>
<text text-anchor="middle" x="471.5" y="-180.8" font-family="arial" font-size="14.00">1</text>
<polyline fill="none" stroke="black" points="444,-173 499,-173 "/>
<text text-anchor="middle" x="471.5" y="-157.8" font-family="arial" font-size="14.00">2</text>
<polyline fill="none" stroke="black" points="444,-150 499,-150 "/>
<text text-anchor="middle" x="471.5" y="-134.8" font-family="arial" font-size="14.00">3</text>
<polyline fill="none" stroke="black" points="444,-127 499,-127 "/>
<text text-anchor="middle" x="471.5" y="-111.8" font-family="arial" font-size="14.00"> </text>
</g>
<!-- X1&#45;&#45;W1 -->
<g id="edge2" class="edge">
<title>X1:p5&#45;&#45;W1:w1i</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M160,-102C235.43,-104.07 232.57,-185.07 304,-183"/>
<path fill="none" stroke="#ffffff" stroke-width="2" d="M160,-104C233.43,-104 230.57,-185 304,-185"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M160,-106C231.43,-103.93 228.57,-184.93 304,-187"/>
</g>
<!-- X1&#45;&#45;W1 -->
<g id="edge4" class="edge">
<title>X1:p2&#45;&#45;W1:w2i</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M160,-171C223,-171.42 238.56,-159.42 304,-159"/>
<path fill="none" stroke="#666600" stroke-width="2" d="M160,-173C224.22,-173 239.78,-161 304,-161"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M160,-175C225.44,-174.58 241,-162.58 304,-163"/>
</g>
<!-- X1&#45;&#45;W1 -->
<g id="edge6" class="edge">
<title>X1:p3&#45;&#45;W1:w3i</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M160,-148C223,-148.42 238.56,-136.42 304,-136"/>
<path fill="none" stroke="#00ff00" stroke-width="2" d="M160,-150C224.22,-150 239.78,-138 304,-138"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M160,-152C225.44,-151.58 241,-139.58 304,-140"/>
</g>
<!-- X1&#45;&#45;W1 -->
<g id="edge8" class="edge">
<title>X1:p5&#45;&#45;W1:wsi</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M160,-104C224.19,-104 239.81,-115 304,-115"/>
</g>
<!-- X2 -->
<g id="node2" class="node">
<title>X2</title>
<path fill="white" stroke="black" d="M655,-58C655,-58 837,-58 837,-58 843,-58 849,-64 849,-70 849,-70 849,-230 849,-230 849,-236 843,-242 837,-242 837,-242 655,-242 655,-242 649,-242 643,-236 643,-230 643,-230 643,-70 643,-70 643,-64 649,-58 655,-58"/>
<text text-anchor="middle" x="746" y="-226.8" font-family="arial" font-size="14.00">X2</text>
<polyline fill="none" stroke="black" points="643,-219 849,-219 "/>
<text text-anchor="middle" x="693.5" y="-203.8" font-family="arial" font-size="14.00">Molex KK 254</text>
<polyline fill="none" stroke="black" points="744,-196 744,-219 "/>
<text text-anchor="middle" x="773" y="-203.8" font-family="arial" font-size="14.00">female</text>
<polyline fill="none" stroke="black" points="802,-196 802,-219 "/>
<text text-anchor="middle" x="825.5" y="-203.8" font-family="arial" font-size="14.00">6&#45;pin</text>
<polyline fill="none" stroke="black" points="643,-196 849,-196 "/>
<text text-anchor="middle" x="688.5" y="-180.8" font-family="arial" font-size="14.00">1</text>
<polyline fill="none" stroke="black" points="643,-173 734,-173 "/>
<text text-anchor="middle" x="688.5" y="-157.8" font-family="arial" font-size="14.00">2</text>
<polyline fill="none" stroke="black" points="643,-150 734,-150 "/>
<text text-anchor="middle" x="688.5" y="-134.8" font-family="arial" font-size="14.00">3</text>
<polyline fill="none" stroke="black" points="643,-127 734,-127 "/>
<text text-anchor="middle" x="688.5" y="-111.8" font-family="arial" font-size="14.00">4</text>
<polyline fill="none" stroke="black" points="643,-104 734,-104 "/>
<text text-anchor="middle" x="688.5" y="-88.8" font-family="arial" font-size="14.00">5</text>
<polyline fill="none" stroke="black" points="643,-81 734,-81 "/>
<text text-anchor="middle" x="688.5" y="-65.8" font-family="arial" font-size="14.00">6</text>
<polyline fill="none" stroke="black" points="734,-58 734,-196 "/>
<text text-anchor="middle" x="791.5" y="-180.8" font-family="arial" font-size="14.00">GND</text>
<polyline fill="none" stroke="black" points="734,-173 849,-173 "/>
<text text-anchor="middle" x="791.5" y="-157.8" font-family="arial" font-size="14.00">RX</text>
<polyline fill="none" stroke="black" points="734,-150 849,-150 "/>
<text text-anchor="middle" x="791.5" y="-134.8" font-family="arial" font-size="14.00">TX</text>
<polyline fill="none" stroke="black" points="734,-127 849,-127 "/>
<text text-anchor="middle" x="791.5" y="-111.8" font-family="arial" font-size="14.00">NC</text>
<polyline fill="none" stroke="black" points="734,-104 849,-104 "/>
<text text-anchor="middle" x="791.5" y="-88.8" font-family="arial" font-size="14.00">OUT</text>
<polyline fill="none" stroke="black" points="734,-81 849,-81 "/>
<text text-anchor="middle" x="791.5" y="-65.8" font-family="arial" font-size="14.00">IN</text>
</g>
<!-- X2&#45;&#45;X2 -->
<g id="edge1" class="edge">
<title>X2:w&#45;&#45;X2:w</title>
<path fill="none" stroke="black" stroke-width="2" d="M643,-92C585.67,-101 471,-101 471,-80.5 471,-60 585.67,-60 643,-69"/>
</g>
<!-- W1&#45;&#45;X2 -->
<g id="edge3" class="edge">
<title>W1:w1o&#45;&#45;X2:p1</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M499,-183C563,-183 579,-183 643,-183"/>
<path fill="none" stroke="#ffffff" stroke-width="2" d="M499,-185C563,-185 579,-185 643,-185"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M499,-187C563,-187 579,-187 643,-187"/>
</g>
<!-- W1&#45;&#45;X2 -->
<g id="edge5" class="edge">
<title>W1:w2o&#45;&#45;X2:p3</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M499,-159C562.12,-159.94 576.49,-136.94 643,-136"/>
<path fill="none" stroke="#666600" stroke-width="2" d="M499,-161C563.81,-161 578.19,-138 643,-138"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M499,-163C565.51,-162.06 579.88,-139.06 643,-140"/>
</g>
<!-- W1&#45;&#45;X2 -->
<g id="edge7" class="edge">
<title>W1:w3o&#45;&#45;X2:p2</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M499,-136C565.6,-136.98 579.84,-160.98 643,-160"/>
<path fill="none" stroke="#00ff00" stroke-width="2" d="M499,-138C563.88,-138 578.12,-162 643,-162"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M499,-140C562.16,-139.02 576.4,-163.02 643,-164"/>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 13 KiB

30
examples/example1.yml Normal file
View File

@ -0,0 +1,30 @@
nodes:
X1:
type: D-Sub
gender: female
pinout: [DCD, RX, TX, DTR, GND, DSR, RTS, CTS, RI]
random: yes
X2:
type: Molex KK 254
gender: female
pinout: [GND, RX, TX, N/C, OUT, IN]
wires:
W1:
mm2: 0.25
length: 0.2
color_code: DIN
num_wires: 3
shield: true
connections:
- # format: connector->wire->connector
- X1: [5,2,1]
- W1: [1,2,3]
- X2: [1,3,2]
- # format: connector->wire or wire->connector
- X1: 5
- W1: s
- # loop: connector-connector
- X2: 5
- X2: 6

View File

@ -1,56 +0,0 @@
graph {
graph [bgcolor=transparent fontname=arial rankdir=LR ranksep=2]
node [fillcolor=white fontname=arial shape=record style="rounded,filled"]
edge [fontname=arial style=bold]
X1 [label="X1|{Molex KK 254|female|8-pin}|{{GND|+5V|SCL|SDA|MISO|MOSI|SCK|N/C}|{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8}}"]
X2 [label="X2|{Molex KK 254|female|4-pin}|{{<p1>1|<p2>2|<p3>3|<p4>4}|{GND|+5V|SCL|SDA}}"]
X3 [label="X3|{Molex KK 254|female|4-pin}|{{<p1>1|<p2>2|<p3>3|<p4>4}|{GND|+5V|SCL|SDA}}"]
X4 [label="X4|{Molex KK 254|female|5-pin}|{{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5}|{GND|+12V|MISO|MOSI|SCK}}"]
X5 [label="X5|{Molex Micro-Fit|male|2-pin}|{{GND|+12V}|{<p1>1|<p2>2}}"]
W1 [label="{4x|0.14 mm² (26 AWG)|0.2 m}|{{<w1>BK|<w2>RD|<w3>YE|<w4>GN}}"]
edge [color="#000000:#000000:#000000"]
X1:p1 -- W1:w1
W1:w1 -- X2:p1
edge [color="#000000:#ff0000:#000000"]
X1:p2 -- W1:w2
W1:w2 -- X2:p2
edge [color="#000000:#ffff00:#000000"]
X1:p3 -- W1:w3
W1:w3 -- X2:p3
edge [color="#000000:#00ff00:#000000"]
X1:p4 -- W1:w4
W1:w4 -- X2:p4
W2 [label="{4x|0.14 mm² (26 AWG)|0.2 m}|{{<w1>BK|<w2>RD|<w3>YE|<w4>GN}}"]
edge [color="#000000:#000000:#000000"]
X1:p1 -- W2:w1
W2:w1 -- X3:p1
edge [color="#000000:#ff0000:#000000"]
X1:p2 -- W2:w2
W2:w2 -- X3:p2
edge [color="#000000:#ffff00:#000000"]
X1:p3 -- W2:w3
W2:w3 -- X3:p3
edge [color="#000000:#00ff00:#000000"]
X1:p4 -- W2:w4
W2:w4 -- X3:p4
W3 [label="{4x|0.14 mm² (26 AWG)|0.2 m}|{{<w1>BK|<w2>BU|<w3>OG|<w4>VT}}"]
edge [color="#000000:#000000:#000000"]
X1:p1 -- W3:w1
W3:w1 -- X4:p1
edge [color="#000000:#0000ff:#000000"]
X1:p5 -- W3:w2
W3:w2 -- X4:p3
edge [color="#000000:#ff8000:#000000"]
X1:p6 -- W3:w3
W3:w3 -- X4:p4
edge [color="#000000:#8000ff:#000000"]
X1:p7 -- W3:w4
W3:w4 -- X4:p5
W4 [label="{2x|0.5 mm² (21 AWG)|0.35 m}|{{<w1>BK|<w2>RD}}"]
edge [color="#000000:#000000:#000000"]
X5:p1 -- W4:w1
W4:w1 -- X4:p1
edge [color="#000000:#ff0000:#000000"]
X5:p2 -- W4:w2
W4:w2 -- X4:p2
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 146 KiB

After

Width:  |  Height:  |  Size: 138 KiB

View File

@ -23,9 +23,9 @@ h.add_cable('W1', mm2=0.14, show_equiv=True, length=0.2, colors=COLORS_I2C, show
h.add_cable('W2', mm2=0.14, show_equiv=True, length=0.2, colors=COLORS_I2C, show_name=False) h.add_cable('W2', mm2=0.14, show_equiv=True, length=0.2, colors=COLORS_I2C, show_name=False)
h.add_cable('W3', mm2=0.14, show_equiv=True, length=0.2, colors=('BK','BU','OG','VT'), show_name=False) h.add_cable('W3', mm2=0.14, show_equiv=True, length=0.2, colors=('BK','BU','OG','VT'), show_name=False)
h.add_cable('W4', mm2=0.5, show_equiv=True, length=0.35, colors=('BK','RD'), show_name=False) h.add_cable('W4', mm2=0.5, show_equiv=True, length=0.35, colors=('BK','RD'), show_name=False)
h.connect('W1','X1',(1,2,3,4),'auto','X2','auto') h.connect('X1',(1,2,3,4),'W1','auto','X2','auto')
h.connect('W2','X1',(1,2,3,4),'auto','X3','auto') h.connect('X1',(1,2,3,4),'W2','auto','X3','auto')
h.connect('W3','X1',(1,5,6,7),'auto','X4',(1,3,4,5)) h.connect('X1',(1,5,6,7),'W3','auto','X4',(1,3,4,5))
h.connect_all_straight('W4','X5','X4') h.connect_all_straight('W4','X5','X4')
h.output(filename='output', format=('png','svg'), view=False) h.output(filename='output', format=('png','svg'), view=False)

View File

@ -1,427 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.44.0 (20200408.0750)
-->
<!-- Pages: 1 -->
<svg width="930pt" height="526pt"
viewBox="0.00 0.00 930.00 525.50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 521.5)">
<!-- X1 -->
<g id="node1" class="node">
<title>X1</title>
<path fill="white" stroke="black" d="M12,-130C12,-130 194,-130 194,-130 200,-130 206,-136 206,-142 206,-142 206,-348 206,-348 206,-354 200,-360 194,-360 194,-360 12,-360 12,-360 6,-360 0,-354 0,-348 0,-348 0,-142 0,-142 0,-136 6,-130 12,-130"/>
<text text-anchor="middle" x="103" y="-344.8" font-family="arial" font-size="14.00">X1</text>
<polyline fill="none" stroke="black" points="0,-337 206,-337 "/>
<text text-anchor="middle" x="50.5" y="-321.8" font-family="arial" font-size="14.00">Molex KK 254</text>
<polyline fill="none" stroke="black" points="101,-314 101,-337 "/>
<text text-anchor="middle" x="130" y="-321.8" font-family="arial" font-size="14.00">female</text>
<polyline fill="none" stroke="black" points="159,-314 159,-337 "/>
<text text-anchor="middle" x="182.5" y="-321.8" font-family="arial" font-size="14.00">8&#45;pin</text>
<polyline fill="none" stroke="black" points="0,-314 206,-314 "/>
<text text-anchor="middle" x="58.5" y="-298.8" font-family="arial" font-size="14.00">GND</text>
<polyline fill="none" stroke="black" points="0,-291 117,-291 "/>
<text text-anchor="middle" x="58.5" y="-275.8" font-family="arial" font-size="14.00">+5V</text>
<polyline fill="none" stroke="black" points="0,-268 117,-268 "/>
<text text-anchor="middle" x="58.5" y="-252.8" font-family="arial" font-size="14.00">SCL</text>
<polyline fill="none" stroke="black" points="0,-245 117,-245 "/>
<text text-anchor="middle" x="58.5" y="-229.8" font-family="arial" font-size="14.00">SDA</text>
<polyline fill="none" stroke="black" points="0,-222 117,-222 "/>
<text text-anchor="middle" x="58.5" y="-206.8" font-family="arial" font-size="14.00">MISO</text>
<polyline fill="none" stroke="black" points="0,-199 117,-199 "/>
<text text-anchor="middle" x="58.5" y="-183.8" font-family="arial" font-size="14.00">MOSI</text>
<polyline fill="none" stroke="black" points="0,-176 117,-176 "/>
<text text-anchor="middle" x="58.5" y="-160.8" font-family="arial" font-size="14.00">SCK</text>
<polyline fill="none" stroke="black" points="0,-153 117,-153 "/>
<text text-anchor="middle" x="58.5" y="-137.8" font-family="arial" font-size="14.00">N/C</text>
<polyline fill="none" stroke="black" points="117,-130 117,-314 "/>
<text text-anchor="middle" x="161.5" y="-298.8" font-family="arial" font-size="14.00">1</text>
<polyline fill="none" stroke="black" points="117,-291 206,-291 "/>
<text text-anchor="middle" x="161.5" y="-275.8" font-family="arial" font-size="14.00">2</text>
<polyline fill="none" stroke="black" points="117,-268 206,-268 "/>
<text text-anchor="middle" x="161.5" y="-252.8" font-family="arial" font-size="14.00">3</text>
<polyline fill="none" stroke="black" points="117,-245 206,-245 "/>
<text text-anchor="middle" x="161.5" y="-229.8" font-family="arial" font-size="14.00">4</text>
<polyline fill="none" stroke="black" points="117,-222 206,-222 "/>
<text text-anchor="middle" x="161.5" y="-206.8" font-family="arial" font-size="14.00">5</text>
<polyline fill="none" stroke="black" points="117,-199 206,-199 "/>
<text text-anchor="middle" x="161.5" y="-183.8" font-family="arial" font-size="14.00">6</text>
<polyline fill="none" stroke="black" points="117,-176 206,-176 "/>
<text text-anchor="middle" x="161.5" y="-160.8" font-family="arial" font-size="14.00">7</text>
<polyline fill="none" stroke="black" points="117,-153 206,-153 "/>
<text text-anchor="middle" x="161.5" y="-137.8" font-family="arial" font-size="14.00">8</text>
</g>
<!-- W1 -->
<g id="node6" class="node">
<title>W1</title>
<path fill="white" stroke="black" d="M362,-367.5C362,-367.5 560,-367.5 560,-367.5 566,-367.5 572,-373.5 572,-379.5 572,-379.5 572,-470.5 572,-470.5 572,-476.5 566,-482.5 560,-482.5 560,-482.5 362,-482.5 362,-482.5 356,-482.5 350,-476.5 350,-470.5 350,-470.5 350,-379.5 350,-379.5 350,-373.5 356,-367.5 362,-367.5"/>
<text text-anchor="middle" x="365.5" y="-467.3" font-family="arial" font-size="14.00">4x</text>
<polyline fill="none" stroke="black" points="381,-459.5 381,-482.5 "/>
<text text-anchor="middle" x="451" y="-467.3" font-family="arial" font-size="14.00">0.14 mm² (26 AWG)</text>
<polyline fill="none" stroke="black" points="521,-459.5 521,-482.5 "/>
<text text-anchor="middle" x="546.5" y="-467.3" font-family="arial" font-size="14.00">0.2 m</text>
<polyline fill="none" stroke="black" points="350,-459.5 572,-459.5 "/>
<text text-anchor="middle" x="461" y="-444.3" font-family="arial" font-size="14.00">BK</text>
<polyline fill="none" stroke="black" points="350,-436.5 572,-436.5 "/>
<text text-anchor="middle" x="461" y="-421.3" font-family="arial" font-size="14.00">RD</text>
<polyline fill="none" stroke="black" points="350,-413.5 572,-413.5 "/>
<text text-anchor="middle" x="461" y="-398.3" font-family="arial" font-size="14.00">YE</text>
<polyline fill="none" stroke="black" points="350,-390.5 572,-390.5 "/>
<text text-anchor="middle" x="461" y="-375.3" font-family="arial" font-size="14.00">GN</text>
</g>
<!-- X1&#45;&#45;W1 -->
<g id="edge1" class="edge">
<title>X1:p1&#45;&#45;W1:w1</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-301C298.76,-303.5 261.11,-448.5 350,-446"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-303C296.82,-303 259.18,-448 350,-448"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-305C294.89,-302.5 257.24,-447.5 350,-450"/>
</g>
<!-- X1&#45;&#45;W1 -->
<g id="edge3" class="edge">
<title>X1:p2&#45;&#45;W1:w2</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-278C298.76,-280.5 261.11,-425.5 350,-423"/>
<path fill="none" stroke="#ff0000" stroke-width="2" d="M206,-280C296.82,-280 259.18,-425 350,-425"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-282C294.89,-279.5 257.24,-424.5 350,-427"/>
</g>
<!-- X1&#45;&#45;W1 -->
<g id="edge5" class="edge">
<title>X1:p3&#45;&#45;W1:w3</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-255C298.76,-257.5 261.11,-402.5 350,-400"/>
<path fill="none" stroke="#ffff00" stroke-width="2" d="M206,-257C296.82,-257 259.18,-402 350,-402"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-259C294.89,-256.5 257.24,-401.5 350,-404"/>
</g>
<!-- X1&#45;&#45;W1 -->
<g id="edge7" class="edge">
<title>X1:p4&#45;&#45;W1:w4</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-231C299.07,-233.51 260.79,-379.51 350,-377"/>
<path fill="none" stroke="#00ff00" stroke-width="2" d="M206,-233C297.14,-233 258.86,-379 350,-379"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-235C295.21,-232.49 256.93,-378.49 350,-381"/>
</g>
<!-- W2 -->
<g id="node7" class="node">
<title>W2</title>
<path fill="white" stroke="black" d="M362,-222.5C362,-222.5 560,-222.5 560,-222.5 566,-222.5 572,-228.5 572,-234.5 572,-234.5 572,-325.5 572,-325.5 572,-331.5 566,-337.5 560,-337.5 560,-337.5 362,-337.5 362,-337.5 356,-337.5 350,-331.5 350,-325.5 350,-325.5 350,-234.5 350,-234.5 350,-228.5 356,-222.5 362,-222.5"/>
<text text-anchor="middle" x="365.5" y="-322.3" font-family="arial" font-size="14.00">4x</text>
<polyline fill="none" stroke="black" points="381,-314.5 381,-337.5 "/>
<text text-anchor="middle" x="451" y="-322.3" font-family="arial" font-size="14.00">0.14 mm² (26 AWG)</text>
<polyline fill="none" stroke="black" points="521,-314.5 521,-337.5 "/>
<text text-anchor="middle" x="546.5" y="-322.3" font-family="arial" font-size="14.00">0.2 m</text>
<polyline fill="none" stroke="black" points="350,-314.5 572,-314.5 "/>
<text text-anchor="middle" x="461" y="-299.3" font-family="arial" font-size="14.00">BK</text>
<polyline fill="none" stroke="black" points="350,-291.5 572,-291.5 "/>
<text text-anchor="middle" x="461" y="-276.3" font-family="arial" font-size="14.00">RD</text>
<polyline fill="none" stroke="black" points="350,-268.5 572,-268.5 "/>
<text text-anchor="middle" x="461" y="-253.3" font-family="arial" font-size="14.00">YE</text>
<polyline fill="none" stroke="black" points="350,-245.5 572,-245.5 "/>
<text text-anchor="middle" x="461" y="-230.3" font-family="arial" font-size="14.00">GN</text>
</g>
<!-- X1&#45;&#45;W2 -->
<g id="edge9" class="edge">
<title>X1:p1&#45;&#45;W2:w1</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-301C270,-301 286,-301 350,-301"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-303C270,-303 286,-303 350,-303"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-305C270,-305 286,-305 350,-305"/>
</g>
<!-- X1&#45;&#45;W2 -->
<g id="edge11" class="edge">
<title>X1:p2&#45;&#45;W2:w2</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-278C270,-278 286,-278 350,-278"/>
<path fill="none" stroke="#ff0000" stroke-width="2" d="M206,-280C270,-280 286,-280 350,-280"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-282C270,-282 286,-282 350,-282"/>
</g>
<!-- X1&#45;&#45;W2 -->
<g id="edge13" class="edge">
<title>X1:p3&#45;&#45;W2:w3</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-255C270,-255 286,-255 350,-255"/>
<path fill="none" stroke="#ffff00" stroke-width="2" d="M206,-257C270,-257 286,-257 350,-257"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-259C270,-259 286,-259 350,-259"/>
</g>
<!-- X1&#45;&#45;W2 -->
<g id="edge15" class="edge">
<title>X1:p4&#45;&#45;W2:w4</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-231C270.13,-231 286.12,-232 350,-232"/>
<path fill="none" stroke="#00ff00" stroke-width="2" d="M206,-233C270,-233 286,-234 350,-234"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-235C269.88,-235 285.87,-236 350,-236"/>
</g>
<!-- W3 -->
<g id="node8" class="node">
<title>W3</title>
<path fill="white" stroke="black" d="M362,-88.5C362,-88.5 560,-88.5 560,-88.5 566,-88.5 572,-94.5 572,-100.5 572,-100.5 572,-191.5 572,-191.5 572,-197.5 566,-203.5 560,-203.5 560,-203.5 362,-203.5 362,-203.5 356,-203.5 350,-197.5 350,-191.5 350,-191.5 350,-100.5 350,-100.5 350,-94.5 356,-88.5 362,-88.5"/>
<text text-anchor="middle" x="365.5" y="-188.3" font-family="arial" font-size="14.00">4x</text>
<polyline fill="none" stroke="black" points="381,-180.5 381,-203.5 "/>
<text text-anchor="middle" x="451" y="-188.3" font-family="arial" font-size="14.00">0.14 mm² (26 AWG)</text>
<polyline fill="none" stroke="black" points="521,-180.5 521,-203.5 "/>
<text text-anchor="middle" x="546.5" y="-188.3" font-family="arial" font-size="14.00">0.2 m</text>
<polyline fill="none" stroke="black" points="350,-180.5 572,-180.5 "/>
<text text-anchor="middle" x="461" y="-165.3" font-family="arial" font-size="14.00">BK</text>
<polyline fill="none" stroke="black" points="350,-157.5 572,-157.5 "/>
<text text-anchor="middle" x="461" y="-142.3" font-family="arial" font-size="14.00">BU</text>
<polyline fill="none" stroke="black" points="350,-134.5 572,-134.5 "/>
<text text-anchor="middle" x="461" y="-119.3" font-family="arial" font-size="14.00">OG</text>
<polyline fill="none" stroke="black" points="350,-111.5 572,-111.5 "/>
<text text-anchor="middle" x="461" y="-96.3" font-family="arial" font-size="14.00">VT</text>
</g>
<!-- X1&#45;&#45;W3 -->
<g id="edge17" class="edge">
<title>X1:p1&#45;&#45;W3:w1</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-301C291.47,-303.45 260.63,-169.45 350,-167"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-303C293.42,-303 262.58,-169 350,-169"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-305C295.37,-302.55 264.53,-168.55 350,-171"/>
</g>
<!-- X1&#45;&#45;W3 -->
<g id="edge19" class="edge">
<title>X1:p5&#45;&#45;W3:w2</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-208C274.04,-209.88 277.97,-145.88 350,-144"/>
<path fill="none" stroke="#0000ff" stroke-width="2" d="M206,-210C276.04,-210 279.96,-146 350,-146"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-212C278.03,-210.12 281.96,-146.12 350,-148"/>
</g>
<!-- X1&#45;&#45;W3 -->
<g id="edge21" class="edge">
<title>X1:p6&#45;&#45;W3:w3</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-185C274.04,-186.88 277.97,-122.88 350,-121"/>
<path fill="none" stroke="#ff8000" stroke-width="2" d="M206,-187C276.04,-187 279.96,-123 350,-123"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-189C278.03,-187.12 281.96,-123.12 350,-125"/>
</g>
<!-- X1&#45;&#45;W3 -->
<g id="edge23" class="edge">
<title>X1:p7&#45;&#45;W3:w4</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-162C274.04,-163.88 277.97,-99.88 350,-98"/>
<path fill="none" stroke="#8000ff" stroke-width="2" d="M206,-164C276.04,-164 279.96,-100 350,-100"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-166C278.03,-164.12 281.96,-100.12 350,-102"/>
</g>
<!-- X2 -->
<g id="node2" class="node">
<title>X2</title>
<path fill="white" stroke="black" d="M728,-379C728,-379 910,-379 910,-379 916,-379 922,-385 922,-391 922,-391 922,-505 922,-505 922,-511 916,-517 910,-517 910,-517 728,-517 728,-517 722,-517 716,-511 716,-505 716,-505 716,-391 716,-391 716,-385 722,-379 728,-379"/>
<text text-anchor="middle" x="819" y="-501.8" font-family="arial" font-size="14.00">X2</text>
<polyline fill="none" stroke="black" points="716,-494 922,-494 "/>
<text text-anchor="middle" x="766.5" y="-478.8" font-family="arial" font-size="14.00">Molex KK 254</text>
<polyline fill="none" stroke="black" points="817,-471 817,-494 "/>
<text text-anchor="middle" x="846" y="-478.8" font-family="arial" font-size="14.00">female</text>
<polyline fill="none" stroke="black" points="875,-471 875,-494 "/>
<text text-anchor="middle" x="898.5" y="-478.8" font-family="arial" font-size="14.00">4&#45;pin</text>
<polyline fill="none" stroke="black" points="716,-471 922,-471 "/>
<text text-anchor="middle" x="761.5" y="-455.8" font-family="arial" font-size="14.00">1</text>
<polyline fill="none" stroke="black" points="716,-448 807,-448 "/>
<text text-anchor="middle" x="761.5" y="-432.8" font-family="arial" font-size="14.00">2</text>
<polyline fill="none" stroke="black" points="716,-425 807,-425 "/>
<text text-anchor="middle" x="761.5" y="-409.8" font-family="arial" font-size="14.00">3</text>
<polyline fill="none" stroke="black" points="716,-402 807,-402 "/>
<text text-anchor="middle" x="761.5" y="-386.8" font-family="arial" font-size="14.00">4</text>
<polyline fill="none" stroke="black" points="807,-379 807,-471 "/>
<text text-anchor="middle" x="864.5" y="-455.8" font-family="arial" font-size="14.00">GND</text>
<polyline fill="none" stroke="black" points="807,-448 922,-448 "/>
<text text-anchor="middle" x="864.5" y="-432.8" font-family="arial" font-size="14.00">+5V</text>
<polyline fill="none" stroke="black" points="807,-425 922,-425 "/>
<text text-anchor="middle" x="864.5" y="-409.8" font-family="arial" font-size="14.00">SCL</text>
<polyline fill="none" stroke="black" points="807,-402 922,-402 "/>
<text text-anchor="middle" x="864.5" y="-386.8" font-family="arial" font-size="14.00">SDA</text>
</g>
<!-- X3 -->
<g id="node3" class="node">
<title>X3</title>
<path fill="white" stroke="black" d="M728,-222C728,-222 910,-222 910,-222 916,-222 922,-228 922,-234 922,-234 922,-348 922,-348 922,-354 916,-360 910,-360 910,-360 728,-360 728,-360 722,-360 716,-354 716,-348 716,-348 716,-234 716,-234 716,-228 722,-222 728,-222"/>
<text text-anchor="middle" x="819" y="-344.8" font-family="arial" font-size="14.00">X3</text>
<polyline fill="none" stroke="black" points="716,-337 922,-337 "/>
<text text-anchor="middle" x="766.5" y="-321.8" font-family="arial" font-size="14.00">Molex KK 254</text>
<polyline fill="none" stroke="black" points="817,-314 817,-337 "/>
<text text-anchor="middle" x="846" y="-321.8" font-family="arial" font-size="14.00">female</text>
<polyline fill="none" stroke="black" points="875,-314 875,-337 "/>
<text text-anchor="middle" x="898.5" y="-321.8" font-family="arial" font-size="14.00">4&#45;pin</text>
<polyline fill="none" stroke="black" points="716,-314 922,-314 "/>
<text text-anchor="middle" x="761.5" y="-298.8" font-family="arial" font-size="14.00">1</text>
<polyline fill="none" stroke="black" points="716,-291 807,-291 "/>
<text text-anchor="middle" x="761.5" y="-275.8" font-family="arial" font-size="14.00">2</text>
<polyline fill="none" stroke="black" points="716,-268 807,-268 "/>
<text text-anchor="middle" x="761.5" y="-252.8" font-family="arial" font-size="14.00">3</text>
<polyline fill="none" stroke="black" points="716,-245 807,-245 "/>
<text text-anchor="middle" x="761.5" y="-229.8" font-family="arial" font-size="14.00">4</text>
<polyline fill="none" stroke="black" points="807,-222 807,-314 "/>
<text text-anchor="middle" x="864.5" y="-298.8" font-family="arial" font-size="14.00">GND</text>
<polyline fill="none" stroke="black" points="807,-291 922,-291 "/>
<text text-anchor="middle" x="864.5" y="-275.8" font-family="arial" font-size="14.00">+5V</text>
<polyline fill="none" stroke="black" points="807,-268 922,-268 "/>
<text text-anchor="middle" x="864.5" y="-252.8" font-family="arial" font-size="14.00">SCL</text>
<polyline fill="none" stroke="black" points="807,-245 922,-245 "/>
<text text-anchor="middle" x="864.5" y="-229.8" font-family="arial" font-size="14.00">SDA</text>
</g>
<!-- X4 -->
<g id="node4" class="node">
<title>X4</title>
<path fill="white" stroke="black" d="M728,-41.5C728,-41.5 910,-41.5 910,-41.5 916,-41.5 922,-47.5 922,-53.5 922,-53.5 922,-190.5 922,-190.5 922,-196.5 916,-202.5 910,-202.5 910,-202.5 728,-202.5 728,-202.5 722,-202.5 716,-196.5 716,-190.5 716,-190.5 716,-53.5 716,-53.5 716,-47.5 722,-41.5 728,-41.5"/>
<text text-anchor="middle" x="819" y="-187.3" font-family="arial" font-size="14.00">X4</text>
<polyline fill="none" stroke="black" points="716,-179.5 922,-179.5 "/>
<text text-anchor="middle" x="766.5" y="-164.3" font-family="arial" font-size="14.00">Molex KK 254</text>
<polyline fill="none" stroke="black" points="817,-156.5 817,-179.5 "/>
<text text-anchor="middle" x="846" y="-164.3" font-family="arial" font-size="14.00">female</text>
<polyline fill="none" stroke="black" points="875,-156.5 875,-179.5 "/>
<text text-anchor="middle" x="898.5" y="-164.3" font-family="arial" font-size="14.00">5&#45;pin</text>
<polyline fill="none" stroke="black" points="716,-156.5 922,-156.5 "/>
<text text-anchor="middle" x="760" y="-141.3" font-family="arial" font-size="14.00">1</text>
<polyline fill="none" stroke="black" points="716,-133.5 804,-133.5 "/>
<text text-anchor="middle" x="760" y="-118.3" font-family="arial" font-size="14.00">2</text>
<polyline fill="none" stroke="black" points="716,-110.5 804,-110.5 "/>
<text text-anchor="middle" x="760" y="-95.3" font-family="arial" font-size="14.00">3</text>
<polyline fill="none" stroke="black" points="716,-87.5 804,-87.5 "/>
<text text-anchor="middle" x="760" y="-72.3" font-family="arial" font-size="14.00">4</text>
<polyline fill="none" stroke="black" points="716,-64.5 804,-64.5 "/>
<text text-anchor="middle" x="760" y="-49.3" font-family="arial" font-size="14.00">5</text>
<polyline fill="none" stroke="black" points="804,-41.5 804,-156.5 "/>
<text text-anchor="middle" x="863" y="-141.3" font-family="arial" font-size="14.00">GND</text>
<polyline fill="none" stroke="black" points="804,-133.5 922,-133.5 "/>
<text text-anchor="middle" x="863" y="-118.3" font-family="arial" font-size="14.00">+12V</text>
<polyline fill="none" stroke="black" points="804,-110.5 922,-110.5 "/>
<text text-anchor="middle" x="863" y="-95.3" font-family="arial" font-size="14.00">MISO</text>
<polyline fill="none" stroke="black" points="804,-87.5 922,-87.5 "/>
<text text-anchor="middle" x="863" y="-72.3" font-family="arial" font-size="14.00">MOSI</text>
<polyline fill="none" stroke="black" points="804,-64.5 922,-64.5 "/>
<text text-anchor="middle" x="863" y="-49.3" font-family="arial" font-size="14.00">SCK</text>
</g>
<!-- X5 -->
<g id="node5" class="node">
<title>X5</title>
<path fill="white" stroke="black" d="M13,-1C13,-1 193,-1 193,-1 199,-1 205,-7 205,-13 205,-13 205,-81 205,-81 205,-87 199,-93 193,-93 193,-93 13,-93 13,-93 7,-93 1,-87 1,-81 1,-81 1,-13 1,-13 1,-7 7,-1 13,-1"/>
<text text-anchor="middle" x="103" y="-77.8" font-family="arial" font-size="14.00">X5</text>
<polyline fill="none" stroke="black" points="1,-70 205,-70 "/>
<text text-anchor="middle" x="56" y="-54.8" font-family="arial" font-size="14.00">Molex Micro&#45;Fit</text>
<polyline fill="none" stroke="black" points="111,-47 111,-70 "/>
<text text-anchor="middle" x="134.5" y="-54.8" font-family="arial" font-size="14.00">male</text>
<polyline fill="none" stroke="black" points="158,-47 158,-70 "/>
<text text-anchor="middle" x="181.5" y="-54.8" font-family="arial" font-size="14.00">2&#45;pin</text>
<polyline fill="none" stroke="black" points="1,-47 205,-47 "/>
<text text-anchor="middle" x="58" y="-31.8" font-family="arial" font-size="14.00">GND</text>
<polyline fill="none" stroke="black" points="1,-24 115,-24 "/>
<text text-anchor="middle" x="58" y="-8.8" font-family="arial" font-size="14.00">+12V</text>
<polyline fill="none" stroke="black" points="115,-1 115,-47 "/>
<text text-anchor="middle" x="160" y="-31.8" font-family="arial" font-size="14.00">1</text>
<polyline fill="none" stroke="black" points="115,-24 205,-24 "/>
<text text-anchor="middle" x="160" y="-8.8" font-family="arial" font-size="14.00">2</text>
</g>
<!-- W4 -->
<g id="node9" class="node">
<title>W4</title>
<path fill="white" stroke="black" d="M362,-0.5C362,-0.5 560,-0.5 560,-0.5 566,-0.5 572,-6.5 572,-12.5 572,-12.5 572,-57.5 572,-57.5 572,-63.5 566,-69.5 560,-69.5 560,-69.5 362,-69.5 362,-69.5 356,-69.5 350,-63.5 350,-57.5 350,-57.5 350,-12.5 350,-12.5 350,-6.5 356,-0.5 362,-0.5"/>
<text text-anchor="middle" x="365.5" y="-54.3" font-family="arial" font-size="14.00">2x</text>
<polyline fill="none" stroke="black" points="381,-46.5 381,-69.5 "/>
<text text-anchor="middle" x="447" y="-54.3" font-family="arial" font-size="14.00">0.5 mm² (21 AWG)</text>
<polyline fill="none" stroke="black" points="513,-46.5 513,-69.5 "/>
<text text-anchor="middle" x="542.5" y="-54.3" font-family="arial" font-size="14.00">0.35 m</text>
<polyline fill="none" stroke="black" points="350,-46.5 572,-46.5 "/>
<text text-anchor="middle" x="461" y="-31.3" font-family="arial" font-size="14.00">BK</text>
<polyline fill="none" stroke="black" points="350,-23.5 572,-23.5 "/>
<text text-anchor="middle" x="461" y="-8.3" font-family="arial" font-size="14.00">RD</text>
</g>
<!-- X5&#45;&#45;W4 -->
<g id="edge25" class="edge">
<title>X5:p1&#45;&#45;W4:w1</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-33C270,-33 286,-33 350,-33"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-35C270,-35 286,-35 350,-35"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-37C270,-37 286,-37 350,-37"/>
</g>
<!-- X5&#45;&#45;W4 -->
<g id="edge27" class="edge">
<title>X5:p2&#45;&#45;W4:w2</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-10C270,-10 286,-10 350,-10"/>
<path fill="none" stroke="#ff0000" stroke-width="2" d="M206,-12C270,-12 286,-12 350,-12"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M206,-14C270,-14 286,-14 350,-14"/>
</g>
<!-- W1&#45;&#45;X2 -->
<g id="edge2" class="edge">
<title>W1:w1&#45;&#45;X2:p1</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-446C637.44,-446.42 653,-458.42 716,-458"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-448C636.22,-448 651.78,-460 716,-460"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-450C635,-449.58 650.56,-461.58 716,-462"/>
</g>
<!-- W1&#45;&#45;X2 -->
<g id="edge4" class="edge">
<title>W1:w2&#45;&#45;X2:p2</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-423C637.34,-423.36 652.96,-434.36 716,-434"/>
<path fill="none" stroke="#ff0000" stroke-width="2" d="M572,-425C636.19,-425 651.81,-436 716,-436"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-427C635.04,-426.64 650.66,-437.64 716,-438"/>
</g>
<!-- W1&#45;&#45;X2 -->
<g id="edge6" class="edge">
<title>W1:w3&#45;&#45;X2:p3</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-400C637.34,-400.36 652.96,-411.36 716,-411"/>
<path fill="none" stroke="#ffff00" stroke-width="2" d="M572,-402C636.19,-402 651.81,-413 716,-413"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-404C635.04,-403.64 650.66,-414.64 716,-415"/>
</g>
<!-- W1&#45;&#45;X2 -->
<g id="edge8" class="edge">
<title>W1:w4&#45;&#45;X2:p4</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-377C637.34,-377.36 652.96,-388.36 716,-388"/>
<path fill="none" stroke="#00ff00" stroke-width="2" d="M572,-379C636.19,-379 651.81,-390 716,-390"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-381C635.04,-380.64 650.66,-391.64 716,-392"/>
</g>
<!-- W2&#45;&#45;X3 -->
<g id="edge10" class="edge">
<title>W2:w1&#45;&#45;X3:p1</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-301C636,-301 652,-301 716,-301"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-303C636,-303 652,-303 716,-303"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-305C636,-305 652,-305 716,-305"/>
</g>
<!-- W2&#45;&#45;X3 -->
<g id="edge12" class="edge">
<title>W2:w2&#45;&#45;X3:p2</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-278C635.88,-278 651.87,-277 716,-277"/>
<path fill="none" stroke="#ff0000" stroke-width="2" d="M572,-280C636,-280 652,-279 716,-279"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-282C636.13,-282 652.12,-281 716,-281"/>
</g>
<!-- W2&#45;&#45;X3 -->
<g id="edge14" class="edge">
<title>W2:w3&#45;&#45;X3:p3</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-255C635.88,-255 651.87,-254 716,-254"/>
<path fill="none" stroke="#ffff00" stroke-width="2" d="M572,-257C636,-257 652,-256 716,-256"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-259C636.13,-259 652.12,-258 716,-258"/>
</g>
<!-- W2&#45;&#45;X3 -->
<g id="edge16" class="edge">
<title>W2:w4&#45;&#45;X3:p4</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-232C635.88,-232 651.87,-231 716,-231"/>
<path fill="none" stroke="#00ff00" stroke-width="2" d="M572,-234C636,-234 652,-233 716,-233"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-236C636.13,-236 652.12,-235 716,-235"/>
</g>
<!-- W3&#45;&#45;X4 -->
<g id="edge18" class="edge">
<title>W3:w1&#45;&#45;X4:p1</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-167C635.16,-167.98 649.4,-143.98 716,-143"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-169C636.88,-169 651.12,-145 716,-145"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-171C638.6,-170.02 652.84,-146.02 716,-147"/>
</g>
<!-- W3&#45;&#45;X4 -->
<g id="edge20" class="edge">
<title>W3:w2&#45;&#45;X4:p3</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-144C637.36,-145.61 646.72,-98.61 716,-97"/>
<path fill="none" stroke="#0000ff" stroke-width="2" d="M572,-146C639.32,-146 648.68,-99 716,-99"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-148C641.28,-146.39 650.64,-99.39 716,-101"/>
</g>
<!-- W3&#45;&#45;X4 -->
<g id="edge22" class="edge">
<title>W3:w3&#45;&#45;X4:p4</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-121C637.36,-122.61 646.72,-75.61 716,-74"/>
<path fill="none" stroke="#ff8000" stroke-width="2" d="M572,-123C639.32,-123 648.68,-76 716,-76"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-125C641.28,-123.39 650.64,-76.39 716,-78"/>
</g>
<!-- W3&#45;&#45;X4 -->
<g id="edge24" class="edge">
<title>W3:w4&#45;&#45;X4:p5</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-98C637.36,-99.61 646.72,-52.61 716,-51"/>
<path fill="none" stroke="#8000ff" stroke-width="2" d="M572,-100C639.32,-100 648.68,-53 716,-53"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-102C641.28,-100.39 650.64,-53.39 716,-55"/>
</g>
<!-- W4&#45;&#45;X4 -->
<g id="edge26" class="edge">
<title>W4:w1&#45;&#45;X4:p1</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-33C654.51,-35.31 637.44,-145.31 716,-143"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-35C652.54,-35 635.46,-145 716,-145"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-37C650.56,-34.69 633.49,-144.69 716,-147"/>
</g>
<!-- W4&#45;&#45;X4 -->
<g id="edge28" class="edge">
<title>W4:w2&#45;&#45;X4:p2</title>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-10C654.51,-12.31 637.44,-122.31 716,-120"/>
<path fill="none" stroke="#ff0000" stroke-width="2" d="M572,-12C652.54,-12 635.46,-122 716,-122"/>
<path fill="none" stroke="#000000" stroke-width="2" d="M572,-14C650.56,-11.69 633.49,-121.69 716,-124"/>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 28 KiB

85
examples/example2.yml Normal file
View File

@ -0,0 +1,85 @@
templates: # defining templates to be used later on
- &molex_f
type: Molex KK 254
gender: female
- &con_i2c
pinout: [GND, +5V, SCL, SDA]
- &wire_i2c
mm2: 0.14
length: 0.2
colors: [BK, RD, YE, GN]
nodes:
X1:
<<: *molex_f # copying items from the template
pinout: [GND, +5V, SCL, SDA, MISO, MOSI, SCK, N/C]
X2:
<<: *molex_f
<<: *con_i2c # it is possible to copy from more than one template
X3:
<<: *molex_f
<<: *con_i2c
X4:
<<: *molex_f
pinout: [GND, +12V, MISO, MOSI, flachstecker]
X5:
type: Molex Micro-Fit
gender: male
pinout: [GND, +12V]
wires:
W1:
<<: *wire_i2c
W2:
<<: *wire_i2c
W3:
mm2: 0.14
length: 0.2
colors: [BK, BU, OG, VT]
W4:
mm2: 0.5
length: 0.35
colors: [BK, RD]
connections:
-
- X1: [1-4]
- W1: [1-4]
- X2: [1-4]
-
- X1: [1-4]
- W2: [1-4]
- X3: [1-4]
-
- X1: [1,5-7]
- W3: [1-4]
- X4: [1,3-5]
-
- X5: [1,2]
- W4: [1,2]
- X4: [1,2]
# -
# - X1: 1
# - W1: 1
# - X2: 1
# -
# - X1: [2,3,4]
# - W1: [2,3,4]
# - X2: [4,3,2]
# -
# - X1: [5-10]
# - W1: [5-7,10,9,8]
# - X2: [10-5]
# -
# - X1: 11
# - W1: s
# -
# - X1: [1-5]
# - W1: [11-15]
# -
# - W1: [12-15]
# - X2: [2-5]
# -
# - X1: [12,14]
# - X1: [13,15]

View File

@ -1,185 +0,0 @@
graph {
graph [bgcolor=transparent fontname=arial rankdir=LR ranksep=2]
node [fillcolor=white fontname=arial shape=record style="rounded,filled"]
edge [fontname=arial style=bold]
X1 [label="X1|{10-pin}|{{}|{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8|<p9>9|<p10>10}}"]
X2 [label="X2|{10-pin}|{{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8|<p9>9|<p10>10}|{}}"]
X3 [label="X3|{20-pin}|{{}|{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8|<p9>9|<p10>10|<p11>11|<p12>12|<p13>13|<p14>14|<p15>15|<p16>16|<p17>17|<p18>18|<p19>19|<p20>20}}"]
X4 [label="X4|{20-pin}|{{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8|<p9>9|<p10>10|<p11>11|<p12>12|<p13>13|<p14>14|<p15>15|<p16>16|<p17>17|<p18>18|<p19>19|<p20>20}|{}}"]
X5 [label="X5|{20-pin}|{{}|{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8|<p9>9|<p10>10|<p11>11|<p12>12|<p13>13|<p14>14|<p15>15|<p16>16|<p17>17|<p18>18|<p19>19|<p20>20}}"]
X6 [label="X6|{20-pin}|{{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8|<p9>9|<p10>10|<p11>11|<p12>12|<p13>13|<p14>14|<p15>15|<p16>16|<p17>17|<p18>18|<p19>19|<p20>20}|{}}"]
X7 [label="X7|{6-pin}|{{}|{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6}}"]
X8 [label="X8|{6-pin}|{{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6}|{}}"]
W1 [label="{10x|None mm²|0 m}|{{<w1>brown|<w2>red|<w3>orange|<w4>yellow|<w5>green|<w6>blue|<w7>violet|<w8>grey|<w9>white|<w10>black}}"]
edge [color="#000000:#666600:#000000"]
X1:p1 -- W1:w1
W1:w1 -- X2:p1
edge [color="#000000:#ff0000:#000000"]
X1:p2 -- W1:w2
W1:w2 -- X2:p2
edge [color="#000000:#ff8000:#000000"]
X1:p3 -- W1:w3
W1:w3 -- X2:p3
edge [color="#000000:#ffff00:#000000"]
X1:p4 -- W1:w4
W1:w4 -- X2:p4
edge [color="#000000:#00ff00:#000000"]
X1:p5 -- W1:w5
W1:w5 -- X2:p5
edge [color="#000000:#0000ff:#000000"]
X1:p6 -- W1:w6
W1:w6 -- X2:p6
edge [color="#000000:#8000ff:#000000"]
X1:p7 -- W1:w7
W1:w7 -- X2:p7
edge [color="#000000:#808080:#000000"]
X1:p8 -- W1:w8
W1:w8 -- X2:p8
edge [color="#000000:#ffffff:#000000"]
X1:p9 -- W1:w9
W1:w9 -- X2:p9
edge [color="#000000:#000000:#000000"]
X1:p10 -- W1:w10
W1:w10 -- X2:p10
W2 [label="{20x|None mm²|0 m}|{{<w1>white|<w2>brown|<w3>green|<w4>yellow|<w5>grey|<w6>pink|<w7>blue|<w8>red|<w9>black|<w10>violet|<w11>white|<w12>brown|<w13>green|<w14>yellow|<w15>grey|<w16>pink|<w17>blue|<w18>red|<w19>black|<w20>violet}}"]
edge [color="#000000:#ffffff:#000000"]
X3:p1 -- W2:w1
W2:w1 -- X4:p1
edge [color="#000000:#666600:#000000"]
X3:p2 -- W2:w2
W2:w2 -- X4:p2
edge [color="#000000:#00ff00:#000000"]
X3:p3 -- W2:w3
W2:w3 -- X4:p3
edge [color="#000000:#ffff00:#000000"]
X3:p4 -- W2:w4
W2:w4 -- X4:p4
edge [color="#000000:#808080:#000000"]
X3:p5 -- W2:w5
W2:w5 -- X4:p5
edge [color="#000000:#ff80c0:#000000"]
X3:p6 -- W2:w6
W2:w6 -- X4:p6
edge [color="#000000:#0000ff:#000000"]
X3:p7 -- W2:w7
W2:w7 -- X4:p7
edge [color="#000000:#ff0000:#000000"]
X3:p8 -- W2:w8
W2:w8 -- X4:p8
edge [color="#000000:#000000:#000000"]
X3:p9 -- W2:w9
W2:w9 -- X4:p9
edge [color="#000000:#8000ff:#000000"]
X3:p10 -- W2:w10
W2:w10 -- X4:p10
edge [color="#000000:#ffffff:#000000"]
X3:p11 -- W2:w11
W2:w11 -- X4:p11
edge [color="#000000:#666600:#000000"]
X3:p12 -- W2:w12
W2:w12 -- X4:p12
edge [color="#000000:#00ff00:#000000"]
X3:p13 -- W2:w13
W2:w13 -- X4:p13
edge [color="#000000:#ffff00:#000000"]
X3:p14 -- W2:w14
W2:w14 -- X4:p14
edge [color="#000000:#808080:#000000"]
X3:p15 -- W2:w15
W2:w15 -- X4:p15
edge [color="#000000:#ff80c0:#000000"]
X3:p16 -- W2:w16
W2:w16 -- X4:p16
edge [color="#000000:#0000ff:#000000"]
X3:p17 -- W2:w17
W2:w17 -- X4:p17
edge [color="#000000:#ff0000:#000000"]
X3:p18 -- W2:w18
W2:w18 -- X4:p18
edge [color="#000000:#000000:#000000"]
X3:p19 -- W2:w19
W2:w19 -- X4:p19
edge [color="#000000:#8000ff:#000000"]
X3:p20 -- W2:w20
W2:w20 -- X4:p20
W3 [label="{20x|None mm²|0 m}|{{<w1>red|<w2>yellow|<w3>blue|<w4>red|<w5>yellow|<w6>blue|<w7>red|<w8>yellow|<w9>blue|<w10>red|<w11>yellow|<w12>blue|<w13>red|<w14>yellow|<w15>blue|<w16>red|<w17>yellow|<w18>blue|<w19>red|<w20>yellow}}"]
edge [color="#000000:#ff0000:#000000"]
X5:p1 -- W3:w1
W3:w1 -- X6:p1
edge [color="#000000:#ffff00:#000000"]
X5:p2 -- W3:w2
W3:w2 -- X6:p2
edge [color="#000000:#0000ff:#000000"]
X5:p3 -- W3:w3
W3:w3 -- X6:p3
edge [color="#000000:#ff0000:#000000"]
X5:p4 -- W3:w4
W3:w4 -- X6:p4
edge [color="#000000:#ffff00:#000000"]
X5:p5 -- W3:w5
W3:w5 -- X6:p5
edge [color="#000000:#0000ff:#000000"]
X5:p6 -- W3:w6
W3:w6 -- X6:p6
edge [color="#000000:#ff0000:#000000"]
X5:p7 -- W3:w7
W3:w7 -- X6:p7
edge [color="#000000:#ffff00:#000000"]
X5:p8 -- W3:w8
W3:w8 -- X6:p8
edge [color="#000000:#0000ff:#000000"]
X5:p9 -- W3:w9
W3:w9 -- X6:p9
edge [color="#000000:#ff0000:#000000"]
X5:p10 -- W3:w10
W3:w10 -- X6:p10
edge [color="#000000:#ffff00:#000000"]
X5:p11 -- W3:w11
W3:w11 -- X6:p11
edge [color="#000000:#0000ff:#000000"]
X5:p12 -- W3:w12
W3:w12 -- X6:p12
edge [color="#000000:#ff0000:#000000"]
X5:p13 -- W3:w13
W3:w13 -- X6:p13
edge [color="#000000:#ffff00:#000000"]
X5:p14 -- W3:w14
W3:w14 -- X6:p14
edge [color="#000000:#0000ff:#000000"]
X5:p15 -- W3:w15
W3:w15 -- X6:p15
edge [color="#000000:#ff0000:#000000"]
X5:p16 -- W3:w16
W3:w16 -- X6:p16
edge [color="#000000:#ffff00:#000000"]
X5:p17 -- W3:w17
W3:w17 -- X6:p17
edge [color="#000000:#0000ff:#000000"]
X5:p18 -- W3:w18
W3:w18 -- X6:p18
edge [color="#000000:#ff0000:#000000"]
X5:p19 -- W3:w19
W3:w19 -- X6:p19
edge [color="#000000:#ffff00:#000000"]
X5:p20 -- W3:w20
W3:w20 -- X6:p20
W4 [label="{6x|1 mm²|1 m}|{{<w1>|<w2>|<w3>|<w4>|<w5>|<w6>}}"]
edge [color="#000000"]
X7:p1 -- W4:w1
W4:w1 -- X8:p1
edge [color="#000000"]
X7:p2 -- W4:w2
W4:w2 -- X8:p2
edge [color="#000000"]
X7:p3 -- W4:w3
W4:w3 -- X8:p3
edge [color="#000000"]
X7:p4 -- W4:w4
W4:w4 -- X8:p4
edge [color="#000000"]
X7:p5 -- W4:w5
W4:w5 -- X8:p5
edge [color="#000000"]
X7:p6 -- W4:w6
W4:w6 -- X8:p6
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 147 KiB

File diff suppressed because it is too large Load Diff

Before

Width:  |  Height:  |  Size: 76 KiB

View File

@ -0,0 +1,43 @@
nodes:
X1:
# type: D-Sub
# gender: female
num_pins: 15
X2:
type: Molex KK 254
gender: female
num_pins: 10
wires:
W1:
mm2: 0.25
length: 0.2
color_code: DIN
num_wires: 15
shield: true
connections:
-
- X1: 1
- W1: 1
- X2: 1
-
- X1: [2,3,4]
- W1: [2,3,4]
- X2: [4,3,2]
-
- X1: [5-10]
- W1: [5-7,10,9,8]
- X2: [10-5]
-
- X1: 11
- W1: s
-
- X1: [1-5]
- W1: [11-15]
-
- W1: [12-15]
- X2: [2-5]
-
- X1: [12,14]
- X1: [13,15]

113
readme.md
View File

@ -2,17 +2,15 @@
## Summary ## Summary
WireViz is a simple yet flexible markup language for documenting cables, wiring harnesses and connector pinouts with beautiful graphical output. WireViz is a simple yet flexible, YAML-based markup language for documenting cables, wiring harnesses and connector pinouts with beautiful graphical output (SVG, PNG, ...) thanks to [GraphViz](https://www.graphviz.org/).
It is based on [GraphViz](https://www.graphviz.org/) and designed as an "extension" of it. A parser reads a WireViz file and generates valid GraphViz output, which can instantly be rendered to SVG/PNG.
## Features ## Features
* WireViz is fully text based * WireViz input files are fully text based
* No special editor required * No special editor required
* Human readable * Human readable
* Easy version control * Easy version control
* GraphViz-like syntax * YAML syntax
* Understands and uses color abbreviations as per [IEC 60757](https://en.wikipedia.org/wiki/Electronic_color_code#Color_band_system) (black=BK, red=RD, ...) * Understands and uses color abbreviations as per [IEC 60757](https://en.wikipedia.org/wiki/Electronic_color_code#Color_band_system) (black=BK, red=RD, ...)
* Optionally outputs colors as abbreviation (e.g. 'YE'), full name (e.g. 'yellow') or hex value (e.g. '#ffff00'), with choice of UPPER or lower case * Optionally outputs colors as abbreviation (e.g. 'YE'), full name (e.g. 'yellow') or hex value (e.g. '#ffff00'), with choice of UPPER or lower case
* Auto-generates standard wire color schemes and allows custom ones if needed * Auto-generates standard wire color schemes and allows custom ones if needed
@ -23,89 +21,54 @@ It is based on [GraphViz](https://www.graphviz.org/) and designed as an "extensi
* Allows more than one connector per side, as well as loopbacks * Allows more than one connector per side, as well as loopbacks
* Allows for easy-autorouting for 1-to-1 wiring * Allows for easy-autorouting for 1-to-1 wiring
_Note_: WireViz is not designed to represent the complete wiring of a system. Its main aim is to document the construction of individual wires and harnesses.
## Example ## Example
WireViz input file: [WireViz input file](examples/example1.yml):
// define connectors nodes:
X1:
type: D-Sub
gender: female
pinout: [DCD, RX, TX, DTR, GND, DSR, RTS, CTS, RI]
random: yes
X2:
type: Molex KK 254
gender: female
pinout: [GND, RX, TX, N/C, OUT, IN]
X1 [type="D-Sub", wires:
gender="female", W1:
pin_labels="DCD|RX|TX|DTR|GND|DSR|RTS|CTS|RI", mm2: 0.25
] length: 0.2
color_code: DIN
num_wires: 3
shield: true
X2 [type="Molex KK 254", connections:
gender="female", - # format: connector->wire->connector
pin_labels="GND|RX|TX|NC|OUT|IN", - X1: [5,2,1]
] - W1: [1,2,3]
- X2: [1,3,2]
// define wire - # format: connector->wire or wire->connector
- X1: 5
W1 [mm2=0.25, - W1: s
length=0.2, - # loop: connector-connector
num_wires=3, - X2: 5
colors="din47100", - X2: 6
shield=true
]
// define connections
X1:5 -> W1:1 -> X2:1 // GND
X1:2 -> W1:2 -> X2:3 // TX-RX
X1:3 -> W1:3 -> X2:2 // RX-TX
X1:5 -> W1:S // shield
X2:5 -> X2:6 // loop
Output file: Output file:
![Sample output diagram](idea/example1.png) ![Sample output diagram](examples/example1.png)
GraphViz code generated by parser: [Example 2](examples/example2.yml)
digraph G { ![](examples/example2.png)
graph [rankdir = LR, ranksep=2, fontname = "arial"];
edge [arrowhead=none, fontname = "arial"];
node [shape=record, style=rounded, fontname = "arial"];
X1[label="X1 | {D-Sub DE-9|female|9-pin} | {{DCD|RX|TX|DTR|GND|DSR|RTS|CTS|RI} | {<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8|<p9>9}}}"]
X2[label="X2 | {Molex KK 254|female|6-pin} | {{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6} | {|||||}}}"]
{edge[style=bold]
X2:p5:w -> X2:p6:w
}
W1[label="W1 | {3x|0.25 mm²| + S|0.2 m} | {{<w1i>1|<w2i>2|<w3i>3|<wsi>} | {WH|BN|GN|Shield} | {<w1o>1|<w2o>2|<w3o>3|<wso>}}}"]
{edge[style=bold]
{edge[color="#000000:#ffffff:#000000"] X1:p5 -> W1:w1i; W1:w1o -> X2:p1}
{edge[color="#000000:#666600:#000000"] X1:p2 -> W1:w2i; W1:w2o -> X2:p3}
{edge[color="#000000:#00ff00:#000000"] X1:p3 -> W1:w3i; W1:w3o -> X2:p2}
{X1:p5 -> W1:wsi; }
}
}
[Example 2](idea/example2.dot)
![](idea/example2.png)
## Status ## Status
This is very much a work in progress. This is very much a [work in progress](todo.md).
A Python module and test scripts are available. Running the test script will generate GraphViz output.
The parser will follow later; contributions are welcome!
## To do
* Add simple connectors (ferrules, cable lugs)
* no pinout
* graphical representation?
* Add support for cable splicing (as connector type)
* Display picture of connector underneath (including pin 1 location)
* Create parser (to make WireViz work as a GraphViz extension)
* Automatic BOM generation
## License ## License

View File

@ -66,8 +66,8 @@ class Harness:
def loop(self, node_name, from_pin, to_pin): def loop(self, node_name, from_pin, to_pin):
self.nodes[node_name].loop(from_pin, to_pin) self.nodes[node_name].loop(from_pin, to_pin)
def connect(self, cable_name, from_name, from_pin, via, to_name, to_pin): def connect(self, from_name, from_pin, via_name, via_pin, to_name, to_pin):
self.cables[cable_name].connect(from_name, from_pin, via, to_name, to_pin) self.cables[via_name].connect(from_name, from_pin, via_pin, to_name, to_pin)
def connect_all_straight(self, cable_name, from_name, to_name): def connect_all_straight(self, cable_name, from_name, to_name):
self.cables[cable_name].connect_all_straight(from_name, to_name) self.cables[cable_name].connect_all_straight(from_name, to_name)
@ -191,6 +191,8 @@ class Node:
self.loops = [] self.loops = []
if pinout is None: if pinout is None:
if num_pins is None:
num_pins = 1
self.pinout = ('',) * num_pins self.pinout = ('',) * num_pins
else: else:
if num_pins is None: if num_pins is None:
@ -245,17 +247,14 @@ class Cable:
cc = cc * int(m) cc = cc * int(m)
self.colors = cc[:n] self.colors = cc[:n]
def connect(self, from_name, from_pin, via, to_name, to_pin): def connect(self, from_name, from_pin, via_pin, to_name, to_pin):
if from_pin == 'auto': from_pin = int2tuple(from_pin)
from_pin = tuple(x+1 for x in range(len(self.colors))) via_pin = int2tuple(via_pin)
if via == 'auto': to_pin = int2tuple(to_pin)
via = tuple(x+1 for x in range(len(self.colors)))
if to_pin == 'auto':
to_pin = tuple(x+1 for x in range(len(self.colors)))
if len(from_pin) != len(to_pin): if len(from_pin) != len(to_pin):
raise Exception('from_pin must have the same number of elements as to_pin') raise Exception('from_pin must have the same number of elements as to_pin')
for i, x in enumerate(from_pin): for i, x in enumerate(from_pin):
self.connections.append((from_name, from_pin[i], via[i], to_name, to_pin[i])) self.connections.append((from_name, from_pin[i], via_pin[i], to_name, to_pin[i]))
def connect_all_straight(self, from_name, to_name): def connect_all_straight(self, from_name, to_name):
self.connect(from_name, 'auto', 'auto', to_name, 'auto') self.connect(from_name, 'auto', 'auto', to_name, 'auto')
@ -275,6 +274,13 @@ def nested(input):
s = '|'.join(l) s = '|'.join(l)
return s return s
def int2tuple(input):
if isinstance(input, tuple):
output = input
else:
output = (input,)
return output
def translate_color(input, color_mode): def translate_color(input, color_mode):
if input == '': if input == '':
output = '' output = ''

427
src/yaml/__init__.py Normal file
View File

@ -0,0 +1,427 @@
from .error import *
from .tokens import *
from .events import *
from .nodes import *
from .loader import *
from .dumper import *
__version__ = '5.3.1'
try:
from .cyaml import *
__with_libyaml__ = True
except ImportError:
__with_libyaml__ = False
import io
#------------------------------------------------------------------------------
# Warnings control
#------------------------------------------------------------------------------
# 'Global' warnings state:
_warnings_enabled = {
'YAMLLoadWarning': True,
}
# Get or set global warnings' state
def warnings(settings=None):
if settings is None:
return _warnings_enabled
if type(settings) is dict:
for key in settings:
if key in _warnings_enabled:
_warnings_enabled[key] = settings[key]
# Warn when load() is called without Loader=...
class YAMLLoadWarning(RuntimeWarning):
pass
def load_warning(method):
if _warnings_enabled['YAMLLoadWarning'] is False:
return
import warnings
message = (
"calling yaml.%s() without Loader=... is deprecated, as the "
"default Loader is unsafe. Please read "
"https://msg.pyyaml.org/load for full details."
) % method
warnings.warn(message, YAMLLoadWarning, stacklevel=3)
#------------------------------------------------------------------------------
def scan(stream, Loader=Loader):
"""
Scan a YAML stream and produce scanning tokens.
"""
loader = Loader(stream)
try:
while loader.check_token():
yield loader.get_token()
finally:
loader.dispose()
def parse(stream, Loader=Loader):
"""
Parse a YAML stream and produce parsing events.
"""
loader = Loader(stream)
try:
while loader.check_event():
yield loader.get_event()
finally:
loader.dispose()
def compose(stream, Loader=Loader):
"""
Parse the first YAML document in a stream
and produce the corresponding representation tree.
"""
loader = Loader(stream)
try:
return loader.get_single_node()
finally:
loader.dispose()
def compose_all(stream, Loader=Loader):
"""
Parse all YAML documents in a stream
and produce corresponding representation trees.
"""
loader = Loader(stream)
try:
while loader.check_node():
yield loader.get_node()
finally:
loader.dispose()
def load(stream, Loader=None):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
"""
if Loader is None:
load_warning('load')
Loader = FullLoader
loader = Loader(stream)
try:
return loader.get_single_data()
finally:
loader.dispose()
def load_all(stream, Loader=None):
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
"""
if Loader is None:
load_warning('load_all')
Loader = FullLoader
loader = Loader(stream)
try:
while loader.check_data():
yield loader.get_data()
finally:
loader.dispose()
def full_load(stream):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
Resolve all tags except those known to be
unsafe on untrusted input.
"""
return load(stream, FullLoader)
def full_load_all(stream):
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
Resolve all tags except those known to be
unsafe on untrusted input.
"""
return load_all(stream, FullLoader)
def safe_load(stream):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
Resolve only basic YAML tags. This is known
to be safe for untrusted input.
"""
return load(stream, SafeLoader)
def safe_load_all(stream):
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
Resolve only basic YAML tags. This is known
to be safe for untrusted input.
"""
return load_all(stream, SafeLoader)
def unsafe_load(stream):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
Resolve all tags, even those known to be
unsafe on untrusted input.
"""
return load(stream, UnsafeLoader)
def unsafe_load_all(stream):
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
Resolve all tags, even those known to be
unsafe on untrusted input.
"""
return load_all(stream, UnsafeLoader)
def emit(events, stream=None, Dumper=Dumper,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None):
"""
Emit YAML parsing events into a stream.
If stream is None, return the produced string instead.
"""
getvalue = None
if stream is None:
stream = io.StringIO()
getvalue = stream.getvalue
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break)
try:
for event in events:
dumper.emit(event)
finally:
dumper.dispose()
if getvalue:
return getvalue()
def serialize_all(nodes, stream=None, Dumper=Dumper,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None):
"""
Serialize a sequence of representation trees into a YAML stream.
If stream is None, return the produced string instead.
"""
getvalue = None
if stream is None:
if encoding is None:
stream = io.StringIO()
else:
stream = io.BytesIO()
getvalue = stream.getvalue
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break,
encoding=encoding, version=version, tags=tags,
explicit_start=explicit_start, explicit_end=explicit_end)
try:
dumper.open()
for node in nodes:
dumper.serialize(node)
dumper.close()
finally:
dumper.dispose()
if getvalue:
return getvalue()
def serialize(node, stream=None, Dumper=Dumper, **kwds):
"""
Serialize a representation tree into a YAML stream.
If stream is None, return the produced string instead.
"""
return serialize_all([node], stream, Dumper=Dumper, **kwds)
def dump_all(documents, stream=None, Dumper=Dumper,
default_style=None, default_flow_style=False,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None, sort_keys=True):
"""
Serialize a sequence of Python objects into a YAML stream.
If stream is None, return the produced string instead.
"""
getvalue = None
if stream is None:
if encoding is None:
stream = io.StringIO()
else:
stream = io.BytesIO()
getvalue = stream.getvalue
dumper = Dumper(stream, default_style=default_style,
default_flow_style=default_flow_style,
canonical=canonical, indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break,
encoding=encoding, version=version, tags=tags,
explicit_start=explicit_start, explicit_end=explicit_end, sort_keys=sort_keys)
try:
dumper.open()
for data in documents:
dumper.represent(data)
dumper.close()
finally:
dumper.dispose()
if getvalue:
return getvalue()
def dump(data, stream=None, Dumper=Dumper, **kwds):
"""
Serialize a Python object into a YAML stream.
If stream is None, return the produced string instead.
"""
return dump_all([data], stream, Dumper=Dumper, **kwds)
def safe_dump_all(documents, stream=None, **kwds):
"""
Serialize a sequence of Python objects into a YAML stream.
Produce only basic YAML tags.
If stream is None, return the produced string instead.
"""
return dump_all(documents, stream, Dumper=SafeDumper, **kwds)
def safe_dump(data, stream=None, **kwds):
"""
Serialize a Python object into a YAML stream.
Produce only basic YAML tags.
If stream is None, return the produced string instead.
"""
return dump_all([data], stream, Dumper=SafeDumper, **kwds)
def add_implicit_resolver(tag, regexp, first=None,
Loader=None, Dumper=Dumper):
"""
Add an implicit scalar detector.
If an implicit scalar value matches the given regexp,
the corresponding tag is assigned to the scalar.
first is a sequence of possible initial characters or None.
"""
if Loader is None:
loader.Loader.add_implicit_resolver(tag, regexp, first)
loader.FullLoader.add_implicit_resolver(tag, regexp, first)
loader.UnsafeLoader.add_implicit_resolver(tag, regexp, first)
else:
Loader.add_implicit_resolver(tag, regexp, first)
Dumper.add_implicit_resolver(tag, regexp, first)
def add_path_resolver(tag, path, kind=None, Loader=None, Dumper=Dumper):
"""
Add a path based resolver for the given tag.
A path is a list of keys that forms a path
to a node in the representation tree.
Keys can be string values, integers, or None.
"""
if Loader is None:
loader.Loader.add_path_resolver(tag, path, kind)
loader.FullLoader.add_path_resolver(tag, path, kind)
loader.UnsafeLoader.add_path_resolver(tag, path, kind)
else:
Loader.add_path_resolver(tag, path, kind)
Dumper.add_path_resolver(tag, path, kind)
def add_constructor(tag, constructor, Loader=None):
"""
Add a constructor for the given tag.
Constructor is a function that accepts a Loader instance
and a node object and produces the corresponding Python object.
"""
if Loader is None:
loader.Loader.add_constructor(tag, constructor)
loader.FullLoader.add_constructor(tag, constructor)
loader.UnsafeLoader.add_constructor(tag, constructor)
else:
Loader.add_constructor(tag, constructor)
def add_multi_constructor(tag_prefix, multi_constructor, Loader=None):
"""
Add a multi-constructor for the given tag prefix.
Multi-constructor is called for a node if its tag starts with tag_prefix.
Multi-constructor accepts a Loader instance, a tag suffix,
and a node object and produces the corresponding Python object.
"""
if Loader is None:
loader.Loader.add_multi_constructor(tag_prefix, multi_constructor)
loader.FullLoader.add_multi_constructor(tag_prefix, multi_constructor)
loader.UnsafeLoader.add_multi_constructor(tag_prefix, multi_constructor)
else:
Loader.add_multi_constructor(tag_prefix, multi_constructor)
def add_representer(data_type, representer, Dumper=Dumper):
"""
Add a representer for the given type.
Representer is a function accepting a Dumper instance
and an instance of the given data type
and producing the corresponding representation node.
"""
Dumper.add_representer(data_type, representer)
def add_multi_representer(data_type, multi_representer, Dumper=Dumper):
"""
Add a representer for the given type.
Multi-representer is a function accepting a Dumper instance
and an instance of the given data type or subtype
and producing the corresponding representation node.
"""
Dumper.add_multi_representer(data_type, multi_representer)
class YAMLObjectMetaclass(type):
"""
The metaclass for YAMLObject.
"""
def __init__(cls, name, bases, kwds):
super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
if isinstance(cls.yaml_loader, list):
for loader in cls.yaml_loader:
loader.add_constructor(cls.yaml_tag, cls.from_yaml)
else:
cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
cls.yaml_dumper.add_representer(cls, cls.to_yaml)
class YAMLObject(metaclass=YAMLObjectMetaclass):
"""
An object that can dump itself to a YAML stream
and load itself from a YAML stream.
"""
__slots__ = () # no direct instantiation, so allow immutable subclasses
yaml_loader = [Loader, FullLoader, UnsafeLoader]
yaml_dumper = Dumper
yaml_tag = None
yaml_flow_style = None
@classmethod
def from_yaml(cls, loader, node):
"""
Convert a representation node to a Python object.
"""
return loader.construct_yaml_object(node, cls)
@classmethod
def to_yaml(cls, dumper, data):
"""
Convert a Python object to a representation node.
"""
return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
flow_style=cls.yaml_flow_style)

139
src/yaml/composer.py Normal file
View File

@ -0,0 +1,139 @@
__all__ = ['Composer', 'ComposerError']
from .error import MarkedYAMLError
from .events import *
from .nodes import *
class ComposerError(MarkedYAMLError):
pass
class Composer:
def __init__(self):
self.anchors = {}
def check_node(self):
# Drop the STREAM-START event.
if self.check_event(StreamStartEvent):
self.get_event()
# If there are more documents available?
return not self.check_event(StreamEndEvent)
def get_node(self):
# Get the root node of the next document.
if not self.check_event(StreamEndEvent):
return self.compose_document()
def get_single_node(self):
# Drop the STREAM-START event.
self.get_event()
# Compose a document if the stream is not empty.
document = None
if not self.check_event(StreamEndEvent):
document = self.compose_document()
# Ensure that the stream contains no more documents.
if not self.check_event(StreamEndEvent):
event = self.get_event()
raise ComposerError("expected a single document in the stream",
document.start_mark, "but found another document",
event.start_mark)
# Drop the STREAM-END event.
self.get_event()
return document
def compose_document(self):
# Drop the DOCUMENT-START event.
self.get_event()
# Compose the root node.
node = self.compose_node(None, None)
# Drop the DOCUMENT-END event.
self.get_event()
self.anchors = {}
return node
def compose_node(self, parent, index):
if self.check_event(AliasEvent):
event = self.get_event()
anchor = event.anchor
if anchor not in self.anchors:
raise ComposerError(None, None, "found undefined alias %r"
% anchor, event.start_mark)
return self.anchors[anchor]
event = self.peek_event()
anchor = event.anchor
if anchor is not None:
if anchor in self.anchors:
raise ComposerError("found duplicate anchor %r; first occurrence"
% anchor, self.anchors[anchor].start_mark,
"second occurrence", event.start_mark)
self.descend_resolver(parent, index)
if self.check_event(ScalarEvent):
node = self.compose_scalar_node(anchor)
elif self.check_event(SequenceStartEvent):
node = self.compose_sequence_node(anchor)
elif self.check_event(MappingStartEvent):
node = self.compose_mapping_node(anchor)
self.ascend_resolver()
return node
def compose_scalar_node(self, anchor):
event = self.get_event()
tag = event.tag
if tag is None or tag == '!':
tag = self.resolve(ScalarNode, event.value, event.implicit)
node = ScalarNode(tag, event.value,
event.start_mark, event.end_mark, style=event.style)
if anchor is not None:
self.anchors[anchor] = node
return node
def compose_sequence_node(self, anchor):
start_event = self.get_event()
tag = start_event.tag
if tag is None or tag == '!':
tag = self.resolve(SequenceNode, None, start_event.implicit)
node = SequenceNode(tag, [],
start_event.start_mark, None,
flow_style=start_event.flow_style)
if anchor is not None:
self.anchors[anchor] = node
index = 0
while not self.check_event(SequenceEndEvent):
node.value.append(self.compose_node(node, index))
index += 1
end_event = self.get_event()
node.end_mark = end_event.end_mark
return node
def compose_mapping_node(self, anchor):
start_event = self.get_event()
tag = start_event.tag
if tag is None or tag == '!':
tag = self.resolve(MappingNode, None, start_event.implicit)
node = MappingNode(tag, [],
start_event.start_mark, None,
flow_style=start_event.flow_style)
if anchor is not None:
self.anchors[anchor] = node
while not self.check_event(MappingEndEvent):
#key_event = self.peek_event()
item_key = self.compose_node(node, None)
#if item_key in node.value:
# raise ComposerError("while composing a mapping", start_event.start_mark,
# "found duplicate key", key_event.start_mark)
item_value = self.compose_node(node, item_key)
#node.value[item_key] = item_value
node.value.append((item_key, item_value))
end_event = self.get_event()
node.end_mark = end_event.end_mark
return node

748
src/yaml/constructor.py Normal file
View File

@ -0,0 +1,748 @@
__all__ = [
'BaseConstructor',
'SafeConstructor',
'FullConstructor',
'UnsafeConstructor',
'Constructor',
'ConstructorError'
]
from .error import *
from .nodes import *
import collections.abc, datetime, base64, binascii, re, sys, types
class ConstructorError(MarkedYAMLError):
pass
class BaseConstructor:
yaml_constructors = {}
yaml_multi_constructors = {}
def __init__(self):
self.constructed_objects = {}
self.recursive_objects = {}
self.state_generators = []
self.deep_construct = False
def check_data(self):
# If there are more documents available?
return self.check_node()
def check_state_key(self, key):
"""Block special attributes/methods from being set in a newly created
object, to prevent user-controlled methods from being called during
deserialization"""
if self.get_state_keys_blacklist_regexp().match(key):
raise ConstructorError(None, None,
"blacklisted key '%s' in instance state found" % (key,), None)
def get_data(self):
# Construct and return the next document.
if self.check_node():
return self.construct_document(self.get_node())
def get_single_data(self):
# Ensure that the stream contains a single document and construct it.
node = self.get_single_node()
if node is not None:
return self.construct_document(node)
return None
def construct_document(self, node):
data = self.construct_object(node)
while self.state_generators:
state_generators = self.state_generators
self.state_generators = []
for generator in state_generators:
for dummy in generator:
pass
self.constructed_objects = {}
self.recursive_objects = {}
self.deep_construct = False
return data
def construct_object(self, node, deep=False):
if node in self.constructed_objects:
return self.constructed_objects[node]
if deep:
old_deep = self.deep_construct
self.deep_construct = True
if node in self.recursive_objects:
raise ConstructorError(None, None,
"found unconstructable recursive node", node.start_mark)
self.recursive_objects[node] = None
constructor = None
tag_suffix = None
if node.tag in self.yaml_constructors:
constructor = self.yaml_constructors[node.tag]
else:
for tag_prefix in self.yaml_multi_constructors:
if tag_prefix is not None and node.tag.startswith(tag_prefix):
tag_suffix = node.tag[len(tag_prefix):]
constructor = self.yaml_multi_constructors[tag_prefix]
break
else:
if None in self.yaml_multi_constructors:
tag_suffix = node.tag
constructor = self.yaml_multi_constructors[None]
elif None in self.yaml_constructors:
constructor = self.yaml_constructors[None]
elif isinstance(node, ScalarNode):
constructor = self.__class__.construct_scalar
elif isinstance(node, SequenceNode):
constructor = self.__class__.construct_sequence
elif isinstance(node, MappingNode):
constructor = self.__class__.construct_mapping
if tag_suffix is None:
data = constructor(self, node)
else:
data = constructor(self, tag_suffix, node)
if isinstance(data, types.GeneratorType):
generator = data
data = next(generator)
if self.deep_construct:
for dummy in generator:
pass
else:
self.state_generators.append(generator)
self.constructed_objects[node] = data
del self.recursive_objects[node]
if deep:
self.deep_construct = old_deep
return data
def construct_scalar(self, node):
if not isinstance(node, ScalarNode):
raise ConstructorError(None, None,
"expected a scalar node, but found %s" % node.id,
node.start_mark)
return node.value
def construct_sequence(self, node, deep=False):
if not isinstance(node, SequenceNode):
raise ConstructorError(None, None,
"expected a sequence node, but found %s" % node.id,
node.start_mark)
return [self.construct_object(child, deep=deep)
for child in node.value]
def construct_mapping(self, node, deep=False):
if not isinstance(node, MappingNode):
raise ConstructorError(None, None,
"expected a mapping node, but found %s" % node.id,
node.start_mark)
mapping = {}
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
if not isinstance(key, collections.abc.Hashable):
raise ConstructorError("while constructing a mapping", node.start_mark,
"found unhashable key", key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
def construct_pairs(self, node, deep=False):
if not isinstance(node, MappingNode):
raise ConstructorError(None, None,
"expected a mapping node, but found %s" % node.id,
node.start_mark)
pairs = []
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
value = self.construct_object(value_node, deep=deep)
pairs.append((key, value))
return pairs
@classmethod
def add_constructor(cls, tag, constructor):
if not 'yaml_constructors' in cls.__dict__:
cls.yaml_constructors = cls.yaml_constructors.copy()
cls.yaml_constructors[tag] = constructor
@classmethod
def add_multi_constructor(cls, tag_prefix, multi_constructor):
if not 'yaml_multi_constructors' in cls.__dict__:
cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy()
cls.yaml_multi_constructors[tag_prefix] = multi_constructor
class SafeConstructor(BaseConstructor):
def construct_scalar(self, node):
if isinstance(node, MappingNode):
for key_node, value_node in node.value:
if key_node.tag == 'tag:yaml.org,2002:value':
return self.construct_scalar(value_node)
return super().construct_scalar(node)
def flatten_mapping(self, node):
merge = []
index = 0
while index < len(node.value):
key_node, value_node = node.value[index]
if key_node.tag == 'tag:yaml.org,2002:merge':
del node.value[index]
if isinstance(value_node, MappingNode):
self.flatten_mapping(value_node)
merge.extend(value_node.value)
elif isinstance(value_node, SequenceNode):
submerge = []
for subnode in value_node.value:
if not isinstance(subnode, MappingNode):
raise ConstructorError("while constructing a mapping",
node.start_mark,
"expected a mapping for merging, but found %s"
% subnode.id, subnode.start_mark)
self.flatten_mapping(subnode)
submerge.append(subnode.value)
submerge.reverse()
for value in submerge:
merge.extend(value)
else:
raise ConstructorError("while constructing a mapping", node.start_mark,
"expected a mapping or list of mappings for merging, but found %s"
% value_node.id, value_node.start_mark)
elif key_node.tag == 'tag:yaml.org,2002:value':
key_node.tag = 'tag:yaml.org,2002:str'
index += 1
else:
index += 1
if merge:
node.value = merge + node.value
def construct_mapping(self, node, deep=False):
if isinstance(node, MappingNode):
self.flatten_mapping(node)
return super().construct_mapping(node, deep=deep)
def construct_yaml_null(self, node):
self.construct_scalar(node)
return None
bool_values = {
'yes': True,
'no': False,
'true': True,
'false': False,
'on': True,
'off': False,
}
def construct_yaml_bool(self, node):
value = self.construct_scalar(node)
return self.bool_values[value.lower()]
def construct_yaml_int(self, node):
value = self.construct_scalar(node)
value = value.replace('_', '')
sign = +1
if value[0] == '-':
sign = -1
if value[0] in '+-':
value = value[1:]
if value == '0':
return 0
elif value.startswith('0b'):
return sign*int(value[2:], 2)
elif value.startswith('0x'):
return sign*int(value[2:], 16)
elif value[0] == '0':
return sign*int(value, 8)
elif ':' in value:
digits = [int(part) for part in value.split(':')]
digits.reverse()
base = 1
value = 0
for digit in digits:
value += digit*base
base *= 60
return sign*value
else:
return sign*int(value)
inf_value = 1e300
while inf_value != inf_value*inf_value:
inf_value *= inf_value
nan_value = -inf_value/inf_value # Trying to make a quiet NaN (like C99).
def construct_yaml_float(self, node):
value = self.construct_scalar(node)
value = value.replace('_', '').lower()
sign = +1
if value[0] == '-':
sign = -1
if value[0] in '+-':
value = value[1:]
if value == '.inf':
return sign*self.inf_value
elif value == '.nan':
return self.nan_value
elif ':' in value:
digits = [float(part) for part in value.split(':')]
digits.reverse()
base = 1
value = 0.0
for digit in digits:
value += digit*base
base *= 60
return sign*value
else:
return sign*float(value)
def construct_yaml_binary(self, node):
try:
value = self.construct_scalar(node).encode('ascii')
except UnicodeEncodeError as exc:
raise ConstructorError(None, None,
"failed to convert base64 data into ascii: %s" % exc,
node.start_mark)
try:
if hasattr(base64, 'decodebytes'):
return base64.decodebytes(value)
else:
return base64.decodestring(value)
except binascii.Error as exc:
raise ConstructorError(None, None,
"failed to decode base64 data: %s" % exc, node.start_mark)
timestamp_regexp = re.compile(
r'''^(?P<year>[0-9][0-9][0-9][0-9])
-(?P<month>[0-9][0-9]?)
-(?P<day>[0-9][0-9]?)
(?:(?:[Tt]|[ \t]+)
(?P<hour>[0-9][0-9]?)
:(?P<minute>[0-9][0-9])
:(?P<second>[0-9][0-9])
(?:\.(?P<fraction>[0-9]*))?
(?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
(?::(?P<tz_minute>[0-9][0-9]))?))?)?$''', re.X)
def construct_yaml_timestamp(self, node):
value = self.construct_scalar(node)
match = self.timestamp_regexp.match(node.value)
values = match.groupdict()
year = int(values['year'])
month = int(values['month'])
day = int(values['day'])
if not values['hour']:
return datetime.date(year, month, day)
hour = int(values['hour'])
minute = int(values['minute'])
second = int(values['second'])
fraction = 0
tzinfo = None
if values['fraction']:
fraction = values['fraction'][:6]
while len(fraction) < 6:
fraction += '0'
fraction = int(fraction)
if values['tz_sign']:
tz_hour = int(values['tz_hour'])
tz_minute = int(values['tz_minute'] or 0)
delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute)
if values['tz_sign'] == '-':
delta = -delta
tzinfo = datetime.timezone(delta)
elif values['tz']:
tzinfo = datetime.timezone.utc
return datetime.datetime(year, month, day, hour, minute, second, fraction,
tzinfo=tzinfo)
def construct_yaml_omap(self, node):
# Note: we do not check for duplicate keys, because it's too
# CPU-expensive.
omap = []
yield omap
if not isinstance(node, SequenceNode):
raise ConstructorError("while constructing an ordered map", node.start_mark,
"expected a sequence, but found %s" % node.id, node.start_mark)
for subnode in node.value:
if not isinstance(subnode, MappingNode):
raise ConstructorError("while constructing an ordered map", node.start_mark,
"expected a mapping of length 1, but found %s" % subnode.id,
subnode.start_mark)
if len(subnode.value) != 1:
raise ConstructorError("while constructing an ordered map", node.start_mark,
"expected a single mapping item, but found %d items" % len(subnode.value),
subnode.start_mark)
key_node, value_node = subnode.value[0]
key = self.construct_object(key_node)
value = self.construct_object(value_node)
omap.append((key, value))
def construct_yaml_pairs(self, node):
# Note: the same code as `construct_yaml_omap`.
pairs = []
yield pairs
if not isinstance(node, SequenceNode):
raise ConstructorError("while constructing pairs", node.start_mark,
"expected a sequence, but found %s" % node.id, node.start_mark)
for subnode in node.value:
if not isinstance(subnode, MappingNode):
raise ConstructorError("while constructing pairs", node.start_mark,
"expected a mapping of length 1, but found %s" % subnode.id,
subnode.start_mark)
if len(subnode.value) != 1:
raise ConstructorError("while constructing pairs", node.start_mark,
"expected a single mapping item, but found %d items" % len(subnode.value),
subnode.start_mark)
key_node, value_node = subnode.value[0]
key = self.construct_object(key_node)
value = self.construct_object(value_node)
pairs.append((key, value))
def construct_yaml_set(self, node):
data = set()
yield data
value = self.construct_mapping(node)
data.update(value)
def construct_yaml_str(self, node):
return self.construct_scalar(node)
def construct_yaml_seq(self, node):
data = []
yield data
data.extend(self.construct_sequence(node))
def construct_yaml_map(self, node):
data = {}
yield data
value = self.construct_mapping(node)
data.update(value)
def construct_yaml_object(self, node, cls):
data = cls.__new__(cls)
yield data
if hasattr(data, '__setstate__'):
state = self.construct_mapping(node, deep=True)
data.__setstate__(state)
else:
state = self.construct_mapping(node)
data.__dict__.update(state)
def construct_undefined(self, node):
raise ConstructorError(None, None,
"could not determine a constructor for the tag %r" % node.tag,
node.start_mark)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:null',
SafeConstructor.construct_yaml_null)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:bool',
SafeConstructor.construct_yaml_bool)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:int',
SafeConstructor.construct_yaml_int)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:float',
SafeConstructor.construct_yaml_float)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:binary',
SafeConstructor.construct_yaml_binary)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:timestamp',
SafeConstructor.construct_yaml_timestamp)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:omap',
SafeConstructor.construct_yaml_omap)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:pairs',
SafeConstructor.construct_yaml_pairs)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:set',
SafeConstructor.construct_yaml_set)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:str',
SafeConstructor.construct_yaml_str)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:seq',
SafeConstructor.construct_yaml_seq)
SafeConstructor.add_constructor(
'tag:yaml.org,2002:map',
SafeConstructor.construct_yaml_map)
SafeConstructor.add_constructor(None,
SafeConstructor.construct_undefined)
class FullConstructor(SafeConstructor):
# 'extend' is blacklisted because it is used by
# construct_python_object_apply to add `listitems` to a newly generate
# python instance
def get_state_keys_blacklist(self):
return ['^extend$', '^__.*__$']
def get_state_keys_blacklist_regexp(self):
if not hasattr(self, 'state_keys_blacklist_regexp'):
self.state_keys_blacklist_regexp = re.compile('(' + '|'.join(self.get_state_keys_blacklist()) + ')')
return self.state_keys_blacklist_regexp
def construct_python_str(self, node):
return self.construct_scalar(node)
def construct_python_unicode(self, node):
return self.construct_scalar(node)
def construct_python_bytes(self, node):
try:
value = self.construct_scalar(node).encode('ascii')
except UnicodeEncodeError as exc:
raise ConstructorError(None, None,
"failed to convert base64 data into ascii: %s" % exc,
node.start_mark)
try:
if hasattr(base64, 'decodebytes'):
return base64.decodebytes(value)
else:
return base64.decodestring(value)
except binascii.Error as exc:
raise ConstructorError(None, None,
"failed to decode base64 data: %s" % exc, node.start_mark)
def construct_python_long(self, node):
return self.construct_yaml_int(node)
def construct_python_complex(self, node):
return complex(self.construct_scalar(node))
def construct_python_tuple(self, node):
return tuple(self.construct_sequence(node))
def find_python_module(self, name, mark, unsafe=False):
if not name:
raise ConstructorError("while constructing a Python module", mark,
"expected non-empty name appended to the tag", mark)
if unsafe:
try:
__import__(name)
except ImportError as exc:
raise ConstructorError("while constructing a Python module", mark,
"cannot find module %r (%s)" % (name, exc), mark)
if name not in sys.modules:
raise ConstructorError("while constructing a Python module", mark,
"module %r is not imported" % name, mark)
return sys.modules[name]
def find_python_name(self, name, mark, unsafe=False):
if not name:
raise ConstructorError("while constructing a Python object", mark,
"expected non-empty name appended to the tag", mark)
if '.' in name:
module_name, object_name = name.rsplit('.', 1)
else:
module_name = 'builtins'
object_name = name
if unsafe:
try:
__import__(module_name)
except ImportError as exc:
raise ConstructorError("while constructing a Python object", mark,
"cannot find module %r (%s)" % (module_name, exc), mark)
if module_name not in sys.modules:
raise ConstructorError("while constructing a Python object", mark,
"module %r is not imported" % module_name, mark)
module = sys.modules[module_name]
if not hasattr(module, object_name):
raise ConstructorError("while constructing a Python object", mark,
"cannot find %r in the module %r"
% (object_name, module.__name__), mark)
return getattr(module, object_name)
def construct_python_name(self, suffix, node):
value = self.construct_scalar(node)
if value:
raise ConstructorError("while constructing a Python name", node.start_mark,
"expected the empty value, but found %r" % value, node.start_mark)
return self.find_python_name(suffix, node.start_mark)
def construct_python_module(self, suffix, node):
value = self.construct_scalar(node)
if value:
raise ConstructorError("while constructing a Python module", node.start_mark,
"expected the empty value, but found %r" % value, node.start_mark)
return self.find_python_module(suffix, node.start_mark)
def make_python_instance(self, suffix, node,
args=None, kwds=None, newobj=False, unsafe=False):
if not args:
args = []
if not kwds:
kwds = {}
cls = self.find_python_name(suffix, node.start_mark)
if not (unsafe or isinstance(cls, type)):
raise ConstructorError("while constructing a Python instance", node.start_mark,
"expected a class, but found %r" % type(cls),
node.start_mark)
if newobj and isinstance(cls, type):
return cls.__new__(cls, *args, **kwds)
else:
return cls(*args, **kwds)
def set_python_instance_state(self, instance, state, unsafe=False):
if hasattr(instance, '__setstate__'):
instance.__setstate__(state)
else:
slotstate = {}
if isinstance(state, tuple) and len(state) == 2:
state, slotstate = state
if hasattr(instance, '__dict__'):
if not unsafe and state:
for key in state.keys():
self.check_state_key(key)
instance.__dict__.update(state)
elif state:
slotstate.update(state)
for key, value in slotstate.items():
if not unsafe:
self.check_state_key(key)
setattr(instance, key, value)
def construct_python_object(self, suffix, node):
# Format:
# !!python/object:module.name { ... state ... }
instance = self.make_python_instance(suffix, node, newobj=True)
yield instance
deep = hasattr(instance, '__setstate__')
state = self.construct_mapping(node, deep=deep)
self.set_python_instance_state(instance, state)
def construct_python_object_apply(self, suffix, node, newobj=False):
# Format:
# !!python/object/apply # (or !!python/object/new)
# args: [ ... arguments ... ]
# kwds: { ... keywords ... }
# state: ... state ...
# listitems: [ ... listitems ... ]
# dictitems: { ... dictitems ... }
# or short format:
# !!python/object/apply [ ... arguments ... ]
# The difference between !!python/object/apply and !!python/object/new
# is how an object is created, check make_python_instance for details.
if isinstance(node, SequenceNode):
args = self.construct_sequence(node, deep=True)
kwds = {}
state = {}
listitems = []
dictitems = {}
else:
value = self.construct_mapping(node, deep=True)
args = value.get('args', [])
kwds = value.get('kwds', {})
state = value.get('state', {})
listitems = value.get('listitems', [])
dictitems = value.get('dictitems', {})
instance = self.make_python_instance(suffix, node, args, kwds, newobj)
if state:
self.set_python_instance_state(instance, state)
if listitems:
instance.extend(listitems)
if dictitems:
for key in dictitems:
instance[key] = dictitems[key]
return instance
def construct_python_object_new(self, suffix, node):
return self.construct_python_object_apply(suffix, node, newobj=True)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/none',
FullConstructor.construct_yaml_null)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/bool',
FullConstructor.construct_yaml_bool)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/str',
FullConstructor.construct_python_str)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/unicode',
FullConstructor.construct_python_unicode)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/bytes',
FullConstructor.construct_python_bytes)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/int',
FullConstructor.construct_yaml_int)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/long',
FullConstructor.construct_python_long)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/float',
FullConstructor.construct_yaml_float)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/complex',
FullConstructor.construct_python_complex)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/list',
FullConstructor.construct_yaml_seq)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/tuple',
FullConstructor.construct_python_tuple)
FullConstructor.add_constructor(
'tag:yaml.org,2002:python/dict',
FullConstructor.construct_yaml_map)
FullConstructor.add_multi_constructor(
'tag:yaml.org,2002:python/name:',
FullConstructor.construct_python_name)
FullConstructor.add_multi_constructor(
'tag:yaml.org,2002:python/module:',
FullConstructor.construct_python_module)
FullConstructor.add_multi_constructor(
'tag:yaml.org,2002:python/object:',
FullConstructor.construct_python_object)
FullConstructor.add_multi_constructor(
'tag:yaml.org,2002:python/object/new:',
FullConstructor.construct_python_object_new)
class UnsafeConstructor(FullConstructor):
def find_python_module(self, name, mark):
return super(UnsafeConstructor, self).find_python_module(name, mark, unsafe=True)
def find_python_name(self, name, mark):
return super(UnsafeConstructor, self).find_python_name(name, mark, unsafe=True)
def make_python_instance(self, suffix, node, args=None, kwds=None, newobj=False):
return super(UnsafeConstructor, self).make_python_instance(
suffix, node, args, kwds, newobj, unsafe=True)
def set_python_instance_state(self, instance, state):
return super(UnsafeConstructor, self).set_python_instance_state(
instance, state, unsafe=True)
UnsafeConstructor.add_multi_constructor(
'tag:yaml.org,2002:python/object/apply:',
UnsafeConstructor.construct_python_object_apply)
# Constructor is same as UnsafeConstructor. Need to leave this in place in case
# people have extended it directly.
class Constructor(UnsafeConstructor):
pass

101
src/yaml/cyaml.py Normal file
View File

@ -0,0 +1,101 @@
__all__ = [
'CBaseLoader', 'CSafeLoader', 'CFullLoader', 'CUnsafeLoader', 'CLoader',
'CBaseDumper', 'CSafeDumper', 'CDumper'
]
from _yaml import CParser, CEmitter
from .constructor import *
from .serializer import *
from .representer import *
from .resolver import *
class CBaseLoader(CParser, BaseConstructor, BaseResolver):
def __init__(self, stream):
CParser.__init__(self, stream)
BaseConstructor.__init__(self)
BaseResolver.__init__(self)
class CSafeLoader(CParser, SafeConstructor, Resolver):
def __init__(self, stream):
CParser.__init__(self, stream)
SafeConstructor.__init__(self)
Resolver.__init__(self)
class CFullLoader(CParser, FullConstructor, Resolver):
def __init__(self, stream):
CParser.__init__(self, stream)
FullConstructor.__init__(self)
Resolver.__init__(self)
class CUnsafeLoader(CParser, UnsafeConstructor, Resolver):
def __init__(self, stream):
CParser.__init__(self, stream)
UnsafeConstructor.__init__(self)
Resolver.__init__(self)
class CLoader(CParser, Constructor, Resolver):
def __init__(self, stream):
CParser.__init__(self, stream)
Constructor.__init__(self)
Resolver.__init__(self)
class CBaseDumper(CEmitter, BaseRepresenter, BaseResolver):
def __init__(self, stream,
default_style=None, default_flow_style=False,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None, sort_keys=True):
CEmitter.__init__(self, stream, canonical=canonical,
indent=indent, width=width, encoding=encoding,
allow_unicode=allow_unicode, line_break=line_break,
explicit_start=explicit_start, explicit_end=explicit_end,
version=version, tags=tags)
Representer.__init__(self, default_style=default_style,
default_flow_style=default_flow_style, sort_keys=sort_keys)
Resolver.__init__(self)
class CSafeDumper(CEmitter, SafeRepresenter, Resolver):
def __init__(self, stream,
default_style=None, default_flow_style=False,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None, sort_keys=True):
CEmitter.__init__(self, stream, canonical=canonical,
indent=indent, width=width, encoding=encoding,
allow_unicode=allow_unicode, line_break=line_break,
explicit_start=explicit_start, explicit_end=explicit_end,
version=version, tags=tags)
SafeRepresenter.__init__(self, default_style=default_style,
default_flow_style=default_flow_style, sort_keys=sort_keys)
Resolver.__init__(self)
class CDumper(CEmitter, Serializer, Representer, Resolver):
def __init__(self, stream,
default_style=None, default_flow_style=False,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None, sort_keys=True):
CEmitter.__init__(self, stream, canonical=canonical,
indent=indent, width=width, encoding=encoding,
allow_unicode=allow_unicode, line_break=line_break,
explicit_start=explicit_start, explicit_end=explicit_end,
version=version, tags=tags)
Representer.__init__(self, default_style=default_style,
default_flow_style=default_flow_style, sort_keys=sort_keys)
Resolver.__init__(self)

62
src/yaml/dumper.py Normal file
View File

@ -0,0 +1,62 @@
__all__ = ['BaseDumper', 'SafeDumper', 'Dumper']
from .emitter import *
from .serializer import *
from .representer import *
from .resolver import *
class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver):
def __init__(self, stream,
default_style=None, default_flow_style=False,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None, sort_keys=True):
Emitter.__init__(self, stream, canonical=canonical,
indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break)
Serializer.__init__(self, encoding=encoding,
explicit_start=explicit_start, explicit_end=explicit_end,
version=version, tags=tags)
Representer.__init__(self, default_style=default_style,
default_flow_style=default_flow_style, sort_keys=sort_keys)
Resolver.__init__(self)
class SafeDumper(Emitter, Serializer, SafeRepresenter, Resolver):
def __init__(self, stream,
default_style=None, default_flow_style=False,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None, sort_keys=True):
Emitter.__init__(self, stream, canonical=canonical,
indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break)
Serializer.__init__(self, encoding=encoding,
explicit_start=explicit_start, explicit_end=explicit_end,
version=version, tags=tags)
SafeRepresenter.__init__(self, default_style=default_style,
default_flow_style=default_flow_style, sort_keys=sort_keys)
Resolver.__init__(self)
class Dumper(Emitter, Serializer, Representer, Resolver):
def __init__(self, stream,
default_style=None, default_flow_style=False,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None, sort_keys=True):
Emitter.__init__(self, stream, canonical=canonical,
indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break)
Serializer.__init__(self, encoding=encoding,
explicit_start=explicit_start, explicit_end=explicit_end,
version=version, tags=tags)
Representer.__init__(self, default_style=default_style,
default_flow_style=default_flow_style, sort_keys=sort_keys)
Resolver.__init__(self)

1137
src/yaml/emitter.py Normal file

File diff suppressed because it is too large Load Diff

75
src/yaml/error.py Normal file
View File

@ -0,0 +1,75 @@
__all__ = ['Mark', 'YAMLError', 'MarkedYAMLError']
class Mark:
def __init__(self, name, index, line, column, buffer, pointer):
self.name = name
self.index = index
self.line = line
self.column = column
self.buffer = buffer
self.pointer = pointer
def get_snippet(self, indent=4, max_length=75):
if self.buffer is None:
return None
head = ''
start = self.pointer
while start > 0 and self.buffer[start-1] not in '\0\r\n\x85\u2028\u2029':
start -= 1
if self.pointer-start > max_length/2-1:
head = ' ... '
start += 5
break
tail = ''
end = self.pointer
while end < len(self.buffer) and self.buffer[end] not in '\0\r\n\x85\u2028\u2029':
end += 1
if end-self.pointer > max_length/2-1:
tail = ' ... '
end -= 5
break
snippet = self.buffer[start:end]
return ' '*indent + head + snippet + tail + '\n' \
+ ' '*(indent+self.pointer-start+len(head)) + '^'
def __str__(self):
snippet = self.get_snippet()
where = " in \"%s\", line %d, column %d" \
% (self.name, self.line+1, self.column+1)
if snippet is not None:
where += ":\n"+snippet
return where
class YAMLError(Exception):
pass
class MarkedYAMLError(YAMLError):
def __init__(self, context=None, context_mark=None,
problem=None, problem_mark=None, note=None):
self.context = context
self.context_mark = context_mark
self.problem = problem
self.problem_mark = problem_mark
self.note = note
def __str__(self):
lines = []
if self.context is not None:
lines.append(self.context)
if self.context_mark is not None \
and (self.problem is None or self.problem_mark is None
or self.context_mark.name != self.problem_mark.name
or self.context_mark.line != self.problem_mark.line
or self.context_mark.column != self.problem_mark.column):
lines.append(str(self.context_mark))
if self.problem is not None:
lines.append(self.problem)
if self.problem_mark is not None:
lines.append(str(self.problem_mark))
if self.note is not None:
lines.append(self.note)
return '\n'.join(lines)

86
src/yaml/events.py Normal file
View File

@ -0,0 +1,86 @@
# Abstract classes.
class Event(object):
def __init__(self, start_mark=None, end_mark=None):
self.start_mark = start_mark
self.end_mark = end_mark
def __repr__(self):
attributes = [key for key in ['anchor', 'tag', 'implicit', 'value']
if hasattr(self, key)]
arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
for key in attributes])
return '%s(%s)' % (self.__class__.__name__, arguments)
class NodeEvent(Event):
def __init__(self, anchor, start_mark=None, end_mark=None):
self.anchor = anchor
self.start_mark = start_mark
self.end_mark = end_mark
class CollectionStartEvent(NodeEvent):
def __init__(self, anchor, tag, implicit, start_mark=None, end_mark=None,
flow_style=None):
self.anchor = anchor
self.tag = tag
self.implicit = implicit
self.start_mark = start_mark
self.end_mark = end_mark
self.flow_style = flow_style
class CollectionEndEvent(Event):
pass
# Implementations.
class StreamStartEvent(Event):
def __init__(self, start_mark=None, end_mark=None, encoding=None):
self.start_mark = start_mark
self.end_mark = end_mark
self.encoding = encoding
class StreamEndEvent(Event):
pass
class DocumentStartEvent(Event):
def __init__(self, start_mark=None, end_mark=None,
explicit=None, version=None, tags=None):
self.start_mark = start_mark
self.end_mark = end_mark
self.explicit = explicit
self.version = version
self.tags = tags
class DocumentEndEvent(Event):
def __init__(self, start_mark=None, end_mark=None,
explicit=None):
self.start_mark = start_mark
self.end_mark = end_mark
self.explicit = explicit
class AliasEvent(NodeEvent):
pass
class ScalarEvent(NodeEvent):
def __init__(self, anchor, tag, implicit, value,
start_mark=None, end_mark=None, style=None):
self.anchor = anchor
self.tag = tag
self.implicit = implicit
self.value = value
self.start_mark = start_mark
self.end_mark = end_mark
self.style = style
class SequenceStartEvent(CollectionStartEvent):
pass
class SequenceEndEvent(CollectionEndEvent):
pass
class MappingStartEvent(CollectionStartEvent):
pass
class MappingEndEvent(CollectionEndEvent):
pass

63
src/yaml/loader.py Normal file
View File

@ -0,0 +1,63 @@
__all__ = ['BaseLoader', 'FullLoader', 'SafeLoader', 'Loader', 'UnsafeLoader']
from .reader import *
from .scanner import *
from .parser import *
from .composer import *
from .constructor import *
from .resolver import *
class BaseLoader(Reader, Scanner, Parser, Composer, BaseConstructor, BaseResolver):
def __init__(self, stream):
Reader.__init__(self, stream)
Scanner.__init__(self)
Parser.__init__(self)
Composer.__init__(self)
BaseConstructor.__init__(self)
BaseResolver.__init__(self)
class FullLoader(Reader, Scanner, Parser, Composer, FullConstructor, Resolver):
def __init__(self, stream):
Reader.__init__(self, stream)
Scanner.__init__(self)
Parser.__init__(self)
Composer.__init__(self)
FullConstructor.__init__(self)
Resolver.__init__(self)
class SafeLoader(Reader, Scanner, Parser, Composer, SafeConstructor, Resolver):
def __init__(self, stream):
Reader.__init__(self, stream)
Scanner.__init__(self)
Parser.__init__(self)
Composer.__init__(self)
SafeConstructor.__init__(self)
Resolver.__init__(self)
class Loader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
def __init__(self, stream):
Reader.__init__(self, stream)
Scanner.__init__(self)
Parser.__init__(self)
Composer.__init__(self)
Constructor.__init__(self)
Resolver.__init__(self)
# UnsafeLoader is the same as Loader (which is and was always unsafe on
# untrusted input). Use of either Loader or UnsafeLoader should be rare, since
# FullLoad should be able to load almost all YAML safely. Loader is left intact
# to ensure backwards compatibility.
class UnsafeLoader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
def __init__(self, stream):
Reader.__init__(self, stream)
Scanner.__init__(self)
Parser.__init__(self)
Composer.__init__(self)
Constructor.__init__(self)
Resolver.__init__(self)

49
src/yaml/nodes.py Normal file
View File

@ -0,0 +1,49 @@
class Node(object):
def __init__(self, tag, value, start_mark, end_mark):
self.tag = tag
self.value = value
self.start_mark = start_mark
self.end_mark = end_mark
def __repr__(self):
value = self.value
#if isinstance(value, list):
# if len(value) == 0:
# value = '<empty>'
# elif len(value) == 1:
# value = '<1 item>'
# else:
# value = '<%d items>' % len(value)
#else:
# if len(value) > 75:
# value = repr(value[:70]+u' ... ')
# else:
# value = repr(value)
value = repr(value)
return '%s(tag=%r, value=%s)' % (self.__class__.__name__, self.tag, value)
class ScalarNode(Node):
id = 'scalar'
def __init__(self, tag, value,
start_mark=None, end_mark=None, style=None):
self.tag = tag
self.value = value
self.start_mark = start_mark
self.end_mark = end_mark
self.style = style
class CollectionNode(Node):
def __init__(self, tag, value,
start_mark=None, end_mark=None, flow_style=None):
self.tag = tag
self.value = value
self.start_mark = start_mark
self.end_mark = end_mark
self.flow_style = flow_style
class SequenceNode(CollectionNode):
id = 'sequence'
class MappingNode(CollectionNode):
id = 'mapping'

589
src/yaml/parser.py Normal file
View File

@ -0,0 +1,589 @@
# The following YAML grammar is LL(1) and is parsed by a recursive descent
# parser.
#
# stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
# implicit_document ::= block_node DOCUMENT-END*
# explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
# block_node_or_indentless_sequence ::=
# ALIAS
# | properties (block_content | indentless_block_sequence)?
# | block_content
# | indentless_block_sequence
# block_node ::= ALIAS
# | properties block_content?
# | block_content
# flow_node ::= ALIAS
# | properties flow_content?
# | flow_content
# properties ::= TAG ANCHOR? | ANCHOR TAG?
# block_content ::= block_collection | flow_collection | SCALAR
# flow_content ::= flow_collection | SCALAR
# block_collection ::= block_sequence | block_mapping
# flow_collection ::= flow_sequence | flow_mapping
# block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
# indentless_sequence ::= (BLOCK-ENTRY block_node?)+
# block_mapping ::= BLOCK-MAPPING_START
# ((KEY block_node_or_indentless_sequence?)?
# (VALUE block_node_or_indentless_sequence?)?)*
# BLOCK-END
# flow_sequence ::= FLOW-SEQUENCE-START
# (flow_sequence_entry FLOW-ENTRY)*
# flow_sequence_entry?
# FLOW-SEQUENCE-END
# flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
# flow_mapping ::= FLOW-MAPPING-START
# (flow_mapping_entry FLOW-ENTRY)*
# flow_mapping_entry?
# FLOW-MAPPING-END
# flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
#
# FIRST sets:
#
# stream: { STREAM-START }
# explicit_document: { DIRECTIVE DOCUMENT-START }
# implicit_document: FIRST(block_node)
# block_node: { ALIAS TAG ANCHOR SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START }
# flow_node: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START }
# block_content: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
# flow_content: { FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
# block_collection: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START }
# flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
# block_sequence: { BLOCK-SEQUENCE-START }
# block_mapping: { BLOCK-MAPPING-START }
# block_node_or_indentless_sequence: { ALIAS ANCHOR TAG SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START BLOCK-ENTRY }
# indentless_sequence: { ENTRY }
# flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
# flow_sequence: { FLOW-SEQUENCE-START }
# flow_mapping: { FLOW-MAPPING-START }
# flow_sequence_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
# flow_mapping_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
__all__ = ['Parser', 'ParserError']
from .error import MarkedYAMLError
from .tokens import *
from .events import *
from .scanner import *
class ParserError(MarkedYAMLError):
pass
class Parser:
# Since writing a recursive-descendant parser is a straightforward task, we
# do not give many comments here.
DEFAULT_TAGS = {
'!': '!',
'!!': 'tag:yaml.org,2002:',
}
def __init__(self):
self.current_event = None
self.yaml_version = None
self.tag_handles = {}
self.states = []
self.marks = []
self.state = self.parse_stream_start
def dispose(self):
# Reset the state attributes (to clear self-references)
self.states = []
self.state = None
def check_event(self, *choices):
# Check the type of the next event.
if self.current_event is None:
if self.state:
self.current_event = self.state()
if self.current_event is not None:
if not choices:
return True
for choice in choices:
if isinstance(self.current_event, choice):
return True
return False
def peek_event(self):
# Get the next event.
if self.current_event is None:
if self.state:
self.current_event = self.state()
return self.current_event
def get_event(self):
# Get the next event and proceed further.
if self.current_event is None:
if self.state:
self.current_event = self.state()
value = self.current_event
self.current_event = None
return value
# stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
# implicit_document ::= block_node DOCUMENT-END*
# explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
def parse_stream_start(self):
# Parse the stream start.
token = self.get_token()
event = StreamStartEvent(token.start_mark, token.end_mark,
encoding=token.encoding)
# Prepare the next state.
self.state = self.parse_implicit_document_start
return event
def parse_implicit_document_start(self):
# Parse an implicit document.
if not self.check_token(DirectiveToken, DocumentStartToken,
StreamEndToken):
self.tag_handles = self.DEFAULT_TAGS
token = self.peek_token()
start_mark = end_mark = token.start_mark
event = DocumentStartEvent(start_mark, end_mark,
explicit=False)
# Prepare the next state.
self.states.append(self.parse_document_end)
self.state = self.parse_block_node
return event
else:
return self.parse_document_start()
def parse_document_start(self):
# Parse any extra document end indicators.
while self.check_token(DocumentEndToken):
self.get_token()
# Parse an explicit document.
if not self.check_token(StreamEndToken):
token = self.peek_token()
start_mark = token.start_mark
version, tags = self.process_directives()
if not self.check_token(DocumentStartToken):
raise ParserError(None, None,
"expected '<document start>', but found %r"
% self.peek_token().id,
self.peek_token().start_mark)
token = self.get_token()
end_mark = token.end_mark
event = DocumentStartEvent(start_mark, end_mark,
explicit=True, version=version, tags=tags)
self.states.append(self.parse_document_end)
self.state = self.parse_document_content
else:
# Parse the end of the stream.
token = self.get_token()
event = StreamEndEvent(token.start_mark, token.end_mark)
assert not self.states
assert not self.marks
self.state = None
return event
def parse_document_end(self):
# Parse the document end.
token = self.peek_token()
start_mark = end_mark = token.start_mark
explicit = False
if self.check_token(DocumentEndToken):
token = self.get_token()
end_mark = token.end_mark
explicit = True
event = DocumentEndEvent(start_mark, end_mark,
explicit=explicit)
# Prepare the next state.
self.state = self.parse_document_start
return event
def parse_document_content(self):
if self.check_token(DirectiveToken,
DocumentStartToken, DocumentEndToken, StreamEndToken):
event = self.process_empty_scalar(self.peek_token().start_mark)
self.state = self.states.pop()
return event
else:
return self.parse_block_node()
def process_directives(self):
self.yaml_version = None
self.tag_handles = {}
while self.check_token(DirectiveToken):
token = self.get_token()
if token.name == 'YAML':
if self.yaml_version is not None:
raise ParserError(None, None,
"found duplicate YAML directive", token.start_mark)
major, minor = token.value
if major != 1:
raise ParserError(None, None,
"found incompatible YAML document (version 1.* is required)",
token.start_mark)
self.yaml_version = token.value
elif token.name == 'TAG':
handle, prefix = token.value
if handle in self.tag_handles:
raise ParserError(None, None,
"duplicate tag handle %r" % handle,
token.start_mark)
self.tag_handles[handle] = prefix
if self.tag_handles:
value = self.yaml_version, self.tag_handles.copy()
else:
value = self.yaml_version, None
for key in self.DEFAULT_TAGS:
if key not in self.tag_handles:
self.tag_handles[key] = self.DEFAULT_TAGS[key]
return value
# block_node_or_indentless_sequence ::= ALIAS
# | properties (block_content | indentless_block_sequence)?
# | block_content
# | indentless_block_sequence
# block_node ::= ALIAS
# | properties block_content?
# | block_content
# flow_node ::= ALIAS
# | properties flow_content?
# | flow_content
# properties ::= TAG ANCHOR? | ANCHOR TAG?
# block_content ::= block_collection | flow_collection | SCALAR
# flow_content ::= flow_collection | SCALAR
# block_collection ::= block_sequence | block_mapping
# flow_collection ::= flow_sequence | flow_mapping
def parse_block_node(self):
return self.parse_node(block=True)
def parse_flow_node(self):
return self.parse_node()
def parse_block_node_or_indentless_sequence(self):
return self.parse_node(block=True, indentless_sequence=True)
def parse_node(self, block=False, indentless_sequence=False):
if self.check_token(AliasToken):
token = self.get_token()
event = AliasEvent(token.value, token.start_mark, token.end_mark)
self.state = self.states.pop()
else:
anchor = None
tag = None
start_mark = end_mark = tag_mark = None
if self.check_token(AnchorToken):
token = self.get_token()
start_mark = token.start_mark
end_mark = token.end_mark
anchor = token.value
if self.check_token(TagToken):
token = self.get_token()
tag_mark = token.start_mark
end_mark = token.end_mark
tag = token.value
elif self.check_token(TagToken):
token = self.get_token()
start_mark = tag_mark = token.start_mark
end_mark = token.end_mark
tag = token.value
if self.check_token(AnchorToken):
token = self.get_token()
end_mark = token.end_mark
anchor = token.value
if tag is not None:
handle, suffix = tag
if handle is not None:
if handle not in self.tag_handles:
raise ParserError("while parsing a node", start_mark,
"found undefined tag handle %r" % handle,
tag_mark)
tag = self.tag_handles[handle]+suffix
else:
tag = suffix
#if tag == '!':
# raise ParserError("while parsing a node", start_mark,
# "found non-specific tag '!'", tag_mark,
# "Please check 'http://pyyaml.org/wiki/YAMLNonSpecificTag' and share your opinion.")
if start_mark is None:
start_mark = end_mark = self.peek_token().start_mark
event = None
implicit = (tag is None or tag == '!')
if indentless_sequence and self.check_token(BlockEntryToken):
end_mark = self.peek_token().end_mark
event = SequenceStartEvent(anchor, tag, implicit,
start_mark, end_mark)
self.state = self.parse_indentless_sequence_entry
else:
if self.check_token(ScalarToken):
token = self.get_token()
end_mark = token.end_mark
if (token.plain and tag is None) or tag == '!':
implicit = (True, False)
elif tag is None:
implicit = (False, True)
else:
implicit = (False, False)
event = ScalarEvent(anchor, tag, implicit, token.value,
start_mark, end_mark, style=token.style)
self.state = self.states.pop()
elif self.check_token(FlowSequenceStartToken):
end_mark = self.peek_token().end_mark
event = SequenceStartEvent(anchor, tag, implicit,
start_mark, end_mark, flow_style=True)
self.state = self.parse_flow_sequence_first_entry
elif self.check_token(FlowMappingStartToken):
end_mark = self.peek_token().end_mark
event = MappingStartEvent(anchor, tag, implicit,
start_mark, end_mark, flow_style=True)
self.state = self.parse_flow_mapping_first_key
elif block and self.check_token(BlockSequenceStartToken):
end_mark = self.peek_token().start_mark
event = SequenceStartEvent(anchor, tag, implicit,
start_mark, end_mark, flow_style=False)
self.state = self.parse_block_sequence_first_entry
elif block and self.check_token(BlockMappingStartToken):
end_mark = self.peek_token().start_mark
event = MappingStartEvent(anchor, tag, implicit,
start_mark, end_mark, flow_style=False)
self.state = self.parse_block_mapping_first_key
elif anchor is not None or tag is not None:
# Empty scalars are allowed even if a tag or an anchor is
# specified.
event = ScalarEvent(anchor, tag, (implicit, False), '',
start_mark, end_mark)
self.state = self.states.pop()
else:
if block:
node = 'block'
else:
node = 'flow'
token = self.peek_token()
raise ParserError("while parsing a %s node" % node, start_mark,
"expected the node content, but found %r" % token.id,
token.start_mark)
return event
# block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
def parse_block_sequence_first_entry(self):
token = self.get_token()
self.marks.append(token.start_mark)
return self.parse_block_sequence_entry()
def parse_block_sequence_entry(self):
if self.check_token(BlockEntryToken):
token = self.get_token()
if not self.check_token(BlockEntryToken, BlockEndToken):
self.states.append(self.parse_block_sequence_entry)
return self.parse_block_node()
else:
self.state = self.parse_block_sequence_entry
return self.process_empty_scalar(token.end_mark)
if not self.check_token(BlockEndToken):
token = self.peek_token()
raise ParserError("while parsing a block collection", self.marks[-1],
"expected <block end>, but found %r" % token.id, token.start_mark)
token = self.get_token()
event = SequenceEndEvent(token.start_mark, token.end_mark)
self.state = self.states.pop()
self.marks.pop()
return event
# indentless_sequence ::= (BLOCK-ENTRY block_node?)+
def parse_indentless_sequence_entry(self):
if self.check_token(BlockEntryToken):
token = self.get_token()
if not self.check_token(BlockEntryToken,
KeyToken, ValueToken, BlockEndToken):
self.states.append(self.parse_indentless_sequence_entry)
return self.parse_block_node()
else:
self.state = self.parse_indentless_sequence_entry
return self.process_empty_scalar(token.end_mark)
token = self.peek_token()
event = SequenceEndEvent(token.start_mark, token.start_mark)
self.state = self.states.pop()
return event
# block_mapping ::= BLOCK-MAPPING_START
# ((KEY block_node_or_indentless_sequence?)?
# (VALUE block_node_or_indentless_sequence?)?)*
# BLOCK-END
def parse_block_mapping_first_key(self):
token = self.get_token()
self.marks.append(token.start_mark)
return self.parse_block_mapping_key()
def parse_block_mapping_key(self):
if self.check_token(KeyToken):
token = self.get_token()
if not self.check_token(KeyToken, ValueToken, BlockEndToken):
self.states.append(self.parse_block_mapping_value)
return self.parse_block_node_or_indentless_sequence()
else:
self.state = self.parse_block_mapping_value
return self.process_empty_scalar(token.end_mark)
if not self.check_token(BlockEndToken):
token = self.peek_token()
raise ParserError("while parsing a block mapping", self.marks[-1],
"expected <block end>, but found %r" % token.id, token.start_mark)
token = self.get_token()
event = MappingEndEvent(token.start_mark, token.end_mark)
self.state = self.states.pop()
self.marks.pop()
return event
def parse_block_mapping_value(self):
if self.check_token(ValueToken):
token = self.get_token()
if not self.check_token(KeyToken, ValueToken, BlockEndToken):
self.states.append(self.parse_block_mapping_key)
return self.parse_block_node_or_indentless_sequence()
else:
self.state = self.parse_block_mapping_key
return self.process_empty_scalar(token.end_mark)
else:
self.state = self.parse_block_mapping_key
token = self.peek_token()
return self.process_empty_scalar(token.start_mark)
# flow_sequence ::= FLOW-SEQUENCE-START
# (flow_sequence_entry FLOW-ENTRY)*
# flow_sequence_entry?
# FLOW-SEQUENCE-END
# flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
#
# Note that while production rules for both flow_sequence_entry and
# flow_mapping_entry are equal, their interpretations are different.
# For `flow_sequence_entry`, the part `KEY flow_node? (VALUE flow_node?)?`
# generate an inline mapping (set syntax).
def parse_flow_sequence_first_entry(self):
token = self.get_token()
self.marks.append(token.start_mark)
return self.parse_flow_sequence_entry(first=True)
def parse_flow_sequence_entry(self, first=False):
if not self.check_token(FlowSequenceEndToken):
if not first:
if self.check_token(FlowEntryToken):
self.get_token()
else:
token = self.peek_token()
raise ParserError("while parsing a flow sequence", self.marks[-1],
"expected ',' or ']', but got %r" % token.id, token.start_mark)
if self.check_token(KeyToken):
token = self.peek_token()
event = MappingStartEvent(None, None, True,
token.start_mark, token.end_mark,
flow_style=True)
self.state = self.parse_flow_sequence_entry_mapping_key
return event
elif not self.check_token(FlowSequenceEndToken):
self.states.append(self.parse_flow_sequence_entry)
return self.parse_flow_node()
token = self.get_token()
event = SequenceEndEvent(token.start_mark, token.end_mark)
self.state = self.states.pop()
self.marks.pop()
return event
def parse_flow_sequence_entry_mapping_key(self):
token = self.get_token()
if not self.check_token(ValueToken,
FlowEntryToken, FlowSequenceEndToken):
self.states.append(self.parse_flow_sequence_entry_mapping_value)
return self.parse_flow_node()
else:
self.state = self.parse_flow_sequence_entry_mapping_value
return self.process_empty_scalar(token.end_mark)
def parse_flow_sequence_entry_mapping_value(self):
if self.check_token(ValueToken):
token = self.get_token()
if not self.check_token(FlowEntryToken, FlowSequenceEndToken):
self.states.append(self.parse_flow_sequence_entry_mapping_end)
return self.parse_flow_node()
else:
self.state = self.parse_flow_sequence_entry_mapping_end
return self.process_empty_scalar(token.end_mark)
else:
self.state = self.parse_flow_sequence_entry_mapping_end
token = self.peek_token()
return self.process_empty_scalar(token.start_mark)
def parse_flow_sequence_entry_mapping_end(self):
self.state = self.parse_flow_sequence_entry
token = self.peek_token()
return MappingEndEvent(token.start_mark, token.start_mark)
# flow_mapping ::= FLOW-MAPPING-START
# (flow_mapping_entry FLOW-ENTRY)*
# flow_mapping_entry?
# FLOW-MAPPING-END
# flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
def parse_flow_mapping_first_key(self):
token = self.get_token()
self.marks.append(token.start_mark)
return self.parse_flow_mapping_key(first=True)
def parse_flow_mapping_key(self, first=False):
if not self.check_token(FlowMappingEndToken):
if not first:
if self.check_token(FlowEntryToken):
self.get_token()
else:
token = self.peek_token()
raise ParserError("while parsing a flow mapping", self.marks[-1],
"expected ',' or '}', but got %r" % token.id, token.start_mark)
if self.check_token(KeyToken):
token = self.get_token()
if not self.check_token(ValueToken,
FlowEntryToken, FlowMappingEndToken):
self.states.append(self.parse_flow_mapping_value)
return self.parse_flow_node()
else:
self.state = self.parse_flow_mapping_value
return self.process_empty_scalar(token.end_mark)
elif not self.check_token(FlowMappingEndToken):
self.states.append(self.parse_flow_mapping_empty_value)
return self.parse_flow_node()
token = self.get_token()
event = MappingEndEvent(token.start_mark, token.end_mark)
self.state = self.states.pop()
self.marks.pop()
return event
def parse_flow_mapping_value(self):
if self.check_token(ValueToken):
token = self.get_token()
if not self.check_token(FlowEntryToken, FlowMappingEndToken):
self.states.append(self.parse_flow_mapping_key)
return self.parse_flow_node()
else:
self.state = self.parse_flow_mapping_key
return self.process_empty_scalar(token.end_mark)
else:
self.state = self.parse_flow_mapping_key
token = self.peek_token()
return self.process_empty_scalar(token.start_mark)
def parse_flow_mapping_empty_value(self):
self.state = self.parse_flow_mapping_key
return self.process_empty_scalar(self.peek_token().start_mark)
def process_empty_scalar(self, mark):
return ScalarEvent(None, None, (True, False), '', mark, mark)

185
src/yaml/reader.py Normal file
View File

@ -0,0 +1,185 @@
# This module contains abstractions for the input stream. You don't have to
# looks further, there are no pretty code.
#
# We define two classes here.
#
# Mark(source, line, column)
# It's just a record and its only use is producing nice error messages.
# Parser does not use it for any other purposes.
#
# Reader(source, data)
# Reader determines the encoding of `data` and converts it to unicode.
# Reader provides the following methods and attributes:
# reader.peek(length=1) - return the next `length` characters
# reader.forward(length=1) - move the current position to `length` characters.
# reader.index - the number of the current character.
# reader.line, stream.column - the line and the column of the current character.
__all__ = ['Reader', 'ReaderError']
from .error import YAMLError, Mark
import codecs, re
class ReaderError(YAMLError):
def __init__(self, name, position, character, encoding, reason):
self.name = name
self.character = character
self.position = position
self.encoding = encoding
self.reason = reason
def __str__(self):
if isinstance(self.character, bytes):
return "'%s' codec can't decode byte #x%02x: %s\n" \
" in \"%s\", position %d" \
% (self.encoding, ord(self.character), self.reason,
self.name, self.position)
else:
return "unacceptable character #x%04x: %s\n" \
" in \"%s\", position %d" \
% (self.character, self.reason,
self.name, self.position)
class Reader(object):
# Reader:
# - determines the data encoding and converts it to a unicode string,
# - checks if characters are in allowed range,
# - adds '\0' to the end.
# Reader accepts
# - a `bytes` object,
# - a `str` object,
# - a file-like object with its `read` method returning `str`,
# - a file-like object with its `read` method returning `unicode`.
# Yeah, it's ugly and slow.
def __init__(self, stream):
self.name = None
self.stream = None
self.stream_pointer = 0
self.eof = True
self.buffer = ''
self.pointer = 0
self.raw_buffer = None
self.raw_decode = None
self.encoding = None
self.index = 0
self.line = 0
self.column = 0
if isinstance(stream, str):
self.name = "<unicode string>"
self.check_printable(stream)
self.buffer = stream+'\0'
elif isinstance(stream, bytes):
self.name = "<byte string>"
self.raw_buffer = stream
self.determine_encoding()
else:
self.stream = stream
self.name = getattr(stream, 'name', "<file>")
self.eof = False
self.raw_buffer = None
self.determine_encoding()
def peek(self, index=0):
try:
return self.buffer[self.pointer+index]
except IndexError:
self.update(index+1)
return self.buffer[self.pointer+index]
def prefix(self, length=1):
if self.pointer+length >= len(self.buffer):
self.update(length)
return self.buffer[self.pointer:self.pointer+length]
def forward(self, length=1):
if self.pointer+length+1 >= len(self.buffer):
self.update(length+1)
while length:
ch = self.buffer[self.pointer]
self.pointer += 1
self.index += 1
if ch in '\n\x85\u2028\u2029' \
or (ch == '\r' and self.buffer[self.pointer] != '\n'):
self.line += 1
self.column = 0
elif ch != '\uFEFF':
self.column += 1
length -= 1
def get_mark(self):
if self.stream is None:
return Mark(self.name, self.index, self.line, self.column,
self.buffer, self.pointer)
else:
return Mark(self.name, self.index, self.line, self.column,
None, None)
def determine_encoding(self):
while not self.eof and (self.raw_buffer is None or len(self.raw_buffer) < 2):
self.update_raw()
if isinstance(self.raw_buffer, bytes):
if self.raw_buffer.startswith(codecs.BOM_UTF16_LE):
self.raw_decode = codecs.utf_16_le_decode
self.encoding = 'utf-16-le'
elif self.raw_buffer.startswith(codecs.BOM_UTF16_BE):
self.raw_decode = codecs.utf_16_be_decode
self.encoding = 'utf-16-be'
else:
self.raw_decode = codecs.utf_8_decode
self.encoding = 'utf-8'
self.update(1)
NON_PRINTABLE = re.compile('[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD\U00010000-\U0010ffff]')
def check_printable(self, data):
match = self.NON_PRINTABLE.search(data)
if match:
character = match.group()
position = self.index+(len(self.buffer)-self.pointer)+match.start()
raise ReaderError(self.name, position, ord(character),
'unicode', "special characters are not allowed")
def update(self, length):
if self.raw_buffer is None:
return
self.buffer = self.buffer[self.pointer:]
self.pointer = 0
while len(self.buffer) < length:
if not self.eof:
self.update_raw()
if self.raw_decode is not None:
try:
data, converted = self.raw_decode(self.raw_buffer,
'strict', self.eof)
except UnicodeDecodeError as exc:
character = self.raw_buffer[exc.start]
if self.stream is not None:
position = self.stream_pointer-len(self.raw_buffer)+exc.start
else:
position = exc.start
raise ReaderError(self.name, position, character,
exc.encoding, exc.reason)
else:
data = self.raw_buffer
converted = len(data)
self.check_printable(data)
self.buffer += data
self.raw_buffer = self.raw_buffer[converted:]
if self.eof:
self.buffer += '\0'
self.raw_buffer = None
break
def update_raw(self, size=4096):
data = self.stream.read(size)
if self.raw_buffer is None:
self.raw_buffer = data
else:
self.raw_buffer += data
self.stream_pointer += len(data)
if not data:
self.eof = True

389
src/yaml/representer.py Normal file
View File

@ -0,0 +1,389 @@
__all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer',
'RepresenterError']
from .error import *
from .nodes import *
import datetime, copyreg, types, base64, collections
class RepresenterError(YAMLError):
pass
class BaseRepresenter:
yaml_representers = {}
yaml_multi_representers = {}
def __init__(self, default_style=None, default_flow_style=False, sort_keys=True):
self.default_style = default_style
self.sort_keys = sort_keys
self.default_flow_style = default_flow_style
self.represented_objects = {}
self.object_keeper = []
self.alias_key = None
def represent(self, data):
node = self.represent_data(data)
self.serialize(node)
self.represented_objects = {}
self.object_keeper = []
self.alias_key = None
def represent_data(self, data):
if self.ignore_aliases(data):
self.alias_key = None
else:
self.alias_key = id(data)
if self.alias_key is not None:
if self.alias_key in self.represented_objects:
node = self.represented_objects[self.alias_key]
#if node is None:
# raise RepresenterError("recursive objects are not allowed: %r" % data)
return node
#self.represented_objects[alias_key] = None
self.object_keeper.append(data)
data_types = type(data).__mro__
if data_types[0] in self.yaml_representers:
node = self.yaml_representers[data_types[0]](self, data)
else:
for data_type in data_types:
if data_type in self.yaml_multi_representers:
node = self.yaml_multi_representers[data_type](self, data)
break
else:
if None in self.yaml_multi_representers:
node = self.yaml_multi_representers[None](self, data)
elif None in self.yaml_representers:
node = self.yaml_representers[None](self, data)
else:
node = ScalarNode(None, str(data))
#if alias_key is not None:
# self.represented_objects[alias_key] = node
return node
@classmethod
def add_representer(cls, data_type, representer):
if not 'yaml_representers' in cls.__dict__:
cls.yaml_representers = cls.yaml_representers.copy()
cls.yaml_representers[data_type] = representer
@classmethod
def add_multi_representer(cls, data_type, representer):
if not 'yaml_multi_representers' in cls.__dict__:
cls.yaml_multi_representers = cls.yaml_multi_representers.copy()
cls.yaml_multi_representers[data_type] = representer
def represent_scalar(self, tag, value, style=None):
if style is None:
style = self.default_style
node = ScalarNode(tag, value, style=style)
if self.alias_key is not None:
self.represented_objects[self.alias_key] = node
return node
def represent_sequence(self, tag, sequence, flow_style=None):
value = []
node = SequenceNode(tag, value, flow_style=flow_style)
if self.alias_key is not None:
self.represented_objects[self.alias_key] = node
best_style = True
for item in sequence:
node_item = self.represent_data(item)
if not (isinstance(node_item, ScalarNode) and not node_item.style):
best_style = False
value.append(node_item)
if flow_style is None:
if self.default_flow_style is not None:
node.flow_style = self.default_flow_style
else:
node.flow_style = best_style
return node
def represent_mapping(self, tag, mapping, flow_style=None):
value = []
node = MappingNode(tag, value, flow_style=flow_style)
if self.alias_key is not None:
self.represented_objects[self.alias_key] = node
best_style = True
if hasattr(mapping, 'items'):
mapping = list(mapping.items())
if self.sort_keys:
try:
mapping = sorted(mapping)
except TypeError:
pass
for item_key, item_value in mapping:
node_key = self.represent_data(item_key)
node_value = self.represent_data(item_value)
if not (isinstance(node_key, ScalarNode) and not node_key.style):
best_style = False
if not (isinstance(node_value, ScalarNode) and not node_value.style):
best_style = False
value.append((node_key, node_value))
if flow_style is None:
if self.default_flow_style is not None:
node.flow_style = self.default_flow_style
else:
node.flow_style = best_style
return node
def ignore_aliases(self, data):
return False
class SafeRepresenter(BaseRepresenter):
def ignore_aliases(self, data):
if data is None:
return True
if isinstance(data, tuple) and data == ():
return True
if isinstance(data, (str, bytes, bool, int, float)):
return True
def represent_none(self, data):
return self.represent_scalar('tag:yaml.org,2002:null', 'null')
def represent_str(self, data):
return self.represent_scalar('tag:yaml.org,2002:str', data)
def represent_binary(self, data):
if hasattr(base64, 'encodebytes'):
data = base64.encodebytes(data).decode('ascii')
else:
data = base64.encodestring(data).decode('ascii')
return self.represent_scalar('tag:yaml.org,2002:binary', data, style='|')
def represent_bool(self, data):
if data:
value = 'true'
else:
value = 'false'
return self.represent_scalar('tag:yaml.org,2002:bool', value)
def represent_int(self, data):
return self.represent_scalar('tag:yaml.org,2002:int', str(data))
inf_value = 1e300
while repr(inf_value) != repr(inf_value*inf_value):
inf_value *= inf_value
def represent_float(self, data):
if data != data or (data == 0.0 and data == 1.0):
value = '.nan'
elif data == self.inf_value:
value = '.inf'
elif data == -self.inf_value:
value = '-.inf'
else:
value = repr(data).lower()
# Note that in some cases `repr(data)` represents a float number
# without the decimal parts. For instance:
# >>> repr(1e17)
# '1e17'
# Unfortunately, this is not a valid float representation according
# to the definition of the `!!float` tag. We fix this by adding
# '.0' before the 'e' symbol.
if '.' not in value and 'e' in value:
value = value.replace('e', '.0e', 1)
return self.represent_scalar('tag:yaml.org,2002:float', value)
def represent_list(self, data):
#pairs = (len(data) > 0 and isinstance(data, list))
#if pairs:
# for item in data:
# if not isinstance(item, tuple) or len(item) != 2:
# pairs = False
# break
#if not pairs:
return self.represent_sequence('tag:yaml.org,2002:seq', data)
#value = []
#for item_key, item_value in data:
# value.append(self.represent_mapping(u'tag:yaml.org,2002:map',
# [(item_key, item_value)]))
#return SequenceNode(u'tag:yaml.org,2002:pairs', value)
def represent_dict(self, data):
return self.represent_mapping('tag:yaml.org,2002:map', data)
def represent_set(self, data):
value = {}
for key in data:
value[key] = None
return self.represent_mapping('tag:yaml.org,2002:set', value)
def represent_date(self, data):
value = data.isoformat()
return self.represent_scalar('tag:yaml.org,2002:timestamp', value)
def represent_datetime(self, data):
value = data.isoformat(' ')
return self.represent_scalar('tag:yaml.org,2002:timestamp', value)
def represent_yaml_object(self, tag, data, cls, flow_style=None):
if hasattr(data, '__getstate__'):
state = data.__getstate__()
else:
state = data.__dict__.copy()
return self.represent_mapping(tag, state, flow_style=flow_style)
def represent_undefined(self, data):
raise RepresenterError("cannot represent an object", data)
SafeRepresenter.add_representer(type(None),
SafeRepresenter.represent_none)
SafeRepresenter.add_representer(str,
SafeRepresenter.represent_str)
SafeRepresenter.add_representer(bytes,
SafeRepresenter.represent_binary)
SafeRepresenter.add_representer(bool,
SafeRepresenter.represent_bool)
SafeRepresenter.add_representer(int,
SafeRepresenter.represent_int)
SafeRepresenter.add_representer(float,
SafeRepresenter.represent_float)
SafeRepresenter.add_representer(list,
SafeRepresenter.represent_list)
SafeRepresenter.add_representer(tuple,
SafeRepresenter.represent_list)
SafeRepresenter.add_representer(dict,
SafeRepresenter.represent_dict)
SafeRepresenter.add_representer(set,
SafeRepresenter.represent_set)
SafeRepresenter.add_representer(datetime.date,
SafeRepresenter.represent_date)
SafeRepresenter.add_representer(datetime.datetime,
SafeRepresenter.represent_datetime)
SafeRepresenter.add_representer(None,
SafeRepresenter.represent_undefined)
class Representer(SafeRepresenter):
def represent_complex(self, data):
if data.imag == 0.0:
data = '%r' % data.real
elif data.real == 0.0:
data = '%rj' % data.imag
elif data.imag > 0:
data = '%r+%rj' % (data.real, data.imag)
else:
data = '%r%rj' % (data.real, data.imag)
return self.represent_scalar('tag:yaml.org,2002:python/complex', data)
def represent_tuple(self, data):
return self.represent_sequence('tag:yaml.org,2002:python/tuple', data)
def represent_name(self, data):
name = '%s.%s' % (data.__module__, data.__name__)
return self.represent_scalar('tag:yaml.org,2002:python/name:'+name, '')
def represent_module(self, data):
return self.represent_scalar(
'tag:yaml.org,2002:python/module:'+data.__name__, '')
def represent_object(self, data):
# We use __reduce__ API to save the data. data.__reduce__ returns
# a tuple of length 2-5:
# (function, args, state, listitems, dictitems)
# For reconstructing, we calls function(*args), then set its state,
# listitems, and dictitems if they are not None.
# A special case is when function.__name__ == '__newobj__'. In this
# case we create the object with args[0].__new__(*args).
# Another special case is when __reduce__ returns a string - we don't
# support it.
# We produce a !!python/object, !!python/object/new or
# !!python/object/apply node.
cls = type(data)
if cls in copyreg.dispatch_table:
reduce = copyreg.dispatch_table[cls](data)
elif hasattr(data, '__reduce_ex__'):
reduce = data.__reduce_ex__(2)
elif hasattr(data, '__reduce__'):
reduce = data.__reduce__()
else:
raise RepresenterError("cannot represent an object", data)
reduce = (list(reduce)+[None]*5)[:5]
function, args, state, listitems, dictitems = reduce
args = list(args)
if state is None:
state = {}
if listitems is not None:
listitems = list(listitems)
if dictitems is not None:
dictitems = dict(dictitems)
if function.__name__ == '__newobj__':
function = args[0]
args = args[1:]
tag = 'tag:yaml.org,2002:python/object/new:'
newobj = True
else:
tag = 'tag:yaml.org,2002:python/object/apply:'
newobj = False
function_name = '%s.%s' % (function.__module__, function.__name__)
if not args and not listitems and not dictitems \
and isinstance(state, dict) and newobj:
return self.represent_mapping(
'tag:yaml.org,2002:python/object:'+function_name, state)
if not listitems and not dictitems \
and isinstance(state, dict) and not state:
return self.represent_sequence(tag+function_name, args)
value = {}
if args:
value['args'] = args
if state or not isinstance(state, dict):
value['state'] = state
if listitems:
value['listitems'] = listitems
if dictitems:
value['dictitems'] = dictitems
return self.represent_mapping(tag+function_name, value)
def represent_ordered_dict(self, data):
# Provide uniform representation across different Python versions.
data_type = type(data)
tag = 'tag:yaml.org,2002:python/object/apply:%s.%s' \
% (data_type.__module__, data_type.__name__)
items = [[key, value] for key, value in data.items()]
return self.represent_sequence(tag, [items])
Representer.add_representer(complex,
Representer.represent_complex)
Representer.add_representer(tuple,
Representer.represent_tuple)
Representer.add_representer(type,
Representer.represent_name)
Representer.add_representer(collections.OrderedDict,
Representer.represent_ordered_dict)
Representer.add_representer(types.FunctionType,
Representer.represent_name)
Representer.add_representer(types.BuiltinFunctionType,
Representer.represent_name)
Representer.add_representer(types.ModuleType,
Representer.represent_module)
Representer.add_multi_representer(object,
Representer.represent_object)

227
src/yaml/resolver.py Normal file
View File

@ -0,0 +1,227 @@
__all__ = ['BaseResolver', 'Resolver']
from .error import *
from .nodes import *
import re
class ResolverError(YAMLError):
pass
class BaseResolver:
DEFAULT_SCALAR_TAG = 'tag:yaml.org,2002:str'
DEFAULT_SEQUENCE_TAG = 'tag:yaml.org,2002:seq'
DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map'
yaml_implicit_resolvers = {}
yaml_path_resolvers = {}
def __init__(self):
self.resolver_exact_paths = []
self.resolver_prefix_paths = []
@classmethod
def add_implicit_resolver(cls, tag, regexp, first):
if not 'yaml_implicit_resolvers' in cls.__dict__:
implicit_resolvers = {}
for key in cls.yaml_implicit_resolvers:
implicit_resolvers[key] = cls.yaml_implicit_resolvers[key][:]
cls.yaml_implicit_resolvers = implicit_resolvers
if first is None:
first = [None]
for ch in first:
cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp))
@classmethod
def add_path_resolver(cls, tag, path, kind=None):
# Note: `add_path_resolver` is experimental. The API could be changed.
# `new_path` is a pattern that is matched against the path from the
# root to the node that is being considered. `node_path` elements are
# tuples `(node_check, index_check)`. `node_check` is a node class:
# `ScalarNode`, `SequenceNode`, `MappingNode` or `None`. `None`
# matches any kind of a node. `index_check` could be `None`, a boolean
# value, a string value, or a number. `None` and `False` match against
# any _value_ of sequence and mapping nodes. `True` matches against
# any _key_ of a mapping node. A string `index_check` matches against
# a mapping value that corresponds to a scalar key which content is
# equal to the `index_check` value. An integer `index_check` matches
# against a sequence value with the index equal to `index_check`.
if not 'yaml_path_resolvers' in cls.__dict__:
cls.yaml_path_resolvers = cls.yaml_path_resolvers.copy()
new_path = []
for element in path:
if isinstance(element, (list, tuple)):
if len(element) == 2:
node_check, index_check = element
elif len(element) == 1:
node_check = element[0]
index_check = True
else:
raise ResolverError("Invalid path element: %s" % element)
else:
node_check = None
index_check = element
if node_check is str:
node_check = ScalarNode
elif node_check is list:
node_check = SequenceNode
elif node_check is dict:
node_check = MappingNode
elif node_check not in [ScalarNode, SequenceNode, MappingNode] \
and not isinstance(node_check, str) \
and node_check is not None:
raise ResolverError("Invalid node checker: %s" % node_check)
if not isinstance(index_check, (str, int)) \
and index_check is not None:
raise ResolverError("Invalid index checker: %s" % index_check)
new_path.append((node_check, index_check))
if kind is str:
kind = ScalarNode
elif kind is list:
kind = SequenceNode
elif kind is dict:
kind = MappingNode
elif kind not in [ScalarNode, SequenceNode, MappingNode] \
and kind is not None:
raise ResolverError("Invalid node kind: %s" % kind)
cls.yaml_path_resolvers[tuple(new_path), kind] = tag
def descend_resolver(self, current_node, current_index):
if not self.yaml_path_resolvers:
return
exact_paths = {}
prefix_paths = []
if current_node:
depth = len(self.resolver_prefix_paths)
for path, kind in self.resolver_prefix_paths[-1]:
if self.check_resolver_prefix(depth, path, kind,
current_node, current_index):
if len(path) > depth:
prefix_paths.append((path, kind))
else:
exact_paths[kind] = self.yaml_path_resolvers[path, kind]
else:
for path, kind in self.yaml_path_resolvers:
if not path:
exact_paths[kind] = self.yaml_path_resolvers[path, kind]
else:
prefix_paths.append((path, kind))
self.resolver_exact_paths.append(exact_paths)
self.resolver_prefix_paths.append(prefix_paths)
def ascend_resolver(self):
if not self.yaml_path_resolvers:
return
self.resolver_exact_paths.pop()
self.resolver_prefix_paths.pop()
def check_resolver_prefix(self, depth, path, kind,
current_node, current_index):
node_check, index_check = path[depth-1]
if isinstance(node_check, str):
if current_node.tag != node_check:
return
elif node_check is not None:
if not isinstance(current_node, node_check):
return
if index_check is True and current_index is not None:
return
if (index_check is False or index_check is None) \
and current_index is None:
return
if isinstance(index_check, str):
if not (isinstance(current_index, ScalarNode)
and index_check == current_index.value):
return
elif isinstance(index_check, int) and not isinstance(index_check, bool):
if index_check != current_index:
return
return True
def resolve(self, kind, value, implicit):
if kind is ScalarNode and implicit[0]:
if value == '':
resolvers = self.yaml_implicit_resolvers.get('', [])
else:
resolvers = self.yaml_implicit_resolvers.get(value[0], [])
resolvers += self.yaml_implicit_resolvers.get(None, [])
for tag, regexp in resolvers:
if regexp.match(value):
return tag
implicit = implicit[1]
if self.yaml_path_resolvers:
exact_paths = self.resolver_exact_paths[-1]
if kind in exact_paths:
return exact_paths[kind]
if None in exact_paths:
return exact_paths[None]
if kind is ScalarNode:
return self.DEFAULT_SCALAR_TAG
elif kind is SequenceNode:
return self.DEFAULT_SEQUENCE_TAG
elif kind is MappingNode:
return self.DEFAULT_MAPPING_TAG
class Resolver(BaseResolver):
pass
Resolver.add_implicit_resolver(
'tag:yaml.org,2002:bool',
re.compile(r'''^(?:yes|Yes|YES|no|No|NO
|true|True|TRUE|false|False|FALSE
|on|On|ON|off|Off|OFF)$''', re.X),
list('yYnNtTfFoO'))
Resolver.add_implicit_resolver(
'tag:yaml.org,2002:float',
re.compile(r'''^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)?
|\.[0-9_]+(?:[eE][-+][0-9]+)?
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
|[-+]?\.(?:inf|Inf|INF)
|\.(?:nan|NaN|NAN))$''', re.X),
list('-+0123456789.'))
Resolver.add_implicit_resolver(
'tag:yaml.org,2002:int',
re.compile(r'''^(?:[-+]?0b[0-1_]+
|[-+]?0[0-7_]+
|[-+]?(?:0|[1-9][0-9_]*)
|[-+]?0x[0-9a-fA-F_]+
|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X),
list('-+0123456789'))
Resolver.add_implicit_resolver(
'tag:yaml.org,2002:merge',
re.compile(r'^(?:<<)$'),
['<'])
Resolver.add_implicit_resolver(
'tag:yaml.org,2002:null',
re.compile(r'''^(?: ~
|null|Null|NULL
| )$''', re.X),
['~', 'n', 'N', ''])
Resolver.add_implicit_resolver(
'tag:yaml.org,2002:timestamp',
re.compile(r'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
|[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]?
(?:[Tt]|[ \t]+)[0-9][0-9]?
:[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)?
(?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X),
list('0123456789'))
Resolver.add_implicit_resolver(
'tag:yaml.org,2002:value',
re.compile(r'^(?:=)$'),
['='])
# The following resolver is only for documentation purposes. It cannot work
# because plain scalars cannot start with '!', '&', or '*'.
Resolver.add_implicit_resolver(
'tag:yaml.org,2002:yaml',
re.compile(r'^(?:!|&|\*)$'),
list('!&*'))

1435
src/yaml/scanner.py Normal file

File diff suppressed because it is too large Load Diff

111
src/yaml/serializer.py Normal file
View File

@ -0,0 +1,111 @@
__all__ = ['Serializer', 'SerializerError']
from .error import YAMLError
from .events import *
from .nodes import *
class SerializerError(YAMLError):
pass
class Serializer:
ANCHOR_TEMPLATE = 'id%03d'
def __init__(self, encoding=None,
explicit_start=None, explicit_end=None, version=None, tags=None):
self.use_encoding = encoding
self.use_explicit_start = explicit_start
self.use_explicit_end = explicit_end
self.use_version = version
self.use_tags = tags
self.serialized_nodes = {}
self.anchors = {}
self.last_anchor_id = 0
self.closed = None
def open(self):
if self.closed is None:
self.emit(StreamStartEvent(encoding=self.use_encoding))
self.closed = False
elif self.closed:
raise SerializerError("serializer is closed")
else:
raise SerializerError("serializer is already opened")
def close(self):
if self.closed is None:
raise SerializerError("serializer is not opened")
elif not self.closed:
self.emit(StreamEndEvent())
self.closed = True
#def __del__(self):
# self.close()
def serialize(self, node):
if self.closed is None:
raise SerializerError("serializer is not opened")
elif self.closed:
raise SerializerError("serializer is closed")
self.emit(DocumentStartEvent(explicit=self.use_explicit_start,
version=self.use_version, tags=self.use_tags))
self.anchor_node(node)
self.serialize_node(node, None, None)
self.emit(DocumentEndEvent(explicit=self.use_explicit_end))
self.serialized_nodes = {}
self.anchors = {}
self.last_anchor_id = 0
def anchor_node(self, node):
if node in self.anchors:
if self.anchors[node] is None:
self.anchors[node] = self.generate_anchor(node)
else:
self.anchors[node] = None
if isinstance(node, SequenceNode):
for item in node.value:
self.anchor_node(item)
elif isinstance(node, MappingNode):
for key, value in node.value:
self.anchor_node(key)
self.anchor_node(value)
def generate_anchor(self, node):
self.last_anchor_id += 1
return self.ANCHOR_TEMPLATE % self.last_anchor_id
def serialize_node(self, node, parent, index):
alias = self.anchors[node]
if node in self.serialized_nodes:
self.emit(AliasEvent(alias))
else:
self.serialized_nodes[node] = True
self.descend_resolver(parent, index)
if isinstance(node, ScalarNode):
detected_tag = self.resolve(ScalarNode, node.value, (True, False))
default_tag = self.resolve(ScalarNode, node.value, (False, True))
implicit = (node.tag == detected_tag), (node.tag == default_tag)
self.emit(ScalarEvent(alias, node.tag, implicit, node.value,
style=node.style))
elif isinstance(node, SequenceNode):
implicit = (node.tag
== self.resolve(SequenceNode, node.value, True))
self.emit(SequenceStartEvent(alias, node.tag, implicit,
flow_style=node.flow_style))
index = 0
for item in node.value:
self.serialize_node(item, node, index)
index += 1
self.emit(SequenceEndEvent())
elif isinstance(node, MappingNode):
implicit = (node.tag
== self.resolve(MappingNode, node.value, True))
self.emit(MappingStartEvent(alias, node.tag, implicit,
flow_style=node.flow_style))
for key, value in node.value:
self.serialize_node(key, node, None)
self.serialize_node(value, node, key)
self.emit(MappingEndEvent())
self.ascend_resolver()

104
src/yaml/tokens.py Normal file
View File

@ -0,0 +1,104 @@
class Token(object):
def __init__(self, start_mark, end_mark):
self.start_mark = start_mark
self.end_mark = end_mark
def __repr__(self):
attributes = [key for key in self.__dict__
if not key.endswith('_mark')]
attributes.sort()
arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
for key in attributes])
return '%s(%s)' % (self.__class__.__name__, arguments)
#class BOMToken(Token):
# id = '<byte order mark>'
class DirectiveToken(Token):
id = '<directive>'
def __init__(self, name, value, start_mark, end_mark):
self.name = name
self.value = value
self.start_mark = start_mark
self.end_mark = end_mark
class DocumentStartToken(Token):
id = '<document start>'
class DocumentEndToken(Token):
id = '<document end>'
class StreamStartToken(Token):
id = '<stream start>'
def __init__(self, start_mark=None, end_mark=None,
encoding=None):
self.start_mark = start_mark
self.end_mark = end_mark
self.encoding = encoding
class StreamEndToken(Token):
id = '<stream end>'
class BlockSequenceStartToken(Token):
id = '<block sequence start>'
class BlockMappingStartToken(Token):
id = '<block mapping start>'
class BlockEndToken(Token):
id = '<block end>'
class FlowSequenceStartToken(Token):
id = '['
class FlowMappingStartToken(Token):
id = '{'
class FlowSequenceEndToken(Token):
id = ']'
class FlowMappingEndToken(Token):
id = '}'
class KeyToken(Token):
id = '?'
class ValueToken(Token):
id = ':'
class BlockEntryToken(Token):
id = '-'
class FlowEntryToken(Token):
id = ','
class AliasToken(Token):
id = '<alias>'
def __init__(self, value, start_mark, end_mark):
self.value = value
self.start_mark = start_mark
self.end_mark = end_mark
class AnchorToken(Token):
id = '<anchor>'
def __init__(self, value, start_mark, end_mark):
self.value = value
self.start_mark = start_mark
self.end_mark = end_mark
class TagToken(Token):
id = '<tag>'
def __init__(self, value, start_mark, end_mark):
self.value = value
self.start_mark = start_mark
self.end_mark = end_mark
class ScalarToken(Token):
id = '<scalar>'
def __init__(self, value, plain, start_mark, end_mark, style=None):
self.value = value
self.plain = plain
self.start_mark = start_mark
self.end_mark = end_mark
self.style = style

127
src/yaml2wireviz.py Normal file
View File

@ -0,0 +1,127 @@
import yaml
import wireviz
filename = '../examples/example2.yml'
def check_designators(what, where):
for i,x in enumerate(what):
# print('Looking for {} in {}'.format(x,where[i]))
if x not in input[where[i]]:
return False
return True
def expand(input):
# input can be:
# - a singleton (normally str or int)
# - a list of str or int
# if str is of the format '#-#', it is treated as a range (inclusive) and expanded
output = []
if not isinstance(input, list):
input = [input,]
for e in input:
e = str(e)
if '-' in e: # list of pins
a, b = tuple(map(int, e.split('-')))
if a < b:
for x in range(a,b+1):
output.append(x)
elif a > b:
for x in range(a,b-1,-1):
output.append(x)
elif a == b:
output.append(a)
else:
try:
x = int(e)
except:
x = e
output.append(x)
return output
with open(filename, 'r') as stream:
try:
input = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
h = wireviz.Harness()
# add nodes
for k, o in input['nodes'].items():
h.add_node(k, type=o.get('type'),
gender=o.get('gender'),
num_pins=o.get('num_pins'),
pinout=o.get('pinout'))
# add wires
for k, o in input['wires'].items():
h.add_cable(k, mm2=o.get('mm2'),
awg=o.get('awg'),
length=o.get('length'),
num_wires=o.get('num_wires'),
colors=o.get('colors'),
color_code=o.get('color_code'),
shield=o.get('shield'))
# add connections
conlist = input['connections']
for con in conlist:
if len(con) == 3: # format: connector -- wire -- conector
for c in con:
if len(list(c.keys())) != 1: # check that each entry in con has only one key, which is the designator
raise Exception('Too many keys')
from_name = list(con[0].keys())[0]
via_name = list(con[1].keys())[0]
to_name = list(con[2].keys())[0]
if not check_designators([from_name,via_name,to_name],('nodes','wires','nodes')):
raise Exception('Bad connection definition (3)')
from_pins = expand(con[0][from_name])
via_pins = expand(con[1][via_name])
to_pins = expand(con[2][to_name])
if len(from_pins) != len(via_pins) or len(via_pins) != len(to_pins):
raise Exception('List length mismatch')
for (from_pin, via_pin, to_pin) in zip(from_pins, via_pins, to_pins):
h.connect(from_name, from_pin, via_name, via_pin, to_name, to_pin)
elif len(con) == 2:
for c in con:
if len(list(c.keys())) != 1: # check that each entry in con has only one key, which is the designator
raise Exception('Too many keys')
from_name = list(con[0].keys())[0]
to_name = list(con[1].keys())[0]
n_w = check_designators([from_name, to_name],('nodes','wires'))
w_n = check_designators([from_name, to_name],('wires','nodes'))
n_n = check_designators([from_name, to_name],('nodes','nodes'))
if not n_w and not w_n and not n_n:
raise Exception('Wrong designators')
from_pins = expand(con[0][from_name])
to_pins = expand(con[1][to_name])
if len(from_pins) != len(to_pins):
raise Exception('List length mismatch')
if n_w == True or w_n == True:
for (from_pin, to_pin) in zip(from_pins, to_pins):
if n_w:
h.connect(from_name, from_pin, to_name, to_pin, None, None)
else: # w_n
h.connect(None, None, from_name, from_pin, to_name, to_pin)
elif n_n == True:
con_name = list(con[0].keys())[0]
from_pins = expand(con[0][from_name])
to_pins = expand(con[1][to_name])
for (from_pin, to_pin) in zip(from_pins, to_pins):
h.loop(con_name, from_pin, to_pin)
else:
raise Exception('Wrong number of connection parameters')
h.output(filename='output', format=('png','svg'), view=False)

28
todo.md Normal file
View File

@ -0,0 +1,28 @@
# To-do:
* Set global parameters (show_pins, ...) and allow override on per-item basis
* Generic connectors
* ferrules
* blade terminals
* loose ends
* graphical representation?
* Support for cable splicing (as connector type)
* new wire look?
* distinguish between cables and wire bundles
* improve nomenclature
* terminal (connector, ferrule, blade, loose)
* link (cable, wire bundle)
* show from/to inside wire node
* Allow custom GraphViz code before/after WireViz-generated code
* Display picture of connector underneath (including pin 1 location)
* export to PDF with frame, title block, ...
* Automatic BOM generation
* Allow
* make "unit tests" for different features/situations
* missing parameters
* connection formats
* single wire 1
* multiple wires [1,2,3]
* wire ranges [1-10]
* loops
* ...