mercurial/ui.py
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
Thu, 05 May 2016 16:29:31 +0200
branchstable
changeset 29082 77de985d7c91
parent 28962 ad2cd2ef25d9
child 29095 3f33831a9202
permissions -rw-r--r--
deprecation: gate deprecation warning behind devel configuration Regular users are not supposed to be exposed to the API deprecation warnings. We now only issue them when the developper warnings are enabled.
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
#
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4633
diff changeset
     3
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10243
diff changeset
     6
# GNU General Public License version 2 or any later version.
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     7
25989
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     8
from __future__ import absolute_import
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     9
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    10
import errno
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    11
import getpass
25629
52e5f68d8363 devel-warn: move the develwarn function as a method of the ui object
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25568
diff changeset
    12
import inspect
25989
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    13
import os
27392
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
    14
import re
25989
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    15
import socket
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    16
import sys
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    17
import tempfile
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    18
import traceback
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    19
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    20
from .i18n import _
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    21
from .node import hex
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    22
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    23
from . import (
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    24
    config,
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    25
    error,
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    26
    formatter,
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    27
    progress,
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    28
    scmutil,
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    29
    util,
2cc4e8385661 ui: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    30
)
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    31
22419
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    32
samplehgrcs = {
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    33
    'user':
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    34
"""# example user config (see "hg help config" for more info)
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    35
[ui]
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    36
# name and email, e.g.
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    37
# username = Jane Doe <jdoe@example.com>
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    38
username =
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    39
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    40
[extensions]
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    41
# uncomment these lines to enable some popular extensions
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    42
# (see "hg help extensions" for more info)
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    43
#
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    44
# pager =
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    45
# color =""",
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    46
22837
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    47
    'cloned':
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    48
"""# example repository config (see "hg help config" for more info)
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    49
[paths]
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    50
default = %s
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    51
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    52
# path aliases to other clones of this repo in URLs or filesystem paths
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    53
# (see "hg help config.paths" for more info)
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    54
#
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    55
# default-push = ssh://jdoe@example.net/hg/jdoes-fork
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    56
# my-fork      = ssh://jdoe@example.net/hg/jdoes-fork
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    57
# my-clone     = /home/jdoe/jdoes-clone
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    58
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    59
[ui]
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    60
# name and email (local to this repository, optional), e.g.
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    61
# username = Jane Doe <jdoe@example.com>
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    62
""",
2be7d5ebd4d0 config: use the same hgrc for a cloned repo as for an uninitted repo
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22836
diff changeset
    63
22419
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    64
    'local':
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    65
"""# example repository config (see "hg help config" for more info)
22836
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    66
[paths]
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    67
# path aliases to other clones of this repo in URLs or filesystem paths
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    68
# (see "hg help config.paths" for more info)
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    69
#
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    70
# default      = http://example.com/hg/example-repo
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    71
# default-push = ssh://jdoe@example.net/hg/jdoes-fork
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    72
# my-fork      = ssh://jdoe@example.net/hg/jdoes-fork
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    73
# my-clone     = /home/jdoe/jdoes-clone
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    74
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    75
[ui]
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    76
# name and email (local to this repository, optional), e.g.
5a831e4e6d7a config: give a more detailed sample repo config
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22783
diff changeset
    77
# username = Jane Doe <jdoe@example.com>
22419
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    78
""",
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    79
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    80
    'global':
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    81
"""# example system-wide hg config (see "hg help config" for more info)
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    82
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    83
[extensions]
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    84
# uncomment these lines to enable some popular extensions
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    85
# (see "hg help extensions" for more info)
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    86
#
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    87
# blackbox =
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    88
# color =
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    89
# pager =""",
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    90
}
fdfc9faca273 ui: move samplehgrcs from config
Matt Mackall <mpm@selenic.com>
parents: 22291
diff changeset
    91
1559
59b3639df0a9 Convert all classes to new-style classes by deriving them from object.
Eric Hopper <hopper@omnifarious.org>
parents: 1483
diff changeset
    92
class ui(object):
8190
9b8ac5fb7760 ui: kill most users of parentui name and arg, replace with .copy()
Matt Mackall <mpm@selenic.com>
parents: 8189
diff changeset
    93
    def __init__(self, src=None):
21132
350dc24a553d ui: pushbuffer can now also capture stderr
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20788
diff changeset
    94
        # _buffers: used for temporary capture of output
8202
4746113269c7 ui: buffers -> _buffers
Matt Mackall <mpm@selenic.com>
parents: 8201
diff changeset
    95
        self._buffers = []
27106
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
    96
        # 3-tuple describing how each buffer in the stack behaves.
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
    97
        # Values are (capture stderr, capture subprocesses, apply labels).
21132
350dc24a553d ui: pushbuffer can now also capture stderr
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20788
diff changeset
    98
        self._bufferstates = []
27106
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
    99
        # When a buffer is active, defines whether we are expanding labels.
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   100
        # This exists to prevent an extra list lookup.
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   101
        self._bufferapplylabels = None
9851
9e7b2c49d25d Make it possible to debug failed hook imports via use of --traceback
Bryan O'Sullivan <bos@serpentine.com>
parents: 9786
diff changeset
   102
        self.quiet = self.verbose = self.debugflag = self.tracebackflag = False
8208
32a2a1e244f1 ui: make interactive a method
Matt Mackall <mpm@selenic.com>
parents: 8206
diff changeset
   103
        self._reportuntrusted = True
8203
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   104
        self._ocfg = config.config() # overlay
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   105
        self._tcfg = config.config() # trusted
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   106
        self._ucfg = config.config() # untrusted
8478
d728f126c1b7 ui: use set instead of dict
Martin Geisler <mg@lazybytes.net>
parents: 8409
diff changeset
   107
        self._trustusers = set()
d728f126c1b7 ui: use set instead of dict
Martin Geisler <mg@lazybytes.net>
parents: 8409
diff changeset
   108
        self._trustgroups = set()
17048
15d4d475de9e ui: add a variable to control whether hooks should be called
Idan Kamara <idankk86@gmail.com>
parents: 16383
diff changeset
   109
        self.callhooks = True
8136
6b5522cb2ad2 ui: refactor option setting
Matt Mackall <mpm@selenic.com>
parents: 8135
diff changeset
   110
8190
9b8ac5fb7760 ui: kill most users of parentui name and arg, replace with .copy()
Matt Mackall <mpm@selenic.com>
parents: 8189
diff changeset
   111
        if src:
14612
4e1ccd4c2b6d ui: add I/O descriptors
Idan Kamara <idankk86@gmail.com>
parents: 14515
diff changeset
   112
            self.fout = src.fout
4e1ccd4c2b6d ui: add I/O descriptors
Idan Kamara <idankk86@gmail.com>
parents: 14515
diff changeset
   113
            self.ferr = src.ferr
4e1ccd4c2b6d ui: add I/O descriptors
Idan Kamara <idankk86@gmail.com>
parents: 14515
diff changeset
   114
            self.fin = src.fin
4e1ccd4c2b6d ui: add I/O descriptors
Idan Kamara <idankk86@gmail.com>
parents: 14515
diff changeset
   115
8203
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   116
            self._tcfg = src._tcfg.copy()
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   117
            self._ucfg = src._ucfg.copy()
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   118
            self._ocfg = src._ocfg.copy()
8201
7cf2b987acd3 ui: trusted_users -> _trustusers, trusted_groups -> _trustgroups
Matt Mackall <mpm@selenic.com>
parents: 8200
diff changeset
   119
            self._trustusers = src._trustusers.copy()
7cf2b987acd3 ui: trusted_users -> _trustusers, trusted_groups -> _trustgroups
Matt Mackall <mpm@selenic.com>
parents: 8200
diff changeset
   120
            self._trustgroups = src._trustgroups.copy()
9887
38170eeed18c ui: add environ property to access os.environ or wsgirequest.environ
Sune Foldager <cryo@cyanite.org>
parents: 9851
diff changeset
   121
            self.environ = src.environ
17048
15d4d475de9e ui: add a variable to control whether hooks should be called
Idan Kamara <idankk86@gmail.com>
parents: 16383
diff changeset
   122
            self.callhooks = src.callhooks
8143
507c49e297e1 ui: simplify init, kill dupconfig
Matt Mackall <mpm@selenic.com>
parents: 8142
diff changeset
   123
            self.fixconfig()
507c49e297e1 ui: simplify init, kill dupconfig
Matt Mackall <mpm@selenic.com>
parents: 8142
diff changeset
   124
        else:
14612
4e1ccd4c2b6d ui: add I/O descriptors
Idan Kamara <idankk86@gmail.com>
parents: 14515
diff changeset
   125
            self.fout = sys.stdout
4e1ccd4c2b6d ui: add I/O descriptors
Idan Kamara <idankk86@gmail.com>
parents: 14515
diff changeset
   126
            self.ferr = sys.stderr
4e1ccd4c2b6d ui: add I/O descriptors
Idan Kamara <idankk86@gmail.com>
parents: 14515
diff changeset
   127
            self.fin = sys.stdin
4e1ccd4c2b6d ui: add I/O descriptors
Idan Kamara <idankk86@gmail.com>
parents: 14515
diff changeset
   128
9887
38170eeed18c ui: add environ property to access os.environ or wsgirequest.environ
Sune Foldager <cryo@cyanite.org>
parents: 9851
diff changeset
   129
            # shared read-only environment
38170eeed18c ui: add environ property to access os.environ or wsgirequest.environ
Sune Foldager <cryo@cyanite.org>
parents: 9851
diff changeset
   130
            self.environ = os.environ
3676
d94664748bc1 Use a variable to explicitly trust global config files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3646
diff changeset
   131
            # we always trust global config files
13984
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13849
diff changeset
   132
            for f in scmutil.rcpath():
8200
865d2c646f29 ui: assumetrusted -> trust
Matt Mackall <mpm@selenic.com>
parents: 8199
diff changeset
   133
                self.readconfig(f, trust=True)
8222
d30a21594812 more whitespace cleanup and some other style nits
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8220
diff changeset
   134
8189
d2899a856f9f ui: replace parentui mechanism with repo.baseui
Matt Mackall <mpm@selenic.com>
parents: 8187
diff changeset
   135
    def copy(self):
8220
6e6ebeb52899 ui: ui.copy() now takes the ui class into account
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 8208
diff changeset
   136
        return self.__class__(self)
1839
876e4e6ad82b Create local ui object per repository, so .hg/hgrc don't get mixed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1637
diff changeset
   137
16135
ae5f92e154d3 ui: add formatter method
Matt Mackall <mpm@selenic.com>
parents: 15919
diff changeset
   138
    def formatter(self, topic, opts):
ae5f92e154d3 ui: add formatter method
Matt Mackall <mpm@selenic.com>
parents: 15919
diff changeset
   139
        return formatter.formatter(self, topic, opts)
ae5f92e154d3 ui: add formatter method
Matt Mackall <mpm@selenic.com>
parents: 15919
diff changeset
   140
14859
dccecfaebdd2 ui: rename _is_trusted to _trusted
Matt Mackall <mpm@selenic.com>
parents: 14738
diff changeset
   141
    def _trusted(self, fp, f):
3677
1a0fa3914c46 Avoid looking up usernames if the current user owns the .hgrc file
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3676
diff changeset
   142
        st = util.fstat(fp)
8657
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8656
diff changeset
   143
        if util.isowner(st):
3677
1a0fa3914c46 Avoid looking up usernames if the current user owns the .hgrc file
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3676
diff changeset
   144
            return True
8141
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   145
8201
7cf2b987acd3 ui: trusted_users -> _trustusers, trusted_groups -> _trustgroups
Matt Mackall <mpm@selenic.com>
parents: 8200
diff changeset
   146
        tusers, tgroups = self._trustusers, self._trustgroups
8141
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   147
        if '*' in tusers or '*' in tgroups:
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   148
            return True
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   149
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   150
        user = util.username(st.st_uid)
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   151
        group = util.groupname(st.st_gid)
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   152
        if user in tusers or group in tgroups or user == util.username():
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   153
            return True
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   154
8204
797586be575d ui: report_untrusted fixes
Matt Mackall <mpm@selenic.com>
parents: 8203
diff changeset
   155
        if self._reportuntrusted:
16939
fa91ddfc3f36 ui: lowercase "not trusting file" warning message
Martin Geisler <mg@aragost.com>
parents: 16938
diff changeset
   156
            self.warn(_('not trusting file %s from untrusted '
8141
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   157
                        'user %s, group %s\n') % (f, user, group))
e40b629bedd1 ui: cleanup _is_trusted a bit
Matt Mackall <mpm@selenic.com>
parents: 8140
diff changeset
   158
        return False
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3489
diff changeset
   159
8200
865d2c646f29 ui: assumetrusted -> trust
Matt Mackall <mpm@selenic.com>
parents: 8199
diff changeset
   160
    def readconfig(self, filename, root=None, trust=False,
8345
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8312
diff changeset
   161
                   sections=None, remap=None):
8142
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8141
diff changeset
   162
        try:
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8141
diff changeset
   163
            fp = open(filename)
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8141
diff changeset
   164
        except IOError:
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8141
diff changeset
   165
            if not sections: # ignore unless we were looking for something
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8141
diff changeset
   166
                return
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8141
diff changeset
   167
            raise
8139
9302404b60f3 ui: always have ucdata
Matt Mackall <mpm@selenic.com>
parents: 8138
diff changeset
   168
8203
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   169
        cfg = config.config()
14859
dccecfaebdd2 ui: rename _is_trusted to _trusted
Matt Mackall <mpm@selenic.com>
parents: 14738
diff changeset
   170
        trusted = sections or trust or self._trusted(fp, filename)
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   171
8142
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8141
diff changeset
   172
        try:
8345
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8312
diff changeset
   173
            cfg.read(filename, fp, sections=sections, remap=remap)
15407
ee112eb69d2a misc: adding missing file close() calls
Matt Mackall <mpm@selenic.com>
parents: 15089
diff changeset
   174
            fp.close()
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25629
diff changeset
   175
        except error.ConfigError as inst:
