Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 2.1, 2.1.1, and 2.1.2

hgrcutil: new hgrcutil module, which contains functions to manipulate hgrc files

For now it contains two functions: loadIniFile, taken from qtlib, and
setConfigValue, taken from hglib.

Changeset 8db65cfc4e1f

Parent a4b404ee094c

by Angel Ezquerra

Changes to 7 files · Browse files at 8db65cfc4e1f Showing diff from parent a4b404ee094c Diff from another changeset...

 
16
17
18
 
19
20
21
 
803
804
805
806
 
807
808
809
 
826
827
828
829
 
830
831
832
 
849
850
851
852
 
853
854
855
 
16
17
18
19
20
21
22
 
804
805
806
 
807
808
809
810
 
827
828
829
 
830
831
832
833
 
850
851
852
 
853
854
855
856
@@ -16,6 +16,7 @@
 from tortoisehg.hgqt.i18n import _  from tortoisehg.util import hglib, shlib, wconfig, bugtraq  from tortoisehg.hgqt import qtlib, qscilib, status, cmdui, branchop, revpanel +from tortoisehg.hgqt import hgrcutil    # Technical Debt for CommitWidget  # disable commit button while no message is entered or no files are selected @@ -803,7 +804,7 @@
  self.saveToPath(hglib.user_rcpath())     def saveToPath(self, path): - fn, cfg = qtlib.loadIniFile(path, self) + fn, cfg = hgrcutil.loadIniFile(path, self)   if not hasattr(cfg, 'write'):   qtlib.WarningMsgBox(_('Unable to save username'),   _('Iniparse must be installed.'), parent=self) @@ -826,7 +827,7 @@
    def savePushAfter(self):   path = os.path.join(self.repo.root, '.hg', 'hgrc') - fn, cfg = qtlib.loadIniFile([path], self) + fn, cfg = hgrcutil.loadIniFile([path], self)   if not hasattr(cfg, 'write'):   qtlib.WarningMsgBox(_('Unable to save after commit push'),   _('Iniparse must be installed.'), parent=self) @@ -849,7 +850,7 @@
    def saveAutoInc(self):   path = os.path.join(self.repo.root, '.hg', 'hgrc') - fn, cfg = qtlib.loadIniFile([path], self) + fn, cfg = hgrcutil.loadIniFile([path], self)   if not hasattr(cfg, 'write'):   qtlib.WarningMsgBox(_('Unable to save auto include list'),   _('Iniparse must be installed.'), parent=self)
Change 1 of 1 Show Entire File tortoisehg/​hgqt/​hgrcutil.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@@ -0,0 +1,56 @@
+# hgrcutils.py - Functions to manipulate hgrc (or similar) files +# +# Copyright 2011 Angel Ezquerra <angel.ezquerra@gmail.com> +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2 or any later version. + +import os + +from tortoisehg.hgqt import qtlib +from tortoisehg.util import wconfig + +def loadIniFile(rcpath, parent=None): + 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\n') + f.close() + break + except EnvironmentError: + pass + else: + qtlib.WarningMsgBox(_('Unable to create a config file'), + _('Insufficient access rights.'), parent=parent) + return None, {} + + return fn, wconfig.readfile(fn) + +def setConfigValue(rcfilepath, cfgpath, value): + ''' + Set a value on a config file, such as an hgrc or a .ini file + + rcpfilepath: Absolute path to a configuration file + cfgpath: Full "path" of a configurable key + Format is section.keyNamee.g. 'web.name') + value: String value for the selected config key + ''' + fn, cfg = loadIniFile([rcfilepath]) + if not hasattr(cfg, 'write'): + return False + if fn is None: + return False + cfgFullKey = cfgpath.split('.') + if len(cfgFullKey) < 2: + return False + cfg.set(cfgFullKey[0], cfgFullKey[1], value) + try: + wconfig.writefile(cfg, fn) + except EnvironmentError, e: + return False + return True
 
17
18
19
20
 
21
22
23
 
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
17
18
19
 
20
21
22
23
 
68
69
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
72
73
@@ -17,7 +17,7 @@
   from mercurial import extensions, error, util   -from tortoisehg.util import hglib, paths, wconfig +from tortoisehg.util import hglib, paths  from tortoisehg.hgqt.i18n import _  from hgext.color import _styles   @@ -68,27 +68,6 @@
  return   QDesktopServices.openUrl(QUrl(fullurl))   -def loadIniFile(rcpath, parent=None): - 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\n') - f.close() - break - except EnvironmentError: - pass - else: - WarningMsgBox(_('Unable to create a config file'), - _('Insufficient access rights.'), parent=parent) - return None, {} - - return fn, wconfig.readfile(fn) -  def editfiles(repo, files, lineno=None, search=None, parent=None):   if len(files) == 1:   path = repo.wjoin(files[0])
 
12
13
14
15
 
16
17
18
 
288
289
290
291
 
292
293
294
 
12
13
14
 
15
16
17
18
 
288
289
290
 
291
292
293
294
@@ -12,7 +12,7 @@
   from tortoisehg.util import hglib  from tortoisehg.hgqt.i18n import _ -from tortoisehg.hgqt import qtlib +from tortoisehg.hgqt import qtlib, hgrcutil    from PyQt4.QtCore import *  from PyQt4.QtGui import * @@ -288,7 +288,7 @@
  if column == 0:   shortname = hglib.fromunicode(value.toString())   abshgrcpath = os.path.join(self.rootpath(), '.hg', 'hgrc') - if not hglib.setConfigValue(abshgrcpath, 'web.name', shortname): + if not hgrcutil.setConfigValue(abshgrcpath, 'web.name', shortname):   qtlib.WarningMsgBox(_('Unable to update repository name'),   _('An error occurred while updating the repository hgrc '   'file (%s)' % abshgrcpath))
 
