-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsexpr_writer.py
169 lines (142 loc) · 5.31 KB
/
sexpr_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Original file from:
# Flexlay - A Generic 2D Game Editor
# Copyright (C) 2014 Ingo Ruhnke <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def write_sexpr(fout, sexpr, indent=0):
if isinstance(sexpr, list):
fout.write("(\n")
for i, e in enumerate(sexpr):
write_sexpr(fout, e)
if i != len(sexpr) - 1:
fout.write(" \n")
fout.write(")\n")
else:
# if sexpr.is_a?(Symbol):
# fout.write(str(sexpr))
# else:
fout.write(repr(sexpr))
class SExprWriter:
def __init__(self, fout):
self.fout = fout
self.indent_depth = 0
def write_comment(self, comment):
self.fout.write(";; " + comment + "\n")
def begin_list(self, listname):
self.indent()
self.fout.write("(" + listname + "\n")
self.indent_depth += 2
def end_list(self, listname=None):
self.indent_depth -= 2
self.indent()
self.fout.write(")\n")
def write(self, name, value):
if type(value) == bool:
self.write_bool(name, value)
elif type(value) == int:
self.write_int(name, value)
elif type(value) == float:
self.write_float(name, value)
elif type(value) == str:
self.write_string(name, value)
elif type(value) == list:
self.write_vector(name, value)
else:
raise RuntimeError("unknown requested in generic write(): %r" % type(value))
def write_bool(self, name, value):
self.indent()
self.fout.write("(" + name + " ")
if value:
self.fout.write("#t")
else:
self.fout.write("#f")
self.fout.write(")\n")
def write_int(self, name, value):
self.indent()
self.fout.write("(%s %d)\n" % (name, value))
def write_float(self, name, value):
self.indent()
# %f makes ugly floats, e.g. 45.000000, %s gives nicely
# trimmed ones, e.g. "45.0"
self.fout.write("(%s %s)\n" % (name, value))
def write_tr_string(self, name, value):
self.write_string(name, value, translatable=True)
def write_string(self, name, value, translatable=False):
self.indent()
self.fout.write("(" + name)
if translatable:
self.fout.write(" (_ \"" + value.replace('"', '\\"') + "\"))\n")
else:
self.fout.write(" \"" + value.replace('"', '\\"') + "\")\n")
def write_rgb(self, name, color):
self.indent()
self.fout.write("(" + name)
self.fout.write(" %s %s %s)\n" % (color[0], color[1], color[2]))
def write_inline_point(self, pos):
self.write_int("x", pos.x)
self.write_int("y", pos.y)
def write_inline_pointf(self, pos):
self.write_float("x", pos.x)
self.write_float("y", pos.y)
def write_inline_size(self, size):
self.write_int("width", size.width)
self.write_int("height", size.height)
def write_inline_sizef(self, size):
self.write_float("width", size.width)
self.write_float("height", size.height)
def write_inline_rect(self, rect):
# ugly this way around, but for compatibilty with the C# supertux-editor
self.write_int("width", rect.width)
self.write_int("height", rect.height)
self.write_int("x", rect.left)
self.write_int("y", rect.top)
def write_inline_rectf(self, rect):
# ugly this way around, but for compatibilty with the C# supertux-editor
self.write_float("width", rect.width)
self.write_float("height", rect.height)
self.write_float("x", rect.left)
self.write_float("y", rect.top)
def write_vector(self, name, values):
self.indent()
self.fout.write("(" + name)
for i in values:
self.fout.write(" %r" % i)
self.fout.write(")\n")
def write_color(self, name, values):
self.indent()
self.fout.write("(" + name)
for i in values:
if i == 1.0:
self.fout.write(" 1")
elif i == 0:
self.fout.write(" 0")
else:
self.fout.write(" %r" % i)
self.fout.write(")\n")
def write_field(self, name, field):
self.indent()
self.fout.write("(%s\n" % name)
for y in range(0, field.height):
self.indent()
for x in range(0, field.width):
if x < field.width - 1:
self.fout.write("%d " % field.at(x, y))
else:
self.fout.write("%d" % field.at(x, y))
self.fout.write("\n")
self.indent()
self.fout.write(")\n")
def indent(self):
self.fout.write(" " * self.indent_depth)
# EOF #