You are not logged in Log in Join
You are here: Home » Members » Jfarr's Zope Page » HowTo » Using DTML With Local File System Objects

Log in
Name

Password

 

Using DTML With Local File System Objects

This how-to explains how to perform common DTML operations using LocalFS objects.

Accessing Local File Objects

The first thing to understand about working with Local File System objects from DTML is how to access sub-objects. Access to child objects uses the dictionary lookup syntax, rather than attribute access like most Zope container objects.

Examples

Suppose you create a LocalFS object called 'test' which points to the directory '/usr/local/zope/test'. If you wanted to access a file called 'foo.txt' in that directory from DTML, this is the correct syntax.

    <dtml-var "test['foo.txt']">

If you the file you wanted were in a directory called 'sub1', this would be the correct syntax.

    <dtml-var "test['sub1']['foo.txt']">

The reason for this syntax is mainly because filenames can contain periods, so a syntax like 'test.sub1.foo.txt' would make no sense. There would be no way to determine whether the object being requested was a file called 'txt' in a directory called 'sub1.foo' or a file called 'foo.txt' in a directory called 'sub1', etc.

Accessing Local File Contents

It is also possible to access the contents of files stored in the local file system from DTML.

Examples

In the previous example, if you wanted to access the contents of the file 'foo.txt', instead of just the file object, you would use the same syntax to refer to the object, then just refer to the 'data' attribute.

    <dtml-var "test['foo.txt'].data">

The object actually returned by LocalFS is a Zope File object, so you use the 'data' attribute to access the data contained in the file.

The exception is when the file ends in the extension '.dtml', in which case the object returned by LocalFS is a DTMLMethod object. To access the raw (unprocessed) text of the file, you would use:

    <dtml-var "test['foo.dtml'].read()">

To insert the text of the file after evaluating it as a DTML template in the current namespace, you would use:

    <dtml-var "test['foo.dtml'](_.None, _)">

You can also specify additional values to the DTML method as keyword arguments, just like any other DTML method call.

Iterating Local Directory Contents

Another common operation on a local file system object is iterating through the contents of a directory. LocalFS represents local directories with a LocalDirectory object, which provides methods for doing just that. These methods are called fileIds, fileValues, and fileItems. They work similarly to the objectIds, objectValues, and objectItems methods of a Folder object. The difference is that fileValues and fileItems return LocalFile objects which are simply pointers to the file objects rather than the actual Zope objects.

Examples

Using the same LocalFS object from the previous example, to print the names of all the files in that directory this is the correct DTML.

    <dtml-in "test.fileIds()">
        <dtml-var sequence-item><br>
    </dtml-in>

Suppose you wanted to print the names of all the files in a subdirectory called 'sub1'. Here's how you would do that.

    <dtml-in "test['sub1'].fileIds()">
        <dtml-var sequence-item><br>
    </dtml-in>

The LocalFile object also provides various meta-information about the local file, so for example, to add the last modified time of the files to the output, you would use DTML like the following.

    <dtml-in "test.fileValues()">
        <dtml-var id> <dtml-var mtime><br>
    </dtml-in>
Refer to the product documentation for more information about LocalFS objects and their attributes.