OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 import collections | 7 import collections |
8 from datetime import date | 8 from datetime import date |
9 import re | 9 import re |
10 import optparse | 10 import optparse |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 // From | 322 // From |
323 // ${SOURCE_PATH} | 323 // ${SOURCE_PATH} |
324 | 324 |
325 package ${PACKAGE}; | 325 package ${PACKAGE}; |
326 | 326 |
327 import android.support.annotation.IntDef; | 327 import android.support.annotation.IntDef; |
328 | 328 |
329 import java.lang.annotation.Retention; | 329 import java.lang.annotation.Retention; |
330 import java.lang.annotation.RetentionPolicy; | 330 import java.lang.annotation.RetentionPolicy; |
331 | 331 |
332 public class ${CLASS_NAME} { | 332 @IntDef({ |
333 @IntDef({ | |
334 ${INT_DEF} | 333 ${INT_DEF} |
335 }) | 334 }) |
336 @Retention(RetentionPolicy.SOURCE) | 335 @Retention(RetentionPolicy.SOURCE) |
337 public @interface ${ANNOTATION} {} | 336 public @interface ${CLASS_NAME} { |
338 ${ENUM_ENTRIES} | 337 ${ENUM_ENTRIES} |
339 } | 338 } |
340 """) | 339 """) |
341 | 340 |
342 enum_template = Template(' public static final int ${NAME} = ${VALUE};') | 341 enum_template = Template(' int ${NAME} = ${VALUE};') |
343 enum_entries_string = [] | 342 enum_entries_string = [] |
344 enum_names = [] | 343 enum_names = [] |
345 for enum_name, enum_value in enum_definition.entries.iteritems(): | 344 for enum_name, enum_value in enum_definition.entries.iteritems(): |
346 values = { | 345 values = { |
347 'NAME': enum_name, | 346 'NAME': enum_name, |
348 'VALUE': enum_value, | 347 'VALUE': enum_value, |
349 } | 348 } |
350 enum_comments = enum_definition.comments.get(enum_name) | 349 enum_comments = enum_definition.comments.get(enum_name) |
351 if enum_comments: | 350 if enum_comments: |
352 enum_comments_indent = ' * ' | 351 enum_comments_indent = ' * ' |
353 comments_line_wrapper = textwrap.TextWrapper( | 352 comments_line_wrapper = textwrap.TextWrapper( |
354 initial_indent=enum_comments_indent, | 353 initial_indent=enum_comments_indent, |
355 subsequent_indent=enum_comments_indent, | 354 subsequent_indent=enum_comments_indent, |
356 width=100) | 355 width=100) |
357 enum_entries_string.append(' /**') | 356 enum_entries_string.append(' /**') |
358 enum_entries_string.append( | 357 enum_entries_string.append( |
359 '\n'.join(comments_line_wrapper.wrap(enum_comments))) | 358 '\n'.join(comments_line_wrapper.wrap(enum_comments))) |
360 enum_entries_string.append(' */') | 359 enum_entries_string.append(' */') |
361 enum_entries_string.append(enum_template.substitute(values)) | 360 enum_entries_string.append(enum_template.substitute(values)) |
362 enum_names.append(enum_name) | 361 enum_names.append(enum_definition.class_name + '.' + enum_name) |
363 enum_entries_string = '\n'.join(enum_entries_string) | 362 enum_entries_string = '\n'.join(enum_entries_string) |
364 | 363 |
365 enum_names_indent = ' ' * 6 | 364 enum_names_indent = ' ' * 4 |
366 wrapper = textwrap.TextWrapper(initial_indent = enum_names_indent, | 365 wrapper = textwrap.TextWrapper(initial_indent = enum_names_indent, |
367 subsequent_indent = enum_names_indent, | 366 subsequent_indent = enum_names_indent, |
368 width = 100) | 367 width = 100) |
369 enum_names_string = '\n'.join(wrapper.wrap(', '.join(enum_names))) | 368 enum_names_string = '\n'.join(wrapper.wrap(', '.join(enum_names))) |
370 | 369 |
371 annotation_template = Template('${NAME}Enum') | |
372 annotation_values = { 'NAME': enum_definition.class_name, } | |
373 annotation_name = annotation_template.substitute(annotation_values) | |
374 | |
375 values = { | 370 values = { |
376 'CLASS_NAME': enum_definition.class_name, | 371 'CLASS_NAME': enum_definition.class_name, |
377 'ENUM_ENTRIES': enum_entries_string, | 372 'ENUM_ENTRIES': enum_entries_string, |
378 'PACKAGE': enum_definition.enum_package, | 373 'PACKAGE': enum_definition.enum_package, |
379 'INT_DEF': enum_names_string, | 374 'INT_DEF': enum_names_string, |
380 'ANNOTATION': annotation_name, | |
381 'SCRIPT_NAME': GetScriptName(), | 375 'SCRIPT_NAME': GetScriptName(), |
382 'SOURCE_PATH': source_path, | 376 'SOURCE_PATH': source_path, |
383 'YEAR': str(date.today().year) | 377 'YEAR': str(date.today().year) |
384 } | 378 } |
385 return template.substitute(values) | 379 return template.substitute(values) |
386 | 380 |
387 | 381 |
388 def DoMain(argv): | 382 def DoMain(argv): |
389 usage = 'usage: %prog [options] [output_dir] input_file(s)...' | 383 usage = 'usage: %prog [options] [output_dir] input_file(s)...' |
390 parser = optparse.OptionParser(usage=usage) | 384 parser = optparse.OptionParser(usage=usage) |
(...skipping 12 matching lines...) Expand all Loading... |
403 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: | 397 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: |
404 for output_path, data in DoGenerate(input_paths): | 398 for output_path, data in DoGenerate(input_paths): |
405 build_utils.AddToZipHermetic(srcjar, output_path, data=data) | 399 build_utils.AddToZipHermetic(srcjar, output_path, data=data) |
406 | 400 |
407 if options.depfile: | 401 if options.depfile: |
408 build_utils.WriteDepfile(options.depfile, options.srcjar) | 402 build_utils.WriteDepfile(options.depfile, options.srcjar) |
409 | 403 |
410 | 404 |
411 if __name__ == '__main__': | 405 if __name__ == '__main__': |
412 DoMain(sys.argv[1:]) | 406 DoMain(sys.argv[1:]) |
OLD | NEW |