summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2020-02-19 17:35:58 +0100
committerEdward Welbourne <eddy@chaos.org.uk>2020-04-02 19:42:45 +0100
commitbb4242341b7d04a861e88828b06f784345e54a9b (patch)
treeac88880d9652eaeb4409f0936f047dc7ca1b3771
parentc3dea1ffca7e46319daed5b44895c6e09f51f3ea (diff)
Add tools to localetools to facilitate source file recreation
For now unused; later commits shall put them to use. Transcriber -- base, takes care of tempfile and renaming. SourceFileEditor -- handles copying parts before and after a common delimiter. Task-number: QTBUG-81344 Change-Id: I28cf977d0a08825fbb873fb330da6823b88ad3ed Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r--util/locale_database/localetools.py99
1 files changed, 99 insertions, 0 deletions
diff --git a/util/locale_database/localetools.py b/util/locale_database/localetools.py
index 0d5c2acbbb..29153366b3 100644
--- a/util/locale_database/localetools.py
+++ b/util/locale_database/localetools.py
@@ -26,14 +26,20 @@
##
#############################################################################
"""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.
"""
+import os
+import tempfile
+
class Error (StandardError):
__upinit = StandardError.__init__
def __init__(self, msg, *args):
@@ -63,3 +69,96 @@ def wrap_list(lst):
head, lst = lst[:size], lst[size:]
yield head
return ",\n".join(", ".join(x) for x in split(lst, 20))
+
+class Transcriber (object):
+ """Helper class to facilitate rewriting source files.
+
+ This class takes care of the temporary file manipulation. 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.
+
+ Callers should call close() on success or cleanup() on failure (to
+ clear away the temporary file).
+ """
+ def __init__(self, path, temp):
+ # Open the old file
+ self.reader = open(path)
+ # Create a temp file to write the new data into
+ temp, tempPath = tempfile.mkstemp(os.path.split(path)[1], dir = temp)
+ self.__names = path, tempPath
+ self.writer = os.fdopen(temp, "w")
+
+ def close(self):
+ self.reader.close()
+ self.writer.close()
+ self.reader = self.writer = None
+ source, temp = self.__names
+ os.remove(source)
+ os.rename(temp, source)
+
+ def cleanup(self):
+ if self.__names:
+ self.reader.close()
+ self.writer.close()
+ # Remove temp-file:
+ os.remove(self.__names[1])
+ self.__names = ()
+
+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 creation, an instance will copy the
+ preamble up to the start marker; its close() will skip over the
+ original's generated content and resume transcribing with the end
+ marker. Derived classes need only implement the generation of the
+ content in between.
+
+ Callers should call close() on success or cleanup() on failure (to
+ clear away the temporary file); see Transcriber.
+ """
+ __upinit = Transcriber.__init__
+ def __init__(self, path, temp):
+ """Set up the source file editor.
+
+ Requires two arguments: the path to the source file to be read
+ and, on success, replaced with a new version; and the
+ directory in which to store the temporary file during the
+ rewrite."""
+ self.__upinit(path, temp)
+ self.__copyPrelude()
+
+ __upclose = Transcriber.close
+ def close(self):
+ self.__copyTail()
+ self.__upclose()
+
+ # Implementation details:
+ GENERATED_BLOCK_START = '// GENERATED PART STARTS HERE'
+ GENERATED_BLOCK_END = '// GENERATED PART ENDS HERE'
+
+ def __copyPrelude(self):
+ # 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 __copyTail(self):
+ # 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)