21
22
23
24
 
25
26
 
27
28
29
 
34
35
36
37
 
38
39
40
 
21
22
23
 
24
25
 
26
27
28
29
 
34
35
36
 
37
38
39
40
@@ -21,9 +21,9 @@
   from mercurial import error, node, merge as mergemod   -from tortoisehg.util import hglib, paths +from tortoisehg.util import hglib, paths  from tortoisehg.hgqt.i18n import _ -from tortoisehg.hgqt import cmdui, csinfo, qtlib, thgrepo, resolve +from tortoisehg.hgqt import cmdui, csinfo, thgrepo, resolve, hgrcutil  from tortoisehg.hgqt.update import UpdateDialog    class rUpdateDialog(UpdateDialog): @@ -34,7 +34,7 @@
  # Get configured paths   self.paths = {}   fn = self.repo.join('hgrc') - fn, cfg = qtlib.loadIniFile([fn], self) + fn, cfg = hgrcutil.loadIniFile([fn], self)   if 'paths' in cfg:   for alias in cfg['paths']:   self.paths[ alias ] = cfg['paths'][alias]
 
19
20
21
22
 
23
24
25
 
349
350
351
352
 
353
354
355
 
897
898
899
900
 
901
902
903
 
995
996
997
998
 
999
1000
1001
 
1065
1066
1067
1068
 
1069
1070
1071
 
1233
1234
1235
1236
 
1237
1238
1239
 
19
20
21
 
22
23
24
25
 
349
350
351
 
352
353
354
355
 
897
898
899
 
900
901
902
903
 
995
996
997
 
998
999
1000
1001
 
1065
1066
1067
 
1068
1069
1070
1071
 
1233
1234
1235
 
1236
1237
1238
1239
@@ -19,7 +19,7 @@
   from tortoisehg.util import hglib, wconfig  from tortoisehg.hgqt.i18n import _ -from tortoisehg.hgqt import qtlib, cmdui, thgrepo, rebase, resolve +from tortoisehg.hgqt import qtlib, cmdui, thgrepo, rebase, resolve, hgrcutil    def parseurl(path):   if path.startswith('ssh://'): @@ -349,7 +349,7 @@
  # Refresh configured paths   self.paths = {}   fn = self.repo.join('hgrc') - fn, cfg = qtlib.loadIniFile([fn], self) + fn, cfg = hgrcutil.loadIniFile([fn], self)   if 'paths' in cfg:   for alias in cfg['paths']:   self.paths[ alias ] = cfg['paths'][ alias ] @@ -897,7 +897,7 @@
  def removeAlias(self, alias):   alias = hglib.fromunicode(alias)   fn = self.repo.join('hgrc') - fn, cfg = qtlib.loadIniFile([fn], self) + fn, cfg = hgrcutil.loadIniFile([fn], self)   if not hasattr(cfg, 'write'):   qtlib.WarningMsgBox(_('Unable to remove URL'),   _('Iniparse must be installed.'), parent=self) @@ -995,7 +995,7 @@
    def accept(self):   path = self.repo.join('hgrc') - fn, cfg = qtlib.loadIniFile([path], self) + fn, cfg = hgrcutil.loadIniFile([path], self)   if not hasattr(cfg, 'write'):   qtlib.WarningMsgBox(_('Unable to save post pull operation'),   _('Iniparse must be installed.'), parent=self) @@ -1065,7 +1065,7 @@
    def accept(self):   fn = self.repo.join('hgrc') - fn, cfg = qtlib.loadIniFile([fn], self) + fn, cfg = hgrcutil.loadIniFile([fn], self)   if not hasattr(cfg, 'write'):   qtlib.WarningMsgBox(_('Unable to save an URL'),   _('Iniparse must be installed.'), parent=self) @@ -1233,7 +1233,7 @@
    def accept(self):   path = hglib.user_rcpath() - fn, cfg = qtlib.loadIniFile(path, self) + fn, cfg = hgrcutil.loadIniFile(path, self)   if not hasattr(cfg, 'write'):   qtlib.WarningMsgBox(_('Unable to save authentication'),   _('Iniparse must be installed.'), parent=self)
 
16
17
18
19
20
21
22
23
 
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
 
16
17
18
 
 
19
20
21
 
720
721
722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@@ -16,8 +16,6 @@
 from mercurial import demandimport, revset  from mercurial import dispatch as hgdispatch   -from tortoisehg.hgqt import qtlib -from tortoisehg.util import wconfig    demandimport.disable()  try: @@ -722,24 +720,3 @@
  else:   # hg <= 1.8   return hgdispatch._dispatch(ui, args) - -def setConfigValue(rcfilepath, cfgpath, value): - ''' - rcpfilepath - absolute path to a configuration file - cfpath - full path of a configurable, ex 'web.name' - value - string value or None. if None, the configurable is removed - ''' - fn, cfg = qtlib.loadIniFile([rcfilepath]) - if not hasattr(cfg, 'write'): - return False - if fn is None: - return False - cfgpath = cfgpath.split('.') - if len(cfgpath) < 2: - return False - cfg.set(cfgpath[0], cfgpath[1], value) - try: - wconfig.writefile(cfg, fn) - except EnvironmentError, e: - return False - return True