From e0a0218c217a4da28762ccfb8dfa6dd490138427 Mon Sep 17 00:00:00 2001 From: Michiel Nauta Date: Sun, 27 Mar 2011 08:13:09 +0000 Subject: [PATCH] Move opening of file to gen.plug.utils svn: r16940 --- src/gen/plug/utils.py | 47 +++++++++++++++++++++++++++++++ src/plugins/export/ExportVCard.py | 26 ++--------------- src/plugins/import/ImportVCard.py | 27 ++---------------- 3 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/gen/plug/utils.py b/src/gen/plug/utils.py index d67ec8fa4..f72986e82 100644 --- a/src/gen/plug/utils.py +++ b/src/gen/plug/utils.py @@ -289,3 +289,50 @@ def load_addon_file(path, callback=None): callback(" " + (_("Registered '%s'") % u_gpr_file) + "\n") file_obj.close() +#------------------------------------------------------------------------- +# +# OpenFileOrStdout class +# +#------------------------------------------------------------------------- +class OpenFileOrStdout: + """Context manager to open a file or stdout for writing.""" + def __init__(self, filename): + self.filename = filename + self.filehandle = None + + def __enter__(self): + if self.filename == '-': + self.filehandle = sys.stdout + else: + self.filehandle = open(self.filename, 'w') + return self.filehandle + + def __exit__(self, exc_type, exc_value, traceback): + if self.filehandle and self.filename != '-': + self.filehandle.close() + return False + +#------------------------------------------------------------------------- +# +# OpenFileOrStdin class +# +#------------------------------------------------------------------------- +class OpenFileOrStdin: + """Context manager to open a file or stdin for reading.""" + def __init__(self, filename): + self.filename = filename + self.filehandle = None + + def __enter__(self): + if self.filename == '-': + # TODO how to add U to mode? + self.filehandle = sys.stdin + else: + self.filehandle = open(self.filename, "rU") + return self.filehandle + + def __exit__(self, exc_type, exc_value, traceback): + if self.filename != '-': + self.filehandle.close() + return False + diff --git a/src/plugins/export/ExportVCard.py b/src/plugins/export/ExportVCard.py index 70cf88a60..b82b4aacb 100644 --- a/src/plugins/export/ExportVCard.py +++ b/src/plugins/export/ExportVCard.py @@ -53,31 +53,9 @@ from gen.lib import Date from gen.lib.urltype import UrlType from gen.lib.eventtype import EventType from gen.display.name import displayer as _nd +from gen.plug.utils import OpenFileOrStdout -#------------------------------------------------------------------------- -# -# ExportOpenFileContextManager class -# -#------------------------------------------------------------------------- -class ExportOpenFileContextManager: - """Context manager to open a file or stdout for writing.""" - def __init__(self, filename): - self.filename = filename - self.filehandle = None - - def __enter__(self): - if self.filename == '-': - self.filehandle = sys.stdout - else: - self.filehandle = open(self.filename, 'w') - return self.filehandle - - def __exit__(self, exc_type, exc_value, traceback): - if self.filehandle and self.filename != '-': - self.filehandle.close() - return False - #------------------------------------------------------------------------- # # Support Functions @@ -171,7 +149,7 @@ class VCardWriter(object): def export_data(self): """Open the file and loop over everyone two write their VCards.""" - with ExportOpenFileContextManager(self.filename) as self.filehandle: + with OpenFileOrStdout(self.filename) as self.filehandle: if self.filehandle: self.count = 0 self.oldval = 0 diff --git a/src/plugins/import/ImportVCard.py b/src/plugins/import/ImportVCard.py index e681e7df1..533295ef9 100644 --- a/src/plugins/import/ImportVCard.py +++ b/src/plugins/import/ImportVCard.py @@ -51,32 +51,9 @@ LOG = logging.getLogger(".ImportVCard") import Errors import gen.lib from gen.db import DbTxn +from gen.plug.utils import OpenFileOrStdin from QuestionDialog import ErrorDialog -#------------------------------------------------------------------------- -# -# ImportOpenFileContextManager class -# -#------------------------------------------------------------------------- -class ImportOpenFileContextManager: - """Context manager to open a file or stdin for reading.""" - def __init__(self, filename): - self.filename = filename - self.filehandle = None - - def __enter__(self): - if self.filename == '-': - # TODO how to add U to mode? - self.filehandle = sys.stdin - else: - self.filehandle = open(self.filename, "rU") - return self.filehandle - - def __exit__(self, exc_type, exc_value, traceback): - if self.filename != '-': - self.filehandle.close() - return False - #------------------------------------------------------------------------- # # Support Functions @@ -244,7 +221,7 @@ class VCardParser(object): self.person = None with DbTxn(_("vCard import"), self.database, batch=True) as self.trans: self.database.disable_signals() - with ImportOpenFileContextManager(self.filename) as self.filehandle: + with OpenFileOrStdin(self.filename) as self.filehandle: self.next_line = self.filehandle.readline() while True: line = self.get_next_line()