| 2553 |
2553 |
class RestoreLoader(Loader): |
| 2554 |
2554 |
"""A Loader which imports protobuffers from a file.""" |
| 2555 |
2555 |
|
| 2556 |
|
def __init__(self, kind): |
|
2556 |
def __init__(self, kind, app_id): |
| 2557 |
2557 |
self.kind = kind |
|
2558 |
self.app_id = app_id |
| 2558 |
2559 |
|
| 2559 |
2560 |
def initialize(self, filename, loader_opts): |
| 2560 |
2561 |
CheckFile(filename) |
| … |
| 2570 |
2571 |
yield record |
| 2571 |
2572 |
|
| 2572 |
2573 |
def create_entity(self, values, key_name=None, parent=None): |
|
2574 |
def convert_key(key, app_id): |
|
2575 |
path = key.to_path() |
|
2576 |
kwargs = {'_app_id_namespace': app_id} |
|
2577 |
return db.Key.from_path(*path,**kwargs) |
|
2578 |
import copy |
| 2573 |
2579 |
key = StrKey(unicode(values[0], 'utf-8')) |
| 2574 |
2580 |
entity_proto = entity_pb.EntityProto(contents=str(values[1])) |
| 2575 |
2581 |
entity_proto.mutable_key().CopyFrom(key._Key__reference) |
| 2576 |
|
return datastore.Entity._FromPb(entity_proto) |
| 2577 |
|
|
|
2582 |
entity = datastore.Entity._FromPb(entity_proto) |
|
2583 |
new_entity = copy.copy(entity) |
|
2584 |
for k,v in entity.iteritems(): |
|
2585 |
if isinstance(v, db.Key): |
|
2586 |
new_entity[k] = convert_key(v, self.app_id) |
|
2587 |
if isinstance(v, list): |
|
2588 |
new_list = [] |
|
2589 |
for item in v: |
|
2590 |
if isinstance(item, db.Key): |
|
2591 |
new_list.append(convert_key(item, self.app_id)) |
|
2592 |
else: |
|
2593 |
new_list.append(item) |
|
2594 |
new_entity[k] = new_list |
|
2595 |
return new_entity |
| 2578 |
2596 |
|
| 2579 |
2597 |
class Exporter(object): |
| 2580 |
2598 |
"""A base class for serializing datastore entities. |
| … |
| 3625 |
3643 |
if dump: |
| 3626 |
3644 |
Exporter.RegisterExporter(DumpExporter(kind, result_db_filename)) |
| 3627 |
3645 |
elif restore: |
| 3628 |
|
Loader.RegisterLoader(RestoreLoader(kind)) |
|
3646 |
Loader.RegisterLoader(RestoreLoader(kind, app_id)) |
| 3629 |
3647 |
else: |
| 3630 |
3648 |
LoadConfig(config_file) |