提交 dfc7aee2 编写于 作者: W wuzewu

Speed up the command line

上级 d2fece3e
...@@ -23,7 +23,7 @@ from paddlehub.commands import register, _commands ...@@ -23,7 +23,7 @@ from paddlehub.commands import register, _commands
class HelpCommand: class HelpCommand:
def execute(self, argv: List) -> bool: def execute(self, argv: List) -> bool:
msg = 'Usage:\n' msg = 'Usage:\n'
msg += '\thub <command> <options>\n\n' msg += ' hub <command> <options>\n\n'
msg += 'Commands:\n' msg += 'Commands:\n'
for command, detail in _commands['hub'].items(): for command, detail in _commands['hub'].items():
if command.startswith('_'): if command.startswith('_'):
...@@ -31,7 +31,7 @@ class HelpCommand: ...@@ -31,7 +31,7 @@ class HelpCommand:
if not '_description' in detail: if not '_description' in detail:
continue continue
msg += '\t{:>15}\t\t{}\n'.format(command, detail['_description']) msg += ' {:<15} {}\n'.format(command, detail['_description'])
print(msg) print(msg)
return True return True
...@@ -23,5 +23,4 @@ from paddlehub.commands import register, get_command ...@@ -23,5 +23,4 @@ from paddlehub.commands import register, get_command
class HubCommand: class HubCommand:
def execute(self, argv): def execute(self, argv):
help = get_command('hub.help') help = get_command('hub.help')
help().execute(argv) return help().execute(argv)
return True
...@@ -32,7 +32,6 @@ def register(name: str, description: str = '') -> Any: ...@@ -32,7 +32,6 @@ def register(name: str, description: str = '') -> Any:
name(str) : The name of the command, separated by '.' (e.g, hub.serving) name(str) : The name of the command, separated by '.' (e.g, hub.serving)
description(str) : The description of the specified command showd in the help command, if not description given, this command would not be shown in help command. Default is None. description(str) : The description of the specified command showd in the help command, if not description given, this command would not be shown in help command. Default is None.
''' '''
def _warpper(command): def _warpper(command):
items = name.split('.') items = name.split('.')
...@@ -66,4 +65,4 @@ def execute(): ...@@ -66,4 +65,4 @@ def execute():
else: else:
idx += 1 idx += 1
exit(com['_entry']().execute(sys.argv[idx:])) return com['_entry']().execute(sys.argv[idx:])
...@@ -22,23 +22,22 @@ PADDLEHUB_PUBLIC_SERVER = 'http://paddlepaddle.org.cn/paddlehub' ...@@ -22,23 +22,22 @@ PADDLEHUB_PUBLIC_SERVER = 'http://paddlepaddle.org.cn/paddlehub'
class HubServer(object): class HubServer(object):
'''PaddleHub server''' '''PaddleHub server'''
def __init__(self): def __init__(self):
self.sources = OrderedDict() self.sources = OrderedDict()
def _generate_source(self, url: str): def _generate_source(self, url: str, source_type: str = 'server'):
if ServerSource.check(url): if source_type == 'server':
source = ServerSource(url) source = ServerSource(url)
elif GitSource.check(url): elif source_type == 'git':
source = GitSource(url) source = GitSource(url)
else: else:
raise RuntimeError() raise RuntimeError()
return source return source
def add_source(self, url: str, key: str = None): def add_source(self, url: str, key: str = None, source_type: str = 'server'):
'''Add a module source(GitSource or ServerSource)''' '''Add a module source(GitSource or ServerSource)'''
key = "source_{}".format(len(self.sources)) if not key else key key = "source_{}".format(len(self.sources)) if not key else key
self.sources[key] = self._generate_source(url) self.sources[key] = self._generate_source(url, source_type)
def remove_source(self, url: str = None, key: str = None): def remove_source(self, url: str = None, key: str = None):
'''Remove a module source''' '''Remove a module source'''
......
...@@ -18,8 +18,6 @@ import platform ...@@ -18,8 +18,6 @@ import platform
import requests import requests
import sys import sys
import paddle
import paddlehub import paddlehub
from paddlehub.utils import utils from paddlehub.utils import utils
...@@ -32,7 +30,6 @@ class ServerSource(object): ...@@ -32,7 +30,6 @@ class ServerSource(object):
url(str) : Url of the server url(str) : Url of the server
timeout(int) : Request timeout timeout(int) : Request timeout
''' '''
def __init__(self, url: str, timeout: int = 10): def __init__(self, url: str, timeout: int = 10):
self._url = url self._url = url
self._timeout = timeout self._timeout = timeout
...@@ -63,6 +60,8 @@ class ServerSource(object): ...@@ -63,6 +60,8 @@ class ServerSource(object):
if version: if version:
payload['version'] = version payload['version'] = version
# Delay module loading to improve command line speed
import paddle
payload['environments']['hub_version'] = paddlehub.__version__ payload['environments']['hub_version'] = paddlehub.__version__
payload['environments']['paddle_version'] = paddle.__version__ payload['environments']['paddle_version'] = paddle.__version__
payload['environments']['python_version'] = '.'.join(map(str, sys.version_info[0:3])) payload['environments']['python_version'] = '.'.join(map(str, sys.version_info[0:3]))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册