1 """Utilities to perform various operations on paths, with support for
2 opening the ROX filer to examine locations if things go wrong.
7 rox.fileutils.makedirs('/path/for/new/directory')
13 def report_patherror(message
, path
):
14 """Display a <Cancel>/<Retry>/<Examine> dialog.
15 This will raise an OSError exception if the user selects Cancel, or
16 will return successfully if the user chooses to retry."""
17 from rox
import g
, filer
, toplevel_ref
, toplevel_unref
, ButtonMixed
19 box
= g
.MessageDialog(None, 0, g
.MESSAGE_QUESTION
,
20 g
.BUTTONS_CANCEL
, message
)
22 button
= ButtonMixed(g
.STOCK_REDO
, _('Retry'))
23 button
.set_flags(g
.CAN_DEFAULT
)
25 box
.add_action_widget(button
, g
.RESPONSE_OK
)
27 button
= ButtonMixed(g
.STOCK_JUMP_TO
, _('Examine'))
28 button
.set_flags(g
.CAN_DEFAULT
)
30 box
.add_action_widget(button
, g
.RESPONSE_APPLY
)
32 box
.set_position(g
.WIN_POS_CENTER
)
33 box
.set_title(_('Error:'))
34 box
.set_default_response(g
.RESPONSE_APPLY
)
37 if resp
!= g
.RESPONSE_APPLY
: break
38 filerpath
= os
.path
.normpath(path
)
39 filer
.show_file(filerpath
)
42 if resp
!= g
.RESPONSE_OK
:
43 raise OSError, message
45 def _makedirs_recursive(path
, mode
):
46 """Recursive part of makedirs. Calls itself to ensure head
47 of path exists, and then makes a new directory at path. Returns
48 an Exception if it can't make a directory at path."""
49 if os
.path
.isdir(path
): return
50 head
, tail
= os
.path
.split(path
)
52 head
, tail
= os
.path
.split(head
)
54 _makedirs_recursive(head
, mode
)
57 if os
.path
.exists(path
):
59 _("Could not create directory `%s' because a file already exists at that path.\n") % path
, path
)
65 report_patherror(("%s.\n" + _("Could not create directory `%s'.\n")) \
66 % (msg
[1], path
), path
)
68 def makedirs(path
, mode
=0777):
69 """Make a directory at the specified path, creating intermediate
70 directories if necessary. No error if 'path' is already a directory.
72 On error, a dialog which allows the user to open the filer to fix
73 things and then retry will be opened.
75 Returns successfully if all directories get created (or already exist),
76 Raises an OSError if there is a problem, in which case the application
77 should not open a dialog box to inform the user, since one will already
80 # Get rid of any ..'s in the path
81 path
= os
.path
.normpath(path
)
82 _makedirs_recursive(path
, mode
)