![]() |
App Engine Python SDK
v1.6.9 rev.445
The Python runtime is available as an experimental Preview feature.
|
Public Member Functions | |
def | __init__ |
def | get_keys_to_reserve |
def | alias_old_names |
def | create_entity |
def | generate_key |
def | handle_entity |
def | initialize |
def | finalize |
def | generate_records |
Static Public Member Functions | |
def | RegisterLoader |
def | RegisteredLoaders |
def | RegisteredLoader |
Static Public Attributes | |
kind = None | |
A base class for creating datastore entities from input data. To add a handler for bulk loading a new entity kind into your datastore, write a subclass of this class that calls Loader.__init__ from your class's __init__. If you need to run extra code to convert entities from the input data, create new properties, or otherwise modify the entities before they're inserted, override handle_entity. See the create_entity method for the creation of entities from the (parsed) input data.
def google.appengine.tools.bulkloader.Loader.__init__ | ( | self, | |
kind, | |||
properties | |||
) |
Constructor. Populates this Loader's kind and properties map. Args: kind: a string containing the entity kind that this loader handles properties: list of (name, converter) tuples. This is used to automatically convert the input columns into properties. The converter should be a function that takes one argument, a string value from the input file, and returns a correctly typed property value that should be inserted. The tuples in this list should match the columns in your input file, in order. For example: [('name', str), ('id_number', int), ('email', datastore_types.Email), ('user', users.User), ('birthdate', lambda x: datetime.datetime.fromtimestamp(float(x))), ('description', datastore_types.Text), ]
def google.appengine.tools.bulkloader.Loader.alias_old_names | ( | self | ) |
Aliases method names so that Loaders defined with old names work.
def google.appengine.tools.bulkloader.Loader.create_entity | ( | self, | |
values, | |||
key_name = None , |
|||
parent = None |
|||
) |
Creates a entity from a list of property values. Args: values: list/tuple of str key_name: if provided, the name for the (single) resulting entity parent: A datastore.Key instance for the parent, or None Returns: list of db.Model The returned entities are populated with the property values from the argument, converted to native types using the properties map given in the constructor, and passed through handle_entity. They're ready to be inserted. Raises: AssertionError: if the number of values doesn't match the number of properties in the properties map. ValueError: if any element of values is None or empty. TypeError: if values is not a list or tuple.
def google.appengine.tools.bulkloader.Loader.finalize | ( | self | ) |
Performs finalization actions after the upload completes.
def google.appengine.tools.bulkloader.Loader.generate_key | ( | self, | |
i, | |||
values | |||
) |
Generates a key_name to be used in creating the underlying object. The default implementation returns None. This method can be overridden to control the key generation for uploaded entities. The value returned should be None (to use a server generated numeric key), or a string which neither starts with a digit nor has the form __*__ (see https://developers.google.com/appengine/docs/python/datastore/entities), or a datastore.Key instance. If you generate your own string keys, keep in mind: 1. The key name for each entity must be unique. 2. If an entity of the same kind and key already exists in the datastore, it will be overwritten. Args: i: Number corresponding to this object (assume it's run in a loop, this is your current count. values: list/tuple of str. Returns: A string to be used as the key_name for an entity.
def google.appengine.tools.bulkloader.Loader.generate_records | ( | self, | |
filename | |||
) |
Subclasses can override this to add custom data input code. This method must yield fixed-length lists of strings. The default implementation uses csv.reader to read CSV rows from filename. Args: filename: The string input for the --filename option. Yields: Lists of strings.
def google.appengine.tools.bulkloader.Loader.get_keys_to_reserve | ( | self | ) |
Returns keys with ids in their paths to be reserved. Returns: A list of keys used to advance the id sequences associated with each id to prevent collisions with future ids.
def google.appengine.tools.bulkloader.Loader.handle_entity | ( | self, | |
entity | |||
) |
Subclasses can override this to add custom entity conversion code. This is called for each entity, after its properties are populated from the input but before it is stored. Subclasses can override this to add custom entity handling code. The entity to be inserted should be returned. If multiple entities should be inserted, return a list of entities. If no entities should be inserted, return None or []. Args: entity: db.Model Returns: db.Model or list of db.Model
def google.appengine.tools.bulkloader.Loader.initialize | ( | self, | |
filename, | |||
loader_opts | |||
) |
Performs initialization and validation of the input file. This implementation checks that the input file exists and can be opened for reading. Args: filename: The string given as the --filename flag argument. loader_opts: The string given as the --loader_opts flag argument.
|
static |
Returns the loader instance for the given kind if it exists.
|
static |
Returns a dict of the Loader instances that have been created.
|
static |
Register loader and the Loader instance for its kind. Args: loader: A Loader instance.