author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Wed, 04 Sep 2019 00:53:27 +0200 | |
changeset 42988 | f4caf910669e |
parent 42987 | 36a0a1951d64 |
child 42989 | 50d9de61ce02 |
permissions | -rw-r--r-- |
42728
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
1 |
# flagutils.py - code to deal with revlog flags and their processors |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
2 |
# |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
3 |
# Copyright 2016 Remi Chaintron <remi@fb.com> |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
4 |
# Copyright 2016-2019 Pierre-Yves David <pierre-yves.david@ens-lyon.org> |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
5 |
# |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
6 |
# This software may be used and distributed according to the terms of the |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
7 |
# GNU General Public License version 2 or any later version. |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
8 |
|
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
9 |
from __future__ import absolute_import |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
10 |
|
42731
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
11 |
from ..i18n import _ |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
12 |
|
42728
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
13 |
from .constants import ( |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
14 |
REVIDX_DEFAULT_FLAGS, |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
15 |
REVIDX_ELLIPSIS, |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
16 |
REVIDX_EXTSTORED, |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
17 |
REVIDX_FLAGS_ORDER, |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
18 |
REVIDX_ISCENSORED, |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
19 |
REVIDX_RAWTEXT_CHANGING_FLAGS, |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
20 |
) |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
21 |
|
42730
92ac6b1697a7
flagutil: move REVIDX_KNOWN_FLAGS source of truth in flagutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42729
diff
changeset
|
22 |
from .. import ( |
42731
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
23 |
error, |
42730
92ac6b1697a7
flagutil: move REVIDX_KNOWN_FLAGS source of truth in flagutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42729
diff
changeset
|
24 |
util |
92ac6b1697a7
flagutil: move REVIDX_KNOWN_FLAGS source of truth in flagutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42729
diff
changeset
|
25 |
) |
92ac6b1697a7
flagutil: move REVIDX_KNOWN_FLAGS source of truth in flagutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42729
diff
changeset
|
26 |
|
42728
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
27 |
# blanked usage of all the name to prevent pyflakes constraints |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
28 |
# We need these name available in the module for extensions. |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
29 |
REVIDX_ISCENSORED |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
30 |
REVIDX_ELLIPSIS |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
31 |
REVIDX_EXTSTORED |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
32 |
REVIDX_DEFAULT_FLAGS |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
33 |
REVIDX_FLAGS_ORDER |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
34 |
REVIDX_RAWTEXT_CHANGING_FLAGS |
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
35 |
|
42730
92ac6b1697a7
flagutil: move REVIDX_KNOWN_FLAGS source of truth in flagutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42729
diff
changeset
|
36 |
REVIDX_KNOWN_FLAGS = util.bitsfrom(REVIDX_FLAGS_ORDER) |
92ac6b1697a7
flagutil: move REVIDX_KNOWN_FLAGS source of truth in flagutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42729
diff
changeset
|
37 |
|
42729
05c80f9ef100
flagutil: move the `flagprocessors` mapping in the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42728
diff
changeset
|
38 |
# Store flag processors (cf. 'addflagprocessor()' to register) |
05c80f9ef100
flagutil: move the `flagprocessors` mapping in the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42728
diff
changeset
|
39 |
flagprocessors = { |
05c80f9ef100
flagutil: move the `flagprocessors` mapping in the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42728
diff
changeset
|
40 |
REVIDX_ISCENSORED: None, |
05c80f9ef100
flagutil: move the `flagprocessors` mapping in the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42728
diff
changeset
|
41 |
} |
42728
ca5ca3badd3c
flagutil: create a `mercurial.revlogutils.flagutil` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
42 |
|
42732
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
43 |
def addflagprocessor(flag, processor): |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
44 |
"""Register a flag processor on a revision data flag. |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
45 |
|
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
46 |
Invariant: |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
47 |
- Flags need to be defined in REVIDX_KNOWN_FLAGS and REVIDX_FLAGS_ORDER, |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
48 |
and REVIDX_RAWTEXT_CHANGING_FLAGS if they can alter rawtext. |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
49 |
- Only one flag processor can be registered on a specific flag. |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
50 |
- flagprocessors must be 3-tuples of functions (read, write, raw) with the |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
51 |
following signatures: |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
52 |
- (read) f(self, rawtext) -> text, bool |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
53 |
- (write) f(self, text) -> rawtext, bool |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
54 |
- (raw) f(self, rawtext) -> bool |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
55 |
"text" is presented to the user. "rawtext" is stored in revlog data, not |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
56 |
directly visible to the user. |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
57 |
The boolean returned by these transforms is used to determine whether |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
58 |
the returned text can be used for hash integrity checking. For example, |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
59 |
if "write" returns False, then "text" is used to generate hash. If |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
60 |
"write" returns True, that basically means "rawtext" returned by "write" |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
61 |
should be used to generate hash. Usually, "write" and "read" return |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
62 |
different booleans. And "raw" returns a same boolean as "write". |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
63 |
|
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
64 |
Note: The 'raw' transform is used for changegroup generation and in some |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
65 |
debug commands. In this case the transform only indicates whether the |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
66 |
contents can be used for hash integrity checks. |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
67 |
""" |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
68 |
insertflagprocessor(flag, processor, flagprocessors) |
6d61be152c55
flagutil: move addflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42731
diff
changeset
|
69 |
|
42731
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
70 |
def insertflagprocessor(flag, processor, flagprocessors): |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
71 |
if not flag & REVIDX_KNOWN_FLAGS: |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
72 |
msg = _("cannot register processor on unknown flag '%#x'.") % (flag) |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
73 |
raise error.ProgrammingError(msg) |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
74 |
if flag not in REVIDX_FLAGS_ORDER: |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
75 |
msg = _("flag '%#x' undefined in REVIDX_FLAGS_ORDER.") % (flag) |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
76 |
raise error.ProgrammingError(msg) |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
77 |
if flag in flagprocessors: |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
78 |
msg = _("cannot register multiple processors on flag '%#x'.") % (flag) |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
79 |
raise error.Abort(msg) |
5109217a9ab6
flagutil: move insertflagprocessor to the new module (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42730
diff
changeset
|
80 |
flagprocessors[flag] = processor |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
81 |
|
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
82 |
class flagprocessorsmixin(object): |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
83 |
"""basic mixin to support revlog flag processing |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
84 |
|
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
85 |
Make sure the `_flagprocessors` attribute is set at ``__init__`` time. |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
86 |
|
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
87 |
See the documentation of the ``_processflags`` method for details. |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
88 |
""" |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
89 |
|
42873
7907008a0bb5
flagutil: make the error class used by the mixin configurable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42871
diff
changeset
|
90 |
_flagserrorclass = error.RevlogError |
7907008a0bb5
flagutil: make the error class used by the mixin configurable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42871
diff
changeset
|
91 |
|
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
92 |
def _processflags(self, text, flags, operation, raw=False): |
42875
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
93 |
"""deprecated entry point to access flag processors""" |
42881
519b45603880
flagprocessors: deprecate _processflags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42875
diff
changeset
|
94 |
msg = ('_processflag(...) use the specialized variant') |
519b45603880
flagprocessors: deprecate _processflags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42875
diff
changeset
|
95 |
util.nouideprecwarn(msg, '5.2', stacklevel=2) |
42875
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
96 |
if raw: |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
97 |
return text, self._processflagsraw(text, flags) |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
98 |
elif operation == 'read': |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
99 |
return self._processflagsread(text, flags) |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
100 |
else: # write operation |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
101 |
return self._processflagswrite(text, flags) |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
102 |
|
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
103 |
def _processflagsread(self, text, flags): |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
104 |
"""Inspect revision data flags and applies read transformations defined |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
105 |
by registered flag processors. |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
106 |
|
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
107 |
``text`` - the revision data to process |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
108 |
``flags`` - the revision flags |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
109 |
``raw`` - an optional argument describing if the raw transform should be |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
110 |
applied. |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
111 |
|
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
112 |
This method processes the flags in the order (or reverse order if |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
113 |
``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
114 |
flag processors registered for present flags. The order of flags defined |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
115 |
in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity. |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
116 |
|
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
117 |
Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
118 |
processed text and ``validatehash`` is a bool indicating whether the |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
119 |
returned text should be checked for hash integrity. |
42875
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
120 |
""" |
42984
66dc5a522f37
flagprocessors: return flagdata in the main processing function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42983
diff
changeset
|
121 |
return self._processflagsfunc(text, flags, 'read') |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
122 |
|
42987
36a0a1951d64
flagprocessors: add a `sidedata` parameters to _processflagswrite
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42985
diff
changeset
|
123 |
def _processflagswrite(self, text, flags, sidedata): |
42875
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
124 |
"""Inspect revision data flags and applies write transformations defined |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
125 |
by registered flag processors. |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
126 |
|
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
127 |
``text`` - the revision data to process |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
128 |
``flags`` - the revision flags |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
129 |
|
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
130 |
This method processes the flags in the order (or reverse order if |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
131 |
``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
132 |
flag processors registered for present flags. The order of flags defined |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
133 |
in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity. |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
134 |
|
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
135 |
Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
136 |
processed text and ``validatehash`` is a bool indicating whether the |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
137 |
returned text should be checked for hash integrity. |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
138 |
""" |
42988
f4caf910669e
flagprocessors: writetransform function take side data as parameter (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42987
diff
changeset
|
139 |
return self._processflagsfunc(text, flags, 'write', |
f4caf910669e
flagprocessors: writetransform function take side data as parameter (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42987
diff
changeset
|
140 |
sidedata=sidedata)[:2] |
42875
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
141 |
|
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
142 |
def _processflagsraw(self, text, flags): |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
143 |
"""Inspect revision data flags to check is the content hash should be |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
144 |
validated. |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
145 |
|
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
146 |
``text`` - the revision data to process |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
147 |
``flags`` - the revision flags |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
148 |
|
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
149 |
This method processes the flags in the order (or reverse order if |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
150 |
``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
151 |
flag processors registered for present flags. The order of flags defined |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
152 |
in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity. |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
153 |
|
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
154 |
Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
155 |
processed text and ``validatehash`` is a bool indicating whether the |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
156 |
returned text should be checked for hash integrity. |
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
157 |
""" |
42882
3bed541aa65d
flagprocessors: small code update to clarify parameters
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42881
diff
changeset
|
158 |
return self._processflagsfunc(text, flags, 'raw')[1] |
42875
87a934684c3b
flagprocessors: introduce specialized functions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42873
diff
changeset
|
159 |
|
42988
f4caf910669e
flagprocessors: writetransform function take side data as parameter (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42987
diff
changeset
|
160 |
def _processflagsfunc(self, text, flags, operation, sidedata=None): |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
161 |
# fast path: no flag processors will run |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
162 |
if flags == 0: |
42984
66dc5a522f37
flagprocessors: return flagdata in the main processing function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42983
diff
changeset
|
163 |
return text, True, {} |
42882
3bed541aa65d
flagprocessors: small code update to clarify parameters
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42881
diff
changeset
|
164 |
if operation not in ('read', 'write', 'raw'): |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
165 |
raise error.ProgrammingError(_("invalid '%s' operation") % |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
166 |
operation) |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
167 |
# Check all flags are known. |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
168 |
if flags & ~REVIDX_KNOWN_FLAGS: |
42873
7907008a0bb5
flagutil: make the error class used by the mixin configurable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42871
diff
changeset
|
169 |
raise self._flagserrorclass(_("incompatible revision flag '%#x'") % |
7907008a0bb5
flagutil: make the error class used by the mixin configurable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42871
diff
changeset
|
170 |
(flags & ~REVIDX_KNOWN_FLAGS)) |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
171 |
validatehash = True |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
172 |
# Depending on the operation (read or write), the order might be |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
173 |
# reversed due to non-commutative transforms. |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
174 |
orderedflags = REVIDX_FLAGS_ORDER |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
175 |
if operation == 'write': |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
176 |
orderedflags = reversed(orderedflags) |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
177 |
|
42984
66dc5a522f37
flagprocessors: return flagdata in the main processing function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42983
diff
changeset
|
178 |
outsidedata = {} |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
179 |
for flag in orderedflags: |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
180 |
# If a flagprocessor has been registered for a known flag, apply the |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
181 |
# related operation transform and update result tuple. |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
182 |
if flag & flags: |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
183 |
vhash = True |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
184 |
|
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
185 |
if flag not in self._flagprocessors: |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
186 |
message = _("missing processor for flag '%#x'") % (flag) |
42873
7907008a0bb5
flagutil: make the error class used by the mixin configurable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42871
diff
changeset
|
187 |
raise self._flagserrorclass(message) |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
188 |
|
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
189 |
processor = self._flagprocessors[flag] |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
190 |
if processor is not None: |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
191 |
readtransform, writetransform, rawtransform = processor |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
192 |
|
42882
3bed541aa65d
flagprocessors: small code update to clarify parameters
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42881
diff
changeset
|
193 |
if operation == 'raw': |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
194 |
vhash = rawtransform(self, text) |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
195 |
elif operation == 'read': |
42985
bd5858c28bbe
flagprocessors: have the read transform function return side data (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42984
diff
changeset
|
196 |
text, vhash, s = readtransform(self, text) |
bd5858c28bbe
flagprocessors: have the read transform function return side data (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42984
diff
changeset
|
197 |
outsidedata.update(s) |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
198 |
else: # write operation |
42988
f4caf910669e
flagprocessors: writetransform function take side data as parameter (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42987
diff
changeset
|
199 |
text, vhash = writetransform(self, text, sidedata) |
42871
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
200 |
validatehash = validatehash and vhash |
5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42732
diff
changeset
|
201 |
|
42984
66dc5a522f37
flagprocessors: return flagdata in the main processing function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42983
diff
changeset
|
202 |
return text, validatehash, outsidedata |