|
20 | 20 | from os import listdir
|
21 | 21 | from os.path import expanduser, isfile, join, exists, isdir
|
22 | 22 | import logging
|
| 23 | +import functools |
| 24 | +import json |
23 | 25 |
|
24 | 26 | from .platform_database import (
|
25 | 27 | PlatformDatabase,
|
|
34 | 36 | logger.addHandler(logging.NullHandler())
|
35 | 37 |
|
36 | 38 |
|
| 39 | +def deprecated(reason): |
| 40 | + """Deprecate a function/method with a decorator""" |
| 41 | + def actual_decorator(func): |
| 42 | + @functools.wraps(func) |
| 43 | + def new_func(*args, **kwargs): |
| 44 | + logger.warning("Call to deprecated function %s. %s", |
| 45 | + func.__name__, reason) |
| 46 | + return func(*args, **kwargs) |
| 47 | + return new_func |
| 48 | + return actual_decorator |
| 49 | + |
| 50 | + |
37 | 51 | class FSInteraction(object):
|
38 | 52 | BeforeFilter = 1
|
39 | 53 | AfterFilter = 2
|
40 | 54 | Never = 3
|
41 | 55 |
|
42 | 56 |
|
43 |
| -class MbedDetectLsToolsBase(object): |
| 57 | +class MbedLsToolsBase(object): |
44 | 58 | """ Base class for mbed-lstools, defines mbed-ls tools interface for
|
45 | 59 | mbed-enabled devices detection for various hosts
|
46 | 60 | """
|
@@ -573,3 +587,168 @@ def _run_cli_process(cmd, shell=True):
|
573 | 587 | p = Popen(cmd, shell=shell, stdout=PIPE, stderr=PIPE)
|
574 | 588 | _stdout, _stderr = p.communicate()
|
575 | 589 | return _stdout, _stderr, p.returncode
|
| 590 | + |
| 591 | + @deprecated("Functionality has been moved into 'list_mbeds'. " |
| 592 | + "Please use list_mbeds with 'unique_names=True' and " |
| 593 | + "'read_details_txt=True'") |
| 594 | + def list_mbeds_ext(self): |
| 595 | + """! Function adds extra information for each mbed device |
| 596 | + @return Returns list of mbed devices plus extended data like 'platform_name_unique' |
| 597 | + @details Get information about mbeds with extended parameters/info included |
| 598 | + """ |
| 599 | + |
| 600 | + return self.list_mbeds(unique_names=True, read_details_txt=True) |
| 601 | + |
| 602 | + @deprecated("List formatting methods are deprecated for a simpler API. " |
| 603 | + "Please use 'list_mbeds' instead.") |
| 604 | + def list_manufacture_ids(self): |
| 605 | + """! Creates list of all available mappings for target_id -> Platform |
| 606 | + @return String with table formatted output |
| 607 | + """ |
| 608 | + from prettytable import PrettyTable, HEADER |
| 609 | + |
| 610 | + columns = ['target_id_prefix', 'platform_name'] |
| 611 | + pt = PrettyTable(columns, junction_char="|", hrules=HEADER) |
| 612 | + for col in columns: |
| 613 | + pt.align[col] = 'l' |
| 614 | + |
| 615 | + for target_id_prefix, platform_name in sorted(self.plat_db.items()): |
| 616 | + pt.add_row([target_id_prefix, platform_name]) |
| 617 | + |
| 618 | + return pt.get_string() |
| 619 | + |
| 620 | + @deprecated("List formatting methods are deprecated to simplify the API. " |
| 621 | + "Please use 'list_mbeds' instead.") |
| 622 | + def list_platforms(self): |
| 623 | + """! Useful if you just want to know which platforms are currently available on the system |
| 624 | + @return List of (unique values) available platforms |
| 625 | + """ |
| 626 | + result = [] |
| 627 | + mbeds = self.list_mbeds() |
| 628 | + for i, val in enumerate(mbeds): |
| 629 | + platform_name = str(val['platform_name']) |
| 630 | + if platform_name not in result: |
| 631 | + result.append(platform_name) |
| 632 | + return result |
| 633 | + |
| 634 | + @deprecated("List formatting methods are deprecated to simplify the API. " |
| 635 | + "Please use 'list_mbeds' instead.") |
| 636 | + def list_platforms_ext(self): |
| 637 | + """! Useful if you just want to know how many platforms of each type are currently available on the system |
| 638 | + @return Dict of platform: platform_count |
| 639 | + """ |
| 640 | + result = {} |
| 641 | + mbeds = self.list_mbeds() |
| 642 | + for i, val in enumerate(mbeds): |
| 643 | + platform_name = str(val['platform_name']) |
| 644 | + if platform_name not in result: |
| 645 | + result[platform_name] = 1 |
| 646 | + else: |
| 647 | + result[platform_name] += 1 |
| 648 | + return result |
| 649 | + |
| 650 | + @deprecated("List formatting methods are deprecated to simplify the API. " |
| 651 | + "Please use 'list_mbeds' instead.") |
| 652 | + def list_mbeds_by_targetid(self): |
| 653 | + """! Get information about mbeds with extended parameters/info included |
| 654 | + @return Returns dictionary where keys are TargetIDs and values are mbed structures |
| 655 | + @details Ordered by target id (key: target_id). |
| 656 | + """ |
| 657 | + result = {} |
| 658 | + mbed_list = self.list_mbeds_ext() |
| 659 | + for mbed in mbed_list: |
| 660 | + target_id = mbed['target_id'] |
| 661 | + result[target_id] = mbed |
| 662 | + return result |
| 663 | + |
| 664 | + @deprecated("List formatting methods are deprecated to simplify the API. " |
| 665 | + "Please use 'list_mbeds' instead.") |
| 666 | + def get_string(self, border=False, header=True, padding_width=1, sortby='platform_name'): |
| 667 | + """! Printing with some sql table like decorators |
| 668 | + @param border Table border visibility |
| 669 | + @param header Table header visibility |
| 670 | + @param padding_width Table padding |
| 671 | + @param sortby Column used to sort results |
| 672 | + @return Returns string which can be printed on console |
| 673 | + """ |
| 674 | + from prettytable import PrettyTable, HEADER |
| 675 | + result = '' |
| 676 | + mbeds = self.list_mbeds(unique_names=True, read_details_txt=True) |
| 677 | + if mbeds: |
| 678 | + """ ['platform_name', 'mount_point', 'serial_port', 'target_id'] - columns generated from USB auto-detection |
| 679 | + ['platform_name_unique', ...] - columns generated outside detection subsystem (OS dependent detection) |
| 680 | + """ |
| 681 | + columns = ['platform_name', 'platform_name_unique', 'mount_point', 'serial_port', 'target_id', 'daplink_version'] |
| 682 | + pt = PrettyTable(columns, junction_char="|", hrules=HEADER) |
| 683 | + for col in columns: |
| 684 | + pt.align[col] = 'l' |
| 685 | + |
| 686 | + for mbed in mbeds: |
| 687 | + row = [] |
| 688 | + for col in columns: |
| 689 | + row.append(mbed[col] if col in mbed and mbed[col] else 'unknown') |
| 690 | + pt.add_row(row) |
| 691 | + result = pt.get_string(border=border, header=header, padding_width=padding_width, sortby=sortby) |
| 692 | + return result |
| 693 | + |
| 694 | + # Private functions supporting API |
| 695 | + |
| 696 | + @deprecated("This method will be removed from the public API. " |
| 697 | + "Please use 'list_mbeds' instead") |
| 698 | + def get_json_data_from_file(self, json_spec_filename, verbose=False): |
| 699 | + """! Loads from file JSON formatted string to data structure |
| 700 | + @return None if JSON can be loaded |
| 701 | + """ |
| 702 | + try: |
| 703 | + with open(json_spec_filename) as data_file: |
| 704 | + try: |
| 705 | + return json.load(data_file) |
| 706 | + except ValueError as json_error_msg: |
| 707 | + logger.error("Parsing file(%s): %s", json_spec_filename, json_error_msg) |
| 708 | + return None |
| 709 | + except IOError as fileopen_error_msg: |
| 710 | + logger.warning(fileopen_error_msg) |
| 711 | + return None |
| 712 | + |
| 713 | + @deprecated("This method will be removed from the public API. " |
| 714 | + "Please use 'list_mbeds' instead") |
| 715 | + def get_htm_target_id(self, mount_point): |
| 716 | + target_id, _ = self._read_htm_ids(mount_point) |
| 717 | + return target_id |
| 718 | + |
| 719 | + @deprecated("This method will be removed from the public API. " |
| 720 | + "Please use 'list_mbeds' instead") |
| 721 | + def get_mbed_htm(self, mount_point): |
| 722 | + _, build_info = self._read_htm_ids(mount_point) |
| 723 | + return build_info |
| 724 | + |
| 725 | + @deprecated("This method will be removed from the public API. " |
| 726 | + "Please use 'list_mbeds' instead") |
| 727 | + def get_mbed_htm_comment_section_ver_build(self, line): |
| 728 | + return self._mbed_htm_comment_section_ver_build(line) |
| 729 | + |
| 730 | + @deprecated("This method will be removed from the public API. " |
| 731 | + "Please use 'list_mbeds' instead") |
| 732 | + def get_mbed_htm_lines(self, mount_point): |
| 733 | + return self._htm_lines(mount_point) |
| 734 | + |
| 735 | + @deprecated("This method will be removed from the public API. " |
| 736 | + "Please use 'list_mbeds' instead") |
| 737 | + def get_details_txt(self, mount_point): |
| 738 | + return self._details_txt(mount_point) |
| 739 | + |
| 740 | + @deprecated("This method will be removed from the public API. " |
| 741 | + "Please use 'list_mbeds' instead") |
| 742 | + def parse_details_txt(self, lines): |
| 743 | + return self._parse_details(lines) |
| 744 | + |
| 745 | + @deprecated("This method will be removed from the public API. " |
| 746 | + "Please use 'list_mbeds' instead") |
| 747 | + def scan_html_line_for_target_id(self, line): |
| 748 | + return self._target_id_from_htm(line) |
| 749 | + |
| 750 | + @staticmethod |
| 751 | + @deprecated("This method will be removed from the public API. " |
| 752 | + "Please use 'list_mbeds' instead") |
| 753 | + def run_cli_process(cmd, shell=True): |
| 754 | + return MbedLsToolsBase._run_cli_process(cmd, shell) |
0 commit comments