This file is indexed.

/usr/share/doc/python-gtkmvc-doc/userman/exam.html is in python-gtkmvc-doc 1.99.1-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>A simple application &mdash; Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '1.99.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation" href="index.html" />
    <link rel="next" title="Adapters" href="adapt.html" />
    <link rel="prev" title="Observers" href="observers.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="adapt.html" title="Adapters"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="observers.html" title="Observers"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="a-simple-application">
<h1>A simple application<a class="headerlink" href="#a-simple-application" title="Permalink to this headline"></a></h1>
<p>This section describes the process of creation of a sample
application, from the design with <em>Glade</em>, to the integration of views
and code inside the <em>MVC-O</em> Infrastructure.</p>
<p>We want to design and implement a simple application constituted by
only one window, containing two string labels. One label shows a text,
while the other shows the number of characters displayed (i.e. the
length of the string) by the first one. There is also a button the
user can press. By pressing the button, the user can change the
displayed text, and of course this action might change also the
displayed text length accordingly. Figure <a class="reference internal" href="#ex-f"><em>The sample Application</em></a> gives an idea on
how the application should appear.</p>
<div class="figure align-center" id="ex-f">
<img alt="_images/example.png" src="_images/example.png" style="width: 6cm;" />
<p class="caption">The sample Application</p>
</div>
<div class="section" id="glade">
<span id="glex"></span><h2>Glade<a class="headerlink" href="#glade" title="Permalink to this headline"></a></h2>
<p>Figure <a class="reference internal" href="#gl-f"><em>Designing the example by means of Glade for GTK2</em></a> shows <em>Glade</em> and a project named <tt class="docutils literal"><span class="pre">example</span></tt>.
The sample <em>GUI</em> has only one top-level window (named
<tt class="docutils literal"><span class="pre">window1</span></tt>).</p>
<div class="figure align-center" id="gl-f">
<img alt="_images/example_glade.png" src="_images/example_glade.png" style="width: 15cm;" />
<p class="caption">Designing the example by means of <em>Glade</em> for GTK2</p>
</div>
<p>The <em>Widget Tree Window</em> shows the widgets hierarchy. There are
essentially the three main components (one button and two labels),
grouped inside a set of <em>containers</em>, which supplies alignments and
resizing capabilities.</p>
<p>On the right side of Figure <a class="reference internal" href="#gl-f"><em>Designing the example by means of Glade for GTK2</em></a>, the <em>Properties Window</em>
shows that the widget named <tt class="docutils literal"><span class="pre">button1</span></tt> has signal
<tt class="docutils literal"><span class="pre">clicked</span></tt> associated with function
<tt class="docutils literal"><span class="pre">on_button1_clicked</span></tt>. This means that the Controller will
have to supply this function in order to handle the <tt class="docutils literal"><span class="pre">click</span></tt>
event occurring in <tt class="docutils literal"><span class="pre">button1</span></tt>.</p>
</div>
<div class="section" id="implementation">
<h2>Implementation<a class="headerlink" href="#implementation" title="Permalink to this headline"></a></h2>
<p>The implementation is slightly elaborate for this example, because the
goal here is to show how the sample application can be implemented by
using the <em>MVC-O</em> Infrastructure.</p>
<p>A basic knowledge of any Object Oriented programming language is
sufficient to understand how this example has been pushed inside the
<em>MVC-O</em> framework. On the contrary, a fair knowledge of the Python
language is required in order to understand the code details.</p>
<p>More description section is <a class="reference internal" href="impl.html"><em>Details of implementation</em></a>.</p>
<div class="section" id="model">
<h3>Model<a class="headerlink" href="#model" title="Permalink to this headline"></a></h3>
<p>Class <tt class="docutils literal"><span class="pre">ExampleModel</span></tt> is as simple as class
<tt class="docutils literal"><span class="pre">ExampleView</span></tt>.  As for <tt class="docutils literal"><span class="pre">ExampleView</span></tt>, it extends a
base class of the <em>MVC-O</em> Infrastructure, class <tt class="docutils literal"><span class="pre">Model</span></tt>.  The
state is represented by a set of possible messages, as well as by the
current message index. The current message index is also an
observable property. A couple of methods are supplied in order to
access the state.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># file model.py</span>
<span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>

