# HG changeset patch # User Jun Wu # Date 1465833726 -3600 # Node ID 8cfe074cd46313753cfe5ae1d50871ab16510e1a # Parent 5d4888f3cd12c89cfdcd91599e58e5434d8f809b maps: use util.fileproperty for RevMap.lastpulled It simplifies code a lot, and will avoid code duplication for the upcoming SqliteRevMap. diff --git a/hgsubversion/maps.py b/hgsubversion/maps.py --- a/hgsubversion/maps.py +++ b/hgsubversion/maps.py @@ -342,6 +342,9 @@ class RevMap(dict): VERSION = 1 + lastpulled = util.fileproperty('_lastpulled', lambda x: x._lastpulled_file, + default=0, deserializer=int) + def __init__(self, revmap_path, lastpulled_path): dict.__init__(self) self._filepath = revmap_path @@ -349,30 +352,11 @@ class RevMap(dict): self._hashes = None self.firstpulled = 0 - if os.path.exists(self._lastpulled_file): - with open(self._lastpulled_file) as f: - self._lastpulled = int(f.read()) - else: - self._lastpulled = 0 - if os.path.isfile(self._filepath): self._load() else: self._write() - def _writelastpulled(self): - with open(self._lastpulled_file, 'w') as f: - f.write('%d\n' % self.lastpulled) - - @property - def lastpulled(self): - return self._lastpulled - - @lastpulled.setter - def lastpulled(self, value): - self._lastpulled = value - self._writelastpulled() - def hashes(self): if self._hashes is None: self._hashes = dict((v, k) for (k, v) in self.iteritems()) @@ -417,8 +401,7 @@ class RevMap(dict): with open(self._filepath, 'a') as f: f.write(''.join('%s %s %s\n' % (revnum, hex(binhash), br or '') for revnum, br, binhash in items)) - with open(self._lastpulled_file, 'w') as f: - f.write('%s\n' % lastpulled) + self.lastpulled = lastpulled def _readmapfile(self): path = self._filepath @@ -437,9 +420,6 @@ class RevMap(dict): def _load(self): lastpulled = self.lastpulled firstpulled = self.firstpulled - if os.path.exists(self._lastpulled_file): - with open(self._lastpulled_file) as f: - lastpulled = int(f.read()) setitem = dict.__setitem__ for l in self._readmapfile(): revnum, ha, branch = l.split(' ', 2) @@ -460,7 +440,6 @@ class RevMap(dict): def _write(self): with open(self._filepath, 'w') as f: f.write('%s\n' % self.VERSION) - self._writelastpulled() def __setitem__(self, key, ha): revnum, branch = key