show.py 4.6 KB
Newer Older
W
wuzewu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (c) 2019  PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
W
wuzewu 已提交
18 19 20 21

import os
import argparse

W
wuzewu 已提交
22
from paddlehub.common.logger import logger
23
from paddlehub.common import utils
W
wuzewu 已提交
24
from paddlehub.commands.base_command import BaseCommand, ENTRY
W
wuzewu 已提交
25
from paddlehub.commands.cml_utils import TablePrinter
W
wuzewu 已提交
26 27
from paddlehub.module.manager import default_module_manager
from paddlehub.module.module import Module
W
wuzewu 已提交
28
from paddlehub.io.parser import yaml_parser
W
wuzewu 已提交
29 30 31


class ShowCommand(BaseCommand):
W
wuzewu 已提交
32
    name = "show"
W
wuzewu 已提交
33

W
wuzewu 已提交
34 35 36
    def __init__(self, name):
        super(ShowCommand, self).__init__(name)
        self.show_in_help = True
Z
Zeyu Chen 已提交
37
        self.description = "Show the information of PaddleHub module."
W
wuzewu 已提交
38 39 40 41 42
        self.parser = self.parser = argparse.ArgumentParser(
            description=self.__class__.__doc__,
            prog='%s %s <module_name/module_dir>' % (ENTRY, name),
            usage='%(prog)s',
            add_help=False)
W
wuzewu 已提交
43

W
wuzewu 已提交
44
    def show_model_info(self, model_info_file):
W
wuzewu 已提交
45
        model_info = yaml_parser.parse(model_info_file)
46 47 48 49
        if utils.is_windows():
            placeholders = [15, 40]
        else:
            placeholders = [15, 50]
W
wuzewu 已提交
50 51
        tp = TablePrinter(
            titles=["ModelName", model_info['name']],
52
            placeholders=placeholders,
W
wuzewu 已提交
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
            title_colors=["yellow", None],
            title_aligns=["^", "<"])
        tp.add_line(
            contents=["Type", model_info['type']],
            colors=["yellow", None],
            aligns=["^", "<"])
        tp.add_line(
            contents=["Version", model_info['version']],
            colors=["yellow", None],
            aligns=["^", "<"])
        tp.add_line(
            contents=["Summary", model_info['description']],
            colors=["yellow", None],
            aligns=["^", "<"])
        tp.add_line(
            contents=["Author", model_info['author']],
            colors=["yellow", None],
            aligns=["^", "<"])
        tp.add_line(
            contents=["Author-Email", model_info['author_email']],
            colors=["yellow", None],
            aligns=["^", "<"])
        print(tp.get_text())
        return True

    def show_module_info(self, module_dir):
        module = Module(module_dir=module_dir)
80 81 82 83
        if utils.is_windows():
            placeholders = [15, 40]
        else:
            placeholders = [15, 50]
W
wuzewu 已提交
84 85
        tp = TablePrinter(
            titles=["ModuleName", module.name],
86
            placeholders=placeholders,
W
wuzewu 已提交
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
            title_colors=["light_red", None],
            title_aligns=["^", "<"])
        tp.add_line(
            contents=["Version", module.version],
            colors=["light_red", None],
            aligns=["^", "<"])
        tp.add_line(
            contents=["Summary", module.summary],
            colors=["light_red", None],
            aligns=["^", "<"])
        tp.add_line(
            contents=["Author", module.author],
            colors=["light_red", None],
            aligns=["^", "<"])
        tp.add_line(
            contents=["Author-Email", module.author_email],
            colors=["light_red", None],
            aligns=["^", "<"])
        tp.add_line(
            contents=["Location", module_dir],
            colors=["light_red", None],
            aligns=["^", "<"])
        print(tp.get_text())
        return True

W
wuzewu 已提交
112
    def exec(self, argv):
W
wuzewu 已提交
113
        if not argv:
W
wuzewu 已提交
114
            print("ERROR: Please specify a module or a model\n")
W
wuzewu 已提交
115 116 117
            self.help()
            return False

W
wuzewu 已提交
118
        module_name = argv[0]
W
wuzewu 已提交
119

W
wuzewu 已提交
120
        # nlp model
W
wuzewu 已提交
121 122 123
        model_info_file = os.path.join(module_name, "info.yml")
        if os.path.exists(model_info_file):
            self.show_model_info(model_info_file)
W
wuzewu 已提交
124 125
            return True

W
wuzewu 已提交
126
        cwd = os.getcwd()
W
wuzewu 已提交
127
        module_dir = default_module_manager.search_module(module_name)
W
wuzewu 已提交
128 129 130
        module_dir = os.path.join(cwd,
                                  module_name) if not module_dir else module_dir
        if not module_dir or not os.path.exists(module_dir):
W
wuzewu 已提交
131
            return True
W
wuzewu 已提交
132

W
wuzewu 已提交
133
        self.show_module_info(module_dir)
W
wuzewu 已提交
134
        return True
W
wuzewu 已提交
135 136 137


command = ShowCommand.instance()