diff options
author | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2021-07-09 15:34:40 +0200 |
---|---|---|
committer | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2021-07-19 22:05:54 +0200 |
commit | 2d0c2c9f1cb4f1637b623a80312251326740f245 (patch) | |
tree | c911b52226f87d23d5120f7f95e6f30a5549240c /util/locale_database/localetools.py | |
parent | e095fa7f9cb994ec2b2639b58a3c0d822d4d2cf6 (diff) |
locale_database: Use pathlib to manipulate paths in Python code
pathlib's API is more modern and easier to use than os.path. It
also allows to distinguish between paths and other strings in type
annotations.
Task-number: QTBUG-83488
Pick-to: 6.2
Change-Id: Ie6d9b4e35596f7f6befa4c9635f4a65ea3b20025
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'util/locale_database/localetools.py')
-rw-r--r-- | util/locale_database/localetools.py | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/util/locale_database/localetools.py b/util/locale_database/localetools.py index 1f415bbdb3..82b0848f4a 100644 --- a/util/locale_database/localetools.py +++ b/util/locale_database/localetools.py @@ -38,6 +38,7 @@ Classes: """ import os +from pathlib import Path import tempfile class Error (Exception): @@ -81,29 +82,40 @@ class Transcriber (object): Callers should call close() on success or cleanup() on failure (to clear away the temporary file). """ - def __init__(self, path, temp): + def __init__(self, path: Path, temp_dir: Path): # 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 + temp, tempPath = tempfile.mkstemp(path.name, dir=temp_dir) + self.path = path + self.tempPath = Path(tempPath) self.writer = os.fdopen(temp, "w") - def close(self): + def close(self) -> None: 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: + # Move the modified file to the original location + self.path.unlink() + self.tempPath.rename(self.path) + + self.tempPath = None + + def cleanup(self) -> None: + if self.reader: self.reader.close() + self.reader = None + + if self.writer: self.writer.close() + self.writer = None + + if self.tempPath: # Remove temp-file: - os.remove(self.__names[1]) - self.__names = () + self.tempPath.unlink(missing_ok=True) + self.tempPath = None + class SourceFileEditor (Transcriber): """Transcriber with transcription of code around a gnerated block. @@ -125,14 +137,14 @@ class SourceFileEditor (Transcriber): Callers should call close() on success or cleanup() on failure (to clear away the temporary file); see Transcriber. """ - def __init__(self, path, temp): + def __init__(self, path: Path, temp_dir: Path): """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.""" - super().__init__(path, temp) + super().__init__(path, temp_dir) self.__copyPrelude() def close(self): |