8142
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8141
diff changeset
   176
            if trusted:
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8143
diff changeset
   177
                raise
16938
ba9bfdc6bfb2 ui: lowercase ConfigError warning message
Martin Geisler <mg@aragost.com>
parents: 16751
diff changeset
   178
            self.warn(_("ignored: %s\n") % str(inst))
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   179
10455
40dfd46d098f ui: add HGPLAIN environment variable for easier scripting
Brodie Rao <me+hg@dackz.net>
parents: 10426
diff changeset
   180
        if self.plain():
10507
79dd96774187 ui: unset ui.slash when HGPLAIN is set
Brodie Rao <me+hg@dackz.net>
parents: 10506
diff changeset
   181
            for k in ('debug', 'fallbackencoding', 'quiet', 'slash',
24663
7d01371e6358 commands: add ui.statuscopies config knob
Mathias De Maré <mathias.demare@gmail.com>
parents: 24250
diff changeset
   182
                      'logtemplate', 'statuscopies', 'style',
10507
79dd96774187 ui: unset ui.slash when HGPLAIN is set
Brodie Rao <me+hg@dackz.net>
parents: 10506
diff changeset
   183
                      'traceback', 'verbose'):
10455
40dfd46d098f ui: add HGPLAIN environment variable for easier scripting
Brodie Rao <me+hg@dackz.net>
parents: 10426
diff changeset
   184
                if k in cfg['ui']:
40dfd46d098f ui: add HGPLAIN environment variable for easier scripting
Brodie Rao <me+hg@dackz.net>
parents: 10426
diff changeset
   185
                    del cfg['ui'][k]
14373
a599431b0ab6 ui: enable alias exception when reading config in plain mode
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14372
diff changeset
   186
            for k, v in cfg.items('defaults'):
a599431b0ab6 ui: enable alias exception when reading config in plain mode
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14372
diff changeset
   187
                del cfg['defaults'][k]
a599431b0ab6 ui: enable alias exception when reading config in plain mode
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14372
diff changeset
   188
        # Don't remove aliases from the configuration if in the exceptionlist
a599431b0ab6 ui: enable alias exception when reading config in plain mode
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14372
diff changeset
   189
        if self.plain('alias'):
10506
42afde35e9f7 ui: suppress aliases when HGPLAIN is set
Brodie Rao <me+hg@dackz.net>
parents: 10455
diff changeset
   190
            for k, v in cfg.items('alias'):
42afde35e9f7 ui: suppress aliases when HGPLAIN is set
Brodie Rao <me+hg@dackz.net>
parents: 10455
diff changeset
   191
                del cfg['alias'][k]
24883
09049042ab99 ui: disable revsetaliases in plain mode (BC)
Siddharth Agarwal <sid0@fb.com>
parents: 24848
diff changeset
   192
        if self.plain('revsetalias'):
09049042ab99 ui: disable revsetaliases in plain mode (BC)
Siddharth Agarwal <sid0@fb.com>
parents: 24848
diff changeset
   193
            for k, v in cfg.items('revsetalias'):
09049042ab99 ui: disable revsetaliases in plain mode (BC)
Siddharth Agarwal <sid0@fb.com>
parents: 24848
diff changeset
   194
                del cfg['revsetalias'][k]
28958
77e566fe31ec ui: drop template aliases by HGPLAIN
Yuya Nishihara <yuya@tcha.org>
parents: 28635
diff changeset
   195
        if self.plain('templatealias'):
77e566fe31ec ui: drop template aliases by HGPLAIN
Yuya Nishihara <yuya@tcha.org>
parents: 28635
diff changeset
   196
            for k, v in cfg.items('templatealias'):
77e566fe31ec ui: drop template aliases by HGPLAIN
Yuya Nishihara <yuya@tcha.org>
parents: 28635
diff changeset
   197
                del cfg['templatealias'][k]
10455
40dfd46d098f ui: add HGPLAIN environment variable for easier scripting
Brodie Rao <me+hg@dackz.net>
parents: 10426
diff changeset
   198
8142
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8141
diff changeset
   199
        if trusted:
8203
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   200
            self._tcfg.update(cfg)
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   201
            self._tcfg.update(self._ocfg)
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   202
        self._ucfg.update(cfg)
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   203
        self._ucfg.update(self._ocfg)
8139
9302404b60f3 ui: always have ucdata
Matt Mackall <mpm@selenic.com>
parents: 8138
diff changeset
   204
3347
bce7c1b4c1c8 ui.py: normalize settings every time the configuration changes
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3346
diff changeset
   205
        if root is None:
bce7c1b4c1c8 ui.py: normalize settings every time the configuration changes
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3346
diff changeset
   206
            root = os.path.expanduser('~')
bce7c1b4c1c8 ui.py: normalize settings every time the configuration changes
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3346
diff changeset
   207
        self.fixconfig(root=root)
3014
01454af644b8 load extensions only after the ui object has been completely initialized
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3013
diff changeset
   208
12764
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   209
    def fixconfig(self, root=None, section=None):
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   210
        if section in (None, 'paths'):
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   211
            # expand vars and ~
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   212
            # translate paths relative to root (or home) into absolute paths
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   213
            root = root or os.getcwd()
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   214
            for c in self._tcfg, self._ucfg, self._ocfg:
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   215
                for n, p in c.items('paths'):
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   216
                    if not p:
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   217
                        continue
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   218
                    if '%%' in p:
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   219
                        self.warn(_("(deprecated '%%' in path %s=%s from %s)\n")
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   220
                                  % (n, p, self.configsource('paths', n)))
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   221
                        p = p.replace('%%', '%')
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   222
                    p = util.expandpath(p)
14076
924c82157d46 url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents: 13984
diff changeset
   223
                    if not util.hasscheme(p) and not os.path.isabs(p):
12764
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   224
                        p = os.path.normpath(os.path.join(root, p))
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   225
                    c.set("paths", n, p)
3347
bce7c1b4c1c8 ui.py: normalize settings every time the configuration changes
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3346
diff changeset
   226
12764
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   227
        if section in (None, 'ui'):
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   228
            # update ui options
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   229
            self.debugflag = self.configbool('ui', 'debug')
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   230
            self.verbose = self.debugflag or self.configbool('ui', 'verbose')
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   231
            self.quiet = not self.debugflag and self.configbool('ui', 'quiet')
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   232
            if self.verbose and self.quiet:
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   233
                self.quiet = self.verbose = False
13493
95b0d4c1c9e1 ui: always report untrusted hgrc files when debug enabled
Ry4an Brase <ry4an-hg@ry4an.org>
parents: 13238
diff changeset
   234
            self._reportuntrusted = self.debugflag or self.configbool("ui",
95b0d4c1c9e1 ui: always report untrusted hgrc files when debug enabled
Ry4an Brase <ry4an-hg@ry4an.org>
parents: 13238
diff changeset
   235
                "report_untrusted", True)
12764
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   236
            self.tracebackflag = self.configbool('ui', 'traceback', False)
3350
ab900698b832 update ui.quiet/verbose/debug/interactive every time the config changes
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3349
diff changeset
   237
12764
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   238
        if section in (None, 'trusted'):
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   239
            # update trust information
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   240
            self._trustusers.update(self.configlist('trusted', 'users'))
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   241
            self._trustgroups.update(self.configlist('trusted', 'groups'))
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3489
diff changeset
   242
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 15407
diff changeset
   243
    def backupconfig(self, section, item):
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 15407
diff changeset
   244
        return (self._ocfg.backup(section, item),
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 15407
diff changeset
   245
                self._tcfg.backup(section, item),
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 15407
diff changeset
   246
                self._ucfg.backup(section, item),)
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 15407
diff changeset
   247
    def restoreconfig(self, data):
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 15407
diff changeset
   248
        self._ocfg.restore(data[0])
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 15407
diff changeset
   249
        self._tcfg.restore(data[1])
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 15407
diff changeset
   250
        self._ucfg.restore(data[2])
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 15407
diff changeset
   251
20788
f144928dd058 config: give a useful hint of source for the most common command line settings
Mads Kiilerich <madski@unity3d.com>
parents: 20787
diff changeset
   252
    def setconfig(self, section, name, value, source=''):
20787
be179da10d5f config: backout 77f1f206e135 - 743daa601445 removed the only use of overlay
Mads Kiilerich <madski@unity3d.com>
parents: 20606
diff changeset
   253
        for cfg in (self._ocfg, self._tcfg, self._ucfg):
20788
f144928dd058 config: give a useful hint of source for the most common command line settings
Mads Kiilerich <madski@unity3d.com>
parents: 20787
diff changeset
   254
            cfg.set(section, name, value, source)
12764
ad2506f097d3 ui: only fix config if the relevant section has changed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12689
diff changeset
   255
        self.fixconfig(section=section)
960
abfb5cc97fcd Add ui.setconfig overlay
mpm@selenic.com
parents: 953
diff changeset
   256
8199
e9f90e5989d9 ui: _get_cdata -> _data
Matt Mackall <mpm@selenic.com>
parents: 8198
diff changeset
   257
    def _data(self, untrusted):
8203
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   258
        return untrusted and self._ucfg or self._tcfg
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   259
8182
b97abc7c1135 showconfig: show source file and line with --debug
Matt Mackall <mpm@selenic.com>
parents: 8175
diff changeset
   260
    def configsource(self, section, name, untrusted=False):
8199
e9f90e5989d9 ui: _get_cdata -> _data
Matt Mackall <mpm@selenic.com>
parents: 8198
diff changeset
   261
        return self._data(untrusted).source(section, name) or 'none'
8182
b97abc7c1135 showconfig: show source file and line with --debug
Matt Mackall <mpm@selenic.com>
parents: 8175
diff changeset
   262
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8143
diff changeset
   263
    def config(self, section, name, default=None, untrusted=False):
15035
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   264
        if isinstance(name, list):
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   265
            alternates = name
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   266
        else:
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   267
            alternates = [name]
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   268
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   269
        for n in alternates:
19536
ab3cf67740d6 ui.config: fix bug in config alternatives from cc669e4fec95
Augie Fackler <durin42@gmail.com>
parents: 19226
diff changeset
   270
            value = self._data(untrusted).get(section, n, None)
15035
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   271
            if value is not None:
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   272
                name = n
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   273
                break
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   274
        else:
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   275
            value = default
cc669e4fec95 ui: allow alternatives for config options
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   276
8204
797586be575d ui: report_untrusted fixes
Matt Mackall <mpm@selenic.com>
parents: 8203
diff changeset
   277
        if self.debugflag and not untrusted and self._reportuntrusted:
19536
ab3cf67740d6 ui.config: fix bug in config alternatives from cc669e4fec95
Augie Fackler <durin42@gmail.com>
parents: 19226
diff changeset
   278
            for n in alternates:
ab3cf67740d6 ui.config: fix bug in config alternatives from cc669e4fec95
Augie Fackler <durin42@gmail.com>
parents: 19226
diff changeset
   279
                uvalue = self._ucfg.get(section, n)
ab3cf67740d6 ui.config: fix bug in config alternatives from cc669e4fec95
Augie Fackler <durin42@gmail.com>
parents: 19226
diff changeset
   280
                if uvalue is not None and uvalue != value:
ab3cf67740d6 ui.config: fix bug in config alternatives from cc669e4fec95
Augie Fackler <durin42@gmail.com>
parents: 19226
diff changeset
   281
                    self.debug("ignoring untrusted configuration option "
ab3cf67740d6 ui.config: fix bug in config alternatives from cc669e4fec95
Augie Fackler <durin42@gmail.com>
parents: 19226
diff changeset
   282
                               "%s.%s = %s\n" % (section, n, uvalue))
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   283
        return value
3341
a7cec14c9b40 ui.py: move common code out of config and configbool
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3340
diff changeset
   284
27252
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   285
    def configsuboptions(self, section, name, default=None, untrusted=False):
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   286
        """Get a config option and all sub-options.
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   287
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   288
        Some config options have sub-options that are declared with the
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   289
        format "key:opt = value". This method is used to return the main
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   290
        option and all its declared sub-options.
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   291
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   292
        Returns a 2-tuple of ``(option, sub-options)``, where `sub-options``
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   293
        is a dict of defined sub-options where keys and values are strings.
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   294
        """
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   295
        data = self._data(untrusted)
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   296
        main = data.get(section, name, default)
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   297
        if self.debugflag and not untrusted and self._reportuntrusted:
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   298
            uvalue = self._ucfg.get(section, name)
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   299
            if uvalue is not None and uvalue != main:
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   300
                self.debug('ignoring untrusted configuration option '
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   301
                           '%s.%s = %s\n' % (section, name, uvalue))
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   302
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   303
        sub = {}
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   304
        prefix = '%s:' % name
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   305
        for k, v in data.items(section):
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   306
            if k.startswith(prefix):
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   307
                sub[k[len(prefix):]] = v
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   308
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   309
        if self.debugflag and not untrusted and self._reportuntrusted:
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   310
            for k, v in sub.items():
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   311
                uvalue = self._ucfg.get(section, '%s:%s' % (name, k))
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   312
                if uvalue is not None and uvalue != v:
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   313
                    self.debug('ignoring untrusted configuration option '
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   314
                               '%s:%s.%s = %s\n' % (section, name, k, uvalue))
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   315
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   316
        return main, sub
dccbebcff075 ui: add method to return option and all sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27153
diff changeset
   317
13238
1b591f9b7fd2 ui: add configpath helper
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
   318
    def configpath(self, section, name, default=None, untrusted=False):
14924
545e00279670 ui: config path relative to repo root
Simon Heimberg <simohe@besonet.ch>
parents: 14923
diff changeset
   319
        'get a path config item, expanded relative to repo root or config file'
13238
1b591f9b7fd2 ui: add configpath helper
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
   320
        v = self.config(section, name, default, untrusted)
14923
351624f8f523 ui: providing no default value to configpath should not raise an Error
Simon Heimberg <simohe@besonet.ch>
parents: 14922
diff changeset
   321
        if v is None:
