@@ -22,24 +22,35 @@ class BadlyFormedJSONError(ValueError):
22
22
pass
23
23
24
24
25
- def sheet_key_field (sheet , key ):
25
+ def sheet_key_field (sheet , key , id_key = None ):
26
26
"""
27
27
Check for a key in the sheet, and return it with any suffix (following a ':') that might be present).
28
28
29
29
If a key does not exist, it will be created.
30
30
31
31
"""
32
- keys = [x for x in sheet if x .split (':' )[0 ] == key ]
33
- if not keys :
34
- sheet .append (key )
35
- return key
36
- elif len (keys ) > 1 :
37
- # This shouldn't ever happen, as the schema parser shouldn't output sheets like this...
38
- raise ValueError ('Sheet contains two conflicting keys' )
32
+ if id_key :
33
+ if key in sheet : # If the key exists without a suffix, use that
34
+ return key
35
+ elif sheet .name == id_key : # also use without a suffix if the suffix matches the sheet name
36
+ sheet .append (key )
37
+ return key
38
+ else : # else use it with the :id_key suffix
39
+ if not key + ':' + id_key in sheet :
40
+ sheet .append (key + ':' + id_key )
41
+ return key + ':' + id_key
39
42
else :
40
- return keys [0 ]
43
+ keys = [x for x in sheet if x .split (':' )[0 ] == key ]
44
+ if not keys :
45
+ sheet .append (key )
46
+ return key
47
+ elif len (keys ) > 1 :
48
+ # This shouldn't ever happen, as the schema parser shouldn't output sheets like this...
49
+ raise ValueError ('Sheet contains two conflicting keys' )
50
+ else :
51
+ return keys [0 ]
41
52
42
- def sheet_key_title (sheet , key ):
53
+ def sheet_key_title (sheet , key , id_key = None ):
43
54
"""
44
55
If the key has a corresponding title, return that. If doesn't, create it in the sheet and return it.
45
56
@@ -64,7 +75,7 @@ def __init__(self, json_filename=None, root_json_dict=None, main_sheet_name='mai
64
75
self .root_id = root_id
65
76
self .use_titles = use_titles
66
77
if schema_parser :
67
- self .sub_sheet_mapping = {} # FIXME !!!!! { '/'.join(k.split('/')[1:]): v for k,v in schema_parser.sub_sheet_mapping.items()}
78
+ self .sub_sheet_mapping = {'/' .join (k .split ('/' )[1 :]): v for k ,v in schema_parser .sub_sheet_mapping .items ()}
68
79
self .main_sheet = schema_parser .main_sheet
69
80
self .sub_sheets = schema_parser .sub_sheets
70
81
# Rollup is pulled from the schema_parser, as rollup is only possible if a schema parser is specified
@@ -97,7 +108,14 @@ def parse(self):
97
108
for json_dict in root_json_list :
98
109
self .parse_json_dict (json_dict , sheet = self .main_sheet )
99
110
100
- def parse_json_dict (self , json_dict , sheet , id_extra_parent_name = '' , parent_name = '' , flattened_dict = None , parent_id_fields = None ):
111
+ def parse_json_dict (self , json_dict , sheet , json_key = None , id_extra_parent_name = '' , parent_name = '' , flattened_dict = None , parent_id_fields = None ):
112
+ """
113
+ Parse a json dictionary.
114
+
115
+ json_dict - the json dictionary
116
+ sheet - a sheet.Sheet object representing the resulting spreadsheet
117
+ json_key - the key that maps to this JSON dict, either directly to the dict, or to a dict that this list contains. Is None if this dict is contained in root_json_list directly.
118
+ """
101
119
# Possibly main_sheet should be main_sheet_columns, but this is
102
120
# currently named for consistency with schema.py
103
121
@@ -116,7 +134,7 @@ def parse_json_dict(self, json_dict, sheet, id_extra_parent_name='', parent_name
116
134
if parent_name == '' :
117
135
# Only add the IDs for the top level of object in an array
118
136
for k , v in parent_id_fields .items ():
119
- flattened_dict [sheet_key (sheet , k )] = v
137
+ flattened_dict [sheet_key (sheet , k , id_key = json_key )] = v
120
138
121
139
if self .root_id and self .root_id in json_dict :
122
140
parent_id_fields [self .root_id ] = json_dict [self .root_id ]
@@ -132,6 +150,7 @@ def parse_json_dict(self, json_dict, sheet, id_extra_parent_name='', parent_name
132
150
self .parse_json_dict (
133
151
value ,
134
152
sheet = sheet ,
153
+ json_key = key ,
135
154
parent_name = parent_name + key + '/' ,
136
155
flattened_dict = flattened_dict ,
137
156
parent_id_fields = parent_id_fields )
@@ -159,13 +178,14 @@ def parse_json_dict(self, json_dict, sheet, id_extra_parent_name='', parent_name
159
178
160
179
sub_sheet_name = self .sub_sheet_mapping [key ] if key in self .sub_sheet_mapping else key
161
180
if sub_sheet_name not in self .sub_sheets :
162
- self .sub_sheets [sub_sheet_name ] = Sheet ()
181
+ self .sub_sheets [sub_sheet_name ] = Sheet (name = sub_sheet_name )
163
182
164
183
165
184
for json_dict in value :
166
185
self .parse_json_dict (
167
186
json_dict ,
168
187
sheet = self .sub_sheets [sub_sheet_name ],
188
+ json_key = key ,
169
189
parent_id_fields = parent_id_fields ,
170
190
id_extra_parent_name = parent_name + key + '[]/' )
171
191
else :
0 commit comments