##// END OF EJS Templates
state: removing remaining instances of opts class variable...
Pulkit Goyal -
r38162:6f67bfe4 default
parent child Browse files
Show More
@@ -1,85 +1,84
1 # state.py - writing and reading state files in Mercurial
1 # state.py - writing and reading state files in Mercurial
2 #
2 #
3 # Copyright 2018 Pulkit Goyal <pulkitmgoyal@gmail.com>
3 # Copyright 2018 Pulkit Goyal <pulkitmgoyal@gmail.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 """
8 """
9 This file contains class to wrap the state for commands and other
9 This file contains class to wrap the state for commands and other
10 related logic.
10 related logic.
11
11
12 All the data related to the command state is stored as dictionary in the object.
12 All the data related to the command state is stored as dictionary in the object.
13 The class has methods using which the data can be stored to disk in a file under
13 The class has methods using which the data can be stored to disk in a file under
14 .hg/ directory.
14 .hg/ directory.
15
15
16 We store the data on disk in cbor, for which we use the third party cbor library
16 We store the data on disk in cbor, for which we use the third party cbor library
17 to serialize and deserialize data.
17 to serialize and deserialize data.
18 """
18 """
19
19
20 from __future__ import absolute_import
20 from __future__ import absolute_import
21
21
22 from .thirdparty import cbor
22 from .thirdparty import cbor
23
23
24 from . import (
24 from . import (
25 error,
25 error,
26 util,
26 util,
27 )
27 )
28
28
29 class cmdstate(object):
29 class cmdstate(object):
30 """a wrapper class to store the state of commands like `rebase`, `graft`,
30 """a wrapper class to store the state of commands like `rebase`, `graft`,
31 `histedit`, `shelve` etc. Extensions can also use this to write state files.
31 `histedit`, `shelve` etc. Extensions can also use this to write state files.
32
32
33 All the data for the state is stored in the form of key-value pairs in a
33 All the data for the state is stored in the form of key-value pairs in a
34 dictionary.
34 dictionary.
35
35
36 The class object can write all the data to a file in .hg/ directory and
36 The class object can write all the data to a file in .hg/ directory and
37 can populate the object data reading that file.
37 can populate the object data reading that file.
38
38
39 Uses cbor to serialize and deserialize data while writing and reading from
39 Uses cbor to serialize and deserialize data while writing and reading from
40 disk.
40 disk.
41 """
41 """
42
42
43 def __init__(self, repo, fname, opts=None):
43 def __init__(self, repo, fname):
44 """ repo is the repo object
44 """ repo is the repo object
45 fname is the file name in which data should be stored in .hg directory
45 fname is the file name in which data should be stored in .hg directory
46 opts is a dictionary of data of the statefile
47 """
46 """
48 self._repo = repo
47 self._repo = repo
49 self.fname = fname
48 self.fname = fname
50
49
51 def read(self):
50 def read(self):
52 """read the existing state file and return a dict of data stored"""
51 """read the existing state file and return a dict of data stored"""
53 return self._read()
52 return self._read()
54
53
55 def save(self, version, data):
54 def save(self, version, data):
56 """write all the state data stored to .hg/<filename> file
55 """write all the state data stored to .hg/<filename> file
57
56
58 we use third-party library cbor to serialize data to write in the file.
57 we use third-party library cbor to serialize data to write in the file.
59 """
58 """
60 if not isinstance(version, int):
59 if not isinstance(version, int):
61 raise error.ProgrammingError("version of state file should be"
60 raise error.ProgrammingError("version of state file should be"
62 " an integer")
61 " an integer")
63
62
64 with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp:
63 with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp:
65 fp.write('%d\n' % version)
64 fp.write('%d\n' % version)
66 cbor.dump(self.opts, fp, canonical=True)
65 cbor.dump(data, fp, canonical=True)
67
66
68 def _read(self):
67 def _read(self):
69 """reads the state file and returns a dictionary which contain
68 """reads the state file and returns a dictionary which contain
70 data in the same format as it was before storing"""
69 data in the same format as it was before storing"""
71 with self._repo.vfs(self.fname, 'rb') as fp:
70 with self._repo.vfs(self.fname, 'rb') as fp:
72 try:
71 try:
73 int(fp.readline())
72 int(fp.readline())
74 except ValueError:
73 except ValueError:
75 raise error.CorruptedState("unknown version of state file"
74 raise error.CorruptedState("unknown version of state file"
76 " found")
75 " found")
77 return cbor.load(fp)
76 return cbor.load(fp)
78
77
79 def delete(self):
78 def delete(self):
80 """drop the state file if exists"""
79 """drop the state file if exists"""
81 util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True)
80 util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True)
82
81
83 def exists(self):
82 def exists(self):
84 """check whether the state file exists or not"""
83 """check whether the state file exists or not"""
85 return self._repo.vfs.exists(self.fname)
84 return self._repo.vfs.exists(self.fname)
General Comments 0
You need to be logged in to leave comments. Login now