351624f8f523 ui: providing no default value to configpath should not raise an Error
Simon Heimberg <simohe@besonet.ch>
parents: 14922
diff changeset
   322
            return None
13238
1b591f9b7fd2 ui: add configpath helper
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
   323
        if not os.path.isabs(v) or "://" not in v:
1b591f9b7fd2 ui: add configpath helper
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
   324
            src = self.configsource(section, name, untrusted)
1b591f9b7fd2 ui: add configpath helper
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
   325
            if ':' in src:
14922
1bc970a77977 ui: fix error, base can not be a list
Simon Heimberg <simohe@besonet.ch>
parents: 14859
diff changeset
   326
                base = os.path.dirname(src.rsplit(':')[0])
13238
1b591f9b7fd2 ui: add configpath helper
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
   327
                v = os.path.join(base, os.path.expanduser(v))
1b591f9b7fd2 ui: add configpath helper
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
   328
        return v
1b591f9b7fd2 ui: add configpath helper
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
   329
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   330
    def configbool(self, section, name, default=False, untrusted=False):
14171
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   331
        """parse a configuration element as a boolean
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   332
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   333
        >>> u = ui(); s = 'foo'
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   334
        >>> u.setconfig(s, 'true', 'yes')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   335
        >>> u.configbool(s, 'true')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   336
        True
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   337
        >>> u.setconfig(s, 'false', 'no')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   338
        >>> u.configbool(s, 'false')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   339
        False
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   340
        >>> u.configbool(s, 'unknown')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   341
        False
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   342
        >>> u.configbool(s, 'unknown', True)
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   343
        True
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   344
        >>> u.setconfig(s, 'invalid', 'somevalue')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   345
        >>> u.configbool(s, 'invalid')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   346
        Traceback (most recent call last):
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   347
            ...
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   348
        ConfigError: foo.invalid is not a boolean ('somevalue')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   349
        """
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   350
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8143
diff changeset
   351
        v = self.config(section, name, None, untrusted)
8527
f9a80054dd3c use 'x is None' instead of 'x == None'
Martin Geisler <mg@lazybytes.net>
parents: 8478
diff changeset
   352
        if v is None:
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8143
diff changeset
   353
            return default
10243
cd3e5c47d663 ui: just return it if it's already a bool
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10220
diff changeset
   354
        if isinstance(v, bool):
cd3e5c47d663 ui: just return it if it's already a bool
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10220
diff changeset
   355
            return v
12087
a88a4720c2f0 parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents: 11984
diff changeset
   356
        b = util.parsebool(v)
a88a4720c2f0 parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents: 11984
diff changeset
   357
        if b is None:
14171
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   358
            raise error.ConfigError(_("%s.%s is not a boolean ('%s')")
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8143
diff changeset
   359
                                    % (section, name, v))
12087
a88a4720c2f0 parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents: 11984
diff changeset
   360
        return b
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   361
14171
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   362
    def configint(self, section, name, default=None, untrusted=False):
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   363
        """parse a configuration element as an integer
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   364
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   365
        >>> u = ui(); s = 'foo'
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   366
        >>> u.setconfig(s, 'int1', '42')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   367
        >>> u.configint(s, 'int1')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   368
        42
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   369
        >>> u.setconfig(s, 'int2', '-42')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   370
        >>> u.configint(s, 'int2')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   371
        -42
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   372
        >>> u.configint(s, 'unknown', 7)
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   373
        7
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   374
        >>> u.setconfig(s, 'invalid', 'somevalue')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   375
        >>> u.configint(s, 'invalid')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   376
        Traceback (most recent call last):
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   377
            ...
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   378
        ConfigError: foo.invalid is not an integer ('somevalue')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   379
        """
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   380
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   381
        v = self.config(section, name, None, untrusted)
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   382
        if v is None:
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   383
            return default
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   384
        try:
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   385
            return int(v)
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   386
        except ValueError:
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   387
            raise error.ConfigError(_("%s.%s is not an integer ('%s')")
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   388
                                    % (section, name, v))
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   389
19065
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   390
    def configbytes(self, section, name, default=0, untrusted=False):
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   391
        """parse a configuration element as a quantity in bytes
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   392
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   393
        Units can be specified as b (bytes), k or kb (kilobytes), m or
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   394
        mb (megabytes), g or gb (gigabytes).
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   395
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   396
        >>> u = ui(); s = 'foo'
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   397
        >>> u.setconfig(s, 'val1', '42')
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   398
        >>> u.configbytes(s, 'val1')
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   399
        42
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   400
        >>> u.setconfig(s, 'val2', '42.5 kb')
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   401
        >>> u.configbytes(s, 'val2')
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   402
        43520
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   403
        >>> u.configbytes(s, 'unknown', '7 MB')
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   404
        7340032
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   405
        >>> u.setconfig(s, 'invalid', 'somevalue')
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   406
        >>> u.configbytes(s, 'invalid')
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   407
        Traceback (most recent call last):
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   408
            ...
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   409
        ConfigError: foo.invalid is not a byte quantity ('somevalue')
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   410
        """
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   411
19195
9311cd5c09ed ui: use util.sizetoint in configbytes
Bryan O'Sullivan <bryano@fb.com>
parents: 19085
diff changeset
   412
        value = self.config(section, name)
9311cd5c09ed ui: use util.sizetoint in configbytes
Bryan O'Sullivan <bryano@fb.com>
parents: 19085
diff changeset
   413
        if value is None:
19065
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   414
            if not isinstance(default, str):
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   415
                return default
19195
9311cd5c09ed ui: use util.sizetoint in configbytes
Bryan O'Sullivan <bryano@fb.com>
parents: 19085
diff changeset
   416
            value = default
19065
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   417
        try:
19195
9311cd5c09ed ui: use util.sizetoint in configbytes
Bryan O'Sullivan <bryano@fb.com>
parents: 19085
diff changeset
   418
            return util.sizetoint(value)
9311cd5c09ed ui: use util.sizetoint in configbytes
Bryan O'Sullivan <bryano@fb.com>
parents: 19085
diff changeset
   419
        except error.ParseError:
19065
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   420
            raise error.ConfigError(_("%s.%s is not a byte quantity ('%s')")
19195
9311cd5c09ed ui: use util.sizetoint in configbytes
Bryan O'Sullivan <bryano@fb.com>
parents: 19085
diff changeset
   421
                                    % (section, name, value))
19065
2c4cd1c42365 ui: add a configbytes method, for space configuration
Bryan O'Sullivan <bryano@fb.com>
parents: 18966
diff changeset
   422
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   423
    def configlist(self, section, name, default=None, untrusted=False):
14171
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   424
        """parse a configuration element as a list of comma/space separated
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   425
        strings
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   426
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   427
        >>> u = ui(); s = 'foo'
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   428
        >>> u.setconfig(s, 'list1', 'this,is "a small" ,test')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   429
        >>> u.configlist(s, 'list1')
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   430
        ['this', 'is', 'a small', 'test']
fa2b596db182 ui: add configint function and tests
Sune Foldager <cryo@cyanite.org>
parents: 14076
diff changeset
   431
        """
10982
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   432
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   433
        def _parse_plain(parts, s, offset):
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   434
            whitespace = False
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   435
            while offset < len(s) and (s[offset].isspace() or s[offset] == ','):
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   436
                whitespace = True
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   437
                offset += 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   438
            if offset >= len(s):
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   439
                return None, parts, offset
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   440
            if whitespace:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   441
                parts.append('')
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   442
            if s[offset] == '"' and not parts[-1]:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   443
                return _parse_quote, parts, offset + 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   444
            elif s[offset] == '"' and parts[-1][-1] == '\\':
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   445
                parts[-1] = parts[-1][:-1] + s[offset]
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   446
                return _parse_plain, parts, offset + 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   447
            parts[-1] += s[offset]
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   448
            return _parse_plain, parts, offset + 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   449
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   450
        def _parse_quote(parts, s, offset):
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   451
            if offset < len(s) and s[offset] == '"': # ""
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   452
                parts.append('')
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   453
                offset += 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   454
                while offset < len(s) and (s[offset].isspace() or
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   455
                        s[offset] == ','):
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   456
                    offset += 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   457
                return _parse_plain, parts, offset
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   458
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   459
            while offset < len(s) and s[offset] != '"':
11036
4efdccaca21d ui: fix check-code error
Henrik Stuart <hg@hstuart.dk>
parents: 10982
diff changeset
   460
                if (s[offset] == '\\' and offset + 1 < len(s)
4efdccaca21d ui: fix check-code error
Henrik Stuart <hg@hstuart.dk>
parents: 10982
diff changeset
   461
                        and s[offset + 1] == '"'):
10982
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   462
                    offset += 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   463
                    parts[-1] += '"'
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   464
                else:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   465
                    parts[-1] += s[offset]
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   466
                offset += 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   467
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   468
            if offset >= len(s):
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   469
                real_parts = _configlist(parts[-1])
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   470
                if not real_parts:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   471
                    parts[-1] = '"'
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   472
                else:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   473
                    real_parts[0] = '"' + real_parts[0]
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   474
                    parts = parts[:-1]
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   475
                    parts.extend(real_parts)
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   476
                return None, parts, offset
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   477
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   478
            offset += 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   479
            while offset < len(s) and s[offset] in [' ', ',']:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   480
                offset += 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   481
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   482
            if offset < len(s):
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   483
                if offset + 1 == len(s) and s[offset] == '"':
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   484
                    parts[-1] += '"'
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   485
                    offset += 1
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   486
                else:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   487
                    parts.append('')
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   488
            else:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   489
                return None, parts, offset
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   490
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   491
            return _parse_plain, parts, offset
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   492
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   493
        def _configlist(s):
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   494
            s = s.rstrip(' ,')
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   495
            if not s:
11945
5094e6b2f640 ui: differentiate empty configlist from None
Alecs King <alecsk@gmail.com>
parents: 11600
diff changeset
   496
                return []
10982
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   497
            parser, parts, offset = _parse_plain, [''], 0
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   498
            while parser:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   499
                parser, parts, offset = parser(parts, s, offset)
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   500
            return parts
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   501
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   502
        result = self.config(section, name, untrusted=untrusted)
2499
894435215344 Added ui.configlist method to get comma/space separated lists of strings.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2498
diff changeset
   503
        if result is None:
2502
18cf95ad3666 Allow using default values with ui.configlist, too, and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2499
diff changeset
   504
            result = default or []
18cf95ad3666 Allow using default values with ui.configlist, too, and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2499
diff changeset
   505
        if isinstance(result, basestring):
11309
ef7636efeb01 ui: handle leading newlines/spaces/commas in configlist
Thomas Arendsen Hein <thomas@intevation.de>
parents: 11302
diff changeset
   506
            result = _configlist(result.lstrip(' ,\n'))
10982
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   507
            if result is None:
0a548640e012 ui: support quotes in configlist (issue2147)
Henrik Stuart <hg@hstuart.dk>
parents: 10815
diff changeset
   508
                result = default or []
2502
18cf95ad3666 Allow using default values with ui.configlist, too, and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2499
diff changeset
   509
        return result
2499
894435215344 Added ui.configlist method to get comma/space separated lists of strings.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2498
diff changeset
   510
27696
e70c97cc9243 config: add hasconfig method and supporting plumbing
Bryan O'Sullivan <bos@serpentine.com>
parents: 27563
diff changeset
   511
    def hasconfig(self, section, name, untrusted=False):
e70c97cc9243 config: add hasconfig method and supporting plumbing
Bryan O'Sullivan <bos@serpentine.com>
parents: 27563
diff changeset
   512
        return self._data(untrusted).hasitem(section, name)
e70c97cc9243 config: add hasconfig method and supporting plumbing
Bryan O'Sullivan <bos@serpentine.com>
parents: 27563
diff changeset
   513
4487
1b5b98837bb5 ui: Rename has_config to has_section.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4258
diff changeset
   514
    def has_section(self, section, untrusted=False):
2343
af81d8770620 add ui.has_config method.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2335
diff changeset
   515
        '''tell whether section exists in config.'''
8199
e9f90e5989d9 ui: _get_cdata -> _data
Matt Mackall <mpm@selenic.com>
parents: 8198
diff changeset
   516
        return section in self._data(untrusted)
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   517
27253
f43988e5954c ui: optionally ignore sub-options from configitems()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27252
diff changeset
   518
    def configitems(self, section, untrusted=False, ignoresub=False):
8199
e9f90e5989d9 ui: _get_cdata -> _data
Matt Mackall <mpm@selenic.com>
parents: 8198
diff changeset
   519
        items = self._data(untrusted).items(section)
27253
f43988e5954c ui: optionally ignore sub-options from configitems()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27252
diff changeset
   520
        if ignoresub:
f43988e5954c ui: optionally ignore sub-options from configitems()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27252
diff changeset
   521
            newitems = {}
f43988e5954c ui: optionally ignore sub-options from configitems()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27252
diff changeset
   522
            for k, v in items:
f43988e5954c ui: optionally ignore sub-options from configitems()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27252
diff changeset
   523
                if ':' not in k:
f43988e5954c ui: optionally ignore sub-options from configitems()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27252
diff changeset
   524
                    newitems[k] = v
f43988e5954c ui: optionally ignore sub-options from configitems()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27252
diff changeset
   525
            items = newitems.items()
8204
797586be575d ui: report_untrusted fixes
Matt Mackall <mpm@selenic.com>
parents: 8203
diff changeset
   526
        if self.debugflag and not untrusted and self._reportuntrusted:
8222
d30a21594812 more whitespace cleanup and some other style nits
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8220
diff changeset
   527
            for k, v in self._ucfg.items(section):
8203
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   528
                if self._tcfg.get(section, k) != v:
14708
8083f4d00bd1 i18n: remove translation of debug messages
David Soria Parra <dsp@php.net>
parents: 14614
diff changeset
   529
                    self.debug("ignoring untrusted configuration option "
8083f4d00bd1 i18n: remove translation of debug messages
David Soria Parra <dsp@php.net>
parents: 14614
diff changeset
   530
                               "%s.%s = %s\n" % (section, k, v))
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8143
diff changeset
   531
        return items
