提交 7d6054ba 编写于 作者: W wuzewu

update commands

上级 c492de97
......@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from . import hub
from . import download
from . import run
from . import show
......@@ -22,3 +21,4 @@ from . import install
from . import uninstall
from . import search
from . import help
from . import hub
......@@ -16,9 +16,10 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from paddle_hub.tools.arg_helper import add_argument, print_arguments
from paddle_hub.tools.logger import logger
import argparse
ENTRY = "hub"
class BaseCommand:
command_dict = {}
......@@ -39,12 +40,14 @@ class BaseCommand:
assert not hasattr(
self.__class__,
'_instance'), 'Please use `instance()` to get Command object!'
self.parser = argparse.ArgumentParser(description=__doc__)
self.args = None
self.name = name
self.show_in_help = True
self.description = ""
def help(self):
self.parser.print_help()
def add_arg(self, argument, type="str", default=None, help=None):
add_argument(
argument=argument,
......
......@@ -16,10 +16,11 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand
from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.tools import utils
from paddle_hub.tools.downloader import default_downloader
from paddle_hub.hub_server import default_hub_server
import argparse
class DownloadCommand(BaseCommand):
......@@ -29,15 +30,21 @@ class DownloadCommand(BaseCommand):
super(DownloadCommand, self).__init__(name)
self.show_in_help = True
self.description = "Download a paddle hub module."
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <module_name>' % (ENTRY, name),
usage='%(prog)s [options]',
add_help=False)
# yapf: disable
self.add_arg('--output_path', str, ".", "path to save the module, default in current directory" )
self.add_arg('--output_path', str, ".", "path to save the module" )
self.add_arg('--uncompress', bool, False, "uncompress the download package or not" )
# yapf: enable
def help(self):
self.parser.print_help()
def exec(self, argv):
if not argv:
print("ERROR: Please specify a module\n")
self.help()
return False
module_name = argv[0]
module_version = None if "==" not in module_name else module_name.split(
"==")[1]
......@@ -55,7 +62,7 @@ class DownloadCommand(BaseCommand):
if module_version:
tips += " with version %s" % module_version
print(tips)
return
return True
self.print_args()
if self.args.uncompress:
......@@ -65,7 +72,7 @@ class DownloadCommand(BaseCommand):
result, tips, file = default_downloader.download_file(
url=url, save_path=self.args.output_path)
print(tips)
return result
return True
command = DownloadCommand.instance()
......@@ -42,6 +42,7 @@ class HelpCommand(BaseCommand):
help_text += " %-15s\t\t%s\n" % (command.name, command.description)
print(help_text)
return True
command = HelpCommand.instance()
......@@ -31,22 +31,24 @@ class HubCommand(BaseCommand):
def __init__(self, name):
super(HubCommand, self).__init__(name)
self.show_in_help = False
# yapf: disable
self.add_arg('command', str, None, "command to run" )
# yapf: enable
def exec(self, argv):
args = self.parser.parse_args(argv[1:2])
if not args.command in BaseCommand.command_dict:
logger.critical("command %s not supported!" % args.command)
if not argv:
help.command.exec(argv)
exit(1)
return False
sub_command = argv[0]
if not sub_command in BaseCommand.command_dict:
print("ERROR: unknown command '%s'" % sub_command)
help.command.exec(argv)
exit(1)
return False
command = BaseCommand.command_dict[args.command]
command.exec(argv[2:])
command = BaseCommand.command_dict[sub_command]
return command.exec(argv[1:])
command = HubCommand.instance()
if __name__ == "__main__":
command.exec(sys.argv)
command.exec(sys.argv[1:])
......@@ -16,9 +16,10 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand
from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.tools import utils
from paddle_hub.module.manager import default_module_manager
import argparse
class InstallCommand(BaseCommand):
......@@ -28,12 +29,18 @@ class InstallCommand(BaseCommand):
super(InstallCommand, self).__init__(name)
self.show_in_help = True
self.description = "Install the specify module to current environment."
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <module_name>' % (ENTRY, name),
usage='%(prog)s',
add_help=False)
#TODO(wuzewu): add --upgrade option
def help(self):
self.parser.print_help()
def exec(self, argv):
if not argv:
print("ERROR: Please specify a module\n")
self.help()
return False
module_name = argv[0]
module_version = None if "==" not in module_name else module_name.split(
"==")[1]
......@@ -42,7 +49,7 @@ class InstallCommand(BaseCommand):
result, tips, module_dir = default_module_manager.install_module(
module_name=module_name, module_version=module_version)
print(tips)
return result
return True
command = InstallCommand.instance()
......@@ -38,6 +38,7 @@ class ListCommand(BaseCommand):
for module_name, module_dir in all_modules.items():
list_text += " %-20s\t\t%s\n" % (module_name, module_dir)
print(list_text)
return True
command = ListCommand.instance()
......@@ -16,11 +16,14 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand
import paddle_hub as hub
from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.data.reader import csv_reader, yaml_reader
from paddle_hub.module.manager import default_module_manager
from paddle_hub.tools import utils
from paddle_hub.tools.arg_helper import add_argument, print_arguments
import paddle_hub as hub
import argparse
import os
class RunCommand(BaseCommand):
......@@ -30,51 +33,59 @@ class RunCommand(BaseCommand):
super(RunCommand, self).__init__(name)
self.show_in_help = True
self.description = "Run the specify module"
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <module>' % (ENTRY, name),
usage='%(prog)s [options]')
# yapf: disable
self.add_arg('--config', str, None, "config file in yaml format" )
self.add_arg('--dataset', str, None, "dataset be used" )
self.add_arg('--module', str, None, "module to run" )
self.add_arg('--signature', str, None, "signature to run" )
# yapf: enable
def _check_module(self):
if not self.args.module:
logger.critical("lack of module")
self.help()
exit(1)
def _check_dataset(self):
if not self.args.dataset:
logger.critical("lack of dataset file")
print("Error! Lack of dataset file")
self.help()
exit(1)
if not utils.is_csv_file(self.args.dataset):
logger.critical("dataset file should in csv format")
print("Error! Dataset file should in csv format")
self.help()
exit(1)
def _check_config(self):
if not self.args.config:
logger.critical("lack of config file")
print("Error! Lack of config file")
self.help()
exit(1)
if not utils.is_yaml_file(self.args.config):
logger.critical("config file should in yaml format")
print("Error! Config file should in yaml format")
self.help()
exit(1)
def help(self):
self.parser.print_help()
def exec(self, argv):
self.args = self.parser.parse_args(argv)
self.print_args()
self._check_module()
if not argv:
print("ERROR: Please specify a key\n")
self.help()
return False
module_name = argv[0]
self.args = self.parser.parse_args(argv[1:])
self._check_dataset()
self._check_config()
module = hub.Module(module_dir=self.args.module)
module_dir = default_module_manager.search_module(module_name)
if not module_dir:
if os.path.exists(module_name):
module_dir = module_name
else:
print("Install Module %s" % module_name)
result, tips, module_dir = default_module_manager.install_module(
module_name)
print(tips)
if not result:
return False
module = hub.Module(module_dir=module_dir)
yaml_config = yaml_reader.read(self.args.config)
if not self.args.signature:
......
......@@ -16,9 +16,10 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand
from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.tools import utils
from paddle_hub.hub_server import default_hub_server
import argparse
class SearchCommand(BaseCommand):
......@@ -28,8 +29,18 @@ class SearchCommand(BaseCommand):
super(SearchCommand, self).__init__(name)
self.show_in_help = True
self.description = "Search a paddle hub module with keyword."
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <key>' % (ENTRY, name),
usage='%(prog)s',
add_help=False)
def exec(self, argv):
if not argv:
print("ERROR: Please specify a key\n")
self.help()
return False
module_name = argv[0]
module_list = default_hub_server.search_module(module_name)
text = "\n"
......@@ -38,6 +49,7 @@ class SearchCommand(BaseCommand):
for module_name, module_version in module_list:
text += " %-20s\t\t%s\n" % (module_name, module_version)
print(text)
return True
command = SearchCommand.instance()
......@@ -16,10 +16,11 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand
from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.module.manager import default_module_manager
from paddle_hub.module.module import Module
import os
import argparse
class ShowCommand(BaseCommand):
......@@ -29,8 +30,18 @@ class ShowCommand(BaseCommand):
super(ShowCommand, self).__init__(name)
self.show_in_help = True
self.description = "Show the specify module's info"
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)
def exec(self, argv):
if not argv:
print("ERROR: Please specify a module\n")
self.help()
return False
module_name = argv[0]
cwd = os.getcwd()
......@@ -38,7 +49,7 @@ class ShowCommand(BaseCommand):
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):
return
return True
module = Module(module_dir=module_dir)
show_text = "Name:%s\n" % module.name
......@@ -50,6 +61,7 @@ class ShowCommand(BaseCommand):
show_text += "Location:%s\n" % module_dir
#TODO(wuzewu): add more signature info
print(show_text)
return True
command = ShowCommand.instance()
......@@ -16,9 +16,10 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand
from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.tools import utils
from paddle_hub.module.manager import default_module_manager
import argparse
class UninstallCommand(BaseCommand):
......@@ -28,13 +29,22 @@ class UninstallCommand(BaseCommand):
super(UninstallCommand, self).__init__(name)
self.show_in_help = True
self.description = "Uninstall the specify module from current environment."
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <module_name>' % (ENTRY, name),
usage='%(prog)s',
add_help=False)
def exec(self, argv):
if not argv:
print("ERROR: Please specify a module\n")
self.help()
return False
module_name = argv[0]
result, tips = default_module_manager.uninstall_module(
module_name=module_name)
print(tips)
return result
return True
command = UninstallCommand.instance()
......@@ -30,6 +30,7 @@ class VersionCommand(BaseCommand):
def exec(self, argv):
print("hub %s" % version.hub_version)
return True
command = VersionCommand.instance()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册