~ryan-c-ahearn/backintime/command_line_interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#    Back In Time
#    Copyright (C) 2010 Ryan Ahearn
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import os.path
import shutil
import urwid
import gettext

import logger
import restorewidget

_=gettext.gettext

class CopyWidget(urwid.WidgetWrap):

    def __init__( self, main_win, config, to_copy ):
        self._main_win = main_win
        self._config = config
        self._to_copy = to_copy

        self._edit_box = urwid.Edit(( 'body', _('Enter destination') + ': ' ))
        pile_list = [
            urwid.AttrWrap( self._edit_box, 'edit_box', 'edit_focus' ),
            urwid.Divider(),
            urwid.GridFlow( [
                urwid.AttrWrap( urwid.Button( _('OK'), self.ok_pressed ), 'button', 'button_focus' ),
                urwid.AttrWrap( urwid.Button( _('Cancel'), self.go_home ), 'button', 'button_focus' )
                ], 10, 2, 1, 'right' )
            ]

        display_widget = urwid.Pile( pile_list )
        urwid.WidgetWrap.__init__( self, display_widget )

    def ok_pressed( self, button ):
	destination = self._edit_box.get_edit_text().strip()
        logger.info( 'copying %s to %s' % ( self._to_copy, destination ) )
        if os.path.isdir( self._to_copy ):
            self._copy_directory( destination )
        else:
            self._copy_file( destination )
        self.go_home()

    def _copy_directory( self, destination ):
        while os.path.exists( destination ):
            destination = os.path.join( destination, os.path.basename( self._to_copy ) )
        shutil.copytree( self._to_copy, destination, True )

    def _copy_file( self, destination ):
        shutil.copy2( self._to_copy, destination )

    def go_home( self, button=None ):
        self._main_win.update_content( restorewidget.RestoreWidget( self._main_win, self._config ), 5 )