Stay organized with collections
Save and categorize content based on your preferences.
Source code for google.appengine.ext.bulkload.simpletext_connector
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Bulkloader Simple Text writing.
Handle the simpletext format specified in a bulkloader.yaml file.
"""
from google.appengine.ext.bulkload import bulkloader_errors
from google.appengine.ext.bulkload import connector_interface
[docs]class SimpleTextConnector(connector_interface.ConnectorInterface):
"""Write a text file from dicts for each record. Does not support import."""
VALID_MODES = ('text', 'nonewline', 'binary')
[docs] @classmethod
def create_from_options(cls, options, name):
"""Factory using an options dictionary.
Args:
options: Dictionary of options containing:
template: A Python dict-interpolation string. Required.
prolog: written before the per-record output.
epilog: written after the per-record output.
mode: one of the following, default is 'text'
text: text file mode, newlines between records.
nonewline: text file mode, no added newlines.
binary: binary file mode, no added newlines.
name: The name of this transformer, for use in error messages.
Returns:
SimpleTextConnector object described by the specified options.
Raises:
InvalidConfiguration: If the config is invalid.
"""
template = options.get('template')
if not template:
raise bulkloader_errors.InvalidConfiguration(
'simpletext must specify template. (In transformer named %s)' % name)
prolog = options.get('prolog')
epilog = options.get('epilog')
mode = options.get('mode', 'text')
return cls(template, prolog, epilog, mode, name)
def __init__(self, template, prolog=None, epilog=None, mode='text', name=''):
"""Constructor.
Args:
template: A Python dict-interpolation string.
prolog: written before the per-record output.
epilog: written after the per-record output.
mode: one of the following, default is 'text'
text: text file mode, newlines between records.
nonewline: text file mode, no added newlines.
binary: binary file mode, no added newlines.
"""
if mode not in self.VALID_MODES:
raise bulkloader_errors.InvalidConfiguration(
'simpletext mode must be one of "%s". (In transformer name %s.)' %
('", "'.join(self.VALID_MODES), name))
self.template = template
self.prolog = prolog
self.epilog = epilog
self.mode = mode
self.export_file_pointer = None
[docs] def initialize_export(self, filename, bulkload_state):
"""Open file and write prolog."""
self.bulkload_state = bulkload_state
mode = 'w'
if self.mode == 'binary':
mode = 'wb'
self.export_file_pointer = open(filename, mode)
if self.prolog:
self.export_file_pointer.write(self.prolog)
if self.mode == 'text':
self.export_file_pointer.write('\n')
[docs] def write_dict(self, dictionary):
"""Write one record for the specified entity."""
self.export_file_pointer.write(self.template % dictionary)
if self.mode == 'text':
self.export_file_pointer.write('\n')
[docs] def finalize_export(self):
"""Write epliog and close file after every record is written."""
if self.epilog:
self.export_file_pointer.write(self.epilog)
if self.mode == 'text':
self.export_file_pointer.write('\n')
self.export_file_pointer.close()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["This code provides the `SimpleTextConnector` class for writing data in a simple text format, which is part of the Google App Engine bulkloader functionality."],["The `SimpleTextConnector` class is designed to export data but does not support importing data, and it is configured via a `bulkloader.yaml` file, specifically through a template."],["The `create_from_options` method constructs a `SimpleTextConnector` instance, requiring a template string and allowing for optional prolog, epilog, and mode settings, where the mode can be text, nonewline, or binary."],["The class supports three modes for output: 'text' (with newlines between records), 'nonewline' (text without added newlines), and 'binary' (binary mode without added newlines)."],["The `initialize_export`, `write_dict`, and `finalize_export` methods manage file operations, writing a prolog, writing records based on a provided template, and adding an epilog before closing the file, respectively."]]],[]]