Esempio n. 1
0
    def parse(self, file_path, extra_args=None):
        from InteractiveHtmlBom.core import ibom
        from InteractiveHtmlBom.errors import (ExitCodes, ParsingException,
                                               exit_error)
        from InteractiveHtmlBom.ecad import get_parser_by_extension
        if not os.path.isfile(file_path):
            exit_error(logger, ExitCodes.ERROR_FILE_NOT_FOUND,
                       "File %s does not exist." % file_path)
        print("Loading %s" % file_path)
        ibom_parser = get_parser_by_extension(os.path.abspath(file_path),
                                              self._config, self._logger)
        if extra_args is not None:
            sys.argv.extend(extra_args.split())
        args = self._argparser.parse_args()
        self._config.set_from_args(args)

        self.pcbdata, self.components = ibom_parser.parse()
        if not self.pcbdata and not self.components:
            raise ParsingException('Parsing failed.')
    if create_wx_app:
        import wx
        app = wx.App()

    parser = argparse.ArgumentParser(
        description='KiCad InteractiveHtmlBom plugin CLI.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('file',
                        type=lambda s: to_utf(s),
                        help="KiCad PCB file")
    config = Config(version)
    config.add_options(parser, config.FILE_NAME_FORMAT_HINT)
    args = parser.parse_args()
    logger = ibom.Logger(cli=True)
    if not os.path.isfile(args.file):
        exit_error(logger, ExitCodes.ERROR_FILE_NOT_FOUND,
                   "File %s does not exist." % args.file)
    print("Loading %s" % args.file)
    parser = get_parser_by_extension(os.path.abspath(args.file), config,
                                     logger)
    if args.show_dialog:
        if not create_wx_app:
            exit_error(
                logger, ExitCodes.ERROR_NO_DISPLAY, "Can not show dialog when "
                "INTERACTIVE_HTML_BOM_NO_DISPLAY is set.")
        try:
            ibom.run_with_dialog(parser, config, logger)
        except ParsingException as e:
            exit_error(logger, ExitCodes.ERROR_PARSE, e)
    else:
        config.set_from_args(args)
        try: