/usr/share/doc/python-cliff-doc/html/demoapp.html is in python-cliff-doc 1.15.0-2ubuntu2.
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | <!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>Exploring the Demo App — cliff 1.15.0 documentation</title>
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/tweaks.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.15.0',
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="cliff 1.15.0 documentation" href="index.html" />
<link rel="next" title="List Commands" href="list_commands.html" />
<link rel="prev" title="Introduction" href="introduction.html" />
</head>
<body role="document">
<div id="header">
<h1 id="logo"><a href="http://www.openstack.org/">OpenStack</a></h1>
<ul id="navigation">
<li><a href="http://www.openstack.org/" title="Go to the Home page" class="link">Home</a></li>
<li><a href="http://www.openstack.org/projects/" title="Go to the OpenStack Projects page">Projects</a></li>
<li><a href="http://www.openstack.org/user-stories/" title="Go to the User Stories page" class="link">User Stories</a></li>
<li><a href="http://www.openstack.org/community/" title="Go to the Community page" class="link">Community</a></li>
<li><a href="http://www.openstack.org/blog/" title="Go to the OpenStack Blog">Blog</a></li>
<li><a href="http://wiki.openstack.org/" title="Go to the OpenStack Wiki">Wiki</a></li>
<li><a href="http://docs.openstack.org/" title="Go to OpenStack Documentation" class="current">Documentation</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="exploring-the-demo-app">
<h1>Exploring the Demo App<a class="headerlink" href="#exploring-the-demo-app" title="Permalink to this headline">¶</a></h1>
<p>The cliff source package includes a <code class="docutils literal"><span class="pre">demoapp</span></code> directory containing
an example main program with several command plugins.</p>
<div class="section" id="setup">
<h2>Setup<a class="headerlink" href="#setup" title="Permalink to this headline">¶</a></h2>
<p>To install and experiment with the demo app you should create a
virtual environment and activate it. This will make it easy to remove
the app later, since it doesn’t do anything useful and you aren’t
likely to want to hang onto it after you understand how it works.</p>
<div class="highlight-python"><div class="highlight"><pre>$ pip install virtualenv
$ virtualenv .venv
$ . .venv/bin/activate
(.venv)$
</pre></div>
</div>
<p>Next, install cliff in the same environment.</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ python setup.py install
</pre></div>
</div>
<p>Finally, install the demo application into the virtual environment.</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cd demoapp
(.venv)$ python setup.py install
</pre></div>
</div>
</div>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<p>Both cliff and the demo installed, you can now run the command
<code class="docutils literal"><span class="pre">cliffdemo</span></code>.</p>
<p>For basic command usage instructions and a list of the commands
available from the plugins, run:</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cliffdemo -h
</pre></div>
</div>
<p>or:</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cliffdemo --help
</pre></div>
</div>
<p>Run the <code class="docutils literal"><span class="pre">simple</span></code> command by passing its name as argument to <code class="docutils literal"><span class="pre">cliffdemo</span></code>.</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cliffdemo simple
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">simple</span></code> command prints this output to the console:</p>
<div class="highlight-python"><div class="highlight"><pre>sending greeting
hi!
</pre></div>
</div>
<p>To see help for an individual command, use the <code class="docutils literal"><span class="pre">help</span></code> command:</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cliffdemo help files
</pre></div>
</div>
<p>or the <code class="docutils literal"><span class="pre">--help</span></code> option:</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cliffdemo files --help
</pre></div>
</div>
</div>
<div class="section" id="the-source">
<h2>The Source<a class="headerlink" href="#the-source" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal"><span class="pre">cliffdemo</span></code> application is defined in a <code class="docutils literal"><span class="pre">cliffdemo</span></code> package
containing several modules.</p>
<div class="section" id="main-py">
<h3>main.py<a class="headerlink" href="#main-py" title="Permalink to this headline">¶</a></h3>
<p>The main application is defined in <code class="docutils literal"><span class="pre">main.py</span></code>:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 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</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">cliff.app</span> <span class="kn">import</span> <span class="n">App</span>
<span class="kn">from</span> <span class="nn">cliff.commandmanager</span> <span class="kn">import</span> <span class="n">CommandManager</span>
<span class="k">class</span> <span class="nc">DemoApp</span><span class="p">(</span><span class="n">App</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="nb">super</span><span class="p">(</span><span class="n">DemoApp</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'cliff demo app'</span><span class="p">,</span>
<span class="n">version</span><span class="o">=</span><span class="s1">'0.1'</span><span class="p">,</span>
<span class="n">command_manager</span><span class="o">=</span><span class="n">CommandManager</span><span class="p">(</span><span class="s1">'cliff.demo'</span><span class="p">),</span>
<span class="n">deferred_help</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="p">)</span>
<span class="k">def</span> <span class="nf">initialize_app</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">argv</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LOG</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'initialize_app'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">prepare_to_run_command</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cmd</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LOG</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'prepare_to_run_command </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span> <span class="n">cmd</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">clean_up</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">err</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LOG</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'clean_up </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span> <span class="n">cmd</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>
<span class="k">if</span> <span class="n">err</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LOG</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'got an error: </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">argv</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]):</span>
<span class="n">myapp</span> <span class="o">=</span> <span class="n">DemoApp</span><span class="p">()</span>
<span class="k">return</span> <span class="n">myapp</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">argv</span><span class="p">)</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">main</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span>
</pre></div>
</td></tr></table></div>
<p>The <code class="xref py py-class docutils literal"><span class="pre">DemoApp</span></code> class inherits from <code class="xref py py-class docutils literal"><span class="pre">App</span></code> and overrides
<code class="xref py py-func docutils literal"><span class="pre">__init__()</span></code> to set the program description and version number. It
also passes a <code class="xref py py-class docutils literal"><span class="pre">CommandManager</span></code> instance configured to look for
plugins in the <code class="docutils literal"><span class="pre">cliff.demo</span></code> namespace.</p>
<p>The <code class="xref py py-func docutils literal"><span class="pre">initialize_app()</span></code> method of <code class="xref py py-class docutils literal"><span class="pre">DemoApp</span></code> will be invoked
after the main program arguments are parsed, but before any command
processing is performed and before the application enters interactive
mode. This hook is intended for opening connections to remote web
services, databases, etc. using arguments passed to the main
application.</p>
<p>The <code class="xref py py-func docutils literal"><span class="pre">prepare_to_run_command()</span></code> method of <code class="xref py py-class docutils literal"><span class="pre">DemoApp</span></code> will be
invoked after a command is identified, but before the command is given
its arguments and run. This hook is intended for pre-command
validation or setup that must be repeated and cannot be handled by
<code class="xref py py-func docutils literal"><span class="pre">initialize_app()</span></code>.</p>
<p>The <code class="xref py py-func docutils literal"><span class="pre">clean_up()</span></code> method of <code class="xref py py-class docutils literal"><span class="pre">DemoApp</span></code> is invoked after a
command runs. If the command raised an exception, the exception object
is passed to <code class="xref py py-func docutils literal"><span class="pre">clean_up()</span></code>. Otherwise the <code class="docutils literal"><span class="pre">err</span></code> argument is
<code class="docutils literal"><span class="pre">None</span></code>.</p>
<p>The <code class="xref py py-func docutils literal"><span class="pre">main()</span></code> function defined in <code class="docutils literal"><span class="pre">main.py</span></code> is registered as a
console script entry point so that <code class="xref py py-class docutils literal"><span class="pre">DemoApp</span></code> can be run from
the command line (see the discussion of <code class="docutils literal"><span class="pre">setup.py</span></code> below).</p>
</div>
<div class="section" id="simple-py">
<h3>simple.py<a class="headerlink" href="#simple-py" title="Permalink to this headline">¶</a></h3>
<p>Two commands are defined in <code class="docutils literal"><span class="pre">simple.py</span></code>:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">cliff.command</span> <span class="kn">import</span> <span class="n">Command</span>
<span class="k">class</span> <span class="nc">Simple</span><span class="p">(</span><span class="n">Command</span><span class="p">):</span>
<span class="s2">"A simple command that prints a message."</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">take_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parsed_args</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'sending greeting'</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'debugging'</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">app</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'hi!</span><span class="se">\n</span><span class="s1">'</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">Error</span><span class="p">(</span><span class="n">Command</span><span class="p">):</span>
<span class="s2">"Always raises an error"</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">take_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parsed_args</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'causing error'</span><span class="p">)</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">'this is the expected exception'</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p><code class="xref py py-class docutils literal"><span class="pre">Simple</span></code> demonstrates using logging to emit messages on the
console at different verbose levels.</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cliffdemo simple
sending greeting
hi!
(.venv)$ cliffdemo -v simple
prepare_to_run_command Simple
sending greeting
debugging
hi!
clean_up Simple
(.venv)$ cliffdemo -q simple
hi!
</pre></div>
</div>
<p><code class="xref py py-class docutils literal"><span class="pre">Error</span></code> always raises a <code class="xref py py-class docutils literal"><span class="pre">RuntimeError</span></code> exception when it
is invoked, and can be used to experiment with the error handling
features of cliff.</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cliffdemo error
causing error
ERROR: this is the expected exception
(.venv)$ cliffdemo -v error
prepare_to_run_command Error
causing error
ERROR: this is the expected exception
clean_up Error
got an error: this is the expected exception
(.venv)$ cliffdemo --debug error
causing error
this is the expected exception
Traceback (most recent call last):
File ".../cliff/app.py", line 218, in run_subcommand
result = cmd.run(parsed_args)
File ".../cliff/command.py", line 43, in run
self.take_action(parsed_args)
File ".../demoapp/cliffdemo/simple.py", line 24, in take_action
raise RuntimeError('this is the expected exception')
RuntimeError: this is the expected exception
Traceback (most recent call last):
File "/Users/dhellmann/Envs/cliff/bin/cliffdemo", line 9, in <module>
load_entry_point('cliffdemo==0.1', 'console_scripts', 'cliffdemo')()
File ".../demoapp/cliffdemo/main.py", line 33, in main
return myapp.run(argv)
File ".../cliff/app.py", line 160, in run
result = self.run_subcommand(remainder)
File ".../cliff/app.py", line 218, in run_subcommand
result = cmd.run(parsed_args)
File ".../cliff/command.py", line 43, in run
self.take_action(parsed_args)
File ".../demoapp/cliffdemo/simple.py", line 24, in take_action
raise RuntimeError('this is the expected exception')
RuntimeError: this is the expected exception
</pre></div>
</div>
</div>
<div class="section" id="list-py">
<span id="demoapp-list"></span><h3>list.py<a class="headerlink" href="#list-py" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal"><span class="pre">list.py</span></code> includes a single command derived from
<a class="reference internal" href="classes.html#cliff.lister.Lister" title="cliff.lister.Lister"><code class="xref py py-class docutils literal"><span class="pre">cliff.lister.Lister</span></code></a> which prints a list of the files in the
current directory.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">cliff.lister</span> <span class="kn">import</span> <span class="n">Lister</span>
<span class="k">class</span> <span class="nc">Files</span><span class="p">(</span><span class="n">Lister</span><span class="p">):</span>
<span class="sd">"""Show a list of files in the current directory.</span>
<span class="sd"> The file name and size are printed by default.</span>
<span class="sd"> """</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">take_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parsed_args</span><span class="p">):</span>
<span class="k">return</span> <span class="p">((</span><span class="s1">'Name'</span><span class="p">,</span> <span class="s1">'Size'</span><span class="p">),</span>
<span class="p">((</span><span class="n">n</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">st_size</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s1">'.'</span><span class="p">))</span>
<span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p><code class="xref py py-class docutils literal"><span class="pre">Files</span></code> prepares the data, and <code class="xref py py-class docutils literal"><span class="pre">Lister</span></code> manages the
output formatter and printing the data to the console.</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cliffdemo files
+---------------+------+
| Name | Size |
+---------------+------+
| build | 136 |
| cliffdemo.log | 2546 |
| Makefile | 5569 |
| source | 408 |
+---------------+------+
(.venv)$ cliffdemo files -f csv
"Name","Size"
"build",136
"cliffdemo.log",2690
"Makefile",5569
"source",408
</pre></div>
</div>
</div>
<div class="section" id="show-py">
<span id="demoapp-show"></span><h3>show.py<a class="headerlink" href="#show-py" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal"><span class="pre">show.py</span></code> includes a single command derived from
<a class="reference internal" href="classes.html#cliff.show.ShowOne" title="cliff.show.ShowOne"><code class="xref py py-class docutils literal"><span class="pre">cliff.show.ShowOne</span></code></a> which prints the properties of the named
file.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 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</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">cliff.show</span> <span class="kn">import</span> <span class="n">ShowOne</span>
<span class="k">class</span> <span class="nc">File</span><span class="p">(</span><span class="n">ShowOne</span><span class="p">):</span>
<span class="s2">"Show details about a file"</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_parser</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prog_name</span><span class="p">):</span>
<span class="n">parser</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">File</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_parser</span><span class="p">(</span><span class="n">prog_name</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'filename'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s1">'?'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s1">'.'</span><span class="p">)</span>
<span class="k">return</span> <span class="n">parser</span>
<span class="k">def</span> <span class="nf">take_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parsed_args</span><span class="p">):</span>
<span class="n">stat_data</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">parsed_args</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
<span class="n">columns</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'Name'</span><span class="p">,</span>
<span class="s1">'Size'</span><span class="p">,</span>
<span class="s1">'UID'</span><span class="p">,</span>
<span class="s1">'GID'</span><span class="p">,</span>
<span class="s1">'Modified Time'</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">(</span><span class="n">parsed_args</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span>
<span class="n">stat_data</span><span class="o">.</span><span class="n">st_size</span><span class="p">,</span>
<span class="n">stat_data</span><span class="o">.</span><span class="n">st_uid</span><span class="p">,</span>
<span class="n">stat_data</span><span class="o">.</span><span class="n">st_gid</span><span class="p">,</span>
<span class="n">stat_data</span><span class="o">.</span><span class="n">st_mtime</span><span class="p">,</span>
<span class="p">)</span>
<span class="k">return</span> <span class="p">(</span><span class="n">columns</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p><code class="xref py py-class docutils literal"><span class="pre">File</span></code> prepares the data, and <code class="xref py py-class docutils literal"><span class="pre">ShowOne</span></code> manages the
output formatter and printing the data to the console.</p>
<div class="highlight-python"><div class="highlight"><pre>(.venv)$ cliffdemo file setup.py
+---------------+--------------+
| Field | Value |
+---------------+--------------+
| Name | setup.py |
| Size | 5825 |
| UID | 502 |
| GID | 20 |
| Modified Time | 1335569964.0 |
+---------------+--------------+
</pre></div>
</div>
</div>
<div class="section" id="setup-py">
<h3>setup.py<a class="headerlink" href="#setup-py" title="Permalink to this headline">¶</a></h3>
<p>The demo application is packaged using <a class="reference external" href="http://packages.python.org/distribute/">distribute</a>, the modern
implementation of setuptools.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 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</pre></div></td><td class="code"><div class="highlight"><pre><span class="ch">#!/usr/bin/env python</span>
<span class="n">PROJECT</span> <span class="o">=</span> <span class="s1">'cliffdemo'</span>
<span class="c1"># Change docs/sphinx/conf.py too!</span>
<span class="n">VERSION</span> <span class="o">=</span> <span class="s1">'0.1'</span>
<span class="kn">from</span> <span class="nn">setuptools</span> <span class="kn">import</span> <span class="n">setup</span><span class="p">,</span> <span class="n">find_packages</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">long_description</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'README.rst'</span><span class="p">,</span> <span class="s1">'rt'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
<span class="n">long_description</span> <span class="o">=</span> <span class="s1">''</span>
<span class="n">setup</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="n">PROJECT</span><span class="p">,</span>
<span class="n">version</span><span class="o">=</span><span class="n">VERSION</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'Demo app for cliff'</span><span class="p">,</span>
<span class="n">long_description</span><span class="o">=</span><span class="n">long_description</span><span class="p">,</span>
<span class="n">author</span><span class="o">=</span><span class="s1">'Doug Hellmann'</span><span class="p">,</span>
<span class="n">author_email</span><span class="o">=</span><span class="s1">'doug.hellmann@gmail.com'</span><span class="p">,</span>
<span class="n">url</span><span class="o">=</span><span class="s1">'https://github.com/dreamhost/cliff'</span><span class="p">,</span>
<span class="n">download_url</span><span class="o">=</span><span class="s1">'https://github.com/dreamhost/cliff/tarball/master'</span><span class="p">,</span>
<span class="n">classifiers</span><span class="o">=</span><span class="p">[</span><span class="s1">'Development Status :: 3 - Alpha'</span><span class="p">,</span>
<span class="s1">'License :: OSI Approved :: Apache Software License'</span><span class="p">,</span>
<span class="s1">'Programming Language :: Python'</span><span class="p">,</span>
<span class="s1">'Programming Language :: Python :: 2'</span><span class="p">,</span>
<span class="s1">'Programming Language :: Python :: 2.7'</span><span class="p">,</span>
<span class="s1">'Programming Language :: Python :: 3'</span><span class="p">,</span>
<span class="s1">'Programming Language :: Python :: 3.2'</span><span class="p">,</span>
<span class="s1">'Intended Audience :: Developers'</span><span class="p">,</span>
<span class="s1">'Environment :: Console'</span><span class="p">,</span>
<span class="p">],</span>
<span class="n">platforms</span><span class="o">=</span><span class="p">[</span><span class="s1">'Any'</span><span class="p">],</span>
<span class="n">scripts</span><span class="o">=</span><span class="p">[],</span>
<span class="n">provides</span><span class="o">=</span><span class="p">[],</span>
<span class="n">install_requires</span><span class="o">=</span><span class="p">[</span><span class="s1">'cliff'</span><span class="p">],</span>
<span class="n">namespace_packages</span><span class="o">=</span><span class="p">[],</span>
<span class="n">packages</span><span class="o">=</span><span class="n">find_packages</span><span class="p">(),</span>
<span class="n">include_package_data</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">entry_points</span><span class="o">=</span><span class="p">{</span>
<span class="s1">'console_scripts'</span><span class="p">:</span> <span class="p">[</span>
<span class="s1">'cliffdemo = cliffdemo.main:main'</span>
<span class="p">],</span>
<span class="s1">'cliff.demo'</span><span class="p">:</span> <span class="p">[</span>
<span class="s1">'simple = cliffdemo.simple:Simple'</span><span class="p">,</span>
<span class="s1">'two_part = cliffdemo.simple:Simple'</span><span class="p">,</span>
<span class="s1">'error = cliffdemo.simple:Error'</span><span class="p">,</span>
<span class="s1">'list files = cliffdemo.list:Files'</span><span class="p">,</span>
<span class="s1">'files = cliffdemo.list:Files'</span><span class="p">,</span>
<span class="s1">'file = cliffdemo.show:File'</span><span class="p">,</span>
<span class="s1">'show file = cliffdemo.show:File'</span><span class="p">,</span>
<span class="s1">'unicode = cliffdemo.encoding:Encoding'</span><span class="p">,</span>
<span class="p">],</span>
<span class="p">},</span>
<span class="n">zip_safe</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>The important parts of the packaging instructions are the
<code class="docutils literal"><span class="pre">entry_points</span></code> settings. All of the commands are registered in the
<code class="docutils literal"><span class="pre">cliff.demo</span></code> namespace. Each main program should define its own
command namespace so that it only loads the command plugins that it
should be managing.</p>
</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="#">Exploring the Demo App</a><ul>
<li><a class="reference internal" href="#setup">Setup</a></li>
<li><a class="reference internal" href="#usage">Usage</a></li>
<li><a class="reference internal" href="#the-source">The Source</a><ul>
<li><a class="reference internal" href="#main-py">main.py</a></li>
<li><a class="reference internal" href="#simple-py">simple.py</a></li>
<li><a class="reference internal" href="#list-py">list.py</a></li>
<li><a class="reference internal" href="#show-py">show.py</a></li>
<li><a class="reference internal" href="#setup-py">setup.py</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="introduction.html"
title="previous chapter">Introduction</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="list_commands.html"
title="next chapter">List Commands</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/demoapp.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" size="18" />
<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" role="navigation" aria-label="related navigation">
<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="list_commands.html" title="List Commands"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="introduction.html" title="Introduction"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">cliff 1.15.0 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2012-2016, Doug Hellmann.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.6.
</div>
<script type="text/javascript">
try {
//Tracking docs.openstack.org/developer/<projectname> only
//The URL is built from the project variable in conf.py
var pageTracker = _gat._getTracker("UA-17511903-1");
pageTracker._setCookiePath("/developer/cliff");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>
|