285
5a1e6d27f399 ui: add configuration file support
mpm@selenic.com
parents: 249
diff changeset
   532
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   533
    def walkconfig(self, untrusted=False):
8203
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   534
        cfg = self._data(untrusted)
3377fa11af67 ui: privatize cdata vars
Matt Mackall <mpm@selenic.com>
parents: 8202
diff changeset
   535
        for section in cfg.sections():
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   536
            for name, value in self.configitems(section, untrusted):
13576
edd06611a7c6 ui: yield unchanged values in walkconfig
Martin Geisler <mg@aragost.com>
parents: 13493
diff changeset
   537
                yield section, name, value
1028
25e7ea0f2cff Add commands.debugconfig.
Bryan O'Sullivan <bos@serpentine.com>
parents: 981
diff changeset
   538
14372
be0daa0eeb3e ui: test plain mode against exceptions
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14171
diff changeset
   539
    def plain(self, feature=None):
11325
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   540
        '''is plain mode active?
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   541
13849
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   542
        Plain mode means that all configuration variables which affect
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   543
        the behavior and output of Mercurial should be
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   544
        ignored. Additionally, the output should be stable,
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   545
        reproducible and suitable for use in scripts or applications.
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   546
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   547
        The only way to trigger plain mode is by setting either the
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   548
        `HGPLAIN' or `HGPLAINEXCEPT' environment variables.
11325
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   549
14372
be0daa0eeb3e ui: test plain mode against exceptions
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14171
diff changeset
   550
        The return value can either be
be0daa0eeb3e ui: test plain mode against exceptions
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14171
diff changeset
   551
        - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT
be0daa0eeb3e ui: test plain mode against exceptions
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14171
diff changeset
   552
        - True otherwise
11325
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   553
        '''
13849
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   554
        if 'HGPLAIN' not in os.environ and 'HGPLAINEXCEPT' not in os.environ:
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   555
            return False
9f97de157aad HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   556
        exceptions = os.environ.get('HGPLAINEXCEPT', '').strip().split(',')
14372
be0daa0eeb3e ui: test plain mode against exceptions
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14171
diff changeset
   557
        if feature and exceptions:
be0daa0eeb3e ui: test plain mode against exceptions
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14171
diff changeset
   558
            return feature not in exceptions
be0daa0eeb3e ui: test plain mode against exceptions
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14171
diff changeset
   559
        return True
10455
40dfd46d098f ui: add HGPLAIN environment variable for easier scripting
Brodie Rao <me+hg@dackz.net>
parents: 10426
diff changeset
   560
608
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
   561
    def username(self):
1985
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   562
        """Return default username to be used in commits.
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   563
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   564
        Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   565
        and stop searching if one of these is set.
6862
7192876ac329 ui: add an option to prompt for the username when it isn't provided
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6762
diff changeset
   566
        If not found and ui.askusername is True, ask the user, else use
7192876ac329 ui: add an option to prompt for the username when it isn't provided
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6762
diff changeset
   567
        ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname".
1985
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   568
        """
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   569
        user = os.environ.get("HGUSER")
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   570
        if user is None:
21955
6dfb78f18bdb config: allow 'user' in .hgrc ui section (issue3169)
anatoly techtonik <techtonik@gmail.com>
parents: 21195
diff changeset
   571
            user = self.config("ui", ["username", "user"])
11225
d6dbd5e4ee72 ui.username(): expand environment variables in username configuration value.
Chad Dombrova <chadrik@gmail.com>
parents: 11036
diff changeset
   572
            if user is not None:
d6dbd5e4ee72 ui.username(): expand environment variables in username configuration value.
Chad Dombrova <chadrik@gmail.com>
parents: 11036
diff changeset
   573
                user = os.path.expandvars(user)
1985
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   574
        if user is None:
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   575
            user = os.environ.get("EMAIL")
6862
7192876ac329 ui: add an option to prompt for the username when it isn't provided
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6762
diff changeset
   576
        if user is None and self.configbool("ui", "askusername"):
7600
f7739cf3833c lowercase prompts
Martin Geisler <mg@daimi.au.dk>
parents: 7497
diff changeset
   577
            user = self.prompt(_("enter a commit username:"), default=None)
9613
c63c336ee2f7 ui: only use "user@host" as username in noninteractive mode
Martin Geisler <mg@lazybytes.net>
parents: 9610
diff changeset
   578
        if user is None and not self.interactive():
3721
98f2507c5551 only print a warning when no username is specified
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3678
diff changeset
   579
            try:
98f2507c5551 only print a warning when no username is specified
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3678
diff changeset
   580
                user = '%s@%s' % (util.getuser(), socket.getfqdn())
16940
6409a5c75125 ui: lowercase "no username" warning
Martin Geisler <mg@aragost.com>
parents: 16939
diff changeset
   581
                self.warn(_("no username found, using '%s' instead\n") % user)
3721
98f2507c5551 only print a warning when no username is specified
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3678
diff changeset
   582
            except KeyError:
4044
78a0dd93db0b Abort on empty username so specifying a username can be forced.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3984
diff changeset
   583
                pass
78a0dd93db0b Abort on empty username so specifying a username can be forced.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3984
diff changeset
   584
        if not user:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26451
diff changeset
   585
            raise error.Abort(_('no username supplied'),
28962
ad2cd2ef25d9 config: use single quotes around command hint
timeless <timeless@mozdev.org>
parents: 28958
diff changeset
   586
                             hint=_("use 'hg config --edit' "
20580
b75a23eec9c9 ui: fix extra space in username abort
Matt Mackall <mpm@selenic.com>
parents: 20574
diff changeset
   587
                                    'to set your username'))
6351
eed0a6a05096 ui: disallow newlines in usernames (issue1034)
Matt Mackall <mpm@selenic.com>
parents: 6333
diff changeset
   588
        if "\n" in user:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26451
diff changeset
   589
            raise error.Abort(_("username %s contains a newline\n")
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26451
diff changeset
   590
                              % repr(user))
1985
c577689006fa Adapted behaviour of ui.username() to documentation and mention it explicitly:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1984
diff changeset
   591
        return user
608
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
   592
1129
ee4f60abad93 Move generating short username to display in hg/hgweb annotate to ui module.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1071
diff changeset
   593
    def shortuser(self, user):
ee4f60abad93 Move generating short username to display in hg/hgweb annotate to ui module.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1071
diff changeset
   594
        """Return a short representation of a user name or email address."""
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   595
        if not self.verbose:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   596
            user = util.shortuser(user)
1129
ee4f60abad93 Move generating short username to display in hg/hgweb annotate to ui module.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1071
diff changeset
   597
        return user
ee4f60abad93 Move generating short username to display in hg/hgweb annotate to ui module.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1071
diff changeset
   598
2494
73ac95671788 push, outgoing, bundle: fall back to "default" if "default-push" not defined
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2470
diff changeset
   599
    def expandpath(self, loc, default=None):
1892
622ee75cb4c9 Directory names take precedence over symbolic names consistently.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1882
diff changeset
   600
        """Return repository location relative to cwd or from [paths]"""
26189
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   601
        try:
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   602
            p = self.paths.getpath(loc)
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   603
            if p:
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   604
                return p.rawloc
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   605
        except error.RepoError:
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   606
            pass
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   607
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   608
        if default:
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   609
            try:
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   610
                p = self.paths.getpath(default)
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   611
                if p:
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   612
                    return p.rawloc
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   613
            except error.RepoError:
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   614
                pass
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
   615
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
   616
        return loc
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
   617
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
   618
    @util.propertycache
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
   619
    def paths(self):
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
   620
        return paths(self)
506
1f81ebff98c9 [PATCH] Add ui.expandpath command
mpm@selenic.com
parents: 350
diff changeset
   621
27106
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   622
    def pushbuffer(self, error=False, subproc=False, labeled=False):
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23053
diff changeset
   623
        """install a buffer to capture standard output of the ui object
21132
350dc24a553d ui: pushbuffer can now also capture stderr
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20788
diff changeset
   624
24848
2f88821856eb ui: allow capture of subprocess output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24687
diff changeset
   625
        If error is True, the error output will be captured too.
2f88821856eb ui: allow capture of subprocess output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24687
diff changeset
   626
2f88821856eb ui: allow capture of subprocess output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24687
diff changeset
   627
        If subproc is True, output from subprocesses (typically hooks) will be
27106
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   628
        captured too.
10815
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   629
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   630
        If labeled is True, any labels associated with buffered
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   631
        output will be handled. By default, this has no effect
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   632
        on the output returned, but extensions and GUI tools may
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   633
        handle this argument and returned styled output. If output
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   634
        is being buffered so it can be captured and parsed or
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   635
        processed, labeled should not be set to True.
27106
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   636
        """
8202
4746113269c7 ui: buffers -> _buffers
Matt Mackall <mpm@selenic.com>
parents: 8201
diff changeset
   637
        self._buffers.append([])
27106
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   638
        self._bufferstates.append((error, subproc, labeled))
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   639
        self._bufferapplylabels = labeled
3737
9f5c46c50118 add a simple nested buffering scheme to ui
Matt Mackall <mpm@selenic.com>
parents: 3721
diff changeset
   640
27109
a93d53f79e6e ui: remove labeled argument from popbuffer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27106
diff changeset
   641
    def popbuffer(self):
a93d53f79e6e ui: remove labeled argument from popbuffer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27106
diff changeset
   642
        '''pop the last buffer and return the buffered output'''
21132
350dc24a553d ui: pushbuffer can now also capture stderr
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20788
diff changeset
   643
        self._bufferstates.pop()
27106
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   644
        if self._bufferstates:
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   645
            self._bufferapplylabels = self._bufferstates[-1][2]
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   646
        else:
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   647
            self._bufferapplylabels = None
6ef17697b03d ui: track label expansion when creating buffers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27068
diff changeset
   648
8202
4746113269c7 ui: buffers -> _buffers
Matt Mackall <mpm@selenic.com>
parents: 8201
diff changeset
   649
        return "".join(self._buffers.pop())
3737
9f5c46c50118 add a simple nested buffering scheme to ui
Matt Mackall <mpm@selenic.com>
parents: 3721
diff changeset
   650
10815
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   651
    def write(self, *args, **opts):
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   652
        '''write args to output
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   653
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   654
        By default, this method simply writes to the buffer or stdout,
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   655
        but extensions or GUI tools may override this method,
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   656
        write_err(), popbuffer(), and label() to style output from
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   657
        various parts of hg.
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   658
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   659
        An optional keyword argument, "label", can be passed in.
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   660
        This should be a string containing label names separated by
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   661
        space. Label names take the form of "topic.type". For example,
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   662
        ui.debug() issues a label of "ui.debug".
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   663
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   664
        When labeling output for a specific command, a label of
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   665
        "cmdname.type" is recommended. For example, status issues
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   666
        a label of "status.modified" for modified files.
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   667
        '''
28633
e35d7f131483 ui: add prompt argument to write (issue5154) (API)
timeless <timeless@mozdev.org>
parents: 28598
diff changeset
   668
        if self._buffers and not opts.get('prompt', False):
27110
f04bd381e8c0 ui: avoid needless casting to a str
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27109
diff changeset
   669
            self._buffers[-1].extend(a for a in args)
3737
9f5c46c50118 add a simple nested buffering scheme to ui
Matt Mackall <mpm@selenic.com>
parents: 3721
diff changeset
   670
        else:
27068
9eeca021a803 ui.write: don't clear progress bar when writing to a buffer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26896
diff changeset
   671
            self._progclear()
3737
9f5c46c50118 add a simple nested buffering scheme to ui
Matt Mackall <mpm@selenic.com>
parents: 3721
diff changeset
   672
            for a in args:
27110
f04bd381e8c0 ui: avoid needless casting to a str
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27109
diff changeset
   673
                self.fout.write(a)
565
9a80418646dd [PATCH] Make ui.warn write to stderr
mpm@selenic.com
parents: 515
diff changeset
   674
10815
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   675
    def write_err(self, *args, **opts):
25499
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
   676
        self._progclear()
1989
0541768fa558 ignore EPIPE in ui.err_write
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1985
diff changeset
   677
        try:
24848
2f88821856eb ui: allow capture of subprocess output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24687
diff changeset
   678
            if self._bufferstates and self._bufferstates[-1][0]:
21132
350dc24a553d ui: pushbuffer can now also capture stderr
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20788
diff changeset
   679
                return self.write(*args, **opts)
14614
afccc64eea73 ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents: 14612
diff changeset
   680
            if not getattr(self.fout, 'closed', False):
afccc64eea73 ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents: 14612
diff changeset
   681
                self.fout.flush()
1989
0541768fa558 ignore EPIPE in ui.err_write
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1985
diff changeset
   682
            for a in args:
27110
f04bd381e8c0 ui: avoid needless casting to a str
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27109
diff changeset
   683
                self.ferr.write(a)
4023
6ea8a3b805ee Flush stderr after write.
Patrick Mezard <pmezard@gmail.com>
parents: 3989
diff changeset
   684
            # stderr may be buffered under win32 when redirected to files,
6ea8a3b805ee Flush stderr after write.
Patrick Mezard <pmezard@gmail.com>
parents: 3989
diff changeset
   685
            # including stdout.
14614
afccc64eea73 ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents: 14612
diff changeset
   686
            if not getattr(self.ferr, 'closed', False):
afccc64eea73 ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents: 14612
diff changeset
   687
                self.ferr.flush()
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25629
diff changeset
   688
        except IOError as inst:
16367
c14898df3b92 ui: swallow EBADF on stderr
Kevin Bullock <kbullock@ringworld.org>
parents: 15919
diff changeset
   689
            if inst.errno not in (errno.EPIPE, errno.EIO, errno.EBADF):
1989
0541768fa558 ignore EPIPE in ui.err_write
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1985
diff changeset
   690
                raise
565
9a80418646dd [PATCH] Make ui.warn write to stderr
mpm@selenic.com
parents: 515
diff changeset
   691
1837
6f67a4c93493 make ui flush output. this makes error happen if printing to /dev/full.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1637
diff changeset
   692
    def flush(self):
14614
afccc64eea73 ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents: 14612
diff changeset
   693
        try: self.fout.flush()
16703
7292a4618f46 cleanup: replace more naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16683
diff changeset
   694
        except (IOError, ValueError): pass
14614
afccc64eea73 ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents: 14612
diff changeset
   695
        try: self.ferr.flush()
16703
7292a4618f46 cleanup: replace more naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16683
diff changeset
   696
        except (IOError, ValueError): pass
1837
6f67a4c93493 make ui flush output. this makes error happen if printing to /dev/full.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1637
diff changeset
   697
16751
2d432a1fc0db ui: add _isatty method to easily disable cooked I/O
Matt Mackall <mpm@selenic.com>
parents: 16703
diff changeset
   698
    def _isatty(self, fh):
2d432a1fc0db ui: add _isatty method to easily disable cooked I/O
Matt Mackall <mpm@selenic.com>
parents: 16703
diff changeset
   699
        if self.configbool('ui', 'nontty', False):
2d432a1fc0db ui: add _isatty method to easily disable cooked I/O
Matt Mackall <mpm@selenic.com>
parents: 16703
diff changeset
   700
            return False
2d432a1fc0db ui: add _isatty method to easily disable cooked I/O
Matt Mackall <mpm@selenic.com>
parents: 16703
diff changeset
   701
        return util.isatty(fh)
1837
6f67a4c93493 make ui flush output. this makes error happen if printing to /dev/full.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1637
diff changeset
   702
28542
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   703
    def interface(self, feature):
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   704
        """what interface to use for interactive console features?
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   705
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   706
        The interface is controlled by the value of `ui.interface` but also by
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   707
        the value of feature-specific configuration. For example:
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   708
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   709
        ui.interface.histedit = text
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   710
        ui.interface.chunkselector = curses
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   711
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   712
        Here the features are "histedit" and "chunkselector".
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   713
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   714
        The configuration above means that the default interfaces for commands
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   715
        is curses, the interface for histedit is text and the interface for
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   716
        selecting chunk is crecord (the best curses interface available).
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   717
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   718
        Consider the following exemple:
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   719
        ui.interface = curses
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   720
        ui.interface.histedit = text
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   721
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   722
        Then histedit will use the text interface and chunkselector will use
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   723
        the default curses interface (crecord at the moment).
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   724
        """
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   725
        alldefaults = frozenset(["text", "curses"])
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   726
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   727
        featureinterfaces = {
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   728
            "chunkselector": [
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   729
                "text",
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   730
                "curses",
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   731
            ]
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   732
        }
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   733
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   734
        # Feature-specific interface
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   735
        if feature not in featureinterfaces.keys():
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   736
            # Programming error, not user error
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   737
            raise ValueError("Unknown feature requested %s" % feature)
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   738
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   739
        availableinterfaces = frozenset(featureinterfaces[feature])
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   740
        if alldefaults > availableinterfaces:
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   741
            # Programming error, not user error. We need a use case to
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   742
            # define the right thing to do here.
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   743
            raise ValueError(
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   744
                "Feature %s does not handle all default interfaces" %
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   745
                feature)
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   746
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   747
        if self.plain():
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   748
            return "text"
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   749
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   750
        # Default interface for all the features
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   751
        defaultinterface = "text"
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   752
        i = self.config("ui", "interface", None)
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   753
        if i in alldefaults:
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   754
            defaultinterface = i
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   755
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   756
        choseninterface = defaultinterface
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   757
        f = self.config("ui", "interface.%s" % feature, None)
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   758
        if f in availableinterfaces:
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   759
            choseninterface = f
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   760
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   761
        if i is not None and defaultinterface != i:
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   762
            if f is not None:
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   763
                self.warn(_("invalid value for ui.interface: %s\n") %
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   764
                          (i,))
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   765
            else:
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   766
                self.warn(_("invalid value for ui.interface: %s (using %s)\n") %
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   767
                         (i, choseninterface))
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   768
        if f is not None and choseninterface != f:
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   769
            self.warn(_("invalid value for ui.interface.%s: %s (using %s)\n") %
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   770
                      (feature, f, choseninterface))
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   771
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   772
        return choseninterface
71e12fc53b80 ui: add new config flag for interface selection
Simon Farnsworth <simonfar@fb.com>
parents: 28498
diff changeset
   773
8208
32a2a1e244f1 ui: make interactive a method
Matt Mackall <mpm@selenic.com>
parents: 8206
diff changeset
   774
    def interactive(self):
11325
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   775
        '''is interactive input allowed?
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   776
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   777
        An interactive session is a session where input can be reasonably read
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   778
        from `sys.stdin'. If this function returns false, any attempt to read
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   779
        from stdin should fail with an error, unless a sensible default has been
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   780
        specified.
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   781
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   782
        Interactiveness is triggered by the value of the `ui.interactive'
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   783
        configuration variable or - if it is unset - when `sys.stdin' points
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   784
        to a terminal device.
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   785
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   786
        This function refers to input only; for output, see `ui.formatted()'.
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   787
        '''
8538
6419bc7b3d9c ui: honor interactive=off even if isatty()
Patrick Mezard <pmezard@gmail.com>
parents: 8527
diff changeset
   788
        i = self.configbool("ui", "interactive", None)
6419bc7b3d9c ui: honor interactive=off even if isatty()
Patrick Mezard <pmezard@gmail.com>
parents: 8527
diff changeset
   789
        if i is None:
14515
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 14373
diff changeset
   790
            # some environments replace stdin without implementing isatty
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 14373
diff changeset
   791
            # usually those are non-interactive
16751
2d432a1fc0db ui: add _isatty method to easily disable cooked I/O
Matt Mackall <mpm@selenic.com>
parents: 16703
diff changeset
   792
            return self._isatty(self.fin)
10077
89617aacb495 make ui.interactive() return false in case stdin lacks isatty
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 9887
diff changeset
   793
8538
6419bc7b3d9c ui: honor interactive=off even if isatty()
Patrick Mezard <pmezard@gmail.com>
parents: 8527
diff changeset
   794
        return i
8208
32a2a1e244f1 ui: make interactive a method
Matt Mackall <mpm@selenic.com>
parents: 8206
diff changeset
   795
12689
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   796
    def termwidth(self):
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   797
        '''how wide is the terminal in columns?
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   798
        '''
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   799
        if 'COLUMNS' in os.environ:
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   800
            try:
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   801
                return int(os.environ['COLUMNS'])
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   802
            except ValueError:
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   803
                pass
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   804
        return util.termwidth()
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12665
diff changeset
   805
11324
cdf6d861b207 ui: add ui.formatted configuration variable and accessor function.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11311
diff changeset
   806
    def formatted(self):
11325
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   807
        '''should formatted output be used?
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   808
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   809
        It is often desirable to format the output to suite the output medium.
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   810
        Examples of this are truncating long lines or colorizing messages.
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   811
        However, this is not often not desirable when piping output into other
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   812
        utilities, e.g. `grep'.
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   813
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   814
        Formatted output is triggered by the value of the `ui.formatted'
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   815
        configuration variable or - if it is unset - when `sys.stdout' points
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   816
        to a terminal device. Please note that `ui.formatted' should be
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   817
        considered an implementation detail; it is not intended for use outside
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   818
        Mercurial or its extensions.
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   819
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   820
        This function refers to output only; for input, see `ui.interactive()'.
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   821
        This function always returns false when in plain mode, see `ui.plain()'.
22a737306ba5 ui: document the formatted(), interactive() & plain() functions.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11324
diff changeset
   822
        '''
11324
cdf6d861b207 ui: add ui.formatted configuration variable and accessor function.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11311
diff changeset
   823
        if self.plain():
cdf6d861b207 ui: add ui.formatted configuration variable and accessor function.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11311
diff changeset
   824
            return False
cdf6d861b207 ui: add ui.formatted configuration variable and accessor function.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11311
diff changeset
   825
cdf6d861b207 ui: add ui.formatted configuration variable and accessor function.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11311
diff changeset
   826
        i = self.configbool("ui", "formatted", None)
cdf6d861b207 ui: add ui.formatted configuration variable and accessor function.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11311
diff changeset
   827
        if i is None:
14515
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 14373
diff changeset
   828
            # some environments replace stdout without implementing isatty
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 14373
diff changeset
   829
            # usually those are non-interactive
16751
2d432a1fc0db ui: add _isatty method to easily disable cooked I/O
Matt Mackall <mpm@selenic.com>
parents: 16703
diff changeset
   830
            return self._isatty(self.fout)
11324
cdf6d861b207 ui: add ui.formatted configuration variable and accessor function.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11311
diff changeset
   831
cdf6d861b207 ui: add ui.formatted configuration variable and accessor function.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11311
diff changeset
   832
        return i
cdf6d861b207 ui: add ui.formatted configuration variable and accessor function.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11311
diff changeset
   833
5337
8c5ef3b87cb1 Don't try to determine interactivity if ui() called with interactive=False.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5154
diff changeset
   834
    def _readline(self, prompt=''):
16751
2d432a1fc0db ui: add _isatty method to easily disable cooked I/O
Matt Mackall <mpm@selenic.com>
parents: 16703
diff changeset
   835
        if self._isatty(self.fin):
5036
ca0d02222d6a ui: get readline and prompt to behave better depending on interactivity
Bryan O'Sullivan <bos@serpentine.com>
parents: 4729
diff changeset
   836
            try:
ca0d02222d6a ui: get readline and prompt to behave better depending on interactivity
Bryan O'Sullivan <bos@serpentine.com>
parents: 4729
diff changeset
   837
                # magically add command line editing support, where
ca0d02222d6a ui: get readline and prompt to behave better depending on interactivity
Bryan O'Sullivan <bos@serpentine.com>
parents: 4729
diff changeset
   838
                # available
ca0d02222d6a ui: get readline and prompt to behave better depending on interactivity
Bryan O'Sullivan <bos@serpentine.com>
parents: 4729
diff changeset
   839
                import readline
ca0d02222d6a ui: get readline and prompt to behave better depending on interactivity
Bryan O'Sullivan <bos@serpentine.com>
parents: 4729
diff changeset
   840
                # force demandimport to really load the module
ca0d02222d6a ui: get readline and prompt to behave better depending on interactivity
Bryan O'Sullivan <bos@serpentine.com>
parents: 4729
diff changeset
   841
                readline.read_history_file
7496
0a27d0db256d issue1419: catch strange readline import error on windows
Brendan Cully <brendan@kublai.com>
parents: 7320
diff changeset
   842
                # windows sometimes raises something other than ImportError
0a27d0db256d issue1419: catch strange readline import error on windows
Brendan Cully <brendan@kublai.com>
parents: 7320
diff changeset
   843
            except Exception:
5036
ca0d02222d6a ui: get readline and prompt to behave better depending on interactivity
Bryan O'Sullivan <bos@serpentine.com>
parents: 4729
diff changeset
   844
                pass
14614
afccc64eea73 ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents: 14612
diff changeset
   845
15000
68b5d7005cca ui: call write() so the prompt string goes through subclassed implementation
Idan Kamara <idankk86@gmail.com>
parents: 14738
diff changeset
   846
        # call write() so output goes through subclassed implementation
68b5d7005cca ui: call write() so the prompt string goes through subclassed implementation
Idan Kamara <idankk86@gmail.com>
parents: 14738
diff changeset
   847
        # e.g. color extension on Windows
28633
e35d7f131483 ui: add prompt argument to write (issue5154) (API)
timeless <timeless@mozdev.org>
parents: 28598
diff changeset
   848
        self.write(prompt, prompt=True)
15000
68b5d7005cca ui: call write() so the prompt string goes through subclassed implementation
Idan Kamara <idankk86@gmail.com>
parents: 14738
diff changeset
   849
15062
0fc95f5cea57 ui: also swap sys.stdout with self.fout in _readline
Martin Geisler <mg@aragost.com>
parents: 15053
diff changeset
   850
        # instead of trying to emulate raw_input, swap (self.fin,
0fc95f5cea57 ui: also swap sys.stdout with self.fout in _readline
Martin Geisler <mg@aragost.com>
parents: 15053
diff changeset
   851
        # self.fout) with (sys.stdin, sys.stdout)
0fc95f5cea57 ui: also swap sys.stdout with self.fout in _readline
Martin Geisler <mg@aragost.com>
parents: 15053
diff changeset
   852
        oldin = sys.stdin
0fc95f5cea57 ui: also swap sys.stdout with self.fout in _readline
Martin Geisler <mg@aragost.com>
parents: 15053
diff changeset
   853
        oldout = sys.stdout
15000
68b5d7005cca ui: call write() so the prompt string goes through subclassed implementation
Idan Kamara <idankk86@gmail.com>
parents: 14738
diff changeset
   854
        sys.stdin = self.fin
15062
0fc95f5cea57 ui: also swap sys.stdout with self.fout in _readline
Martin Geisler <mg@aragost.com>
parents: 15053
diff changeset
   855
        sys.stdout = self.fout
22291
3b39e1522d8f ui: add brief comment why raw_input() needs dummy ' ' prompt string
Yuya Nishihara <yuya@tcha.org>
parents: 22205
diff changeset
   856
        # prompt ' ' must exist; otherwise readline may delete entire line
3b39e1522d8f ui: add brief comment why raw_input() needs dummy ' ' prompt string
Yuya Nishihara <yuya@tcha.org>
parents: 22205
diff changeset
   857
        # - http://bugs.python.org/issue12833
15053
17ffb30d9174 ui: pass ' ' to raw_input when prompting
Idan Kamara <idankk86@gmail.com>
parents: 15000
diff changeset
   858
        line = raw_input(' ')
15062
0fc95f5cea57 ui: also swap sys.stdout with self.fout in _readline
Martin Geisler <mg@aragost.com>
parents: 15053
diff changeset
   859
        sys.stdin = oldin
0fc95f5cea57 ui: also swap sys.stdout with self.fout in _readline
Martin Geisler <mg@aragost.com>
parents: 15053
diff changeset
   860
        sys.stdout = oldout
