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

02/18/06 22:02:22 (13 years ago)



  • WidgetPackages

    v2 v3  
    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" 
     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. 
     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: 
     33 * projname/ 
     34   * static/ 
     35     * css/ 
     36     * images/ 
     37     * javascript/ 
     38   * templates/ 
     39   * test/ 
     40 * projname.egg-info 
     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: 
     46import os.path 
     47import pkg_resources 
     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) 
     57(see http://peak.telecommunity.com/DevCenter/PkgResources for information on pkg_resources work) Now you can fetch resource files using your packages. 
     62l = CSSLink("foobar.css", "combo.css") 
     63js = JSLink("foobar.javascript", "foobar.js") 
     67You can also browse your package by going to the url localhost:8080/tg_widgets/foobar.javascript/.  
     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.''