src/extras/dir_iter.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 # Copyright (c) 2005-2009 Nokia Corporation
       
     2 #
       
     3 # Licensed under the Apache License, Version 2.0 (the "License");
       
     4 # you may not use this file except in compliance with the License.
       
     5 # You may obtain a copy of the License at
       
     6 #
       
     7 #     http://www.apache.org/licenses/LICENSE-2.0
       
     8 #
       
     9 # Unless required by applicable law or agreed to in writing, software
       
    10 # distributed under the License is distributed on an "AS IS" BASIS,
       
    11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    12 # See the License for the specific language governing permissions and
       
    13 # limitations under the License.
       
    14 #
       
    15 
       
    16 #
       
    17 # dir_iter.py
       
    18 #
       
    19 # Utility module for filebrowser.
       
    20 #
       
    21 
       
    22 import sys
       
    23 import os
       
    24 import time
       
    25 
       
    26 
       
    27 class Directory_iter:
       
    28 
       
    29     def __init__(self, drive_list):
       
    30         self.drives = [((i, u"Drive")) for i in drive_list]
       
    31         self.at_root = 1
       
    32         self.path = '\\'
       
    33 
       
    34     def pop(self):
       
    35         if os.path.splitdrive(self.path)[1] == '\\':
       
    36             self.path = '\\'
       
    37             self.at_root = 1
       
    38         else:
       
    39             self.path = os.path.split(self.path)[0]
       
    40 
       
    41     def add(self, i):
       
    42         if self.at_root:
       
    43             self.path = str(self.drives[i][0] + u'\\')
       
    44             self.at_root = 0
       
    45         else:
       
    46             self.path = os.path.join(self.path, os.listdir(self.name())[i])
       
    47 
       
    48     def name(self):
       
    49         return self.path
       
    50 
       
    51     def list_repr(self):
       
    52         if self.at_root:
       
    53             return self.drives
       
    54         else:
       
    55 
       
    56             def item_format(i):
       
    57                 full_name = os.path.join(str(self.name()), i)
       
    58                 try:
       
    59                     time_field = time.strftime("%d.%m.%Y %H:%M",\
       
    60                                  time.localtime(os.stat(full_name).st_mtime))
       
    61                     info_field = time_field + "  " + \
       
    62                                  str(os.stat(full_name).st_size) + "b"
       
    63                     if os.path.isdir(full_name):
       
    64                         name_field = "["+i+"]"
       
    65                     else:
       
    66                         name_field = i
       
    67                 except:
       
    68                     info_field = "[inaccessible]"
       
    69                     name_field = "["+i+"]"
       
    70                 return (unicode(name_field), unicode(info_field))
       
    71             try:
       
    72                 l = map(item_format, os.listdir(self.name()))
       
    73             except:
       
    74                 l = []
       
    75             return l
       
    76 
       
    77     def entry(self, i):
       
    78         return os.path.join(self.name(), os.listdir(self.name())[i])