14614
afccc64eea73 ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents: 14612
diff changeset
   861
5613
2e76e5a23c2b workaround for raw_input() on Windows
Steve Borho <steve@borho.org>
parents: 5337
diff changeset
   862
        # When stdin is in binary mode on Windows, it can cause
2e76e5a23c2b workaround for raw_input() on Windows
Steve Borho <steve@borho.org>
parents: 5337
diff changeset
   863
        # raw_input() to emit an extra trailing carriage return
2e76e5a23c2b workaround for raw_input() on Windows
Steve Borho <steve@borho.org>
parents: 5337
diff changeset
   864
        if os.linesep == '\r\n' and line and line[-1] == '\r':
2e76e5a23c2b workaround for raw_input() on Windows
Steve Borho <steve@borho.org>
parents: 5337
diff changeset
   865
            line = line[:-1]
2e76e5a23c2b workaround for raw_input() on Windows
Steve Borho <steve@borho.org>
parents: 5337
diff changeset
   866
        return line
5036
ca0d02222d6a ui: get readline and prompt to behave better depending on interactivity
Bryan O'Sullivan <bos@serpentine.com>
parents: 4729
diff changeset
   867
9048
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   868
    def prompt(self, msg, default="y"):
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   869
        """Prompt user with msg, read response.
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   870
        If ui is not interactive, the default is returned.
5751
bc475d1f74ca prompt: kill matchflags
Kirill Smelkov <kirr@mns.spb.ru>
parents: 5709
diff changeset
   871
        """
8208
32a2a1e244f1 ui: make interactive a method
Matt Mackall <mpm@selenic.com>
parents: 8206
diff changeset
   872
        if not self.interactive():
28039
89003c49315c ui: fix crash by non-interactive prompt echo for user name
Yuya Nishihara <yuya@tcha.org>
parents: 27696
diff changeset
   873
            self.write(msg, ' ', default or '', "\n")
7320
8dca507e56ce ui: log non-interactive default response to stdout when verbose
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 6862
diff changeset
   874
            return default
9048
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   875
        try:
15053
17ffb30d9174 ui: pass ' ' to raw_input when prompting
Idan Kamara <idankk86@gmail.com>
parents: 15000
diff changeset
   876
            r = self._readline(self.label(msg, 'ui.prompt'))
9048
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   877
            if not r:
22589
9ab18a912c44 ui: show prompt choice if input is not a tty but is forced to be interactive
Mads Kiilerich <madski@unity3d.com>
parents: 22419
diff changeset
   878
                r = default
23053
5ba11ab48fcf ui: separate option to show prompt echo, enabled only in tests (issue4417)
Yuya Nishihara <yuya@tcha.org>
parents: 22837
diff changeset
   879
            if self.configbool('ui', 'promptecho'):
22589
9ab18a912c44 ui: show prompt choice if input is not a tty but is forced to be interactive
Mads Kiilerich <madski@unity3d.com>
parents: 22419
diff changeset
   880
                self.write(r, "\n")
9048
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   881
            return r
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   882
        except EOFError:
26896
5e46123e6c35 error: add structured exception for EOF at prompt
Siddharth Agarwal <sid0@fb.com>
parents: 26820
diff changeset
   883
            raise error.ResponseExpected()
9048
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   884
20265
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   885
    @staticmethod
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   886
    def extractchoices(prompt):
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   887
        """Extract prompt message and list of choices from specified prompt.
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   888
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   889
        This returns tuple "(message, choices)", and "choices" is the
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   890
        list of tuple "(response character, text without &)".
27392
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   891
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   892
        >>> ui.extractchoices("awake? $$ &Yes $$ &No")
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   893
        ('awake? ', [('y', 'Yes'), ('n', 'No')])
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   894
        >>> ui.extractchoices("line\\nbreak? $$ &Yes $$ &No")
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   895
        ('line\\nbreak? ', [('y', 'Yes'), ('n', 'No')])
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   896
        >>> ui.extractchoices("want lots of $$money$$?$$Ye&s$$N&o")
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   897
        ('want lots of $$money$$?', [('s', 'Yes'), ('o', 'No')])
20265
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   898
        """
27392
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   899
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   900
        # Sadly, the prompt string may have been built with a filename
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   901
        # containing "$$" so let's try to find the first valid-looking
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   902
        # prompt to start parsing. Sadly, we also can't rely on
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   903
        # choices containing spaces, ASCII, or basically anything
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   904
        # except an ampersand followed by a character.
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   905
        m = re.match(r'(?s)(.+?)\$\$([^\$]*&[^ \$].*)', prompt)
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   906
        msg = m.group(1)
00aa37c65e0a ui: try to handle $$ more robustly in prompts (issue4970)
Matt Mackall <mpm@selenic.com>
parents: 26820
diff changeset
   907
        choices = [p.strip(' ') for p in m.group(2).split('$$')]
20265
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   908
        return (msg,
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   909
                [(s[s.index('&') + 1].lower(), s.replace('&', '', 1))
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   910
                 for s in choices])
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   911
19226
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   912
    def promptchoice(self, prompt, default=0):
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   913
        """Prompt user with a message, read response, and ensure it matches
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   914
        one of the provided choices. The prompt is formatted as follows:
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   915
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   916
           "would you like fries with that (Yn)? $$ &Yes $$ &No"
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   917
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   918
        The index of the choice is returned. Responses are case
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   919
        insensitive. If ui is not interactive, the default is
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   920
        returned.
9048
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   921
        """
19226
c58b6ab4c26f ui: merge prompt text components into a singe string
Matt Mackall <mpm@selenic.com>
parents: 19195
diff changeset
   922
20265
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   923
        msg, choices = self.extractchoices(prompt)
e5803150ea1d ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19886
diff changeset
   924
        resps = [r for r, t in choices]
5671
b5605d88dc27 Make ui.prompt repeat on "unrecognized response" again (issue897)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5337
diff changeset
   925
        while True:
9048
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   926
            r = self.prompt(msg, resps[default])
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   927
            if r.lower() in resps:
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   928
                return resps.index(r.lower())
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   929
            self.write(_("unrecognized response\n"))
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8940
diff changeset
   930
2281
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2206
diff changeset
   931
    def getpass(self, prompt=None, default=None):
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   932
        if not self.interactive():
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   933
            return default
7798
57fee79e5588 catch CTRL-D at password prompt
Steve Borho <steve@borho.org>
parents: 7600
diff changeset
   934
        try:
19880
ba2be32d14f2 ui: send password prompts to stderr again (issue4056)
Matt Mackall <mpm@selenic.com>
parents: 19226
diff changeset
   935
            self.write_err(self.label(prompt or _('password: '), 'ui.prompt'))
21195
9336bc7dca8e cmdserver: forcibly use L channel to read password input (issue3161)
Yuya Nishihara <yuya@tcha.org>
parents: 21132
diff changeset
   936
            # disable getpass() only if explicitly specified. it's still valid
9336bc7dca8e cmdserver: forcibly use L channel to read password input (issue3161)
Yuya Nishihara <yuya@tcha.org>
parents: 21132
diff changeset
   937
            # to interact with tty even if fin is not a tty.
9336bc7dca8e cmdserver: forcibly use L channel to read password input (issue3161)
Yuya Nishihara <yuya@tcha.org>
parents: 21132
diff changeset
   938
            if self.configbool('ui', 'nontty'):
9336bc7dca8e cmdserver: forcibly use L channel to read password input (issue3161)
Yuya Nishihara <yuya@tcha.org>
parents: 21132
diff changeset
   939
                return self.fin.readline().rstrip('\n')
9336bc7dca8e cmdserver: forcibly use L channel to read password input (issue3161)
Yuya Nishihara <yuya@tcha.org>
parents: 21132
diff changeset
   940
            else:
9336bc7dca8e cmdserver: forcibly use L channel to read password input (issue3161)
Yuya Nishihara <yuya@tcha.org>
parents: 21132
diff changeset
   941
                return getpass.getpass('')
7798
57fee79e5588 catch CTRL-D at password prompt
Steve Borho <steve@borho.org>
parents: 7600
diff changeset
   942
        except EOFError:
26896
5e46123e6c35 error: add structured exception for EOF at prompt
Siddharth Agarwal <sid0@fb.com>
parents: 26820
diff changeset
   943
            raise error.ResponseExpected()
10815
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   944
    def status(self, *msg, **opts):
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   945
        '''write status message to output (if ui.quiet is False)
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   946
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   947
        This adds an output label of "ui.status".
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   948
        '''
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   949
        if not self.quiet:
10815
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   950
            opts['label'] = opts.get('label', '') + ' ui.status'
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   951
            self.write(*msg, **opts)
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   952
    def warn(self, *msg, **opts):
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   953
        '''write warning message to output (stderr)
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   954
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   955
        This adds an output label of "ui.warning".
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   956
        '''
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   957
        opts['label'] = opts.get('label', '') + ' ui.warning'
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   958
        self.write_err(*msg, **opts)
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   959
    def note(self, *msg, **opts):
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   960
        '''write note to output (if ui.verbose is True)
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   961
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   962
        This adds an output label of "ui.note".
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   963
        '''
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   964
        if self.verbose:
10815
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   965
            opts['label'] = opts.get('label', '') + ' ui.note'
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   966
            self.write(*msg, **opts)
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   967
    def debug(self, *msg, **opts):
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   968
        '''write debug message to output (if ui.debugflag is True)
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   969
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   970
        This adds an output label of "ui.debug".
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   971
        '''
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   972
        if self.debugflag:
10815
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   973
            opts['label'] = opts.get('label', '') + ' ui.debug'
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
   974
            self.write(*msg, **opts)
26750
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26587
diff changeset
   975
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26587
diff changeset
   976
    def edit(self, text, user, extra=None, editform=None, pending=None):
28635
87f92d6f0bc3 edit: allow to configure the suffix of the temporary filename
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28633
diff changeset
   977
        extra_defaults = {
87f92d6f0bc3 edit: allow to configure the suffix of the temporary filename
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28633
diff changeset
   978
            'prefix': 'editor',
87f92d6f0bc3 edit: allow to configure the suffix of the temporary filename
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28633
diff changeset
   979
            'suffix': '.txt',
87f92d6f0bc3 edit: allow to configure the suffix of the temporary filename
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28633
diff changeset
   980
        }
27153
3553e40d0770 ui: allow open editor with custom filename
Mykola Nikishov <mn@mn.com.ua>
parents: 27110
diff changeset
   981
        if extra is not None:
3553e40d0770 ui: allow open editor with custom filename
Mykola Nikishov <mn@mn.com.ua>
parents: 27110
diff changeset
   982
            extra_defaults.update(extra)
3553e40d0770 ui: allow open editor with custom filename
Mykola Nikishov <mn@mn.com.ua>
parents: 27110
diff changeset
   983
        extra = extra_defaults
3553e40d0770 ui: allow open editor with custom filename
Mykola Nikishov <mn@mn.com.ua>
parents: 27110
diff changeset
   984
        (fd, name) = tempfile.mkstemp(prefix='hg-' + extra['prefix'] + '-',
28635
87f92d6f0bc3 edit: allow to configure the suffix of the temporary filename
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28633
diff changeset
   985
                                      suffix=extra['suffix'], text=True)
1984
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
   986
        try:
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
   987
            f = os.fdopen(fd, "w")
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
   988
            f.write(text)
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
   989
            f.close()
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
   990
20605
a8aa699a812a ui: edit(): rebase, graft: set HGREVISION environment variable for an editor
Alexander Drozdov <al.drozdov@gmail.com>
parents: 20603
diff changeset
   991
            environ = {'HGUSER': user}
20606
be140ebd506b ui: edit(): transplant: set HGREVISION environment variable for an editor
Alexander Drozdov <al.drozdov@gmail.com>
parents: 20605
diff changeset
   992
            if 'transplant_source' in extra:
be140ebd506b ui: edit(): transplant: set HGREVISION environment variable for an editor
Alexander Drozdov <al.drozdov@gmail.com>
parents: 20605
diff changeset
   993
                environ.update({'HGREVISION': hex(extra['transplant_source'])})
24687
28d76bc069db editor: prefer 'intermediate-source' extra to use for HGREVISION environment variable
Alexander Drozdov <al.drozdov@gmail.com>
parents: 24663
diff changeset
   994
            for label in ('intermediate-source', 'source', 'rebase_source'):
20605
a8aa699a812a ui: edit(): rebase, graft: set HGREVISION environment variable for an editor
Alexander Drozdov <al.drozdov@gmail.com>
parents: 20603
diff changeset
   995
                if label in extra:
a8aa699a812a ui: edit(): rebase, graft: set HGREVISION environment variable for an editor
Alexander Drozdov <al.drozdov@gmail.com>
parents: 20603
diff changeset
   996
                    environ.update({'HGREVISION': extra[label]})
a8aa699a812a ui: edit(): rebase, graft: set HGREVISION environment variable for an editor
Alexander Drozdov <al.drozdov@gmail.com>
parents: 20603
diff changeset
   997
                    break
22205
9fa429723f26 ui: invoke editor for committing with HGEDITFORM environment variable
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21955
diff changeset
   998
            if editform:
9fa429723f26 ui: invoke editor for committing with HGEDITFORM environment variable
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21955
diff changeset
   999
                environ.update({'HGEDITFORM': editform})
26750
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26587
diff changeset
  1000
            if pending:
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26587
diff changeset
  1001
                environ.update({'HG_PENDING': pending})
20605
a8aa699a812a ui: edit(): rebase, graft: set HGREVISION environment variable for an editor
Alexander Drozdov <al.drozdov@gmail.com>
parents: 20603
diff changeset
  1002
5660
3c80ecdc1bcd Use VISUAL in addition to EDITOR when choosing the editor to use.
Osku Salerma <osku@iki.fi>
parents: 5613
diff changeset
  1003
            editor = self.geteditor()
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
  1004
