summaryrefslogtreecommitdiffstats
path: root/util/locale_database/localetools.py
blob: ee6abd559304d09855a3e7b6e7063291b0db2e63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#############################################################################
##
## Copyright (C) 2020 The Qt Company Ltd.
## Contact: https://www.qt.io/licensing/
##
## This file is part of the test suite of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:GPL-EXCEPT$
## Commercial License Usage
## Licensees holding valid commercial Qt licenses may use this file in
## accordance with the commercial license agreement provided with the
## Software or, alternatively, in accordance with the terms contained in
## a written agreement between you and The Qt Company. For licensing terms
## and conditions see https://www.qt.io/terms-conditions. For further
## information use the contact form at https://www.qt.io/contact-us.
##
## GNU General Public License Usage
## Alternatively, this file may be used under the terms of the GNU
## General Public License version 3 as published by the Free Software
## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
## included in the packaging of this file. Please review the following
## information to ensure the GNU General Public License requirements will
## be met: https://www.gnu.org/licenses/gpl-3.0.html.
##
## $QT_END_LICENSE$
##
#############################################################################
"""Utilities shared among the CLDR extraction tools.

Functions:
  unicode2hex() -- converts unicode text to UCS-2 in hex form.
  wrap_list() -- map list to comma-separated string, 20 entries per line.

Classes:
  Error -- A shared error class.
  Transcriber -- edit a file by writing a temporary file, then renaming.
  SourceFileEditor -- adds standard prelude and tail handling to Transcriber.
"""

from contextlib import ExitStack, contextmanager
from pathlib import Path
from tempfile import NamedTemporaryFile

class Error (Exception):
    def __init__(self, msg, *args):
        super().__init__(msg, *args)
        self.message = msg
    def __str__(self):
        return self.message

def unicode2hex(s):
    lst = []
    for x in s:
        v = ord(x)
        if v > 0xFFFF:
            # make a surrogate pair
            # copied from qchar.h
            high = (v >> 10) + 0xd7c0
            low = (v % 0x400 + 0xdc00)
            lst.append(hex(high))
            lst.append(hex(low))
        else:
            lst.append(hex(v))
    return lst

def wrap_list(lst):
    def split(lst, size):
        while lst:
            head, lst = lst[:size], lst[size:]
            yield head
    return ",\n".join(", ".join(x) for x in split(lst, 20))


@contextmanager
def AtomicRenameTemporaryFile(originalLocation: Path, *, prefix: str, dir: Path):
    """Context manager for safe file update via a temporary file.

    Accepts path to the file to be updated. Yields a temporary file to the user
    code, open for writing.

    On success closes the temporary file and moves its content to the original
    location. On error, removes temporary file, without disturbing the original.
    """
    tempFile = NamedTemporaryFile('w', prefix=prefix, dir=dir, delete=False)
    try:
        yield tempFile
        tempFile.close()
        # Move the modified file to the original location
        Path(tempFile.name).rename(originalLocation)
    except Exception:
        # delete the temporary file in case of error
        tempFile.close()
        Path(tempFile.name).unlink()
        raise


class Transcriber:
    """Context manager base-class to manage source file rewrites.

    Derived classes need to implement transcribing of the content, with
    whatever modifications they may want.  Members reader and writer
    are exposed; use writer.write() to output to the new file; use
    reader.readline() or iterate reader to read the original.

    This class is intended to be used as context manager only (inside a
    `with` statement).

    Reimplement onEnter() to write any preamble the file may have,
    onExit() to write any tail. The body of the with statement takes
    care of anything in between, using methods provided by derived classes.

    The data is written to a temporary file first. The temporary file data
    is then moved to the original location if there were no errors. Otherwise
    the temporary file is removed and the original is left unchanged.
    """
    def __init__(self, path: Path, temp_dir: Path):
        self.path = path
        self.tempDir = temp_dir

    def onEnter(self) -> None:
        """
        Called before transferring control to user code.

        This function can be overridden in derived classes to perform actions
        before transferring control to the user code.

        The default implementation does nothing.
        """
        pass

    def onExit(self) -> None:
        """
        Called after return from user code.

        This function can be overridden in derived classes to perform actions
        after successful return from user code.

        The default implementation does nothing.
        """
        pass

    def __enter__(self):
        with ExitStack() as resources:
            # Create a temp file to write the new data into
            self.writer = resources.enter_context(
                AtomicRenameTemporaryFile(self.path, prefix=self.path.name, dir=self.tempDir))
            # Open the old file
            self.reader = resources.enter_context(open(self.path))

            self.onEnter()

            # Prevent resources from being closed on normal return from this
            # method and make them available inside __exit__():
            self.__resources = resources.pop_all()
            return self

    def __exit__(self, exc_type, exc_value, traceback):
        if exc_type is None:
            with self.__resources:
               self.onExit()
        else:
            self.__resources.__exit__(exc_type, exc_value, traceback)

        return False


class SourceFileEditor (Transcriber):
    """Transcriber with transcription of code around a gnerated block.

    We have a common pattern of source files with a generated part
    embedded in a context that's not touched by the regeneration
    scripts. The generated part is, in each case, marked with a common
    pair of start and end markers. We transcribe the old file to a new
    temporary file; on success, we then remove the original and move
    the new version to replace it.

    This class takes care of transcribing the parts before and after
    the generated content; on entering the context, an instance will
    copy the preamble up to the start marker; on exit from the context
    it will skip over the original's generated content and resume
    transcribing with the end marker.

    This class is only intended to be used as a context manager:
    see Transcriber. Derived classes implement suitable methods for use in
    the body of the with statement, using self.writer to rewrite the part
    of the file between the start and end markers.
    """
    GENERATED_BLOCK_START = '// GENERATED PART STARTS HERE'
    GENERATED_BLOCK_END = '// GENERATED PART ENDS HERE'

    def onEnter(self) -> None:
        # Copy over the first non-generated section to the new file
        for line in self.reader:
            self.writer.write(line)
            if line.strip() == self.GENERATED_BLOCK_START:
                break

    def onExit(self) -> None:
        # Skip through the old generated data in the old file
        for line in self.reader:
            if line.strip() == self.GENERATED_BLOCK_END:
                self.writer.write(line)
                break
        # Transcribe the remainder:
        for line in self.reader:
            self.writer.write(line)