Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 2 and Version 3 of WidgetPackages


Ignore:
Timestamp:
02/18/06 22:02:22 (13 years ago)
Author:
bjourne
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WidgetPackages

    v2 v3  
    2222 
    2323class ComboBox(Widget): 
    24     css = CSSLink(foobar, "combo.css") 
    25     javascript = [mochikit, JSLink(JSLink(foobar, "combo.js") 
    26     template = "foobar.widgets.templates.combo" 
     24    css = CSSLink("package.name", "combo.css") 
     25    javascript = [mochikit, JSLink(JSLink("package.name", "combo.js") 
     26    template = "package.name.templates.combo" 
    2727}}} 
    28      
     28 
     29Note that you do not retrieve resource files using a normal file path, instead you use TurboGears packaging system, which is an abstraction on top of a regular file system. Its usefulness comes from being able to specify an url by using   a package name plus a filename and not have to bother with where exactly the file is mounted on your web server. It can also be confusing because the file has three "locations" - the path on your disk, the mounted path on your web server and the name of the package it belongs to. 
     30 
     31In this example, the ComboBox widget requests the three files `combo.css`, `combo.js` and `combo.kid`. `package.name` is the name of the package where you have put your resource files. You must first create a package by registering it. Say that you have a normal directory tree for your project looking like this: 
     32 
     33 * projname/ 
     34   * static/ 
     35     * css/ 
     36     * images/ 
     37     * javascript/ 
     38   * templates/ 
     39   * test/ 
     40 * projname.egg-info 
     41      
     42Lets also say that you want to package the stuff inside the static directory. One way to do it would be to make three packages: `foobar.css` for the stylesheets, `foobar.javascript` for the javascripts and `foobar.images` for the images. You accomplish that by first obtaining a filename for a resource and then registering it as a directory. Write this in your `controllers.py` file: 
     43 
     44{{{ 
     45#!python 
     46import os.path 
     47import pkg_resources 
     48 
     49pkg_path = pkg_resources.resource_filename(__name__, os.path.join("static", "javascript")) 
     50register_static_directory("foobar.javascript", pkg_path) 
     51pkg_path = pkg_resources.resource_filename(__name__, os.path.join("static", "css")) 
     52register_static_directory("foobar.css", pkg_path) 
     53pkg_path = pkg_resources.resource_filename(__name__, os.path.join("static", "images")) 
     54register_static_directory("foobar.images", pkg_path) 
     55}}} 
     56 
     57(see http://peak.telecommunity.com/DevCenter/PkgResources for information on pkg_resources work) Now you can fetch resource files using your packages. 
     58 
     59{{{ 
     60#!python 
     61 
     62l = CSSLink("foobar.css", "combo.css") 
     63js = JSLink("foobar.javascript", "foobar.js") 
     64 
     65}}} 
     66 
     67You can also browse your package by going to the url localhost:8080/tg_widgets/foobar.javascript/.  
     68 
     69''pkg_resources probably also contain heaps more useful functionality and it would be good if someone who knows about can write it down here.''