Created
July 29, 2013 01:30
-
-
Save RavuAlHemio/6101622 to your computer and use it in GitHub Desktop.
Fuel rod configuration diagram creation script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Creates fuel-rod diagrams. | |
import re | |
MARGIN_TOP = 4.5 | |
MARGIN_LEFT = 4.5 | |
MARGIN_RIGHT = 5.5 | |
MARGIN_BOTTOM = 5.5 | |
BOX_WIDTH = 50 | |
BOX_HEIGHT = 50 | |
BOX_ADVANCE_X = 50 | |
BOX_ADVANCE_Y = 50 | |
TEXT_HEIGHT = 24 | |
TEXT_Y_DELTA = -4 | |
LETTERS_COLORS = { | |
"_": None, | |
".": "#033000", | |
"g": "#00B150", | |
"y": "#FED800", | |
"b": "#0067CE", | |
"r": "#DE1700" | |
} | |
LETTERS_OPACITIES = { | |
".": "0.3569" | |
} | |
LETTERS_TEXTS = { | |
".": "•" | |
} | |
specre = re.compile("([^0-9])([0-9]*)") | |
# Sample specification for the fateful Chernobyl configuration: | |
SPEC_CHERNOBYL = """ | |
_________________.............._________________ | |
______________...g0...g0...g0...g0....______________ | |
____________........................____________ | |
__________.y0...g0...y86...g0...y20...g0...y0..__________ | |
_________.............................._________ | |
________.g0...g0...g240...g0...g0...g0...g0...g0..________ | |
_______.................................._______ | |
______.g0...g0...b...g0...g110...g0...b...g0...g0..______ | |
_____......................................_____ | |
____.g0...g0...g0...g0...g0...g0...g0...g0...g0...g0..____ | |
___..........................................___ | |
___y0...g0...y0...g30...y0...r140...y0...g0...y0...g0...y0.___ | |
__............................................__ | |
__...g0...g0...g0...g0...g0...g0...g120...g0...g0...g0....__ | |
_.............................................._ | |
_..g0...b...g70...r160...g0...b...g0...r160...g30...b...g0..._ | |
_.............................................._ | |
.g0...g0...g120...g0...g0...g100...g0...g30...g0...g20...g140...g0.. | |
................................................ | |
...y0...g0...y0...g0...y0...r20...y0...g80...y120...g0...y190.... | |
................................................ | |
.g0...g0...g0...g0...g20...g0...g0...g0...g140...g0...g0...g180.. | |
................................................ | |
...g0...g0...r140...b...r20...g0...r20...b...r140...g0...g0.... | |
................................................ | |
.g0...g0...g0...g0...g0...g0...g90...g0...g0...g0...g0...g0.. | |
................................................ | |
...y180...g0...y0...g0...y0...r20...y20...g0...y100...g170...y0.... | |
................................................ | |
.g20...g120...g0...g0...g20...g260...g0...g0...g40...g0...g20...g0.. | |
................................................ | |
_..g0...b...g0...r160...g0...b...g0...r160...g0...b...g0..._ | |
_.............................................._ | |
_....g0...g0...g0...g0...g0...g0...g0...g0...g30...g0...g0._ | |
__............................................__ | |
__.y0...g20...y0...g0...y0...r140...y0...g0...y0...g0...y0..__ | |
___..........................................___ | |
___..g0...g0...g0...g0...g0...g0...g0...g80...g0...g0...___ | |
____........................................____ | |
_____..g0...g0...b...g0...g20...g0...b...g0...g0..._____ | |
______....................................______ | |
_______..g0...g0...g0...g0...g0...g30...g0...g0..._______ | |
________................................________ | |
_________..y0...g0...y0...g0...y0...g0...y0..._________ | |
__________............................__________ | |
____________.....g0...g0...g0...g0...g0..____________ | |
______________....................______________ | |
_________________.............._________________ | |
""" | |
WIDTH_CHERNOBYL = 48 | |
HEIGHT_CHERNOBYL = 48 | |
def genboxes(spec, width=48): | |
x = MARGIN_LEFT | |
y = MARGIN_TOP | |
xn = 0 | |
ret = "" | |
# iterate over the specifications | |
for match in specre.finditer(spec): | |
field = match.group(1) | |
text = match.group(2) | |
if field in LETTERS_COLORS: | |
color = LETTERS_COLORS[field] | |
if color != None: | |
opacity = 1 | |
if field in LETTERS_OPACITIES: | |
opacity = LETTERS_OPACITIES[field] | |
if field in LETTERS_TEXTS: | |
text = LETTERS_TEXTS[field] | |
# make the box | |
ret += ' <rect x="{x}" y="{y}" width="{w}" height="{h}" style="fill:{color};stroke:black;stroke-width:1px;fill-opacity:{opacity}" />\n'.format( | |
x=x, y=y, w=BOX_WIDTH, h=BOX_HEIGHT, color=color, opacity=opacity | |
) | |
# text? | |
if len(text) != 0: | |
# text. | |
ret += ' <text x="{x}" y="{y}">{text}</text>'.format( | |
fntsz = TEXT_HEIGHT, | |
x = x + BOX_WIDTH/2, | |
y = y + (BOX_HEIGHT+TEXT_HEIGHT)/2 + TEXT_Y_DELTA, | |
text = text | |
) | |
elif not field.isspace(): | |
raise ValueError("Unknown field {0}".format(field)) | |
if xn >= width: | |
y += BOX_ADVANCE_Y | |
x = MARGIN_LEFT | |
xn = 0 | |
else: | |
x += BOX_ADVANCE_X | |
xn += 1 | |
return ret | |
def gendoc(spec, width=48, height=48): | |
ret = "" | |
picwidth = MARGIN_LEFT + MARGIN_RIGHT + width * BOX_ADVANCE_X | |
picheight = MARGIN_TOP + MARGIN_BOTTOM + height * BOX_ADVANCE_Y | |
# preamble | |
ret += '<?xml version="1.0"?>\n' | |
ret += """<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="{h}" version="1.1" style="font-family:'DejaVu Sans';text-anchor:middle;font-size:{fntsz}px">\n""".format( | |
w=picwidth, h=picheight, fntsz=TEXT_HEIGHT | |
) | |
# placement | |
ret += genboxes(spec, width) | |
# tail | |
ret += '</svg>\n' | |
return ret | |
if __name__ == '__main__': | |
import sys | |
spec = sys.stdin.read() | |
sys.stdout.write(gendoc(spec)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment