Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.9, 1.9.1, and 1.9.2

sync: factor out a function to read a config file

Will need this to add global auth entries

Changeset 630ef3ed1f08

Parent b0c0553dce30

by Steve Borho

Changes to one file · Browse files at 630ef3ed1f08 Showing diff from parent b0c0553dce30 Diff from another changeset...

 
13
14
15
16
 
17
18
19
 
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 
136
137
138
 
141
142
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
145
146
 
13
14
15
 
16
17
18
19
 
114
115
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
118
119
120
 
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@@ -13,7 +13,7 @@
 from PyQt4.QtCore import *  from PyQt4.QtGui import *   -from mercurial import url +from mercurial import url, config    from tortoisehg.util import hglib  from tortoisehg.hgqt.i18n import _ @@ -114,25 +114,7 @@
    def refresh(self):   fn = os.path.join(self.root, '.hg', 'hgrc') - try: - import iniparse - # Monkypatch this regex to prevent iniparse from considering - # 'rem' as a comment - iniparse.ini.CommentLine.regex = \ - re.compile(r'^(?P<csep>[%;#])(?P<comment>.*)$') - cfg = iniparse.INIConfig(file(fn), optionxformvalue=None) - self.readonly = False - except ImportError: - from mercurial import config - cfg = config.config() - cfg.read(fn) - self.readonly = True - except Exception, e: - qtlib.WarningMsgBox(_('Unable to parse a config file'), - _('%s\nReverting to read-only mode.') % str(e), - parent=self) - self.readonly = True - cfg = {} + cfg = self.loadIniFile([fn])   self.paths = {}   if 'paths' not in cfg:   return @@ -141,6 +123,46 @@
  tm = PathsModel(self.paths, self)   self.tv.setModel(tm)   + def loadIniFile(self, rcpath): + for fn in rcpath: + if os.path.exists(fn): + break + else: + for fn in rcpath: + # Try to create a file from rcpath + try: + f = open(fn, 'w') + f.write('# Generated by TortoiseHg setting dialog\n') + f.close() + break + except EnvironmentError: + pass + else: + qtlib.WarningMsgBox(_('Unable to create a config file'), + _('Insufficient access rights.'), parent=self) + fn = rcpath[0] + cfg = config.config() + return cfg + try: + import iniparse + # Monkypatch this regex to prevent iniparse from considering + # 'rem' as a comment + iniparse.ini.CommentLine.regex = \ + re.compile(r'^(?P<csep>[%;#])(?P<comment>.*)$') + return iniparse.INIConfig(file(fn), optionxformvalue=None) + except ImportError: + cfg = config.config() + cfg.read(fn) + return cfg + except Exception, e: + qtlib.WarningMsgBox(_('Unable to parse a config file'), + hglib.tounicode(e), parent=self) + cfg = config.config() + try: + cfg.read(fn) + except: + pass + return cfg     def refreshUrl(self):   'User has changed schema/host/port/path'