Difference between pages "Zope HOWTO" and "ConsoleOutput MediaWiki Extension"

(Difference between pages)
 
 
Line 1: Line 1:
This page documents how to use Zope with Funtoo Experimental, which currently has good Zope support thanks to [[Progress Overlay Python]] integration.
+
The ConsoleOutput MediaWiki extension was created by Daniel Robbins to provide highlighting of user input for interactive terminal session blocks. To use it, surround user input with <tt>&lt;console&gt;</tt> opening and closing tags, and put a <tt>##i##</tt> input code immediately before user input on each line. This will cause all text from the <tt>##i##</tt> point onwards to be in a darker color. The rest of the text will be in a lighter grey, allowing it to be distinguished more easily from user input. Here are other sequences that can be used:
  
== About Zope ==
+
* <tt>##b##</tt> - highlight the rest of the line in bold.
 +
* <tt>##b## text here ##!b##</tt> - highlight the text between both markers in bold.
 +
* <tt>##i## text here ##!i##</tt> - highlight the text between both markers in a darker color.
  
Zope is an Open Source application server framework written in Python. It has an interesting history which you should familiarize yourself with before starting Zope development, as it contains several interesting twists and turns.
+
This extension can be further extended to add color support, as desired, and some of the CSS for color support can already be found in the code below.
  
=== Zope History ===
+
Here is an example of ConsoleOutput output:
  
{{fancynote| This HOWTO targets Zope 2.13, which includes Five. It is typically the version you should be using for new Zope projects.}}
 
 
* There are two versions of Zope: Zope 2 and Zope 3. One might assume that Zope 3 is the version that people should use for new software development projects by default, but this is not the case. Most Zope-based projects continue to use Zope 2. Zope 3 was an attempt to redesign Zope 2 from scratch, and is completely different from Zope 2, but it was not adopted by the community.
 
 
* There is also something called [http://codespeak.net/z3/five/ Five] (named because it is "2 + 3") that backports many of the new features of Zope 3 into the Zope 2 framework. Several projects will use Zope 2 plus Five in order to use some of the newer features in Zope. Five was merged into mainline Zope 2 in early 2010, and first appeared in Zope 2.8.
 
 
* You can learn more about the history of Zope 2, 3 and Five in the [http://svn.zope.org/Zope/trunk/src/Products/Five/README.txt?view=markup Five README].
 
 
* To make things even more interesting, work on [http://docs.zope.org/zope2/releases/4.0/ Zope 4] is underway, and it will be based on 2.13 rather than 3.x. It includes a number of [http://docs.zope.org/zope2/releases/4.0/CHANGES.html#restructuring incompatible changes] with prior versions.
 
=== Zope Resources ===
 
 
Now that you understand what version of Zope you should be targeting (2.13), we can point you towards the correct documentation :)
 
 
; '''[http://docs.zope.org/zope2/zope2book/ The Zope 2 Book]'''
 
: This book provides a general introduction to Zope concepts and ZMI. It is a good place to start, but doesn't provide a direct introduction to Zope development. It's recommended that you skim through this book to familiarize yourself with Zope. It generally does not assume much prior knowledge about Web development or Python.
 
; '''[http://docs.zope.org/zope2/zdgbook/ Zope Developer's Guide]'''
 
: This guide will give you a better introduction to Zope development. It assumes you already know Python. Skip chapters 1 and 2 and start in [http://docs.zope.org/zope2/zdgbook/ComponentsAndInterfaces.html chapter 3], which covers components and interfaces. [http://docs.zope.org/zope2/zdgbook/Products.html Chapter 5] covers the creation of your first product.
 
; '''[http://codespeak.net/z3/five/manual.html The Five Manual]'''
 
: We're not done yet. There is a bunch of stuff in Zope 2.13 that is not in the official documentation. Namely, the stuff in Five.
 
; '''[http://docs.zope.org/ztkpackages.html ZTK Documentation]'''
 
: ZTK 
 
; '''ZCA'''
 
: [http://www.muthukadan.net/docs/zca.html A Comprehensive Guide to Zope Component Architecture] offers a good introduction to the programming concepts of ZCA. We also have a new page on [[Zope Component Architecture]] which will help you to understand the big picture of ZCA and why it is useful. ZCML ("Z-camel") is a part of ZCA and  was introduced in Zope 3, so typically you will find ZCML documented within Zope 3 documentation and book.
 
; '''Content Components'''
 
: Views and Viewlets: [http://docs.zope.org/zope.viewlet/index.html This tutorial on viewlets] also contains some viewlet-related ZCML examples near the end. The "Content Component way" of developing in Zope seems to be a Zope 3 thing and tied to ZCML. Chapter 13+ of Stephan Richter's ''Zope 3 Developer's Handbook'' (book) seems to cover this quite well. You will probably also want to check out Philipp Weitershausen's ''Web Component Development with Zope 3'' (book).
 
; '''[http://wiki.zope.org/zope2/Zope2Wiki Zope 2 Wiki]'''
 
: Main wiki page for all things related to Zope 2.
 
; '''[http://docs.zope.org docs.zope.org]'''
 
: This is the main site for Zope documentation.
 
 
== First Steps ==
 
 
First, you will need to emerge {{Package|net-zope/zope}}:
 
 
<console>
 
<console>
###i## emerge zope
+
www@www-smw ~/public_html $ ##i##ls
 +
COPYING  LocalSettings.php    api.php  ##b##extensions##!b##    index.php  maintenance          redirect.php    skins              thumb_handler.php5
 +
CREDITS  README                api.php5  images        index.php5  mw-config            redirect.php5  tests              wiki.phtml
 +
FAQ      RELEASE-NOTES-1.19    bin      img_auth.php  languages  opensearch_desc.php  redirect.phtml  thumb.php
 +
HISTORY  StartProfiler.sample  cache    img_auth.php5  load.php    opensearch_desc.php5  resources      thumb.php5
 +
INSTALL  UPGRADE              docs      includes      load.php5  profileinfo.php      serialized      thumb_handler.php
 +
www@www-smw ~/public_html $ ##i##cd extensions/
 
</console>
 
</console>
 +
To install, make the following modifications to your skin:
  
Zope is now installed.
+
<syntaxhighlight lang="css">
 +
--- mediawiki-1.19.1/skins/vector/screen.css    2012-06-13 18:22:39.000000000 +0000
 +
+++ public_html/skins/vector/screen.css 2012-08-27 04:34:47.507912892 +0000
 +
@@ -683,10 +683,47 @@
 +
        list-style-image: url(images/bullet-icon.png);
 +
}
 +
 +
-pre {
 +
-      line-height: 1.3em;
 +
+/* ConsoleOutput.php start */
 +
+
 +
+.shell, pre, code, tt, div.mw-geshi {
 +
+        font-size: 12px;
 +
+        font-family: Consolas, 'andale mono','lucida console', monospace;
 +
+}
 +
+
 +
+.shell, pre, div.mw-geshi {
 +
+        background-color: #F8F8FF;
 +
+        line-height: 15px;
 +
+        padding: 10px;
 +
+        border: none;
 +
+        border-top: 2px solid #C6C9E0;
 +
+        border-bottom: 2px solid #C6C9E0;
 +
+        margin: 0;
 +
+        overflow-x: auto;
 +
+        overflow-y: hidden;
 +
+}
 +
+
 +
+.code {
 +
+        color: #666;
 +
+}
 +
+
 +
+.code_input {
 +
+        color: #000;
 +
}
 +
 +
+.code_red {
 +
+        color: #f00;
 +
+}
 +
+
 +
+.code_blue {
 +
+        color: #00f;
 +
+}
 +
+
 +
+.shell_green {
 +
+        color: #080;
 +
+}
 +
+
 +
+/* ConsoleOutput.php end */
 +
+
 +
/* Site Notice (includes notices from CentralNotice extension) */
 +
#siteNotice {
 +
        font-size: 0.8em;
 +
</syntaxhighlight>
  
== Project Skeleton ==
+
Then install the following code in your <tt>extensions</tt> directory and include it with a <tt>require_once( "$IP/extensions/ConsoleOutput.php" );</tt> in <tt>LocalSettings.php</tt>:
  
{{fancynote| Zope should be run by a regular user account, not as the root user.}}
+
<syntaxhighlight lang="php">
 +
<?php
 +
$wgExtensionCredits['validextensionclass'][] = array(
 +
    'name' => 'ConsoleOutput',
 +
    'author' => 'Daniel Robbins',
 +
    'url' => 'https://github.com/danielrobbins/mediawiki-consoleoutput',
 +
    'description' => 'This extension allows you to display colorized console output in mediawiki'
 +
);
  
The first step in using Zope is to ensure that you are using a regular user account. As a regular user, create a new directory called <tt>zope_test</tt>:
+
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
<console>
+
        $wgHooks['ParserFirstCallInit'][] = 'consoleOutputSetup';
$##i## cd
+
} else {
$##i## mkdir zope_test
+
        $wgExtensionFunctions[] = 'consoleOutputSetup';
</console>
+
}
  
Now, enter the directory, and create an "instance", which is a set of files and directories that are used to contain a Zope project:
+
function consoleOutputSetup( $data )
<console>
+
{
$##i## cd zope_test
+
    global $wgParser;
$##i## /usr/lib/zope-2.13/bin/mkzopeinstance
+
    $wgParser->setHook('console', 'consoleRender');
</console>
+
    return true;
 +
}
  
You will see the following output and will be prompted to answer a few questions:
+
function consoleRender($input, $args, $parser)
<console>
+
{
Please choose a directory in which you'd like to install
+
    if (count($args))
Zope "instance home" files such as database files, configuration
+
    {
files, etc.
+
        return "<strong class='error'>" .
 +
              "ConsoleOutput: arguments not supported" .
 +
              "</strong>";
 +
    }
  
Directory: instance
+
    # Display < and > as literals, so escape them:
Please choose a username and password for the initial user.
+
These will be the credentials you use to initially manage
+
your new Zope instance.
+
  
Username: admin
+
    $input = preg_replace('/>/','&gt;', $input);
Password: ****
+
    $input = preg_replace('/</','&lt;', $input);
Verify password: ****
+
</console>
+
  
Now, we will start our Zope instance:
+
    # http://www.perlmonks.org/?node_id=518444
<console>
+
    # See "Matching a pattern that doesn't include another pattern:
$##i## cd instance
+
$##i## bin/runzope
+
</console>
+
  
Now that Zope is functional, you can go to the <tt>localhost:8080/manage</tt> URL in your web browser: you will be prompted to log in. Enter the username and password you specified. You are now logged in to the ZMI (Zope Management Interface.)
+
    $input = preg_replace('/##i##((?:(?!##!i##).)*)##!i##/','<span class="code_input">$1</span>', $input);
 
+
    $input = preg_replace('/##i##(.*)/','<span class="code_input">$1</span>', $input);
You can stop your application by pressing Control-C. In the future, you can start and stop your Zope instance using the following commands:
+
    $input = preg_replace('/##b##((?:(?!##!b##).)*)##!b##/','<b>$1</b>', $input);
 
+
    $input = preg_replace('/##b##(.*)/','<b>$1</b>', $input);
<console>
+
    return "<pre class=\"code\">" . $input . "&lt;/pre>";
$##i## zopectl start
+
}
$##i## zopectl stop
+
?>
</console>
+
 
+
{{fancynote| <tt>zopectl start</tt> will cause your instance to run in the background rather than consuming a shell console.}}
+
 
+
== First Project ==
+
 
+
We will create a single, very primitive Zope package, consisting of an Interface for a TODO class, and a TODO class.
+
 
+
Create the following files and directories relative to your project root:
+
 
+
* Create the directory <tt>lib/python/example</tt>.
+
* Create the file <tt>lib/python/example/__init__.py</tt> by typing <tt>touch lib/python/example/__init__.py</tt>.
+
* Create these files:
+
 
+
=== <tt>example-configure.zcml</tt> ===
+
 
+
This file registers the <tt>example</tt> directory you created in <tt>lib/python</tt> as a ''package'', so that it is seen by Zope. Edit <code>/etc/package-includes/example-configure.zcml</code>:
+
 
+
<pre>
+
<include package="example" />
+
</pre>
+
 
+
=== <tt>interfaces.py</tt> ===
+
 
+
The following file defines the <tt>ITODO</tt> interface, and also uses some Zope Schema functions to define what kind of data we expect to store in objects that implement <tt>ITODO</tt>. Edit <code>/lib/python/example/interfaces.py</code> with your favorite text editor:
+
 
+
<syntaxhighlight lang="python">
+
from zope.interface import Interface
+
from zope.schema import List, Text, TextLine, Int
+
 
+
class ITODO(Interface):
+
    name = TextLine(title=u'Name', required=True)
+
    todo = List(title=u"TODO Items", required=True, value_type=TextLine(title=u'TODO'))
+
    daysleft = Int(title=u'Days left to complete', required=True)
+
    description = Text(title=u'Description', required=True)
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== <tt>TODO.py</tt> ===
+
[[Category:MediaWiki Hacks]]
 
+
Now, we define <tt>TODO</tt> to be a ''persistent'' object, meaning it can be stored in the ZODB. We specify that it implements our previously-defined <tt>ITODO</tt> interface, and provide reasonable defaults for all values when we create a new TODO object. Edit <code>/lib/python/example/TODO.py<code> using your favorite text editor:
+
<syntaxhighlight lang="python">
+
from persistent import Persistent
+
from zope.interface import implements
+
from example.interfaces import ITODO
+
 
+
class TODO(Persistent):
+
    implements(ITODO)
+
    name = u''
+
    todo = []
+
    daysleft = 0
+
    description = u''
+
</syntaxhighlight>
+
 
+
=== <tt>configure.zcml</tt> ===
+
 
+
Create the <tt>/lib/python/example/configure.zcml</tt> configuration file:
+
<syntaxhighlight lang="xml">
+
<configure xmlns="http://namespaces.zope.org/zope"
+
    xmlns:five="http://namespaces.zope.org/five"
+
    xmlns:browser="http://namespaces.zope.org/browser">
+
</configure>
+
</syntaxhighlight>
+
 
+
== Debug Mode ==
+
 
+
We can test our first project by entering debug mode:
+
<console>
+
$##i## bin/zopectl debug
+
Starting debugger (the name "app" is bound to the top-level Zope object)
+
</console>
+
 
+
Now, let's try creating a new TODO object and writing it out to a ZODB database:
+
<console>
+
>>> from ZODB import FileStorage, DB
+
>>> storage = FileStorage.FileStorage('mydatabase.fs')
+
>>> db = DB(storage)
+
>>> connection = db.open()
+
>>> import transaction
+
>>> root = connection.root()
+
>>> from example.TODO import TODO
+
>>> a = TODO
+
>>> a.name = u'My TODOs'
+
>>> a.TODOS = [ u'Do Laundry', u'Wash Dishes' ]
+
>>> a.daysleft = 1
+
>>> a.description = u'Things I need to do today.'
+
>>> root[u'today'] = a
+
>>> transaction.commit()
+
</console>
+
 
+
[[Category:HOWTO]]
+
[[Category:Featured]]
+

Revision as of 01:48, December 21, 2012

The ConsoleOutput MediaWiki extension was created by Daniel Robbins to provide highlighting of user input for interactive terminal session blocks. To use it, surround user input with <console> opening and closing tags, and put a ##i## input code immediately before user input on each line. This will cause all text from the ##i## point onwards to be in a darker color. The rest of the text will be in a lighter grey, allowing it to be distinguished more easily from user input. Here are other sequences that can be used:

  • ##b## - highlight the rest of the line in bold.
  • ##b## text here ##!b## - highlight the text between both markers in bold.
  • ##i## text here ##!i## - highlight the text between both markers in a darker color.

This extension can be further extended to add color support, as desired, and some of the CSS for color support can already be found in the code below.

Here is an example of ConsoleOutput output:

www@www-smw ~/public_html $ ls
COPYING  LocalSettings.php     api.php   extensions     index.php   maintenance           redirect.php    skins              thumb_handler.php5
CREDITS  README                api.php5  images         index.php5  mw-config             redirect.php5   tests              wiki.phtml
FAQ      RELEASE-NOTES-1.19    bin       img_auth.php   languages   opensearch_desc.php   redirect.phtml  thumb.php
HISTORY  StartProfiler.sample  cache     img_auth.php5  load.php    opensearch_desc.php5  resources       thumb.php5
INSTALL  UPGRADE               docs      includes       load.php5   profileinfo.php       serialized      thumb_handler.php
www@www-smw ~/public_html $ cd extensions/

To install, make the following modifications to your skin:

--- mediawiki-1.19.1/skins/vector/screen.css    2012-06-13 18:22:39.000000000 +0000
+++ public_html/skins/vector/screen.css 2012-08-27 04:34:47.507912892 +0000
@@ -683,10 +683,47 @@
        list-style-image: url(images/bullet-icon.png);
 }
 
-pre {
-       line-height: 1.3em;
+/* ConsoleOutput.php start */
+
+.shell, pre, code, tt, div.mw-geshi {
+        font-size: 12px;
+        font-family: Consolas, 'andale mono','lucida console', monospace;
+}
+
+.shell, pre, div.mw-geshi {
+        background-color: #F8F8FF;
+        line-height: 15px;
+        padding: 10px;
+        border: none;
+        border-top: 2px solid #C6C9E0;
+        border-bottom: 2px solid #C6C9E0;
+        margin: 0;
+        overflow-x: auto;
+        overflow-y: hidden;
+}
+
+.code {
+        color: #666;
+}
+
+.code_input {
+        color: #000;
 }
 
+.code_red {
+        color: #f00;
+}
+
+.code_blue {
+        color: #00f;
+}
+
+.shell_green {
+        color: #080;
+}
+
+/* ConsoleOutput.php end */
+
 /* Site Notice (includes notices from CentralNotice extension) */
 #siteNotice {
        font-size: 0.8em;

Then install the following code in your extensions directory and include it with a require_once( "$IP/extensions/ConsoleOutput.php" ); in LocalSettings.php:

<?php
$wgExtensionCredits['validextensionclass'][] = array(
    'name' => 'ConsoleOutput',
    'author' => 'Daniel Robbins',
    'url' => 'https://github.com/danielrobbins/mediawiki-consoleoutput',
    'description' => 'This extension allows you to display colorized console output in mediawiki'
);
 
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
        $wgHooks['ParserFirstCallInit'][] = 'consoleOutputSetup';
} else {
        $wgExtensionFunctions[] = 'consoleOutputSetup';
}
 
function consoleOutputSetup( $data )
{
    global $wgParser;
    $wgParser->setHook('console', 'consoleRender');
    return true;
}
 
function consoleRender($input, $args, $parser)
{
    if (count($args))
    {
        return "<strong class='error'>" .
               "ConsoleOutput: arguments not supported" .
               "</strong>";
    }
 
    # Display < and > as literals, so escape them:

    $input = preg_replace('/>/','&gt;', $input);
    $input = preg_replace('/</','&lt;', $input);
 
    # http://www.perlmonks.org/?node_id=518444
    # See "Matching a pattern that doesn't include another pattern:

    $input = preg_replace('/##i##((?:(?!##!i##).)*)##!i##/','<span class="code_input">$1</span>', $input);
    $input = preg_replace('/##i##(.*)/','<span class="code_input">$1</span>', $input);
    $input = preg_replace('/##b##((?:(?!##!b##).)*)##!b##/','<b>$1</b>', $input);
    $input = preg_replace('/##b##(.*)/','<b>$1</b>', $input);
    return "<pre class=\"code\">" . $input . "&lt;/pre>";
}
?>