mercurial/ui.py
changeset 8142 912bfef12ba6
parent 8141 e40b629bedd1
child 8143 507c49e297e1
equal deleted inserted replaced
8141:e40b629bedd1 8142:912bfef12ba6
    18     if not sections:
    18     if not sections:
    19         sections = source.sections()
    19         sections = source.sections()
    20     for section in sections:
    20     for section in sections:
    21         if not dest.has_section(section):
    21         if not dest.has_section(section):
    22             dest.add_section(section)
    22             dest.add_section(section)
       
    23         if not source.has_section(section):
       
    24             continue
    23         for name, value in source.items(section, raw=True):
    25         for name, value in source.items(section, raw=True):
    24             dest.set(section, name, value)
    26             dest.set(section, name, value)
    25 
    27 
    26 class ui(object):
    28 class ui(object):
    27     def __init__(self, parentui=None):
    29     def __init__(self, parentui=None):
    37             self.overlay = util.configparser()
    39             self.overlay = util.configparser()
    38             self.cdata = util.configparser()
    40             self.cdata = util.configparser()
    39             self.ucdata = util.configparser()
    41             self.ucdata = util.configparser()
    40 
    42 
    41             # we always trust global config files
    43             # we always trust global config files
    42             self.readconfig(util.rcpath(), assumetrusted=True)
    44             for f in util.rcpath():
       
    45                 self.readconfig(f, assumetrusted=True)
    43         else:
    46         else:
    44             # parentui may point to an ui object which is already a child
    47             # parentui may point to an ui object which is already a child
    45             self.parentui = parentui.parentui or parentui
    48             self.parentui = parentui.parentui or parentui
    46             self.buffers = parentui.buffers
    49             self.buffers = parentui.buffers
    47             self.trusted_users = parentui.trusted_users.copy()
    50             self.trusted_users = parentui.trusted_users.copy()
    49             self.cdata = dupconfig(self.parentui.cdata)
    52             self.cdata = dupconfig(self.parentui.cdata)
    50             self.ucdata = dupconfig(self.parentui.ucdata)
    53             self.ucdata = dupconfig(self.parentui.ucdata)
    51 
    54 
    52             # we want the overlay from the parent, not the root
    55             # we want the overlay from the parent, not the root
    53             self.overlay = dupconfig(parentui.overlay)
    56             self.overlay = dupconfig(parentui.overlay)
       
    57             self.fixconfig()
    54 
    58 
    55     def __getattr__(self, key):
    59     def __getattr__(self, key):
    56         return getattr(self.parentui, key)
    60         return getattr(self.parentui, key)
    57 
    61 
    58     _isatty = None
    62     _isatty = None
    82         if self.report_untrusted:
    86         if self.report_untrusted:
    83             self.warn(_('Not trusting file %s from untrusted '
    87             self.warn(_('Not trusting file %s from untrusted '
    84                         'user %s, group %s\n') % (f, user, group))
    88                         'user %s, group %s\n') % (f, user, group))
    85         return False
    89         return False
    86 
    90 
    87     def readconfig(self, fn, root=None, assumetrusted=False):
    91     def readconfig(self, filename, root=None, assumetrusted=False,
       
    92                    sections = None):
       
    93         try:
       
    94             fp = open(filename)
       
    95         except IOError:
       
    96             if not sections: # ignore unless we were looking for something
       
    97                 return
       
    98             raise
       
    99 
    88         cdata = util.configparser()
   100         cdata = util.configparser()
    89 
   101         trusted = sections or assumetrusted or self._is_trusted(fp, filename)
    90         if isinstance(fn, basestring):
   102 
    91             fn = [fn]
   103         try:
    92         for f in fn:
   104             cdata.readfp(fp, filename)
    93             try:
   105         except ConfigParser.ParsingError, inst:
    94                 fp = open(f)
   106             msg = _("Failed to parse %s\n%s") % (filename, inst)
    95             except IOError:
       
    96                 continue
       
    97 
       
    98             trusted = assumetrusted or self._is_trusted(fp, f)
       
    99 
       
   100             try:
       
   101                 cdata.readfp(fp, f)
       
   102             except ConfigParser.ParsingError, inst:
       
   103                 msg = _("Failed to parse %s\n%s") % (f, inst)
       
   104                 if trusted:
       
   105                     raise util.Abort(msg)
       
   106                 self.warn(_("Ignored: %s\n") % msg)
       
   107 
       
   108             if trusted:
   107             if trusted:
   109                 updateconfig(cdata, self.cdata)
   108                 raise util.Abort(msg)
   110                 updateconfig(self.overlay, self.cdata)
   109             self.warn(_("Ignored: %s\n") % msg)
   111             updateconfig(cdata, self.ucdata)
   110 
   112             updateconfig(self.overlay, self.ucdata)
   111         if trusted:
       
   112             updateconfig(cdata, self.cdata, sections)
       
   113             updateconfig(self.overlay, self.cdata, sections)
       
   114         updateconfig(cdata, self.ucdata, sections)
       
   115         updateconfig(self.overlay, self.ucdata, sections)
   113 
   116 
   114         if root is None:
   117         if root is None:
   115             root = os.path.expanduser('~')
   118             root = os.path.expanduser('~')
   116         self.fixconfig(root=root)
   119         self.fixconfig(root=root)
   117 
       
   118     def readsections(self, filename, *sections):
       
   119         """Read filename and add only the specified sections to the config data
       
   120 
       
   121         The settings are added to the trusted config data.
       
   122         """
       
   123         if not sections:
       
   124             return
       
   125 
       
   126         cdata = util.configparser()
       
   127         try:
       
   128             try:
       
   129                 fp = open(filename)
       
   130             except IOError, inst:
       
   131                 raise util.Abort(_("unable to open %s: %s") %
       
   132                                  (filename, getattr(inst, "strerror", inst)))
       
   133             try:
       
   134                 cdata.readfp(fp, filename)
       
   135             finally:
       
   136                 fp.close()
       
   137         except ConfigParser.ParsingError, inst:
       
   138             raise util.Abort(_("failed to parse %s\n%s") % (filename, inst))
       
   139 
       
   140         for section in sections:
       
   141             if not cdata.has_section(section):
       
   142                 cdata.add_section(section)
       
   143 
       
   144         updateconfig(cdata, self.cdata, sections)
       
   145         updateconfig(cdata, self.ucdata, sections)
       
   146 
   120 
   147     def fixconfig(self, section=None, name=None, value=None, root=None):
   121     def fixconfig(self, section=None, name=None, value=None, root=None):
   148         # translate paths relative to root (or home) into absolute paths
   122         # translate paths relative to root (or home) into absolute paths
   149         if section is None or section == 'paths':
   123         if section is None or section == 'paths':
   150             if root is None:
   124             if root is None: