comparison hgsubversion/svnmeta.py @ 1267:5f86e2ac7659

svnmeta: add pre-transformation parameter to cached generator This allows us to do some custom transformation of a value when loading a config value.
author Sean Farley <sean.michael.farley@gmail.com>
date Mon, 24 Mar 2014 11:20:50 -0500
parents 4744b7bfa476
children e74fad41077b
comparison
equal deleted inserted replaced
1266:5ee3e5030a35 1267:5f86e2ac7659
59 # misc 59 # misc
60 self.branches = util.load(self.branch_info_file) or {} 60 self.branches = util.load(self.branch_info_file) or {}
61 self.prevbranches = dict(self.branches) 61 self.prevbranches = dict(self.branches)
62 self._layout = layouts.detect.layout_from_file(self) 62 self._layout = layouts.detect.layout_from_file(self)
63 63
64 def _get_cachedconfig(self, name, filename, configname, default): 64 def _get_cachedconfig(self, name, filename, configname, default, pre):
65 """Return a cached value for a config option. If the cache is uninitialized 65 """Return a cached value for a config option. If the cache is uninitialized
66 then try to read its value from disk. Option can be overridden by the 66 then try to read its value from disk. Option can be overridden by the
67 commandline. 67 commandline.
68 name: property name, e.g. 'lastpulled' 68 name: property name, e.g. 'lastpulled'
69 filename: name of file in .hg/svn 69 filename: name of file in .hg/svn
70 configname: commandline option name 70 configname: commandline option name
71 default: default value 71 default: default value
72 pre: transformation to apply to a value before caching it.
72 """ 73 """
73 varname = '_' + name 74 varname = '_' + name
74 if getattr(self, varname) is None: 75 if getattr(self, varname) is None:
75 # construct the file path from metapath (e.g. .hg/svn) plus the 76 # construct the file path from metapath (e.g. .hg/svn) plus the
76 # filename 77 # filename
97 98
98 # prefer the non-default, and the one sent from command-line 99 # prefer the non-default, and the one sent from command-line
99 if c is not None and c != val and c != default: 100 if c is not None and c != val and c != default:
100 val = c 101 val = c
101 102
103 # apply transformation if necessary
104 if pre:
105 val = pre(val)
106
102 # set the value as the one from disk (or default if not found) 107 # set the value as the one from disk (or default if not found)
103 setattr(self, varname, val) 108 setattr(self, varname, val)
104 109
105 # save the value to disk by using the setter property 110 # save the value to disk by using the setter property
106 setattr(self, name, val) 111 setattr(self, name, val)
112 f = os.path.join(self.metapath, filename) 117 f = os.path.join(self.metapath, filename)
113 setattr(self, varname, value) 118 setattr(self, varname, value)
114 util.dump(value, f) 119 util.dump(value, f)
115 120
116 def _gen_cachedconfig(self, name, default=None, filename=None, 121 def _gen_cachedconfig(self, name, default=None, filename=None,
117 configname=None): 122 configname=None, pre=None):
118 """Generate an attribute for reading (and caching) config data. 123 """Generate an attribute for reading (and caching) config data.
119 124
120 This method constructs a new attribute on self with the given name. 125 This method constructs a new attribute on self with the given name.
121 The actual value from the config file will be read lazily, and then 126 The actual value from the config file will be read lazily, and then
122 cached once that read has occurred. No cache invalidation will happen, 127 cached once that read has occurred. No cache invalidation will happen,
125 setattr(SVNMeta, '_' + name, None) 130 setattr(SVNMeta, '_' + name, None)
126 if filename is None: 131 if filename is None:
127 filename = name 132 filename = name
128 if configname is None: 133 if configname is None:
129 configname = name 134 configname = name
130 prop = property(lambda x: x._get_cachedconfig(name, 135 prop = property(lambda x: self._get_cachedconfig(name,
131 filename, 136 filename,
132 configname, 137 configname,
133 default), 138 default,
134 lambda x, y: x._set_cachedconfig(y, 139 pre=pre),
135 name, 140 lambda x, y: self._set_cachedconfig(y,
136 filename)) 141 name,
142 filename))
137 setattr(SVNMeta, name, prop) 143 setattr(SVNMeta, name, prop)
138 144
139 @property 145 @property
140 def layout_file(self): 146 def layout_file(self):
141 return os.path.join(self.metapath, 'layout') 147 return os.path.join(self.metapath, 'layout')