23269
d9d8d2e0f701 ui: introduce util.system() wrapper to make sure ui.fout is used
Yuya Nishihara <yuya@tcha.org>
parents: 23139
diff changeset
  1005
            self.system("%s \"%s\"" % (editor, name),
20605
a8aa699a812a ui: edit(): rebase, graft: set HGREVISION environment variable for an editor
Alexander Drozdov <al.drozdov@gmail.com>
parents: 20603
diff changeset
  1006
                        environ=environ,
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26451
diff changeset
  1007
                        onerr=error.Abort, errprefix=_("edit failed"))
608
d2994b5298fb Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents: 565
diff changeset
  1008
1984
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
  1009
            f = open(name)
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
  1010
            t = f.read()
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
  1011
            f.close()
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
  1012
        finally:
df7436f439a0 Improved ui.edit():
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1983
diff changeset
  1013
            os.unlink(name)
662
b55a78595ef6 Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents: 613
diff changeset
  1014
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
  1015
        return t
2200
9f43b6e24232 move mail sending code into core, so extensions can share it.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2166
diff changeset
  1016
26312
60558319ce72 ui: avoid mutable default arguments
Siddharth Agarwal <sid0@fb.com>
parents: 26235
diff changeset
  1017
    def system(self, cmd, environ=None, cwd=None, onerr=None, errprefix=None):
23269
d9d8d2e0f701 ui: introduce util.system() wrapper to make sure ui.fout is used
Yuya Nishihara <yuya@tcha.org>
parents: 23139
diff changeset
  1018
        '''execute shell command with appropriate output stream. command
d9d8d2e0f701 ui: introduce util.system() wrapper to make sure ui.fout is used
Yuya Nishihara <yuya@tcha.org>
parents: 23139
diff changeset
  1019
        output will be redirected if fout is not stdout.
d9d8d2e0f701 ui: introduce util.system() wrapper to make sure ui.fout is used
Yuya Nishihara <yuya@tcha.org>
parents: 23139
diff changeset
  1020
        '''
24848
2f88821856eb ui: allow capture of subprocess output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24687
diff changeset
  1021
        out = self.fout
25149
3f0744eeaeaf cleanup: use __builtins__.any instead of util.any
Augie Fackler <augie@google.com>
parents: 25125
diff changeset
  1022
        if any(s[1] for s in self._bufferstates):
24848
2f88821856eb ui: allow capture of subprocess output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24687
diff changeset
  1023
            out = self
23269
d9d8d2e0f701 ui: introduce util.system() wrapper to make sure ui.fout is used
Yuya Nishihara <yuya@tcha.org>
parents: 23139
diff changeset
  1024
        return util.system(cmd, environ=environ, cwd=cwd, onerr=onerr,
24848
2f88821856eb ui: allow capture of subprocess output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24687
diff changeset
  1025
                           errprefix=errprefix, out=out)
23269
d9d8d2e0f701 ui: introduce util.system() wrapper to make sure ui.fout is used
Yuya Nishihara <yuya@tcha.org>
parents: 23139
diff changeset
  1026
18966
5572f688e0a9 ui: add 'force' parameter to traceback() to override the current print setting
Matt Harbison <matt_harbison@yahoo.com>
parents: 18965
diff changeset
  1027
    def traceback(self, exc=None, force=False):
5572f688e0a9 ui: add 'force' parameter to traceback() to override the current print setting
Matt Harbison <matt_harbison@yahoo.com>
parents: 18965
diff changeset
  1028
        '''print exception traceback if traceback printing enabled or forced.
2335
f0680b2d1d64 add ui.print_exc(), make all traceback printing central.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2293
diff changeset
  1029
        only to call in exception handler. returns true if traceback
f0680b2d1d64 add ui.print_exc(), make all traceback printing central.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2293
diff changeset
  1030
        printed.'''
18966
5572f688e0a9 ui: add 'force' parameter to traceback() to override the current print setting
Matt Harbison <matt_harbison@yahoo.com>
parents: 18965
diff changeset
  1031
        if self.tracebackflag or force:
18965
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1032
            if exc is None:
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1033
                exc = sys.exc_info()
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1034
            cause = getattr(exc[1], 'cause', None)
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1035
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1036
            if cause is not None:
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1037
                causetb = traceback.format_tb(cause[2])
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1038
                exctb = traceback.format_tb(exc[2])
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1039
                exconly = traceback.format_exception_only(cause[0], cause[1])
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1040
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1041
                # exclude frame where 'exc' was chained and rethrown from exctb
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1042
                self.write_err('Traceback (most recent call last):\n',
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1043
                               ''.join(exctb[:-1]),
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1044
                               ''.join(causetb),
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1045
                               ''.join(exconly))
0062508b1900 ui: add support for fully printing chained exception stacks in ui.traceback()
Matt Harbison <matt_harbison@yahoo.com>
parents: 18669
diff changeset
  1046
            else:
25568
c1ff82daed62 ui: flush stderr after printing a non-chained exception for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 25519
diff changeset
  1047
                output = traceback.format_exception(exc[0], exc[1], exc[2])
c1ff82daed62 ui: flush stderr after printing a non-chained exception for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 25519
diff changeset
  1048
                self.write_err(''.join(output))
18966
5572f688e0a9 ui: add 'force' parameter to traceback() to override the current print setting
Matt Harbison <matt_harbison@yahoo.com>
parents: 18965
diff changeset
  1049
        return self.tracebackflag or force
5660
3c80ecdc1bcd Use VISUAL in addition to EDITOR when choosing the editor to use.
Osku Salerma <osku@iki.fi>
parents: 5613
diff changeset
  1050
3c80ecdc1bcd Use VISUAL in addition to EDITOR when choosing the editor to use.
Osku Salerma <osku@iki.fi>
parents: 5613
diff changeset
  1051
    def geteditor(self):
3c80ecdc1bcd Use VISUAL in addition to EDITOR when choosing the editor to use.
Osku Salerma <osku@iki.fi>
parents: 5613
diff changeset
  1052
        '''return editor to use'''
16383
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16373
diff changeset
  1053
        if sys.platform == 'plan9':
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16373
diff changeset
  1054
            # vi is the MIPS instruction simulator on Plan 9. We
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16373
diff changeset
  1055
            # instead default to E to plumb commit messages to
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16373
diff changeset
  1056
            # avoid confusion.
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16373
diff changeset
  1057
            editor = 'E'
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16373
diff changeset
  1058
        else:
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16373
diff changeset
  1059
            editor = 'vi'
5660
3c80ecdc1bcd Use VISUAL in addition to EDITOR when choosing the editor to use.
Osku Salerma <osku@iki.fi>
parents: 5613
diff changeset
  1060
        return (os.environ.get("HGEDITOR") or
3c80ecdc1bcd Use VISUAL in addition to EDITOR when choosing the editor to use.
Osku Salerma <osku@iki.fi>
parents: 5613
diff changeset
  1061
                self.config("ui", "editor") or
3c80ecdc1bcd Use VISUAL in addition to EDITOR when choosing the editor to use.
Osku Salerma <osku@iki.fi>
parents: 5613
diff changeset
  1062
                os.environ.get("VISUAL") or
16383
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16373
diff changeset
  1063
                os.environ.get("EDITOR", editor))
9153
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1064
25499
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1065
    @util.propertycache
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1066
    def _progbar(self):
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1067
        """setup the progbar singleton to the ui object"""
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1068
        if (self.quiet or self.debugflag
25519
09e2cb2a00d7 progress: display progress bars by default with core Mercurial
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25499
diff changeset
  1069
                or self.configbool('progress', 'disable', False)
25499
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1070
                or not progress.shouldprint(self)):
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1071
            return None
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1072
        return getprogbar(self)
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1073
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1074
    def _progclear(self):
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1075
        """clear progress bar output if any. use it before any output"""
26781
1aee2ab0f902 spelling: trivial spell checking
Mads Kiilerich <madski@unity3d.com>
parents: 26750
diff changeset
  1076
        if '_progbar' not in vars(self): # nothing loaded yet
25499
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1077
            return
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1078
        if self._progbar is not None and self._progbar.printed:
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1079
            self._progbar.clear()
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1080
9153
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1081
    def progress(self, topic, pos, item="", unit="", total=None):
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1082
        '''show a progress message
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1083
28598
c30d5ca4945b progress: update comment to reflect implementation
timeless <timeless@mozdev.org>
parents: 28542
diff changeset
  1084
        By default a textual progress bar will be displayed if an operation
c30d5ca4945b progress: update comment to reflect implementation
timeless <timeless@mozdev.org>
parents: 28542
diff changeset
  1085
        takes too long. 'topic' is the current operation, 'item' is a
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17059
diff changeset
  1086
        non-numeric marker of the current position (i.e. the currently
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17059
diff changeset
  1087
        in-process file), 'pos' is the current numeric position (i.e.
9398
3fb8c6dbeeec ui: fix NameError in ui.progress due to unit/units typo
Brodie Rao <me+hg@dackz.net>
parents: 9312
diff changeset
  1088
        revision, bytes, etc.), unit is a corresponding unit label,
9153
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1089
        and total is the highest expected pos.
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1090
10425
f8a9de664a1c ui.progress: clarify termination requirement
Augie Fackler <durin42@gmail.com>
parents: 10383
diff changeset
  1091
        Multiple nested topics may be active at a time.
f8a9de664a1c ui.progress: clarify termination requirement
Augie Fackler <durin42@gmail.com>
parents: 10383
diff changeset
  1092
f8a9de664a1c ui.progress: clarify termination requirement
Augie Fackler <durin42@gmail.com>
parents: 10383
diff changeset
  1093
        All topics should be marked closed by setting pos to None at
f8a9de664a1c ui.progress: clarify termination requirement
Augie Fackler <durin42@gmail.com>
parents: 10383
diff changeset
  1094
        termination.
9153
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1095
        '''
25499
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1096
        if self._progbar is not None:
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1097
            self._progbar.progress(topic, pos, item=item, unit=unit,
0fa964d6fd48 progress: move all logic altering the ui object logic in mercurial.ui
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25498
diff changeset
  1098
                                   total=total)
25125
bd625cd4e5e7 progress: get the extremely verbose output out of default debug
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24883
diff changeset
  1099
        if pos is None or not self.configbool('progress', 'debug'):
9153
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1100
            return
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1101
9398
3fb8c6dbeeec ui: fix NameError in ui.progress due to unit/units typo
Brodie Rao <me+hg@dackz.net>
parents: 9312
diff changeset
  1102
        if unit:
3fb8c6dbeeec ui: fix NameError in ui.progress due to unit/units typo
Brodie Rao <me+hg@dackz.net>
parents: 9312
diff changeset
  1103
            unit = ' ' + unit
9153
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1104
        if item:
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1105
            item = ' ' + item
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1106
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1107
        if total:
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1108
            pct = 100.0 * pos / total
10220
500d09be7ace ui: display progress with decimal notation
Patrick Mezard <pmezard@gmail.com>
parents: 9851
diff changeset
  1109
            self.debug('%s:%s %s/%s%s (%4.2f%%)\n'
9398
3fb8c6dbeeec ui: fix NameError in ui.progress due to unit/units typo
Brodie Rao <me+hg@dackz.net>
parents: 9312
diff changeset
  1110
                     % (topic, item, pos, total, unit, pct))
9153
6adc899c98d0 Add ui.progress API
Matt Mackall <mpm@selenic.com>
parents: 9048
diff changeset
  1111
        else:
9749
1b1b33ae5a24 Related to Issue919: ui.progress, apparently unused before now, is busted.
Jesse Glick <jesse.glick@sun.com>
parents: 9613
diff changeset
  1112
            self.debug('%s:%s %s%s\n' % (topic, item, pos, unit))
10815
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1113
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents: 18054
diff changeset
  1114
    def log(self, service, *msg, **opts):
11984
2db0fccc8143 ui: add logging hook
Matt Mackall <mpm@selenic.com>
parents: 11970
diff changeset
  1115
        '''hook for logging facility extensions
2db0fccc8143 ui: add logging hook
Matt Mackall <mpm@selenic.com>
parents: 11970
diff changeset
  1116
2db0fccc8143 ui: add logging hook
Matt Mackall <mpm@selenic.com>
parents: 11970
diff changeset
  1117
        service should be a readily-identifiable subsystem, which will
2db0fccc8143 ui: add logging hook
Matt Mackall <mpm@selenic.com>
parents: 11970
diff changeset
  1118
        allow filtering.
26235
6c962145f523 ui: improve docs on ui.log
Augie Fackler <augie@google.com>
parents: 26189
diff changeset
  1119
6c962145f523 ui: improve docs on ui.log
Augie Fackler <augie@google.com>
parents: 26189
diff changeset
  1120
        *msg should be a newline-terminated format string to log, and
6c962145f523 ui: improve docs on ui.log
Augie Fackler <augie@google.com>
parents: 26189
diff changeset
  1121
        then any values to %-format into that format string.
6c962145f523 ui: improve docs on ui.log
Augie Fackler <augie@google.com>
parents: 26189
diff changeset
  1122
6c962145f523 ui: improve docs on ui.log
Augie Fackler <augie@google.com>
parents: 26189
diff changeset
  1123
        **opts currently has no defined meanings.
11984
2db0fccc8143 ui: add logging hook
Matt Mackall <mpm@selenic.com>
parents: 11970
diff changeset
  1124
        '''
2db0fccc8143 ui: add logging hook
Matt Mackall <mpm@selenic.com>
parents: 11970
diff changeset
  1125
10815
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1126
    def label(self, msg, label):
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1127
        '''style msg based on supplied label
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1128
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1129
        Like ui.write(), this just returns msg unchanged, but extensions
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1130
        and GUI tools can override it to allow styling output without
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1131
        writing it.
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1132
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1133
        ui.write(s, 'label') is equivalent to
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1134
        ui.write(ui.label(s, 'label')).
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1135
        '''
32b213b9b22c ui: add ui.write() output labeling API
Brodie Rao <brodie@bitheap.org>
parents: 10567
diff changeset
  1136
        return msg
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1137
27274
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1138
    def develwarn(self, msg, stacklevel=1):
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1139
        """issue a developer warning message
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1140
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1141
        Use 'stacklevel' to report the offender some layers further up in the
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1142
        stack.
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1143
        """
