Move opening of file to gen.plug.utils
svn: r16940
This commit is contained in:
parent
01a0822680
commit
e0a0218c21
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user