-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuno_transform.py
133 lines (96 loc) · 3.91 KB
/
nuno_transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# Copyright 2009 Google Inc.
#
import os
import re
import tempfile
from google.appengine.api import datastore
from google.appengine.ext import db
from google.appengine.ext.bulkload import transform
from google.appengine.api import datastore_types
from google.appengine.ext.bulkload.transform import *
# SimpleXML list Helpers
def string_list_from_child_node(xpath):
#xml to gae
def string_list_from_child_node_lambda(unused_value, bulkload_state):
result = []
for node in bulkload_state.current_dictionary['__node__'].findall(xpath):
if not node or node.text == None:
result.append("")
else:
result.append(node.text)
return result
return string_list_from_child_node_lambda
def text_list_from_child_node(xpath):
#xml to gae
def text_list_from_child_node_lambda(unused_value, bulkload_state):
result = []
for node in bulkload_state.current_dictionary['__node__'].findall(xpath):
if not node or node.text == None:
result.append(db.Text(""))
else:
result.append(db.Text(node.text))
return result
return text_list_from_child_node_lambda
def link_list_from_child_node(xpath):
def link_list_from_child_node_lambda(unused_value, bulkload_state):
result = []
for node in bulkload_state.current_dictionary['__node__'].findall(xpath):
if node and node.text is not None:
result.append(db.Link(node.text))
return result
return link_list_from_child_node_lambda
def int_list_from_child_node(xpath):
#xml to gae
def int_list_from_child_node_lambda(unused_value, bulkload_state):
result = []
for node in bulkload_state.current_dictionary['__node__'].findall(xpath):
if node and node is not None:
result.append(int(node.text))
return result
return int_list_from_child_node_lambda
def long_list_from_child_node(xpath):
#xml to gae
def long_list_from_child_node_lambda(unused_value, bulkload_state):
result = []
for node in bulkload_state.current_dictionary['__node__'].findall(xpath):
if node and node is not None:
result.append(long(node.text))
return result
return long_list_from_child_node_lambda
def key_list_from_child_node(xpath, kind):
def key_list_from_child_node_lambda(unused_value, bulkload_state):
result = []
for node in bulkload_state.current_dictionary['__node__'].findall(xpath):
if node and node.text is not None:
result.append(datastore.Key.from_path(kind, int(node.text)))
return result
return key_list_from_child_node_lambda
def key_list_from_child_node_empty_if_none(xpath, kind):
def key_list_from_child_node_empty_if_none_lambda(unused_value, bulkload_state):
result = []
for node in bulkload_state.current_dictionary['__node__'].findall(xpath):
if node and node.text is not None:
result.append(datastore.Key.from_path(kind, int(node.text)))
return result
return key_list_from_child_node_empty_if_none_lambda
def child_node_from_list(child_node_name):
def child_node_from_list_lambda(values):
if values == '' or values is None:
return []
return [(child_node_name, str(value)) for value in values]
return child_node_from_list_lambda
def child_node_from_list_key(child_node_name):
def child_node_from_list_key_lambda(values):
if values == '' or values is None:
return []
return [(child_node_name, transform.key_id_or_name_as_string(value)) for value in values]
return child_node_from_list_key_lambda
def create_foreign_key_none_if_empty(kind, key_is_id=False):
def create_foreign_key_none_if_empty_lambda(value):
if value is None:
return None
if key_is_id:
value = int(value)
return datastore.Key.from_path(kind, value)
return create_foreign_key_none_if_empty_lambda