server.py 2.5 KB
Newer Older
W
wuzewu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#coding:utf-8
# Copyright (c) 2020  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 collections import OrderedDict

from paddlehub.server import ServerSource, GitSource

PADDLEHUB_PUBLIC_SERVER = 'http://paddlepaddle.org.cn/paddlehub'


class HubServer(object):
W
wuzewu 已提交
24
    '''PaddleHub server'''
W
wuzewu 已提交
25 26 27
    def __init__(self):
        self.sources = OrderedDict()

W
wuzewu 已提交
28 29
    def _generate_source(self, url: str, source_type: str = 'server'):
        if source_type == 'server':
W
wuzewu 已提交
30
            source = ServerSource(url)
W
wuzewu 已提交
31
        elif source_type == 'git':
W
wuzewu 已提交
32 33 34 35 36
            source = GitSource(url)
        else:
            raise RuntimeError()
        return source

W
wuzewu 已提交
37
    def add_source(self, url: str, key: str = None, source_type: str = 'server'):
W
wuzewu 已提交
38
        '''Add a module source(GitSource or ServerSource)'''
W
wuzewu 已提交
39
        key = "source_{}".format(len(self.sources)) if not key else key
W
wuzewu 已提交
40
        self.sources[key] = self._generate_source(url, source_type)
W
wuzewu 已提交
41

W
wuzewu 已提交
42 43
    def remove_source(self, url: str = None, key: str = None):
        '''Remove a module source'''
W
wuzewu 已提交
44 45
        self.sources.pop(key)

W
wuzewu 已提交
46 47 48 49 50 51 52 53
    def search_module(self, name: str, version: str = None, source: str = None) -> dict:
        '''
        Search PaddleHub module

        Args:
            name(str) : PaddleHub module name
            version(str) : PaddleHub module version
        '''
W
wuzewu 已提交
54 55
        return self.search_resouce(type='module', name=name, version=version, source=source)

W
wuzewu 已提交
56 57 58 59 60 61 62 63 64
    def search_resouce(self, type: str, name: str, version: str = None, source: str = None) -> dict:
        '''
        Search PaddleHub Resource

        Args:
            type(str) : Resource type
            name(str) : Resource name
            version(str) : Resource version
        '''
W
wuzewu 已提交
65 66 67 68 69 70 71 72 73 74
        sources = self.sources.values() if not source else [self._generate_source(source)]
        for source in sources:
            result = source.search_resouce(name=name, type=type, version=version)
            if result:
                return result
        return None


module_server = HubServer()
module_server.add_source(PADDLEHUB_PUBLIC_SERVER)