25629
52e5f68d8363 devel-warn: move the develwarn function as a method of the ui object
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25568
diff changeset
  1144
        msg = 'devel-warn: ' + msg
27274
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1145
        stacklevel += 1 # get in develwarn
25629
52e5f68d8363 devel-warn: move the develwarn function as a method of the ui object
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25568
diff changeset
  1146
        if self.tracebackflag:
27274
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1147
            util.debugstacktrace(msg, stacklevel, self.ferr, self.fout)
28498
d09be0b8a3c6 ui: log devel warnings
timeless <timeless@mozdev.org>
parents: 28218
diff changeset
  1148
            self.log('develwarn', '%s at:\n%s' %
d09be0b8a3c6 ui: log devel warnings
timeless <timeless@mozdev.org>
parents: 28218
diff changeset
  1149
                     (msg, ''.join(util.getstackframes(stacklevel))))
25629
52e5f68d8363 devel-warn: move the develwarn function as a method of the ui object
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25568
diff changeset
  1150
        else:
52e5f68d8363 devel-warn: move the develwarn function as a method of the ui object
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25568
diff changeset
  1151
            curframe = inspect.currentframe()
52e5f68d8363 devel-warn: move the develwarn function as a method of the ui object
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25568
diff changeset
  1152
            calframe = inspect.getouterframes(curframe, 2)
27274
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1153
            self.write_err('%s at: %s:%s (%s)\n'
82910fdc216f ui: add a 'stacklevel' argument to 'develwarn'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27266
diff changeset
  1154
                           % ((msg,) + calframe[stacklevel][1:4]))
28498
d09be0b8a3c6 ui: log devel warnings
timeless <timeless@mozdev.org>
parents: 28218
diff changeset
  1155
            self.log('develwarn', '%s at: %s:%s (%s)\n',
d09be0b8a3c6 ui: log devel warnings
timeless <timeless@mozdev.org>
parents: 28218
diff changeset
  1156
                     msg, *calframe[stacklevel][1:4])
25629
52e5f68d8363 devel-warn: move the develwarn function as a method of the ui object
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25568
diff changeset
  1157
27275
f2cd240f2f7c ui: add a 'deprecwarn' helper to issue deprecation warnings
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27274
diff changeset
  1158
    def deprecwarn(self, msg, version):
f2cd240f2f7c ui: add a 'deprecwarn' helper to issue deprecation warnings
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27274
diff changeset
  1159
        """issue a deprecation warning
f2cd240f2f7c ui: add a 'deprecwarn' helper to issue deprecation warnings
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27274
diff changeset
  1160
f2cd240f2f7c ui: add a 'deprecwarn' helper to issue deprecation warnings
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27274
diff changeset
  1161
        - msg: message explaining what is deprecated and how to upgrade,
f2cd240f2f7c ui: add a 'deprecwarn' helper to issue deprecation warnings
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27274
diff changeset
  1162
        - version: last version where the API will be supported,
f2cd240f2f7c ui: add a 'deprecwarn' helper to issue deprecation warnings
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27274
diff changeset
  1163
        """
29082
77de985d7c91 deprecation: gate deprecation warning behind devel configuration
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28962
diff changeset
  1164
        if not (self.configbool('devel', 'all-warnings')
77de985d7c91 deprecation: gate deprecation warning behind devel configuration
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28962
diff changeset
  1165
                or self.configbool('devel', 'deprec-warn')):
77de985d7c91 deprecation: gate deprecation warning behind devel configuration
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28962
diff changeset
  1166
            return
27275
f2cd240f2f7c ui: add a 'deprecwarn' helper to issue deprecation warnings
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27274
diff changeset
  1167
        msg += ("\n(compatibility will be dropped after Mercurial-%s,"
f2cd240f2f7c ui: add a 'deprecwarn' helper to issue deprecation warnings
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27274
diff changeset
  1168
                " update your code.)") % version
f2cd240f2f7c ui: add a 'deprecwarn' helper to issue deprecation warnings
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 27274
diff changeset
  1169
        self.develwarn(msg, stacklevel=2)
25629
52e5f68d8363 devel-warn: move the develwarn function as a method of the ui object
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25568
diff changeset
  1170
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1171
class paths(dict):
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1172
    """Represents a collection of paths and their configs.
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1173
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1174
    Data is initially derived from ui instances and the config files they have
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1175
    loaded.
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1176
    """
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1177
    def __init__(self, ui):
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1178
        dict.__init__(self)
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1179
27266
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1180
        for name, loc in ui.configitems('paths', ignoresub=True):
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1181
            # No location is the same as not existing.
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1182
            if not loc:
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1183
                continue
27266
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1184
            loc, sub = ui.configsuboptions('paths', name)
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1185
            self[name] = path(ui, name, rawloc=loc, suboptions=sub)
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1186
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1187
    def getpath(self, name, default=None):
27561
723413ee000e paths: make getpath() accept multiple defaults
Yuya Nishihara <yuya@tcha.org>
parents: 27500
diff changeset
  1188
        """Return a ``path`` from a string, falling back to default.
26056
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1189
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1190
        ``name`` can be a named path or locations. Locations are filesystem
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1191
        paths or URIs.
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1192
26189
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1193
        Returns None if ``name`` is not a registered path, a URI, or a local
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1194
        path to a repo.
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1195
        """
26189
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1196
        # Only fall back to default if no path was requested.
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1197
        if name is None:
27561
723413ee000e paths: make getpath() accept multiple defaults
Yuya Nishihara <yuya@tcha.org>
parents: 27500
diff changeset
  1198
            if not default:
723413ee000e paths: make getpath() accept multiple defaults
Yuya Nishihara <yuya@tcha.org>
parents: 27500
diff changeset
  1199
                default = ()
723413ee000e paths: make getpath() accept multiple defaults
Yuya Nishihara <yuya@tcha.org>
parents: 27500
diff changeset
  1200
            elif not isinstance(default, (tuple, list)):
723413ee000e paths: make getpath() accept multiple defaults
Yuya Nishihara <yuya@tcha.org>
parents: 27500
diff changeset
  1201
                default = (default,)
723413ee000e paths: make getpath() accept multiple defaults
Yuya Nishihara <yuya@tcha.org>
parents: 27500
diff changeset
  1202
            for k in default:
26189
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1203
                try:
27561
723413ee000e paths: make getpath() accept multiple defaults
Yuya Nishihara <yuya@tcha.org>
parents: 27500
diff changeset
  1204
                    return self[k]
26189
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1205
                except KeyError:
27561
723413ee000e paths: make getpath() accept multiple defaults
Yuya Nishihara <yuya@tcha.org>
parents: 27500
diff changeset
  1206
                    continue
723413ee000e paths: make getpath() accept multiple defaults
Yuya Nishihara <yuya@tcha.org>
parents: 27500
diff changeset
  1207
            return None
26189
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1208
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1209
        # Most likely empty string.
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1210
        # This may need to raise in the future.
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1211
        if not name:
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1212
            return None
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1213
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1214
        try:
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1215
            return self[name]
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1216
        except KeyError:
26056
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1217
            # Try to resolve as a local path or URI.
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1218
            try:
27265
47539ea08bdb ui: pass ui instance to path.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27264
diff changeset
  1219
                # We don't pass sub-options in, so no need to pass ui instance.
47539ea08bdb ui: pass ui instance to path.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27264
diff changeset
  1220
                return path(None, None, rawloc=name)
26056
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1221
            except ValueError:
26189
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1222
                raise error.RepoError(_('repository %s does not exist') %
663fbc336e22 ui: change default path fallback mechanism (issue4796)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26076
diff changeset
  1223
                                        name)
26056
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1224
27266
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1225
_pathsuboptions = {}
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1226
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1227
def pathsuboption(option, attr):
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1228
    """Decorator used to declare a path sub-option.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1229
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1230
    Arguments are the sub-option name and the attribute it should set on
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1231
    ``path`` instances.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1232
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1233
    The decorated function will receive as arguments a ``ui`` instance,
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1234
    ``path`` instance, and the string value of this option from the config.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1235
    The function should return the value that will be set on the ``path``
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1236
    instance.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1237
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1238
    This decorator can be used to perform additional verification of
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1239
    sub-options and to change the type of sub-options.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1240
    """
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1241
    def register(func):
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1242
        _pathsuboptions[option] = (attr, func)
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1243
        return func
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1244
    return register
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1245
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1246
@pathsuboption('pushurl', 'pushloc')
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1247
def pushurlpathoption(ui, path, value):
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1248
    u = util.url(value)
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1249
    # Actually require a URL.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1250
    if not u.scheme:
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1251
        ui.warn(_('(paths.%s:pushurl not a URL; ignoring)\n') % path.name)
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1252
        return None
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1253
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1254
    # Don't support the #foo syntax in the push URL to declare branch to
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1255
    # push.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1256
    if u.fragment:
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1257
        ui.warn(_('("#fragment" in paths.%s:pushurl not supported; '
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1258
                  'ignoring)\n') % path.name)
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1259
        u.fragment = None
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1260
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1261
    return str(u)
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1262
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1263
class path(object):
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1264
    """Represents an individual path and its configuration."""
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1265
27266
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1266
    def __init__(self, ui, name, rawloc=None, suboptions=None):
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1267
        """Construct a path from its config options.
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1268
27265
47539ea08bdb ui: pass ui instance to path.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27264
diff changeset
  1269
        ``ui`` is the ``ui`` instance the path is coming from.
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1270
        ``name`` is the symbolic name of the path.
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1271
        ``rawloc`` is the raw location, as defined in the config.
26064
1b1ab6ff58c4 ui: capture push location on path instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26056
diff changeset
  1272
        ``pushloc`` is the raw locations pushes should be made to.
26056
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1273
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1274
        If ``name`` is not defined, we require that the location be a) a local
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1275
        filesystem path with a .hg directory or b) a URL. If not,
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1276
        ``ValueError`` is raised.
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1277
        """
26056
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1278
        if not rawloc:
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1279
            raise ValueError('rawloc must be defined')
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1280
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1281
        # Locations may define branches via syntax <base>#<branch>.
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1282
        u = util.url(rawloc)
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1283
        branch = None
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1284
        if u.fragment:
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1285
            branch = u.fragment
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1286
            u.fragment = None
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1287
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1288
        self.url = u
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1289
        self.branch = branch
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1290
24250
9c32eea2ca04 ui: represent paths as classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23269
diff changeset
  1291
        self.name = name
26056
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1292
        self.rawloc = rawloc
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1293
        self.loc = str(u)
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1294
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1295
        # When given a raw location but not a symbolic name, validate the
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1296
        # location is valid.
26076
cebf7e48365e paths: move path validation logic to its own function
Durham Goode <durham@fb.com>
parents: 26064
diff changeset
  1297
        if not name and not u.scheme and not self._isvalidlocalpath(self.loc):
26056
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1298
            raise ValueError('location is not a URL or path to a local '
5f2a4fc3c4fa ui: move URL and path detection into path API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25989
diff changeset
  1299
                             'repo: %s' % rawloc)
25498
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1300
27266
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1301
        suboptions = suboptions or {}
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1302
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1303
        # Now process the sub-options. If a sub-option is registered, its
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1304
        # attribute will always be present. The value will be None if there
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1305
        # was no valid sub-option.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1306
        for suboption, (attr, func) in _pathsuboptions.iteritems():
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1307
            if suboption not in suboptions:
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1308
                setattr(self, attr, None)
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1309
                continue
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1310
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1311
            value = func(ui, self, suboptions[suboption])
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1312
            setattr(self, attr, value)
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1313
26076
cebf7e48365e paths: move path validation logic to its own function
Durham Goode <durham@fb.com>
parents: 26064
diff changeset
  1314
    def _isvalidlocalpath(self, path):
cebf7e48365e paths: move path validation logic to its own function
Durham Goode <durham@fb.com>
parents: 26064
diff changeset
  1315
        """Returns True if the given path is a potentially valid repository.
cebf7e48365e paths: move path validation logic to its own function
Durham Goode <durham@fb.com>
parents: 26064
diff changeset
  1316
        This is its own function so that extensions can change the definition of
cebf7e48365e paths: move path validation logic to its own function
Durham Goode <durham@fb.com>
parents: 26064
diff changeset
  1317
        'valid' in this case (like when pulling from a git repo into a hg
cebf7e48365e paths: move path validation logic to its own function
Durham Goode <durham@fb.com>
parents: 26064
diff changeset
  1318
        one)."""
cebf7e48365e paths: move path validation logic to its own function
Durham Goode <durham@fb.com>
parents: 26064
diff changeset
  1319
        return os.path.isdir(os.path.join(path, '.hg'))
cebf7e48365e paths: move path validation logic to its own function
Durham Goode <durham@fb.com>
parents: 26064
diff changeset
  1320
26064
1b1ab6ff58c4 ui: capture push location on path instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26056
diff changeset
  1321
    @property
27266
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1322
    def suboptions(self):
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1323
        """Return sub-options and their values for this path.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1324
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1325
        This is intended to be used for presentation purposes.
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1326
        """
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1327
        d = {}
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1328
        for subopt, (attr, _func) in _pathsuboptions.iteritems():
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1329
            value = getattr(self, attr)
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1330
            if value is not None:
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1331
                d[subopt] = value
4dccc37b87bd ui: support declaring path push urls as sub-options
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27265
diff changeset
  1332
        return d
26064
1b1ab6ff58c4 ui: capture push location on path instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26056
diff changeset
  1333
25498
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1334
# we instantiate one globally shared progress bar to avoid
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1335
# competing progress bars when multiple UI objects get created
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1336
_progresssingleton = None
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1337
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1338
def getprogbar(ui):
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1339
    global _progresssingleton
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1340
    if _progresssingleton is None:
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1341
        # passing 'ui' object to the singleton is fishy,
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1342
        # this is how the extension used to work but feel free to rework it.
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1343
        _progresssingleton = progress.progbar(ui)
7a5335ed7e1a progress: move the singleton logic to the ui module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25363
diff changeset
  1344
    return _progresssingleton