forked from YelpArchive/parcelgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparcelgen.py
More file actions
617 lines (561 loc) · 25 KB
/
parcelgen.py
File metadata and controls
617 lines (561 loc) · 25 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/env python
import sys, re, os.path, json
import argparse
import yaml
from collections import defaultdict
# Parcelgen generates parcelable Java classes based
# on a json dictionary of types and properties. It generates
# a class with the appropriate members and working
# writeToParcel and readFromParcel methods.
# Primary Author: Alex Pretzlav <alex@pretzlav.com>
class ObjectProperty(object):
"""
A property of an ObjectDescription that includes its name (as returned by the API),
its type, an optional description, and an optional example value for the property.
"""
def __init__(self, name, type_, description='', example='', collection=False):
self.name = name
self.type_ = type_
self.description = description
self.example = example
self.collection = collection
class ParcelGen:
BASE_IMPORTS = ("android.os.Parcel", "android.os.Parcelable")
CLASS_STR = "/* package */ abstract class %s implements %s {"
CHILD_CLASS_STR = "public class {0} extends _{0} {{"
NATIVE_TYPES = ["string", "byte", "double", "float", "int", "long"]
JSON_IMPORTS = ["org.json.JSONException", "org.json.JSONObject"]
tablevel = 0
outfile = None
def __init__(self):
self.props = {}
self.package = 'org.pretz.parcelgen'
self.do_json = True
self.imports = []
self.json_map = {}
self.default_values = {}
self.transient = []
self.do_json_writer = False
self.json_blacklist = []
self.serializables = []
self.implements = []
self.from_yaml = False
def tabify(self, string):
return ("\t" * self.tablevel) + string
def printtab(self, string):
self.output(self.tabify(string))
def newline(self, count=1):
self.output("\n" * (count-1))
def output(self, string=""):
if self.outfile:
self.outfile.write(string + "\n")
else:
print string
def uptab(self):
self.tablevel += 1
def downtab(self):
self.tablevel -= 1
def memberize(self, name):
return "m%s%s" % (name[0].capitalize(), name[1:])
def member_map(self):
for typ in self.get_types():
for member in self.props[typ]:
yield (typ, member)
def gen_getter(self, typ, member):
method_name = ""
if typ == "boolean" and member.startswith("is"):
method_name = member
else:
method_name = "get%s%s" % (member[0].capitalize(), member[1:])
return "\tpublic %s %s() {\n\t\t return %s;\n\t}" % (typ, method_name, self.memberize(member))
def list_type(self, typ):
match = re.match(r"(List|ArrayList)<(.*)>", typ)
if match:
return match.group(2)
return None
def gen_list_parcelable(self, typ, memberized):
classname = self.list_type(typ)
if not classname:
return None
elif classname == "String":
return self.tabify("parcel.writeStringList(%s);" % memberized)
else:
return self.tabify("parcel.writeTypedList(%s);" % memberized)
def gen_list_unparcel(self, typ, memberized):
classname = self.list_type(typ)
if not classname:
return None
if (classname == "String"):
return self.tabify("%s = source.createStringArrayList();" % memberized)
else:
return self.tabify("%s = source.createTypedArrayList(%s.CREATOR);" % (memberized, classname))
def gen_parcelable_line(self, typ, member):
memberized = self.memberize(member)
if typ.lower() in self.NATIVE_TYPES:
return self.tabify("parcel.write%s(%s);" % (typ.capitalize(), memberized))
elif typ == "Date":
return self.tabify("parcel.writeLong(%s == null ? Integer.MIN_VALUE : %s.getTime());" % (
memberized, memberized))
elif self.list_type(typ):
return self.gen_list_parcelable(typ, memberized)
elif typ in self.serializables:
return self.tabify("parcel.writeSerializable(%s);" % memberized)
else:
return self.tabify("parcel.writeParcelable(%s, 0);" % memberized)
def get_types(self):
types = self.props.keys()
types.sort()
return types
def gen_parcelable(self):
result = ""
for typ in self.get_types():
if typ == "boolean":
joined = ", ".join(map(self.memberize, self.props[typ]))
result += self.tabify("parcel.writeBooleanArray(new boolean[] {%s});\n" % joined)
else:
for member in self.props[typ]:
result += self.gen_parcelable_line(typ, member) + "\n"
return result[:-1] # Strip last newline because I'm too lazy to do this right
def print_creator(self, class_name, parcel_class, close=True):
# Simple parcelable creator that uses readFromParcel
self.printtab("public static final {0}<{1}> CREATOR = new {0}<{1}>() {{".format(
parcel_class, class_name))
self.uptab()
self.newline()
self.printtab("public {0}[] newArray(int size) {{\n{1}return new {0}[size];\n\t\t}}".format(
class_name, "\t" * (self.tablevel + 1)))
self.newline()
self.printtab("public %s createFromParcel(Parcel source) {" % class_name)
self.uptab()
self.printtab("{0} object = new {0}();".format(class_name))
self.printtab("object.readFromParcel(source);")
self.printtab("return object;")
self.downtab()
self.printtab("}")
if close:
self.downtab()
self.printtab("};\n")
self.downtab()
def print_child(self, child_name, package):
self.tablevel = 0
self.printtab("package %s;\n" % package)
imports = ["android.os.Parcel"]
if self.do_json:
imports.extend(self.JSON_IMPORTS)
imports.append("com.yelp.parcelgen.JsonParser.DualCreator")
else:
imports.append("android.os.Parcelable")
for import_string in imports:
self.printtab("import %s;" % import_string)
self.newline(2)
self.printtab(self.CHILD_CLASS_STR.format(child_name))
self.newline()
self.uptab()
if self.do_json:
self.print_creator(child_name, "DualCreator", False)
self.newline()
self.printtab("@Override")
self.printtab("public %s parse(JSONObject obj) throws JSONException {" % child_name)
self.uptab()
self.printtab("{0} newInstance = new {0}();".format(child_name))
self.printtab("newInstance.readFromJson(obj);")
self.printtab("return newInstance;")
self.downtab()
self.printtab("}\n\t};\n")
self.downtab()
else:
self.print_creator(child_name, "Parcelable.Creator")
self.downtab()
self.printtab("}")
def needs_jsonutil(self):
if "Date" in self.props:
return True
for key in self.props.keys():
if "List" in key:
return True
return False
def print_gen(self, class_name):
self.tablevel = 0
# Imports and open class definition
self.printtab("package %s;\n" % self.package)
imports = set(tuple(self.imports) + self.BASE_IMPORTS)
for prop in self.props.keys():
if prop.startswith("List"):
imports.add("java.util.List")
elif prop.startswith("ArrayList"):
imports.add("java.util.ArrayList")
elif prop == "Date":
imports.add("java.util.Date")
elif prop == "Uri":
imports.add("android.net.Uri")
if self.do_json:
imports.update(self.JSON_IMPORTS)
if self.needs_jsonutil():
imports.add("com.yelp.parcelgen.JsonUtil")
if 'Serializable' in self.implements:
imports.add("java.io.Serializable")
imports = list(imports)
imports.sort()
for imp in imports:
self.printtab("import %s;" % imp)
self.output("")
self.printtab("/** Automatically generated Parcelable implementation for %s." % class_name)
self.printtab(" * DO NOT MODIFY THIS FILE MANUALLY! IT WILL BE OVERWRITTEN THE NEXT TIME")
self.printtab(" * %s's PARCELABLE DESCRIPTION IS CHANGED." % class_name)
self.printtab(" */")
implements = ", ".join(['Parcelable'] + self.implements)
self.printtab((self.CLASS_STR % (class_name, implements)) + "\n")
# Protected member variables
self.uptab()
for typ, member in self.member_map():
if member in self.transient:
typ = "transient " + typ
self.printtab("protected %s %s;" % (typ, self.memberize(member)))
self.output("")
# Parameterized Constructor
constructor = "protected %s(" % class_name
params = []
for typ, member in self.member_map():
params.append("%s %s" % (typ, member))
constructor += "%s) {" % ", ".join(params)
self.printtab(constructor)
self.uptab()
self.printtab("this();")
for typ, member in self.member_map():
self.printtab("%s = %s;" % (self.memberize(member), member))
self.tablevel -= 1
self.printtab("}\n")
# Empty constructor for Parcelable
self.printtab("protected %s() {" % class_name)
self.uptab()
self.printtab("super();")
self.downtab()
self.printtab("}\n")
# Getters for member variables
for typ, member in self.member_map():
self.output(self.gen_getter(typ, member))
self.output("\n")
# Parcelable writeToParcel
self.printtab("public int describeContents() {\n\t\treturn 0;\n\t}")
self.output("")
self.printtab("public void writeToParcel(Parcel parcel, int flags) {")
self.uptab()
self.output(self.gen_parcelable())
self.downtab()
self.printtab("}\n")
# readFromParcel that allows subclasses to use parcelable-ness of their superclass
self.printtab("public void readFromParcel(Parcel source) {")
self.tablevel += 1
i = 0
all_members = []
for typ in self.get_types():
if typ == "boolean":
self.printtab("boolean[] bools = source.createBooleanArray();")
for j in xrange(len(self.props[typ])):
self.printtab("%s = bools[%d];" % (self.memberize(self.props[typ][j]), j))
else:
for member in self.props[typ]:
memberized = self.memberize(member)
list_gen = self.gen_list_unparcel(typ, memberized)
if list_gen:
self.output(list_gen)
elif typ == "Date":
self.printtab("long date%d = source.readLong();" % i)
self.printtab("if (date%d != Integer.MIN_VALUE) {" % i)
self.uptab()
self.printtab("%s = new Date(date%d);" % (memberized, i))
self.downtab()
self.printtab("}")
i += 1
elif typ.lower() in self.NATIVE_TYPES:
self.printtab("%s = source.read%s();" % (memberized, typ.capitalize()))
elif typ in self.serializables:
self.printtab("%s = (%s)source.readSerializable();" % (memberized, typ))
else:
self.printtab("%s = source.readParcelable(%s.class.getClassLoader());" % (memberized, typ))
self.tablevel -= 1
self.printtab("}\n")
# self.print_creator(class_name, "Parcelable.Creator")
if self.do_json:
self.output(self.generate_json_reader(self.props))
if self.do_json_writer:
self.output(self.generate_json_writer(self.props))
self.downtab()
self.printtab("}")
def generate_json_reader(self, props):
self.props = props
fun = self.tabify("public void readFromJson(JSONObject json) throws JSONException {\n")
self.uptab()
# Parcelable doesn't support boolean without help, JSON does
NATIVES = self.NATIVE_TYPES + ["boolean"]
for typ in self.get_types():
list_type = self.list_type(typ)
# Always protect strings with isNull check because JSONObject.optString()
# returns the string "null" for null strings. AWESOME.
protect = typ not in [native for native in NATIVES if native != "String"]
for member in props[typ]:
# Some object members are derived and not stored in JSON
if member in self.json_blacklist:
continue
# Some members have different names in JSON
if member in self.json_map:
key = self.json_map[member]
else:
key = camel_to_under(member)
# Need to check if key is defined if we have a default value too
if member in self.default_values:
protect = True
if protect:
fun += self.tabify("if (!json.isNull(\"%s\")) {\n" % key)
self.uptab()
fun += self.tabify("%s = " % self.memberize(member))
if typ.lower() == "float":
fun += "(float)json.optDouble(\"%s\")" % key
elif typ.lower() in NATIVES:
fun += "json.opt%s(\"%s\")" % (typ.capitalize(), key)
elif typ == "List<String>":
fun += "JsonUtil.getStringList(json.optJSONArray(\"%s\"))" % key
elif typ == "Date":
fun += "JsonUtil.parseTimestamp(json, \"%s\")" % key
elif typ == "Uri":
fun += "Uri.parse(json.getString(\"%s\"))" % key
elif list_type:
fun += "JsonUtil.parseJsonList(json.optJSONArray(\"%s\"), %s.CREATOR)" % (key, list_type)
else:
fun += "%s.CREATOR.parse(json.getJSONObject(\"%s\"))" % (typ, key)
fun += ";\n"
if protect:
self.downtab()
listmatcher = re.match(r"(?P<list_type>Array)?List(?P<content_type>[<>a-zA-Z0-9_]*)", typ)
if listmatcher is not None:
match_dict = listmatcher.groupdict()
fun += self.tabify("} else {\n")
self.uptab()
fun += self.tabify(("%s = " % self.memberize(member)))
if match_dict['list_type'] is not None and match_dict['content_type'] is not None:
fun += ("new %sList%s()" % (match_dict['list_type'], match_dict['content_type']))
else:
fun += "java.util.Collections.emptyList()"
fun += ";\n"
self.downtab()
elif member in self.default_values:
fun += self.tabify("} else {\n")
self.uptab()
fun += self.tabify(("%s = %s;\n" % (self.memberize(member), self.default_values[member])))
self.downtab()
fun += self.tabify("}\n")
self.downtab()
fun += self.tabify("}\n")
return fun
def generate_json_writer(self, foo):
fun = self.tabify("public JSONObject writeJSON() throws JSONException {\n")
self.uptab()
fun += self.tabify("JSONObject json = new JSONObject();\n")
# Parcelable doesn't support boolean without help, JSON does
NATIVES = self.NATIVE_TYPES + ["boolean", "String"]
for typ in self.get_types():
list_type = self.list_type(typ)
# Always protect strings with isNull check because JSONObject.optString()
# returns the string "null" for null strings. AWESOME.
protect = typ not in [native for native in NATIVES if native != "String"]
for member in self.props[typ]:
# Some object members are derived and not stored in JSON
if member in self.json_blacklist:
continue
# Some members have different names in JSON
if member in self.json_map:
key = self.json_map[member]
else:
key = camel_to_under(member)
if protect:
fun += self.tabify("if (%s != null) {\n" % self.memberize(member))
self.uptab()
if typ == "List<String>":
fun += self.tabify("// TODO list writing %s\n" % self.memberize(member))
elif typ == "Date":
fun += self.tabify("json.put(\"%s\", %s.getTime() / 1000);\n" % (key, self.memberize(member)))
elif typ == "Uri":
fun += self.tabify("json.put(\"%s\", String.valueOf(%s));\n" % (key, self.memberize(member)))
elif list_type:
fun += self.tabify("// TODO LIST writing %s \n" % self.memberize(member))
elif typ in NATIVES:
fun += self.tabify("json.put(\"%s\", %s);\n" % (key, self.memberize(member)))
else:
fun += self.tabify("json.put(\"%s\", %s.writeJSON());\n" % (key, self.memberize(member)))
if protect:
self.downtab()
fun += self.tabify("}\n")
fun += self.tabify("return json;\n")
self.downtab()
fun += self.tabify("}\n")
return fun
def camel_to_under(member):
""" Convert NamesInCamelCase to jsonic_underscore_names"""
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', member)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
def under_to_camel(member):
""" Convert jsonic_underscore_names to namesInCamelCase"""
def upper(match):
return match.group(1).upper()
return re.sub('_([a-z0-9])', upper, member)
def config_prop(dic, keypath, default=''):
paths = keypath.split('.')
for entry in paths:
if entry in dic:
dic = dic[entry]
else:
return default
return dic
def read_json(file_path):
"""
Returns a ParcelGen generator configured for the
the object described by the json file at file_path.
"""
with open(file_path, 'rU') as json_file:
description = json.load(json_file)
generator = ParcelGen()
generator.props = description.get("props") or {}
generator.package = description.get("package") or None
imports = description.get("imports") or ()
json_map = description.get("json_map") or {}
default_values = description.get("default_values") or {}
transient = description.get("transient") or []
make_serializable = description.get("make_serializable")
do_json_writer = description.get("do_json_writer")
json_blacklist = description.get("json_blacklist") or []
serializables = description.get("serializables") or ()
if 'do_json' in description:
do_json = description.get("do_json")
else:
do_json = True
# object_properties = []
# for type_, values in generator.props.iteritems():
# for name in values:
# object_properties.append(ObjectProperty(name, type_))
generator.json_map = json_map
generator.json_blacklist = json_blacklist
generator.serializables = serializables
generator.do_json = do_json
generator.do_json_writer = do_json_writer
if make_serializable:
generator.implements = ['Serializable']
else:
generator.implements = []
generator.default_values = default_values
return generator
def read_yaml(file_path, config_file):
yaml_to_java_types = {
'Integer': 'int',
'Boolean': 'boolean',
'Float': 'float',
'Long': 'long',
'Double': 'double',
'Url': 'Uri'
}
def process_yaml_node(typ, values):
typ = yaml_to_java_types.get(typ, typ)
if isinstance(values, dict):
for name, meta in values.iteritems():
if isinstance(meta, basestring):
yield ObjectProperty(name, typ, description=meta)
else:
yield ObjectProperty(name, typ, description=meta.get('desc'), example=meta.get('ex'))
else:
yield ObjectProperty(name, typ)
with open(file_path, 'rU') as yaml_file:
description = yaml.safe_load(yaml_file)
object_properties = []
for type_, values in description.iteritems():
if isinstance(values, dict):
object_properties.extend(process_yaml_node(type_, values))
else:
for value in values:
object_properties.extend(process_yaml_node(type_, value))
object_name = os.path.basename(file_path).split(".")[0]
generator = ParcelGen()
generator.from_yaml = True
rename = {}
if (config_file):
with open(config_file, 'rU') as yaml_config:
config = yaml.safe_load(yaml_config)
generator.package = config_prop(
config,
'Target.default_package',
default=generator.package)
obj_config = config.get('Config', {}).get(object_name, None)
if obj_config:
rename = obj_config.get('rename', rename)
generator.implements = obj_config.get('implement', [])
generator.transient.extend(obj_config.get('transient', []))
for prop in ['do_json_writer', 'serializables', 'json_blacklist', 'default_values', 'imports', 'package']:
if prop in obj_config:
setattr(generator, prop, obj_config[prop])
# TODO: invert this and pass object properties into generator
props = defaultdict(list)
json_map = {}
for object_prop in object_properties:
type_ = object_prop.type_
if type_.endswith('[]'):
type_ = "ArrayList<%s>" % type_[:-2]
if object_prop.name in rename:
name = rename[object_prop.name]
else:
name = under_to_camel(object_prop.name)
json_map[name] = object_prop.name
props[type_].append(name)
# For compatibility, json_map contains every ivar->json mapping
generator.json_map = json_map
generator.props = props
return generator
def generate_class(filePath, output, config=None):
# Read parcelable description json
if filePath.endswith('json'):
generator = read_json(filePath)
elif filePath.endswith('yaml'):
generator = read_yaml(filePath, config)
else:
raise Exception("Unsupported file type: %s" % filePath)
class_name = "_" + os.path.basename(filePath).split(".")[0]
if output:
package = generator.package
if (os.path.isdir(output)): # Resolve file location based on package + path
dirs = package.split(".")
dirs.append(class_name + ".java")
targetFile = os.path.join(output, *dirs)
# Generate child subclass if it doesn't exist
if class_name.startswith("_"):
child = class_name[1:]
new_dirs = package.split(".")
new_dirs.append(child + ".java")
child_file = os.path.join(output, *new_dirs)
if not os.path.exists(child_file):
generator.outfile = open(child_file, 'w')
generator.print_child(child, package)
else:
targetFile = output
generator.outfile = open(targetFile, 'w')
generator.print_gen(class_name)
if __name__ == "__main__":
usage = """
Generates a parcelable Java implementation for provided description file.
Writes to stdout unless destination is specified.
If destination is a directory, it is assumed to be the top level
directory of your Java source. Your class file will be written in the
appropriate folder based on its Java package.
If destination is a file, your class will be written to that file."""
parser = argparse.ArgumentParser(description=usage)
parser.add_argument('parcelfile', help='The parcelable file or a directory of files to generate source from')
parser.add_argument('-c', '--config', help='Yaml config file to use while generating source code')
parser.add_argument('destination', nargs='?', help='Output file or directory for ' +
'generated files, outputs to stdout if unspecified')
args = parser.parse_args()
source = args.parcelfile
destination = args.destination
# If both source and destination are directories, run in
# fake make mode
if (os.path.isdir(source) and os.path.isdir(destination)):
for sourcefile in [sourcefile for sourcefile in os.listdir(source) if sourcefile.endswith(".json")]:
print "decoding ", sourcefile
generate_class(os.path.join(source, sourcefile), destination)
else:
generate_class(source, destination, config=args.config)