Move opening of file to gen.plug.utils

svn: r16939
This commit is contained in:
Michiel Nauta
2011-03-27 08:11:51 +00:00
parent 5dadd0c866
commit 5835c8e933
3 changed files with 51 additions and 49 deletions

View File

@@ -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