mercurial/ui.py
author benoit.boissinot@ens-lyon.fr
Fri, 26 Aug 2005 13:06:58 +0200
changeset 1062 6d5a62a549fa
parent 1028 25e7ea0f2cff
child 1071 8f0ac653f85e
permissions -rw-r--r--
pep-0008 cleanup - Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     1
# ui.py - user interface bits for mercurial
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     2
#
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     3
# Copyright 2005 Matt Mackall <mpm@selenic.com>
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     4
#
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     7
613
5374955ec5b1 Demand-load most modules in the commands and ui modules.
Bryan O'Sullivan <bos@serpentine.com>
parents: 608
diff changeset
     8
import os, ConfigParser
5374955ec5b1 Demand-load most modules in the commands and ui modules.
Bryan O'Sullivan <bos@serpentine.com>
parents: 608
diff changeset
     9
from demandload import *
5374955ec5b1 Demand-load most modules in the commands and ui modules.
Bryan O'Sullivan <bos@serpentine.com>
parents: 608
diff changeset
    10
demandload(globals(), "re socket sys util")
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    11
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    12
class ui:
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    13
    def __init__(self, verbose=False, debug=False, quiet=False,
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    14
                 interactive=True):
960
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    15
        self.overlay = {}
285
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    16
        self.cdata = ConfigParser.SafeConfigParser()
951
859de3ebc041 Read global config file /etc/mercurial/hgrc and fix reading hgrc on Windows.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 691
diff changeset
    17
        self.cdata.read([os.path.normpath(hgrc) for hgrc in
859de3ebc041 Read global config file /etc/mercurial/hgrc and fix reading hgrc on Windows.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 691
diff changeset
    18
                         "/etc/mercurial/hgrc", os.path.expanduser("~/.hgrc")])
285
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    19
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    20
        self.quiet = self.configbool("ui", "quiet")
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    21
        self.verbose = self.configbool("ui", "verbose")
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    22
        self.debugflag = self.configbool("ui", "debug")
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    23
        self.interactive = self.configbool("ui", "interactive", True)
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    24
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    25
        self.quiet = (self.quiet or quiet) and not verbose and not debug
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    26
        self.verbose = (self.verbose or verbose) or debug
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    27
        self.debugflag = (self.debugflag or debug)
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    28
        self.interactive = (self.interactive and interactive)
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    29
337
c3d873ef4b31 Add support for .hg/hgrc file
mpm@selenic.com
parents: 285
diff changeset
    30
    def readconfig(self, fp):
c3d873ef4b31 Add support for .hg/hgrc file
mpm@selenic.com
parents: 285
diff changeset
    31
        self.cdata.readfp(fp)
c3d873ef4b31 Add support for .hg/hgrc file
mpm@selenic.com
parents: 285
diff changeset
    32
960
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    33
    def setconfig(self, section, name, val):
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    34
        self.overlay[(section, name)] = val
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    35
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    36
    def config(self, section, name, default=None):
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    37
        if self.overlay.has_key((section, name)):
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    38
            return self.overlay[(section, name)]
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    39
        if self.cdata.has_option(section, name):
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    40
            return self.cdata.get(section, name)
285
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    41
        return default
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    42
960
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    43
    def configbool(self, section, name, default=False):
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    44
        if self.overlay.has_key((section, name)):
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    45
            return self.overlay[(section, name)]
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    46
        if self.cdata.has_option(section, name):
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
    47
            return self.cdata.getboolean(section, name)
285
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    48
        return default
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    49
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    50
    def configitems(self, section):
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    51
        if self.cdata.has_section(section):
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    52
            return self.cdata.items(section)
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    53
        return []
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
    54
1028
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    55
    def walkconfig(self):
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    56
        seen = {}
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    57
        for (section, name), value in self.overlay.iteritems():
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    58
            yield section, name, value
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    59
            seen[section, name] = 1
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    60
        for section in self.cdata.sections():
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    61
            for name, value in self.cdata.items(section):
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    62
                if (section, name) in seen: continue
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    63
                yield section, name, value.replace('\n', '\\n')
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    64
                seen[section, name] = 1
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
    65
608
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
    66
    def username(self):
691
61c6b4178b9e HG environment variables take precedence over hgrc
mpm@selenic.com
parents: 662
diff changeset
    67
        return (os.environ.get("HGUSER") or
61c6b4178b9e HG environment variables take precedence over hgrc
mpm@selenic.com
parents: 662
diff changeset
    68
                self.config("ui", "username") or
608
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
    69
                os.environ.get("EMAIL") or
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
    70
                (os.environ.get("LOGNAME",
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
    71
                                os.environ.get("USERNAME", "unknown"))
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
    72
                 + '@' + socket.getfqdn()))
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
    73
