|
9 | 9 | import json
|
10 | 10 | import argparse
|
11 | 11 | from prettytable import PrettyTable
|
12 |
| -from StringIO import StringIO |
13 | 12 |
|
14 | 13 | from utils import argparse_filestring_type, \
|
15 | 14 | argparse_lowercase_hyphen_type, argparse_uppercase_type
|
@@ -386,59 +385,55 @@ def search_objects(self, path):
|
386 | 385 |
|
387 | 386 | export_formats = ["json", "csv-ci", "table"]
|
388 | 387 |
|
389 |
| - def generate_output(self, export_format, file_output=None, silent=False): |
| 388 | + def generate_output(self, export_format, file_output=None): |
390 | 389 | """ Generates summary of memory map data
|
391 | 390 |
|
392 | 391 | Positional arguments:
|
393 | 392 | export_format - the format to dump
|
394 | 393 |
|
395 | 394 | Keyword arguments:
|
396 | 395 | file_desc - descriptor (either stdout or file)
|
| 396 | +
|
| 397 | + Returns: generated string for the 'table' format, otherwise None |
397 | 398 | """
|
398 | 399 |
|
399 | 400 | try:
|
400 |
| - if silent: |
401 |
| - file_desc = None |
| 401 | + if file_output: |
| 402 | + file_desc = open(file_output, 'wb') |
402 | 403 | else:
|
403 |
| - if file_output: |
404 |
| - file_desc = open(file_output, 'wb') |
405 |
| - else: |
406 |
| - file_desc = sys.stdout |
| 404 | + file_desc = sys.stdout |
407 | 405 | except IOError as error:
|
408 | 406 | print "I/O error({0}): {1}".format(error.errno, error.strerror)
|
409 | 407 | return False
|
410 | 408 |
|
411 | 409 | to_call = {'json': self.generate_json,
|
412 | 410 | 'csv-ci': self.generate_csv,
|
413 | 411 | 'table': self.generate_table}[export_format]
|
414 |
| - output_string = to_call(file_desc) |
| 412 | + output = to_call(file_desc) |
415 | 413 |
|
416 |
| - if file_desc is not sys.stdout and file_desc is not None: |
| 414 | + if file_desc is not sys.stdout: |
417 | 415 | file_desc.close()
|
418 | 416 |
|
419 |
| - return output_string |
| 417 | + return output |
420 | 418 |
|
421 | 419 | def generate_json(self, file_desc):
|
422 | 420 | """Generate a json file from a memory map
|
423 | 421 |
|
424 | 422 | Positional arguments:
|
425 | 423 | file_desc - the file to write out the final report to
|
426 | 424 | """
|
427 |
| - output = json.dumps(self.mem_report, indent=4) |
428 |
| - if file_desc: |
429 |
| - file_desc.write(output) |
430 |
| - file_desc.write('\n') |
| 425 | + file_desc.write(json.dumps(self.mem_report, indent=4)) |
| 426 | + file_desc.write('\n') |
431 | 427 |
|
432 |
| - return output |
| 428 | + return None |
433 | 429 |
|
434 | 430 | def generate_csv(self, file_desc):
|
435 | 431 | """Generate a CSV file from a memoy map
|
436 | 432 |
|
437 | 433 | Positional arguments:
|
438 | 434 | file_desc - the file to write out the final report to
|
439 | 435 | """
|
440 |
| - string_io = StringIO() |
441 |
| - csv_writer = csv.writer(string_io, delimiter=',', |
| 436 | + csv_writer = csv.writer(file_desc, delimiter=',', |
442 | 437 | quoting=csv.QUOTE_MINIMAL)
|
443 | 438 |
|
444 | 439 | csv_module_section = []
|
@@ -472,16 +467,15 @@ def generate_csv(self, file_desc):
|
472 | 467 | csv_writer.writerow(csv_module_section)
|
473 | 468 | csv_writer.writerow(csv_sizes)
|
474 | 469 |
|
475 |
| - if file_desc: |
476 |
| - file_desc.write(string_io.getvalue()) |
477 |
| - |
478 |
| - return string_io.getvalue() |
| 470 | + return None |
479 | 471 |
|
480 | 472 | def generate_table(self, file_desc):
|
481 | 473 | """Generate a table from a memoy map
|
482 | 474 |
|
483 | 475 | Positional arguments:
|
484 | 476 | file_desc - the file to write out the final report to
|
| 477 | +
|
| 478 | + Returns: string of the generated table |
485 | 479 | """
|
486 | 480 | # Create table
|
487 | 481 | columns = ['Module']
|
@@ -539,9 +533,6 @@ def generate_table(self, file_desc):
|
539 | 533 | output += "Total Flash memory (text + data + misc): %s bytes\n" % \
|
540 | 534 | str(self.mem_summary['total_flash'])
|
541 | 535 |
|
542 |
| - if file_desc: |
543 |
| - file_desc.write(output) |
544 |
| - |
545 | 536 | return output
|
546 | 537 |
|
547 | 538 | toolchains = ["ARM", "ARM_STD", "ARM_MICRO", "GCC_ARM", "IAR"]
|
@@ -666,11 +657,15 @@ def main():
|
666 | 657 | if memap.parse(args.file, args.toolchain) is False:
|
667 | 658 | sys.exit(0)
|
668 | 659 |
|
| 660 | + returned_string = None |
669 | 661 | # Write output in file
|
670 | 662 | if args.output != None:
|
671 |
| - memap.generate_output(args.export, args.output) |
| 663 | + returned_string = memap.generate_output(args.export, args.output) |
672 | 664 | else: # Write output in screen
|
673 |
| - memap.generate_output(args.export) |
| 665 | + returned_string = memap.generate_output(args.export) |
| 666 | + |
| 667 | + if args.export == 'table' and returned_string: |
| 668 | + print returned_string |
674 | 669 |
|
675 | 670 | sys.exit(0)
|
676 | 671 |
|
|
0 commit comments