hgconfig /hgconf/uname_gtk.py

Language Python Lines 140
MD5 Hash b70dac751ee5255876a3b6137d64721f Estimated Cost $2,611 (why?)
Repository https://bitbucket.org/sborho/hgconfig View Raw File View Project SPDX
  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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Username dialog (Gtk)

from mercurial import util
import iniparse
import os, os.path, socket
import sys

try:
    import pygtk
    import gtk
    import gtk.glade
    pygtk.require("2.0")
except:
    sys.exit(1)

class UsernameDialogGtk:
    def __init__(self, hgui, repo, tolocal):
        self.hgui = hgui
        self.repo = repo

        #Set the Glade file
        p = os.path.dirname(os.path.abspath(__file__))
        self.gladefile = os.sep.join([p, "uname_dialog.glade"])
        if not os.path.exists(self.gladefile): # MSI path layout
            self.gladefile = os.sep.join([p, '..', '..', '..',
                'share', 'hgconf', 'uname_dialog.glade'])
        self.wTree = gtk.glade.XML(self.gladefile) 
        
        #Get the Main Window, and connect the "destroy" event
        self.window = self.wTree.get_widget("usernameDialog")
        self.window.connect("destroy", gtk.main_quit)

        #Create our dictionary and connect it
        dic = { "on_okButton_clicked" : self.okButton_clicked,
                "on_cancelButton_clicked" : gtk.main_quit,
                "on_usernameDialog_destroy" : gtk.main_quit }
        self.wTree.signal_autoconnect(dic)

        self.usernameComboBox = self.wTree.get_widget('usernameComboBox')
        self.textEntry = self.usernameComboBox.get_child()
        self.localRadioButton = self.wTree.get_widget('localRadioButton')

        # Get current username or decent default guess
        try:
            user = os.environ.get("HGUSER") or \
                self.hgui.config("ui", "username") or \
                os.environ.get("EMAIL") or \
                '%s@%s' % (util.getuser(), socket.getfqdn())
        except KeyError:
            user = ''

        # If no localrepo is provided, disable that option
        if not repo:
            self.localRadioButton.set_enabled(False)
        elif tolocal:
            self.localRadioButton.set_active(True)

        self.textEntry.set_text(user)
        self.configfn = os.path.expanduser("~/.config/hgconfig")
        self.unamehist = []
        if not os.path.exists(self.configfn):
            return

        ini = iniparse.INIConfig(file(self.configfn))
        if 'uname-history' in list(ini):
            for i in xrange(0, 10):
                try:
                    text = ini['uname-history']['%d' % i]
                    self.unamehist.append(text)
                except:
                    break
        if user in self.unamehist:
            self.unamehist.remove(user)
        self.unamehist.append(user)

        # Fill combo box
        liststore = gtk.ListStore(str)
        for name in self.unamehist:
            iter = liststore.append()
            liststore.set(iter, 0, name)
        self.usernameComboBox.set_model(liststore)
        self.usernameComboBox.set_text_column(0)

    def okButton_clicked(self, state):
        # Add new username to history
        username = str(self.textEntry.get_text())

        if username in self.unamehist:
            self.unamehist.remove(username)
        self.unamehist.append(username)

        # Create empty config file if necessary
        configdir = os.path.dirname(self.configfn)
        if not os.path.exists(configdir):
            os.mkdir(configdir)
        if not os.path.exists(self.configfn):
            f = open(self.configfn, "w")
            f.write("# Generated by hg-config\n")
            f.close()

        # Store username history 'self.unamehist'
        ini = iniparse.INIConfig(file(self.configfn))
        if 'uname-history' not in list(ini):
            ini.new_namespace('uname-history')
        for i, name in enumerate(self.unamehist):
            ini['uname-history']['%d' % i] = name
        f = open(self.configfn, "w")
        f.write(str(ini))
        f.close()

        if self.localRadioButton.get_active():
            path = [os.sep.join([self.repo.root, '.hg', 'hgrc'])]
        else:
            path = util.user_rcpath()

        for fn in path:
            if os.path.exists(fn):
                break
        else:
            fn = path[0]
            self.hgui.warn("Creating %s\n" % fn)
            f = open(fn, "w")
            f.write("# Generated by hg-config\n")
            f.close()

        ini = iniparse.INIConfig(file(fn))
        if 'ui' not in list(ini):
            ini.new_namespace('ui')
        if 'hooks' not in list(ini):
            ini.new_namespace('hooks')
        ini['ui']['username'] = username
        ini['hooks']['precommit.username'] = ''
        f = open(fn, "w")
        f.write(str(ini))
        f.close()

        # set username for this commit
        self.hgui.setconfig("ui", "username", username)
        gtk.main_quit()
Back to Top