_new_tree_callback is not specific to drawing
[pysize.git] / core / chdir_browsing.py
blob7f54e14dfdc305a98f98401ad727e95d5d6fc63b
1 import os
3 # We cannot use a generator because we rely on the finally block to
4 # be sure _finalize_chdir_browsing will be called. Python 2.5 allows
5 # finally block in generator, but it relies on the GC so it's not sure
6 # it will be called at the right time.
8 # The expected use of these browsing functions is:
10 # previous_cwd = chdir_browsing.init(path)
11 # try:
12 # for child in os.listdir('.'):
13 # .. Do something with child ..
14 # finally:
15 # chdir_browsing.finalize(previous_cwd)
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 finalize(previous_cwd):
27 try:
28 os.fchdir(previous_cwd)
29 finally:
30 os.close(previous_cwd)