<span class="k">class</span> <span class="nc">ExampleModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="sd">&quot;&quot;&quot;The model contains a set of messages</span>
<span class="sd">   and an observable property that represent the current message</span>
<span class="sd">   index&quot;&quot;&quot;</span>

   <span class="c"># Observable property: code for that is automatically generated</span>
   <span class="c"># by metaclass constructor. The controller will be the observer</span>
   <span class="c"># for this property</span>
   <span class="n">message_index</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>   <span class="c"># -1 is the initial value</span>
   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;message_index&quot;</span><span class="p">,)</span>

   <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
       <span class="n">Model</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

       <span class="bp">self</span><span class="o">.</span><span class="n">messages</span><span class="o">=</span> <span class="p">(</span><span class="s">&quot;I am patient with stupidity&quot;</span><span class="p">,</span>
                       <span class="s">&quot;but not with those&quot;</span><span class="p">,</span>
                       <span class="s">&quot;who are proud of it.&quot;</span><span class="p">,</span>
                       <span class="s">&quot;(Edith Sitwell)&quot;</span><span class="p">,</span>
                       <span class="p">)</span>
       <span class="k">return</span>

   <span class="k">def</span> <span class="nf">get_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">messages</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>

   <span class="k">def</span> <span class="nf">set_next_message</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
       <span class="c"># this changes the observable property:</span>
       <span class="bp">self</span><span class="o">.</span><span class="n">message_index</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message_index</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">messages</span><span class="p">)</span>
       <span class="k">return</span>

   <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>Notice that class instance members are declared to be observable
through the special class variable <tt class="docutils literal"><span class="pre">__observables__</span></tt>,
which is a list of names (string) of the properties that are
observable.</p>
<p>The base class Model belongs to a
meta-class which automatically searches for observable properties and
generates the needed code to handle the notification.  When the value
of variable <tt class="docutils literal"><span class="pre">message_index</span></tt> changes, all registered
observers will be notified.</p>
</div>
<div class="section" id="view">
<h3>View<a class="headerlink" href="#view" title="Permalink to this headline"></a></h3>
<p>In the example, the View is implemented inside the class
<tt class="docutils literal"><span class="pre">ExampleView</span></tt> shown below.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># file view.py</span>
<span class="kn">import</span> <span class="nn">os.path</span>
<span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">View</span>

<span class="n">GLADE_PATH</span> <span class="o">=</span> <span class="s">&quot;./&quot;</span>

