File: initialize.py

package info (click to toggle)
bzr-gtk 0.103.0+bzr792-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,524 kB
  • sloc: python: 9,616; makefile: 16; sh: 10
file content (120 lines) | stat: -rw-r--r-- 4,767 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (C) 2007 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from gi.repository import GObject
from gi.repository import Gtk

import os

from bzrlib import (
    bzrdir,
    errors,
    transport,
    )

from bzrlib.plugins.gtk.dialog import error_dialog
from bzrlib.plugins.gtk.errors import show_bzr_error
from bzrlib.plugins.gtk.i18n import _i18n


class InitDialog(Gtk.Dialog):
    """ Initialize dialog. """

    def __init__(self, path, parent=None):
        """ Initialize the Initialize dialog. """
        super(InitDialog, self).__init__(
            title="Initialize - Olive", parent=parent, flags=0,
            buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL))
        
        # Get arguments
        self.path = path
        
        # Create the widgets
        self._button_init = Gtk.Button(_i18n("_Initialize"), use_underline=True)
        self._label_question = Gtk.Label(label=_i18n("Which directory do you want to initialize?"))
        self._radio_current = Gtk.RadioButton.new_with_label(
            None, _i18n("Current directory"))
        self._radio_custom = Gtk.RadioButton.new_with_label_from_widget(
            self._radio_current, _i18n("Create a new directory with the name:"))
        self._entry_custom = Gtk.Entry()
        self._hbox_custom = Gtk.HBox()
        
        # Set callbacks
        self._button_init.connect('clicked', self._on_init_clicked)
        self._radio_custom.connect('toggled', self._on_custom_toggled)
        
        # Set properties
        self._entry_custom.set_sensitive(False)
        
        # Construct the dialog
        self.action_area.pack_end(self._button_init, False, False, 0)
        
        self._hbox_custom.pack_start(self._radio_custom, False, False, 0)
        self._hbox_custom.pack_start(self._entry_custom, True, True, 0)
        
        content_area = self.get_content_area()
        content_area.pack_start(self._label_question, True, True, 0)
        content_area.pack_start(self._radio_current, True, True, 0)
        content_area.pack_start(self._hbox_custom, True, True, 0)
        
        # Display the dialog
        content_area.show_all()
    
    def _on_custom_toggled(self, widget):
        """ Occurs if the Custom radiobutton is toggled. """
        if self._radio_custom.get_active():
            self._entry_custom.set_sensitive(True)
            self._entry_custom.grab_focus()
        else:
            self._entry_custom.set_sensitive(False)
    
    @show_bzr_error
    def _on_init_clicked(self, widget):
        if self._radio_custom.get_active() and len(self._entry_custom.get_text()) == 0:
            error_dialog(_i18n("Directory name not specified"),
                         _i18n("You should specify a new directory name."))
            return
        
        if self._radio_current.get_active():
            location = self.path
        else:
            location = self.path + os.sep + self._entry_custom.get_text()
        
        format = bzrdir.format_registry.make_bzrdir('default')        
        to_transport = transport.get_transport(location)
        
        try:
            to_transport.mkdir('.')
        except errors.FileExists:
            pass
                    
        try:
            existing_bzrdir = bzrdir.BzrDir.open(location)
        except errors.NotBranchError:
            branch = bzrdir.BzrDir.create_branch_convenience(to_transport.base,
                                                             format=format)
        else:
            from bzrlib.transport.local import LocalTransport
            if existing_bzrdir.has_branch():
                if (isinstance(to_transport, LocalTransport)
                    and not existing_bzrdir.has_workingtree()):
                        raise errors.BranchExistsWithoutWorkingTree(location)
                raise errors.AlreadyBranchError(location)
            else:
                branch = existing_bzrdir.create_branch()
                existing_bzrdir.create_workingtree()
        
        self.response(Gtk.ResponseType.OK)