506
1f81ebff98c9 [PATCH] Add ui.expandpath command
mpm@selenic.com
parents: 350
diff changeset
    74
    def expandpath(self, loc):
1f81ebff98c9 [PATCH] Add ui.expandpath command
mpm@selenic.com
parents: 350
diff changeset
    75
        paths = {}
1f81ebff98c9 [PATCH] Add ui.expandpath command
mpm@selenic.com
parents: 350
diff changeset
    76
        for name, path in self.configitems("paths"):
1f81ebff98c9 [PATCH] Add ui.expandpath command
mpm@selenic.com
parents: 350
diff changeset
    77
            paths[name] = path
1f81ebff98c9 [PATCH] Add ui.expandpath command
mpm@selenic.com
parents: 350
diff changeset
    78
1f81ebff98c9 [PATCH] Add ui.expandpath command
mpm@selenic.com
parents: 350
diff changeset
    79
        return paths.get(loc, loc)
1f81ebff98c9 [PATCH] Add ui.expandpath command
mpm@selenic.com
parents: 350
diff changeset
    80
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    81
    def write(self, *args):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    82
        for a in args:
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    83
            sys.stdout.write(str(a))
565
9a80418646dd [PATCH] Make ui.warn write to stderr
mpm@selenic.com
parents: 515
diff changeset
    84
9a80418646dd [PATCH] Make ui.warn write to stderr
mpm@selenic.com
parents: 515
diff changeset
    85
    def write_err(self, *args):
9a80418646dd [PATCH] Make ui.warn write to stderr
mpm@selenic.com
parents: 515
diff changeset
    86
        sys.stdout.flush()
9a80418646dd [PATCH] Make ui.warn write to stderr
mpm@selenic.com
parents: 515
diff changeset
    87
        for a in args:
9a80418646dd [PATCH] Make ui.warn write to stderr
mpm@selenic.com
parents: 515
diff changeset
    88
            sys.stderr.write(str(a))
9a80418646dd [PATCH] Make ui.warn write to stderr
mpm@selenic.com
parents: 515
diff changeset
    89
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    90
    def readline(self):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    91
        return sys.stdin.readline()[:-1]
1062
6d5a62a549fa pep-0008 cleanup
benoit.boissinot@ens-lyon.fr
parents: 1028
diff changeset
    92
    def prompt(self, msg, pat, default="y"):
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    93
        if not self.interactive: return default
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    94
        while 1:
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    95
            self.write(msg, " ")
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    96
            r = self.readline()
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    97
            if re.match(pat, r):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    98
                return r
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    99
            else:
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   100
                self.write("unrecognized response\n")
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   101
    def status(self, *msg):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   102
        if not self.quiet: self.write(*msg)
234
3427806d5ab9 ui.warn can use more than one argument like the other ui methods.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 207
diff changeset
   103
    def warn(self, *msg):
565
9a80418646dd [PATCH] Make ui.warn write to stderr
mpm@selenic.com
parents: 515
diff changeset
   104
        self.write_err(*msg)
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   105
    def note(self, *msg):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   106
        if self.verbose: self.write(*msg)
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   107
    def debug(self, *msg):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   108
        if self.debugflag: self.write(*msg)
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   109
    def edit(self, text):
249
619e775aa7f9 import and startup cleanups
mpm@selenic.com
parents: 241
diff changeset
   110
        import tempfile
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   111
        (fd, name) = tempfile.mkstemp("hg")
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   112
        f = os.fdopen(fd, "w")
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   113
        f.write(text)
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   114
        f.close()
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   115
691
61c6b4178b9e HG environment variables take precedence over hgrc
mpm@selenic.com
parents: 662
diff changeset
   116
        editor = (os.environ.get("HGEDITOR") or
61c6b4178b9e HG environment variables take precedence over hgrc
mpm@selenic.com
parents: 662
diff changeset
   117
                  self.config("ui", "editor") or
608
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
   118
                  os.environ.get("EDITOR", "vi"))
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
   119
662
b55a78595ef6 Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents: 613
diff changeset
   120
        os.environ["HGUSER"] = self.username()
1062
6d5a62a549fa pep-0008 cleanup
benoit.boissinot@ens-lyon.fr
parents: 1028
diff changeset
   121
        util.system("%s %s" % (editor, name), errprefix="edit failed")
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   122
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   123
        t = open(name).read()
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   124
        t = re.sub("(?m)^HG:.*\n", "", t)
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   125
662
b55a78595ef6 Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents: 613
diff changeset
   126
        os.unlink(name)
b55a78595ef6 Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents: 613
diff changeset
   127
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
   128
        return t