Mercurial > evolve
view hgext3rd/evolve/state.py @ 4814:48b30ff742cb
python3: use format-source to run byteify-strings in .py files
Using the format-source extension smooth out the pain of merging after
auto-formatting.
This change makes all of the Evolve test suite pass under python3 and has
added benefit of being 100% automated using mercurial's `byteify-strings`
script version 1.0 (revision 11498aa91c036c6d70f7ac5ee5af2664a84a1130).
How to benefit from the help of format-source is explained in the README.
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Tue, 06 Aug 2019 15:06:38 +0200 |
parents | d04be27dcce9 |
children | 38ce7fe4d3f2 |
line wrap: on
line source
# This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. """ This file contains class to wrap the state for commands and other related logic. All the data related to the command state is stored as dictionary in the object. The class has methods using which the data can be stored to disk in a file under .hg/ directory. We store the data on disk in cbor, for which we use cbor library to serialize and deserialize data. """ from __future__ import absolute_import import errno import struct from .thirdparty import cbor from mercurial import ( error, util, ) from mercurial.i18n import _ class cmdstate(): """a wrapper class to store the state of commands like `evolve`, `pick` All the data for the state is stored in the form of key-value pairs in a dictionary. The class object can write all the data to a file in .hg/ directory and also can populate the object data reading that file """ def __init__(self, repo, path=b'evolvestate', opts={}): self._repo = repo self.path = path self.opts = opts def __nonzero__(self): return self.exists() __bool__ = __nonzero__ def __contains__(self, key): return key in self.opts def __getitem__(self, key): return self.opts[key] def get(self, key, default=None): return self.opts.get(key, default) def __setitem__(self, key, value): updates = {key: value} self.opts.update(updates) def load(self): """load the existing evolvestate file into the class object""" op = self._read() if isinstance(op, dict): self.opts.update(op) elif self.path == b'evolvestate': # it is the old evolvestate file oldop = _oldevolvestateread(self._repo) self.opts.update(oldop) def addopts(self, opts): """add more key-value pairs to the data stored by the object""" self.opts.update(opts) def save(self): """write all the evolvestate data stored in .hg/evolvestate file we use third-party library cbor to serialize data to write in the file. """ with self._repo.vfs(self.path, b'wb', atomictemp=True) as fp: cbor.dump(self.opts, fp) def _read(self): """reads the evolvestate file and returns a dictionary which contain data in the same format as it was before storing""" with self._repo.vfs(self.path, b'rb') as fp: return cbor.load(fp) def delete(self): """drop the evolvestate file if exists""" util.unlinkpath(self._repo.vfs.join(self.path), ignoremissing=True) def exists(self): """check whether the evolvestate file exists or not""" return self._repo.vfs.exists(self.path) def _oldevolvestateread(repo): """function to read the old evolvestate file This exists for BC reasons.""" try: f = repo.vfs(b'evolvestate') except IOError as err: if err.errno != errno.ENOENT: raise try: versionblob = f.read(4) if len(versionblob) < 4: repo.ui.debug(b'ignoring corrupted evolvestate (file contains %i bits)' % len(versionblob)) return None version = struct._unpack(b'>I', versionblob)[0] if version != 0: msg = _(b'unknown evolvestate version %i') % version raise error.Abort(msg, hint=_(b'upgrade your evolve')) records = [] data = f.read() off = 0 end = len(data) while off < end: rtype = data[off] off += 1 length = struct._unpack(b'>I', data[off:(off + 4)])[0] off += 4 record = data[off:(off + length)] off += length if rtype == b't': rtype, record = record[0], record[1:] records.append((rtype, record)) state = {} for rtype, rdata in records: if rtype == b'C': state[b'current'] = rdata elif rtype.lower(): repo.ui.debug(b'ignore evolve state record type %s' % rtype) else: raise error.Abort(_(b"unknown evolvestate field type '%s'") % rtype, hint=_(b'upgrade your evolve')) return state finally: f.close()