Mercurial > p > roundup > code
annotate roundup/cgi/ZTUtils/Batch.py @ 5525:bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
The roundup-admin export and import commands are used for migrating
between different database backends. It is desirable that they should
be usable also for migrations between Python 2 and Python 3, and in
some cases (e.g. with the anydbm backend) this may be required.
To be usable for such migrations, the format of the generated CSV
files needs to be stable, meaning the same as currently used with
Python 2. The export process uses repr() to produce the fields in the
CSV files and eval() to convert them back to Python data structures.
repr() of strings with non-ASCII characters produces different results
for Python 2 and Python 3.
This patch adds repr_export and eval_import functions to
roundup/anypy/strings.py which provide the required operations that
are just repr() and eval() in Python 2, but are more complicated in
Python 3 to use data representations compatible with Python 2. These
functions are then used in the required places for export and import.
repr() and eval() are also used in storing the dict of changed values
in the journal for the RDBMS backends. It is similarly desirable that
the database be compatible between Python 2 and Python 3, so that
export and import do not need to be used for a migration between
Python versions for non-anydbm back ends. Thus, this patch changes
rdbms_common.py in the places involved in storing journals in the
database, not just in those involved in import/export.
Given this patch, import/export with non-ASCII characters appear based
on some limited testing to work across Python versions, and an
instance using the sqlite backend appears to be compatible between
Python versions without needing import/export, *if* the sessions/otks
databases (which use anydbm) are deleted when changing Python version.
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Sun, 02 Sep 2018 23:48:04 +0000 |
| parents | 35ea9b1efc14 |
| children |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. | |
| 4 # | |
| 5 # This software is subject to the provisions of the Zope Public License, | |
| 6 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | |
| 7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | |
| 8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | |
| 10 # FOR A PARTICULAR PURPOSE | |
| 11 # | |
| 12 ############################################################################## | |
| 13 __doc__='''Batch class, for iterating over a sequence in batches | |
| 14 | |
|
4570
6e3e4f24c753
Remove keyword expansions from CVS. All regression tests passed afterwards.
Eric S. Raymond <esr@thyrsus.com>
parents:
4532
diff
changeset
|
15 ''' |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1120
diff
changeset
|
16 __docformat__ = 'restructuredtext' |
| 1049 | 17 |
| 18 class LazyPrevBatch: | |
| 19 def __of__(self, parent): | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
20 return Batch(parent._sequence, parent.size, |
| 1049 | 21 parent.first - parent._size + parent.overlap, 0, |
| 22 parent.orphan, parent.overlap) | |
| 23 | |
| 24 class LazyNextBatch: | |
| 25 def __of__(self, parent): | |
| 26 try: parent._sequence[parent.end] | |
| 27 except IndexError: return None | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
28 return Batch(parent._sequence, parent.size, |
| 1049 | 29 parent.end - parent.overlap, 0, |
| 30 parent.orphan, parent.overlap) | |
| 31 | |
| 32 class LazySequenceLength: | |
| 33 def __of__(self, parent): | |
| 34 parent.sequence_length = l = len(parent._sequence) | |
| 35 return l | |
| 36 | |
| 37 class Batch: | |
| 38 """Create a sequence batch""" | |
| 39 __allow_access_to_unprotected_subobjects__ = 1 | |
| 40 | |
| 41 previous = LazyPrevBatch() | |
| 42 next = LazyNextBatch() | |
| 43 sequence_length = LazySequenceLength() | |
| 44 | |
| 45 def __init__(self, sequence, size, start=0, end=0, | |
| 46 orphan=0, overlap=0): | |
| 47 '''Encapsulate "sequence" in batches of "size". | |
| 48 | |
| 49 Arguments: "start" and "end" are 0-based indexes into the | |
| 50 sequence. If the next batch would contain no more than | |
| 51 "orphan" elements, it is combined with the current batch. | |
| 52 "overlap" is the number of elements shared by adjacent | |
| 53 batches. If "size" is not specified, it is computed from | |
|
4532
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
54 "start" and "end". If "size" is 0, it is the length of |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
55 the sequence. Failing that, it is 7. |
| 1049 | 56 |
| 57 Attributes: Note that the "start" attribute, unlike the | |
| 58 argument, is a 1-based index (I know, lame). "first" is the | |
| 59 0-based index. "length" is the actual number of elements in | |
| 60 the batch. | |
| 61 | |
| 62 "sequence_length" is the length of the original, unbatched, sequence | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
63 |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
64 Note: "_size" is the "actual" size used to perform batch calulcations, |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
65 while "size" is the "representative" size. (ie. a "special value" of |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
66 "size" used by the templates may translate to a different value for |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
67 "_size" which is used internally for batch calculations). |
| 1049 | 68 ''' |
| 69 | |
| 70 start = start + 1 | |
| 71 | |
| 72 start,end,sz = opt(start,end,size,orphan,sequence) | |
| 73 | |
| 74 self._sequence = sequence | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
75 self.size = size |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
76 self._size = sz |
| 1049 | 77 self.start = start |
| 78 self.end = end | |
| 79 self.orphan = orphan | |
| 80 self.overlap = overlap | |
| 81 self.first = max(start - 1, 0) | |
| 82 self.length = self.end - self.first | |
| 83 if self.first == 0: | |
| 84 self.previous = None | |
| 85 | |
| 86 | |
| 87 def __getitem__(self, index): | |
| 88 if index < 0: | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5087
diff
changeset
|
89 if index + self.end < self.first: raise IndexError(index) |
| 1049 | 90 return self._sequence[index + self.end] |
| 91 | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5087
diff
changeset
|
92 if index >= self.length: raise IndexError(index) |
|
1120
c26471971d18
Exposed the Batch mechanism through the top-level "utils" variable.
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
93 return self._sequence[index + self.first] |
| 1049 | 94 |
| 95 def __len__(self): | |
| 96 return self.length | |
| 97 | |
| 98 def opt(start,end,size,orphan,sequence): | |
| 99 if size < 1: | |
|
4532
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
100 if size == 0: |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
101 size=len(sequence) |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
102 elif start > 0 and end > 0 and end >= start: |
| 1049 | 103 size=end+1-start |
| 104 else: size=7 | |
| 105 | |
| 106 if start > 0: | |
| 107 | |
| 108 try: sequence[start-1] | |
| 109 except IndexError: start=len(sequence) | |
| 110 | |
| 111 if end > 0: | |
| 112 if end < start: end=start | |
| 113 else: | |
| 114 end=start+size-1 | |
| 115 try: sequence[end+orphan-1] | |
| 116 except IndexError: end=len(sequence) | |
| 117 elif end > 0: | |
| 118 try: sequence[end-1] | |
| 119 except IndexError: end=len(sequence) | |
| 120 start=end+1-size | |
| 121 if start - 1 < orphan: start=1 | |
| 122 else: | |
| 123 start=1 | |
| 124 end=start+size-1 | |
| 125 try: sequence[end+orphan-1] | |
| 126 except IndexError: end=len(sequence) | |
| 127 return start,end,size |
