File: examples.rpy

package info (click to toggle)
renpy 6.10.2.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 19,468 kB
  • ctags: 5,383
  • sloc: python: 17,801; ansic: 7,116; makefile: 127; sh: 15
file content (215 lines) | stat: -rw-r--r-- 5,437 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# This file is responsible for displaying code examples. It expects to see
# comments like #begin foo and #end foo a the start of lines. The code is
# then used to create example fragments.
#
# When we see:
#
# show example foo bar
#
# We concatenate fragements foo and bar, higlight them, wrap them into a
# viewport, button and transform, and display them to the user.

transform example_transform:
    ypos 450 yanchor 1.0 xpos 0 xanchor 0

    on replace:
        crop (0, 0, 800, 120)
            
    on show:
        crop (0, 0, 800, 0)
        linear .5 crop (0, 0, 800, 120)

    on hide:
        linear .5 crop (0, 0, 800, 0)

init python:

    import re
    
    KEYWORDS = """\
                and
                as
                assert
                break
                class
                continue
                def
                elif
                else
                except
                exec
                finally
                for
                from
                global
                if
                import
                in
                is
                lambda
                not
                or
                pass
                print
                raise
                return
                try
                while
                with
                yield

                at
                behind
                call
                expression
                hide
                image
                init
                jump
                label
                menu
                onlayer
                python
                scene
                set
                show
                play
                queue
                stop
                sound
                music
                fadeout
                fadein
                loop
                noloop
                if_changed
                voice
                sustain
                nvl
                clear
                window
                pause
                define
                transform
                """

    KEYWORDS = [ i.strip() for i in KEYWORDS.split() ]
    KWREGEX = r"|".join(KEYWORDS)

    regex = r"(?P<keyword>\b(" + KWREGEX + r")\b)|(?P<string>\"([^\"]|\\.)*\")|(?P<comment>#.*)"
    regex = re.compile(regex)
    
    # This maps from example name to the text of the fragment.    
    examples = { }

    class __Example(object):
        """
         When parameterized, this displays an example window, containing
         example text from blocks with those parameters.
         """

        def colorize(self, m):
            if m.group("string"):
                return "{color=#060}" + m.group(0) + "{/color}"

            if m.group("keyword"):
                return "{color=#840}" + m.group(0) + "{/color}"

            if m.group("comment"):
                return "{color=#600}" + m.group(0) + "{/color}"
            
            return m.group(0)

        def predict(self, callback):
            return
        
        def parameterize(self, name, args):

            # Collect the examples we use.            
            lines1 = [ ]
            
            for i in args:
                if i not in examples:
                    raise Exception("Unknown example %r." % i)
                lines1.extend(examples[i])


            # Strip off doubled blank lines.
            last_blank = False
            lines = [ ]
            
            for i in lines1:

                if not i and last_blank:
                    continue

                last_blank = not i

                i = regex.sub(self.colorize, i)
                
                lines.append(i)
            
            # Join them into a single string.
            code = "\n".join(lines) + "\n "

            ct = Text(code, size=16, color="#000")
            vp = Viewport(ct, child_size=(2000, 2000), ymaximum=120, draggable=True, mousewheel=True)
            w = Window(vp, background = "#fffc", top_padding=0, right_padding=0, bottom_padding=0, yminimum=0)
            return example_transform(w)

image example = __Example()
        
                
init python hide:

    import os.path
    import re
    
    # A list of files we will be scanning.
    files = [ ]
    
    for i in os.listdir(config.gamedir):
        if i.endswith(".rpy"):
            files.append(os.path.join(config.gamedir, i))

    for fn in files:

        f = file(fn, "r")

        open_examples = set()
        
        for l in f:

            l = l.decode("utf-8")
            l = l.rstrip()
            
            m = re.match("\s*#begin (\w+)", l)
            if m:
                example = m.group(1)

                if example in examples:
                    raise Exception("Example %r is defined in two places.", example)

                open_examples.add(example)
                examples[example] = [ ]

                continue

            m = re.match("\s*#end (\w+)", l)
            if m:
                example = m.group(1)

                if example not in open_examples:
                    raise Exception("Example %r is not open.", example)

                open_examples.remove(example)
                continue

            for i in open_examples:
                examples[i].append(l)

        if open_examples:
            raise Exception("Examples %r remain open at the end of %r" % (open_examples, fn))

        f.close()