degal/filesystem.py
author Tero Marttila <terom@fixme.fi>
Fri, 05 Jun 2009 23:42:03 +0300
changeset 76 e22d9f699081
parent 68 49388e9fd5fa
child 77 2a53c5ade434
permissions -rw-r--r--
misc. fixes
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Filesystem path handling
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
     5
import os, os.path, errno
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
     6
import codecs, shutil
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
     7
import itertools
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
     8
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
     9
from utils import lazy_load
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
class Node (object) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
        A filesystem object is basically just complicated representation of a path.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
        
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
        On the plus side, it has a parent node and can handle unicode/binary paths.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    # the binary name
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    fsname = None
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    # the unicode name
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    name = None
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    def decode_fsname (self, fsname) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
            Decode the given raw byte string representing a filesystem name into an user-readable unicode name.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
            XXX: currently just hardcoded as utf-8
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
        return fsname.decode('utf-8', 'replace')
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    def encode_name (self, name) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
            Returns a suitable fsname for the given unicode name or strict ASCII str
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
            XXX: currently just hardcoded as utf-8
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        # this should fail for non-ASCII str
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        return name.encode('utf-8')
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
    @classmethod
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    def from_node (cls, node) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
            Construct from a Node object
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        return cls(node.parent, node.fsname, node.name, node.config)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
    def __init__ (self, parent, fsname=None, name=None, config=None) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
            Initialize the node with a parent and both name/fsname.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
            If not given, fsname is encoded from name, or name decoded from fsname, using encode/decode_name.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
            If parent is given, but both fsname and name are None, then this node will be equal to the parent.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        assert not fsname or isinstance(fsname, str)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
        if parent and not fsname and not name :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
            # no name given -> we're the same as parent
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
            self.parent, self.config, self.fsname, self.name = parent.parent, parent.config, parent.fsname, parent.name
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        else :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
            # store
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
            self.parent = parent
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
            
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
            # config, either as given, or copy from parent
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
            if config :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
                self.config = config
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    73
            
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    74
            elif parent : # XXX: else :
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
                self.config = parent.config
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
     
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
            # fsname
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
            if fsname :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
                self.fsname = fsname
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
            else :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
                self.fsname = self.encode_name(name)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
           
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
            # name
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
            if name :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
                self.name = name
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
            else :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
                self.name = self.decode_fsname(fsname)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
        
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    def subnode (self, name) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
            Returns a Node object representing the given name behind this node.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
            The name should either be a plain ASCII string or unicode object.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
        return Node(self, name=name)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
 
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   100
    def nodepath (self) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   101
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   102
            Returns the path of nodes from this node to the root node, inclusive
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   103
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   104
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   105
        return self.parent.nodepath() + [self]
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   106
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   107
    @lazy_load
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    def path (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
        """
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   110
            Return the machine-readable root-path for this node
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
        
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
        # build using parent path and our fsname
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
        return os.path.join(self.parent.path, self.fsname)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
    
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   116
    @lazy_load
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
    def unicodepath (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
        """
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   119
            Return the human-readable root-path for this node
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
        
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
        # build using parent unicodepath and our name
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
        return os.path.join(self.parent.path, self.name)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
   
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   125
    def path_segments (self, unicode=True) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   126
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   127
            Return a series of single-level names describing the path from the root to this node
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   128
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   129
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   130
        return self.parent.path_segments(unicode=unicode) + [self.name if unicode else self.fsname]
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   131
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
    def exists (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
            Tests if this node exists on the physical filesystem
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
        return os.path.exists(self.path)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
    def is_dir (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
            Tests if this node represents a directory on the filesystem
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
        return os.path.isdir(self.path)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
    def is_file (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
            Tests if this node represents a normal file on the filesystem
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
        return os.path.isfile(self.path)
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   152
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   153
    def test (self) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   154
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   155
            Tests that this node exists. Raises an error it not, otherwise, returns the node itself
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   156
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   157
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   158
        if not self.exists() :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   159
            raise Exception("Filesystem node does not exist: %s" % self)
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   160
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   161
        return self
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
    
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   163
    def path_from (self, node) :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   164
        """
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   165
            Returns a relative path to this node from the given node.
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   166
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   167
            This is the same as path_to, but just reversed.
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   168
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   169
        
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   170
        return node.path_to(self)
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   171
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   172
    def path_to (self, node) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   173
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   174
            Returns a relative path from this node to the given node
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   175
        """
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   176
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   177
        # get real paths for both
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   178
        from_path = self.nodepath()
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   179
        to_path = node.nodepath()
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   180
        pivot = None
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   181
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   182
        # reduce common prefix
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   183
        while from_path[0] == to_path[0] :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   184
            from_path.pop(0)
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   185
            pivot = to_path.pop(0)
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   186
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   187
        # build path
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   188
        return Path(*itertools.chain(reversed(from_path), [pivot], to_path))
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   189
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   190
    def stat (self, soft=False) :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   191
        """
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   192
            Returns the os.stat struct for this node.
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   193
            
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   194
            If `soft` is given, returns None if this node doesn't exist
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   195
        """
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   196
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   197
        try :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   198
            return os.stat(self.path)
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   199
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   200
        except OSError, e :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   201
            # trap ENOENT for soft
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   202
            if soft and e.errno == errno.ENOENT :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   203
                return None
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   204
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   205
            else :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   206
                raise
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   207
    
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   208
    # alias str/unicode
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   209
    __str__ = path
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   210
    __unicode__ = unicodepath
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
    
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
    def __repr__ (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   213
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   214
            Returns a str representing this dir
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   215
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
        return "Node(%r, %r)" % (self.parent.path, self.fsname)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   218
    
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   219
    def __cmp__ (self, other) :
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   220
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   221
            Comparisons between Nodes
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   222
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   223
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   224
        return cmp((self.parent, self.name), (other.parent if self.parent else None, other.name))
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   225
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   226
class Path (object) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   227
    """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   228
        A Path is a sequence of Nodes that form a path through the node tree.
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   229
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   230
        Each node must either be the parent or the child of the following node.
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   231
    """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   232
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   233
    def __init__ (self, *nodes) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   234
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   235
            Initialize with the given node path
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   236
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   237
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   238
        self.nodes = nodes
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   239
    
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   240
    def subpath (self, *nodes) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   241
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   242
            Returns a new path with the given node(s) appended
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   243
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   244
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   245
        return Path(*itertools.chain(self.nodes, nodes))
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   246
    
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   247
    def path_segments (self, unicode=True) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   248
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   249
            Yields a series of physical path segments for this path
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   250
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   251
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   252
        prev = None
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   253
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   254
        for node in self.nodes :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   255
            if not prev :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   256
                # ignore
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   257
                pass
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   258
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   259
            elif prev.parent and prev.parent == node :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   260
                # up a level
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   261
                yield u'..' if unicode else '..'
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   262
            
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   263
            else :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   264
                # down a level
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   265
                yield node.name if unicode else node.fsname
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   266
            
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   267
            # chained together
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   268
            prev = node
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   269
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   270
    def __iter__ (self) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   271
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   272
            Iterate over the nodes
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   273
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   274
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   275
        return iter(self.nodes)
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   276
    
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   277
    def __unicode__ (self) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   278
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   279
            Returns the unicode human-readable path
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   280
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   281
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   282
        return os.path.join(*self.path_segments(unicode=True))
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   283
    
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   284
    def __str__ (self) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   285
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   286
            Returns the binary machine-readable path
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   287
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   288
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   289
        return os.path.join(*self.path_segments(unicode=False))
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   290
    
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   291
    def __repr__ (self) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   292
        return "Path(%s)" % ', '.join(repr(segment) for segment in self.path_segments(unicode=False))
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   293
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   294
class File (Node) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   295
    """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   296
        A file. Simple, eh?
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   297
    """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   298
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   299
    @property
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   300
    def basename (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   301
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   302
            Returns the "base" part of this file's name, i.e. the filename without the extension
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   303
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   304
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   305
        basename, _ = os.path.splitext(self.name)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   306
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   307
        return basename
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   308
    
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   309
    @property
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   310
    def fileext (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   311
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   312
            Returns the file extension part of the file's name, without any leading dot
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   313
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   314
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   315
        _, fileext = os.path.splitext(self.name)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   316
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   317
        return fileext.rstrip('.')
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   318
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   319
    def matchext (self, ext_list) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   320
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   321
            Tests if this file's extension is part of the recognized list of extensions
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   322
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   323
        
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   324
        return (self.fileext.lower() in ext_list)
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   325
    
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   326
    def test (self) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   327
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   328
            Tests that this file exists as a file. Raises an error it not, otherwise, returns itself
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   329
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   330
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   331
        if not self.is_file() :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   332
            raise Exception("File does not exist: %s" % self)
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   333
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   334
        return self
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   335
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   336
    def open (self, mode='r', encoding=None, errors=None, bufsize=None) :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   337
        """
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   338
            Wrapper for open/codecs.open.
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   339
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   340
            Raises an error if read_only mode is set and mode contains any of 'wa+'
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   341
        """
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   342
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   343
        if self.config.read_only and any((c in mode) for c in 'wa+') :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   344
            raise Exception("Unable to open file for %s due to read_only mode: %s" % (mode, self))
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   345
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   346
        if encoding :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   347
            return codecs.open(self.path, mode, encoding, errors, bufsize)
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   348
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   349
        else :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   350
            return open(self.path, mode, bufsize)
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   351
68
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   352
    def open_write (self, encoding=None, errors=None, bufsize=None) :
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   353
        """
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   354
            Open for write using open('w').
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   355
        """
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   356
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   357
        return self.open('w', encoding, errors, bufsize)
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   358
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   359
    def copy_from (self, file) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   360
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   361
            Replace this file with a copy of the given file with default permissions.
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   362
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   363
            Raises an error if read_only mode is set.
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   364
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   365
            XXX: accept mode
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   366
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   367
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   368
        if self.config.read_only :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   369
            raise Exception("Not copying file as read_only mode is set: %s -> %s" % (file, self))
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   370
        
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   371
        # perform the copy
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   372
        shutil.copyfile(file.path, self.path)
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   373
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   374
class Directory (Node) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   375
    """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   376
        A directory is a node that contains other nodes.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   377
    """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   378
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   379
    # a list of (test_func, node_type) tuples for use by children() to build subnodes with
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   380
    NODE_TYPES = None
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   381
   
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   382
    def subdir (self, name, create=False) :
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   383
        """
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   384
            Returns a Directory object representing the name underneath this dir.
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   385
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   386
            If the create option is given, the directory will be created if it does not exist. Note that this will
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   387
            raise an error if read_only mode is set
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   388
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   389
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   390
        subdir = Directory(self, name=name)
68
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   391
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   392
        if create and not subdir.is_dir() :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   393
            # create it!
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   394
            subdir.mkdir()
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   395
68
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   396
        return dir
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   397
    
68
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   398
    def test_subdir (self, name, create=False) :
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   399
        """
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   400
            Test for the presence of a subdir with the given name, and possibly return it, or None.
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   401
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   402
            Returns a (exists, created, dir) tuple.
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   403
            
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   404
            XXX: ugly, not used
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   405
        """
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   406
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   407
        subdir = Directory(self, name=name)
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   408
        
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   409
        # already exists?
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   410
        if subdir.is_dir() :
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   411
            if create :
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   412
                # create it!
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   413
                subdir.mkdir()
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   414
                
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   415
                # didn't exist, did create
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   416
                return True, True, subdir
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   417
            
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   418
            else :
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   419
                # doesn't exist, don't create
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   420
                return False, False, subdir
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   421
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   422
        else :
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   423
            # already existing
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   424
            return True, False, subdir
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   425
49388e9fd5fa add open_write method for filesystem.File
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   426
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   427
    def subfile (self, name) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   428
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   429
            Returns a File object representing the name underneath this dir
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   430
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   431
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   432
        return Directory(self, name=name)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   433
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   434
    def test (self) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   435
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   436
            Tests that this dir exists as a dir. Raises an error it not, otherwise, returns itself
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   437
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   438
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   439
        if not self.is_dir() :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   440
            raise Exception("Directory does not exist: %s" % self)
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   441
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   442
        return self
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   443
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   444
    def mkdir (self) :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   445
        """
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   446
            Create this directory with default permissions.
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   447
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   448
            This will fail if read_only mode is set
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   449
            
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   450
            XXX: mode argument
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   451
        """
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   452
        
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   453
        if self.config.read_only :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   454
            # forbidden
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   455
            raise Exception("Unable to create dir due to read_only mode: %s" % self)
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   456
        
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   457
        # do it
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   458
        os.mkdir(self.path)
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   459
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   460
    def listdir (self, skip_dotfiles=True) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   461
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   462
            Yield a series of raw fsnames for nodes in this dir
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   463
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   464
        
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   465
        # expressed 
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   466
        return (fsname for fsname in os.listdir(self.path) if not (skip_dotfiles and fsname.startswith('.')))
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   467
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   468
    def subnodes (self, skip_dotfiles=True, sort=True) :
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   469
        """
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   470
            Yield a series of Nodes contained in this dir.
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   471
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   472
            If skip_dotfiles is given, nodes that begin with a . are omitted.
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   473
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   474
            If `sort` is given, the returned nodes will be in sorted order.
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   475
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   476
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   477
        iter = (Node(self, fsname) for fsname in self.listdir(skip_dotfiles))
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   478
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   479
        if sort :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   480
            return sorted(iter)
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   481
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   482
        else :
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   483
            return iter
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   484
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   485
    def __iter__ (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   486
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   487
            Iterating over a Directory yields sub-Nodes.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   488
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   489
            Dotfiles are skipped.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   490
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   491
        
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   492
        return self.subnodes()
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   493
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   494
    @property
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   495
    def root_path (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   496
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   497
            Build and return a relative path to the root of this dir tree
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   498
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   499
            XXX: move to node
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   500
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   501
        
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   502
        # build using parent root_path
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   503
        return os.path.join('..', self.parent.root_path)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   504
 
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   505
    def children (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   506
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   507
            Yield a series of Node subclasses representing the items in this dir.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   508
            
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   509
            This uses self.NODE_TYPES to figure out what kind of sub-node object to build. This should be a list of
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   510
                (test_func, node_type)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   511
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   512
            tuples, of which the first is a function that takes a Node as it's sole argument, and returns a boolean.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   513
            For the first test_func which returns True, a Node-subclass object is constructed using node_type.from_node.
55
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   514
77abe8dca695 further work on filesystem.py (path_from proto, stat, open, subdir-create, mkdir, subnodes
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   515
            XXX: never used
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   516
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   517
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   518
        for node in self :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   519
            # figure out what type to use
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   520
            for test_func, node_type in self.NODE_TYPES :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   521
                if test_func(node) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   522
                    # matches, build
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   523
                    yield node_type.from_node(node)
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   524
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   525
            else :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   526
                # unknown file type!
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   527
                raise Exception("unrecongized type of file: %s" % node);
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   528
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   529
# assign default Directory.NODE_TYPES
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   530
Directory.NODE_TYPES = [
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   531
    (Node.is_dir,   Directory),
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   532
    (Node.is_file,  File),
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   533
]
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   534
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   535
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   536
class Root (Directory) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   537
    """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   538
        A special Directory that overrides the Node methods to anchor the recursion/etc at some 'real' filesystem path.
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   539
    """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   540
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   541
    # XXX: config needs a default
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   542
    def __init__ (self, fspath, config=None) :
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   543
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   544
            Construct the directory tree root at the given 'real' path, which must be a raw str
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   545
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   546
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   547
        # abuse Node's concept of a "name" a bit
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   548
        super(Root, self).__init__(None, fspath)
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   549
        
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   550
        # store our config
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   551
        self.config = config
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   552
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   553
    def nodepath (self) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   554
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   555
            Just return ourself
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   556
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   557
        
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   558
        return [self]
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   559
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   560
    @property
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   561
    def path (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   562
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   563
            Returns the raw path
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   564
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   565
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   566
        return self.fsname
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   567
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   568
    @property
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   569
    def unicodepath (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   570
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   571
            Returns the raw decoded path
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   572
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   573
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   574
        return self.name
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   575
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   576
    @property
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   577
    def root_path (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   578
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   579
            Returns an empty string representing this dir
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   580
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   581
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   582
        return ''
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   583
60
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   584
    def path_segments (self, unicode=True) :
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   585
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   586
            No path segments
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   587
        """
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   588
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   589
        return []
406da27a4be2 do some filesystem.Path stuff, and read_only mode
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   590
51
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   591
    def __repr__ (self) :
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   592
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   593
            Override Node.__repr__ to not use self.parent.path
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   594
        """
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   595
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   596
        return "Root(%r)" % self.fsname
0f39cb5e4b11 start writing new structure, with config, render, filesystem modules
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   597