set_sensitive() on Back and Forward when needed
[pysize.git] / pysize / core / chdir_browsing.py
blobe5da0a9a3f85ca039d921be736e9929d29f70103
1 import os
3 # We cannot use a generator because we rely on the finally block to
4 # be sure finalize() will be called. Python 2.5 allows finally block
5 # in generator, but it relies on the GC so it's not sure it will be
6 # called at the right time.
8 # The expected use of these browsing functions is:
10 # cookie = chdir_browsing.init(path)
11 # try:
12 # for child in chdir_browsing.listdir(cookie):
13 # .. Do something with child ..
14 # finally:
15 # chdir_browsing.finalize(cookie)
17 def init(path):
18 previous_cwd = os.open('.', os.O_RDONLY)
19 try:
20 os.chdir(path)
21 return previous_cwd
22 except OSError, e:
23 os.close(previous_cwd)
24 raise e
26 def listdir(unused_cookie):
27 res = os.listdir('.')
28 # We want the order to be always so same so that hard links
29 # detection will be consistent across filesystems
30 res.sort()
31 return res
33 def finalize(previous_cwd):
34 try:
35 os.fchdir(previous_cwd)
36 finally:
37 os.close(previous_cwd)