-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathcode_writer.py
More file actions
47 lines (37 loc) · 963 Bytes
/
code_writer.py
File metadata and controls
47 lines (37 loc) · 963 Bytes
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
import sys
if sys.version_info.major == 3:
import io
StringIO = io.StringIO
else:
from StringIO import StringIO
class Writer(object):
def __init__(self):
self.inline_glsl = False
self.inline_skip = ('@', 'def ', 'while ', 'if ', 'for ', 'var(')
self.level = 0
self.buffer = list()
self.output = StringIO()
self.functions = []
def is_at_global_level(self):
return self.level == 0
def push(self):
self.level += 1
def pull(self):
self.level -= 1
def append(self, code):
self.buffer.append(code)
def write(self, code):
for content in self.buffer:
self._write(content)
self.buffer = list()
self._write(code)
def _write(self, code):
indentation = self.level * 4 * ' '
if self.inline_glsl and not code.startswith( self.inline_skip ):
code = "inline('''%s''')" %code
s = '%s%s\n' % (indentation, code)
self.output.write(s)
def getvalue(self):
s = self.output.getvalue()
self.output = StringIO()
return s