<span class="k">class</span> <span class="nc">ExampleView</span> <span class="p">(</span><span class="n">View</span><span class="p">):</span>
   <span class="sd">&quot;&quot;&quot;The application view. Contains only the main window1 tree.&quot;&quot;&quot;</span>
   <span class="n">builder</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">GLADE_PATH</span><span class="p">,</span> <span class="s">&quot;example.glade&quot;</span><span class="p">)</span>
   <span class="n">top</span> <span class="o">=</span> <span class="s">&quot;window1&quot;</span>

   <span class="k">def</span> <span class="nf">set_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
      <span class="bp">self</span><span class="p">[</span><span class="s">&#39;label_text&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">set_text</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
      <span class="bp">self</span><span class="p">[</span><span class="s">&#39;label_text_len&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">set_text</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">msg</span><span class="p">)))</span>
      <span class="k">return</span>

   <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>Class <tt class="docutils literal"><span class="pre">ExampleView</span></tt> extends the generic <tt class="docutils literal"><span class="pre">View</span></tt>
class, which performs most of the job, as described above.
Class members <tt class="docutils literal"><span class="pre">builder</span></tt> and <tt class="docutils literal"><span class="pre">top</span></tt> are used instead of
calling <tt class="docutils literal"><span class="pre">View</span></tt> constructor directly.</p>
</div>
<div class="section" id="controller">
<h3>Controller<a class="headerlink" href="#controller" title="Permalink to this headline"></a></h3>
<p>Class <tt class="docutils literal"><span class="pre">ExampleController</span></tt> contains the <em>GUI logic</em> of the
application. The controller handles two signals and the observable
property notification. Signals are the <tt class="docutils literal"><span class="pre">destroy</span></tt> event,
invoked when the application quits, and the
<tt class="docutils literal"><span class="pre">on_button1_clicked</span></tt>, fired when <tt class="docutils literal"><span class="pre">button1</span></tt> is
pressed.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># file ctrl.py</span>
<span class="kn">from</span> <span class="nn">gtk</span> <span class="kn">import</span> <span class="n">main_quit</span>
<span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Controller</span>

<span class="k">class</span> <span class="nc">ExampleController</span><span class="p">(</span><span class="n">Controller</span><span class="p">):</span>
   <span class="sd">&quot;&quot;&quot;The only one controller. Handles the button clicked signal, and</span>
<span class="sd">   notifications about one observable property.&quot;&quot;&quot;</span>

   <span class="k">def</span> <span class="nf">register_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">view</span><span class="p">):</span>
       <span class="c"># Connects the exiting signal:</span>
       <span class="n">view</span><span class="o">.</span><span class="n">get_top_widget</span><span class="p">()</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;destroy&quot;</span><span class="p">,</span> <span class="n">main_quit</span><span class="p">)</span>
       <span class="k">return</span>

   <span class="c"># Signal</span>
   <span class="k">def</span> <span class="nf">on_button1_clicked</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">):</span>
       <span class="sd">&quot;&quot;&quot;Handles the signal clicked for button1. Changes the model.&quot;&quot;&quot;</span>
       <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">set_next_message</span><span class="p">()</span>
       <span class="k">return</span>

   <span class="c"># Observables notifications</span>
   <span class="nd">@Controller.observe</span><span class="p">(</span><span class="s">&quot;message_index&quot;</span><span class="p">,</span> <span class="n">assign</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
   <span class="k">def</span> <span class="nf">value_change</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">info</span><span class="p">):</span>
       <span class="sd">&quot;&quot;&quot;The model is changed and the view must be updated&quot;&quot;&quot;</span>
       <span class="n">msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">get_message</span><span class="p">(</span><span class="n">info</span><span class="o">.</span><span class="n">new</span><span class="p">)</span>

       <span class="bp">self</span><span class="o">.</span><span class="n">view</span><span class="o">.</span><span class="n">set_msg</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
       <span class="k">return</span>

   <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">destroy</span></tt> signal is connected when the View registers itself
inside the controller, by using the method override of
<tt class="docutils literal"><span class="pre">register_view</span></tt>.  Method <tt class="docutils literal"><span class="pre">on_button1_clicked</span></tt> calls a method
inside the model which changes a part of the state inside the
model. Since that part of the state is an observable property, the
associated observer (which is the controller itself) is notified of
the modification, by calling method <tt class="docutils literal"><span class="pre">value_change</span></tt>. This method
updates the view connected to the controller.</p>
</div>
<div class="section" id="the-launcher-main">
<h3>The Launcher (main)<a class="headerlink" href="#the-launcher-main" title="Permalink to this headline"></a></h3>
<p>The <tt class="xref py py-func docutils literal"><span class="pre">main()</span></tt> code creates a <cite>(m,v,c)</cite> triple and launches
<tt class="xref py py-func docutils literal"><span class="pre">gtk.main()</span></tt>. All the rest is cosmetics.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">gtk</span>

<span class="kn">from</span> <span class="nn">model</span> <span class="kn">import</span> <span class="n">ExampleModel</span>
<span class="kn">from</span> <span class="nn">ctrl</span> <span class="kn">import</span> <span class="n">ExampleController</span>
<span class="kn">from</span> <span class="nn">view</span> <span class="kn">import</span> <span class="n">ExampleView</span>
<span class="kn">import</span> <span class="nn">gtkmvc</span>

<span class="k">def</span> <span class="nf">check_requirements</span><span class="p">():</span>
   <span class="n">gtkmvc</span><span class="o">.</span><span class="n">require</span><span class="p">(</span><span class="s">&quot;1.99.1&quot;</span><span class="p">)</span>
   <span class="k">return</span>

<span class="k">def</span> <span class="nf">setup_env</span><span class="p">(</span><span class="n">development_state</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
   <span class="c"># This is how developers should set gtkmvc logging level (by</span>
   <span class="c"># default debugging info is not shown):</span>
   <span class="k">if</span> <span class="n">development_state</span><span class="p">:</span>
       <span class="kn">import</span> <span class="nn">logging</span>
       <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&quot;gtkmvc&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
       <span class="k">pass</span>
   <span class="k">return</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
   <span class="n">m</span> <span class="o">=</span> <span class="n">ExampleModel</span><span class="p">()</span>
   <span class="n">v</span> <span class="o">=</span> <span class="n">ExampleView</span><span class="p">()</span>
   <span class="n">c</span> <span class="o">=</span> <span class="n">ExampleController</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>

   <span class="n">gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
   <span class="k">return</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
   <span class="n">check_requirements</span><span class="p">()</span>
   <span class="n">setup_env</span><span class="p">(</span><span class="n">development_state</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
   <span class="n">main</span><span class="p">()</span>
   <span class="k">pass</span>
</pre></div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">A simple application</a><ul>
<li><a class="reference internal" href="#glade">Glade</a></li>
<li><a class="reference internal" href="#implementation">Implementation</a><ul>
<li><a class="reference internal" href="#model">Model</a></li>
<li><a class="reference internal" href="#view">View</a></li>
<li><a class="reference internal" href="#controller">Controller</a></li>
<li><a class="reference internal" href="#the-launcher-main">The Launcher (main)</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="observers.html"
                        title="previous chapter">Observers</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="adapt.html"
                        title="next chapter">Adapters</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/exam.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="adapt.html" title="Adapters"
             >next</a> |</li>
        <li class="right" >
          <a href="observers.html" title="Observers"
             >previous</a> |</li>
        <li><a href="index.html">Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, Roberto Cavada.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>