comparison hgsubversion/svnmeta.py @ 1181:eb4c7bc23f9e

svnmeta: add generator for cached svn configuration properties This will allow use to unify the reading and writing of configuration options into a central object to simplify their use sprinkled throughout the codebase. The idea is that after this patch, we will move each option to the svnmeta class thereby allowing us to remove lots of I/O cruft. Once the cruft is gone, we'll refactor objects where necessary. After refactoring, we'll have a framework for easily adding new configuration options.
author Sean Farley <sean.michael.farley@gmail.com>
date Mon, 24 Mar 2014 11:20:44 -0500
parents 8693c9558f1a
children 09b20039192c
comparison
equal deleted inserted replaced
1180:cdad3b3e4a09 1181:eb4c7bc23f9e
64 self.filemap.load(filemap) 64 self.filemap.load(filemap)
65 65
66 self.lastdate = '1970-01-01 00:00:00 -0000' 66 self.lastdate = '1970-01-01 00:00:00 -0000'
67 self.addedtags = {} 67 self.addedtags = {}
68 self.deletedtags = {} 68 self.deletedtags = {}
69
70 def _get_cachedconfig(self, name, filename, configname, default):
71 """Return a cached value for a config option. If the cache is uninitialized
72 then try to read its value from disk. Option can be overridden by the
73 commandline.
74 name: property name, e.g. 'lastpulled'
75 filename: name of file in .hg/svn
76 configname: commandline option name
77 default: default value
78 """
79 varname = '_' + name
80 if getattr(self, varname) is None:
81 # construct the file path from metapath (e.g. .hg/svn) plus the
82 # filename
83 f = os.path.join(self.metapath, filename)
84
85 # load the config property (i.e. command-line or .hgrc)
86 c = None
87 if configname:
88 # a little awkward but we need to convert the option from a
89 # string to whatever type the default value is, so we use the
90 # type of `default` to determine with ui.config method to call
91 c = None
92 if isinstance(default, bool):
93 c = self.ui.configbool('hgsubversion', configname, default)
94 elif isinstance(default, int):
95 c = self.ui.configint('hgsubversion', configname, default)
96 elif isinstance(default, list):
97 c = self.ui.configlist('hgsubversion', configname, default)
98 else:
99 c = self.ui.config('hgsubversion', configname, default)
100
101 # load the value from disk
102 val = util.load(f, default=default)
103
104 # prefer the non-default, and the one sent from command-line
105 if c and c != val and c != default:
106 val = c
107
108 # set the value as the one from disk (or default if not found)
109 setattr(self, varname, val)
110
111 # save the value to disk by using the setter property
112 setattr(self, name, val)
113
114 return getattr(self, varname)
115
116 def _set_cachedconfig(self, value, name, filename):
117 varname = '_' + name
118 f = os.path.join(self.metapath, filename)
119 setattr(self, varname, value)
120 util.dump(value, f)
121
122 def _gen_cachedconfig(self, name, default=None, filename=None,
123 configname=None):
124 """Generate an attribute for reading (and caching) config data.
125
126 This method constructs a new attribute on self with the given name.
127 The actual value from the config file will be read lazily, and then
128 cached once that read has occurred. No cache invalidation will happen,
129 so within a session these values shouldn't be required to mutate.
130 """
131 setattr(SVNMeta, '_' + name, None)
132 if filename is None:
133 filename = name
134 if configname is None:
135 configname = name
136 prop = property(lambda x: x._get_cachedconfig(name,
137 filename,
138 configname,
139 default),
140 lambda x, y: x._set_cachedconfig(y,
141 name,
142 filename))
143 setattr(SVNMeta, name, prop)
69 144
70 @property 145 @property
71 def layout(self): 146 def layout(self):
72 # this method can't determine the layout, but it needs to be 147 # this method can't determine the layout, but it needs to be
73 # resolved into something other than auto before this ever 148 # resolved into something other than auto before this ever