show.py 3.0 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 23 24 25 26
from paddlehub.common.logger import logger
from paddlehub.commands.base_command import BaseCommand, ENTRY
from paddlehub.module.manager import default_module_manager
from paddlehub.module.module import Module
from paddlehub.io.reader import yaml_reader
W
wuzewu 已提交
27 28 29


class ShowCommand(BaseCommand):
W
wuzewu 已提交
30
    name = "show"
W
wuzewu 已提交
31

W
wuzewu 已提交
32 33 34
    def __init__(self, name):
        super(ShowCommand, self).__init__(name)
        self.show_in_help = True
Z
Zeyu Chen 已提交
35
        self.description = "Show the information of PaddleHub module."
W
wuzewu 已提交
36 37 38 39 40
        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 已提交
41 42

    def exec(self, argv):
W
wuzewu 已提交
43
        if not argv:
W
wuzewu 已提交
44
            print("ERROR: Please specify a module or a model\n")
W
wuzewu 已提交
45 46 47
            self.help()
            return False

W
wuzewu 已提交
48
        module_name = argv[0]
W
wuzewu 已提交
49

W
wuzewu 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
        # nlp model
        model_info = os.path.join(module_name, "info.yml")
        if os.path.exists(model_info):
            model_info = yaml_reader.read(model_info)
            show_text = "Name:%s\n" % model_info['name']
            show_text += "Type:%s\n" % model_info['type']
            show_text += "Version:%s\n" % model_info['version']
            show_text += "Summary:\n"
            show_text += "  %s\n" % model_info['description']
            show_text += "Author:%s\n" % model_info['author']
            show_text += "Author-Email:%s\n" % model_info['author_email']
            print(show_text)
            return True

W
wuzewu 已提交
64
        cwd = os.getcwd()
W
wuzewu 已提交
65
        module_dir = default_module_manager.search_module(module_name)
W
wuzewu 已提交
66 67 68
        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 已提交
69
            return True
W
wuzewu 已提交
70 71 72 73 74 75 76 77 78 79

        module = Module(module_dir=module_dir)
        show_text = "Name:%s\n" % module.name
        show_text += "Version:%s\n" % module.version
        show_text += "Summary:\n"
        show_text += "  %s\n" % module.summary
        show_text += "Author:%s\n" % module.author
        show_text += "Author-Email:%s\n" % module.author_email
        show_text += "Location:%s\n" % module_dir
        print(show_text)
W
wuzewu 已提交
80
        return True
W
wuzewu 已提交
81 82 83


command = ShowCommand.instance()