otherdata.py 1.27 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1

Wei Shoulin's avatar
Wei Shoulin committed
2
from ..common.service import grpc_channel
Wei Shoulin's avatar
Wei Shoulin committed
3
from ..common.utils import *
Wei Shoulin's avatar
C9    
Wei Shoulin committed
4
from ..common.constants import *
Wei Shoulin's avatar
Wei Shoulin committed
5
6
class OtherDataApi(object):
    def __init__(self):
Wei Shoulin's avatar
Wei Shoulin committed
7
        self.stub = None
Wei Shoulin's avatar
Wei Shoulin committed
8

Wei Shoulin's avatar
Wei Shoulin committed
9
    @grpc_channel
Wei Shoulin's avatar
Wei Shoulin committed
10
    def find(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
11
        return find_req("OtherDataServicer.Find", kwargs)
Wei Shoulin's avatar
Wei Shoulin committed
12

Wei Shoulin's avatar
Wei Shoulin committed
13
    @grpc_channel
Wei Shoulin's avatar
Wei Shoulin committed
14
    def get(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
15
        return get_req("OtherDataServicer.Get", kwargs)
Wei Shoulin's avatar
Wei Shoulin committed
16

Wei Shoulin's avatar
Wei Shoulin committed
17
    @grpc_channel
Wei Shoulin's avatar
Wei Shoulin committed
18
19
    def write(self, **kwargs):
        try:
Wei Shoulin's avatar
C9    
Wei Shoulin committed
20
21
22
            file_path = get_parameter(kwargs, "file_path", "")
            filename = get_parameter(kwargs, "filename", "")            
            if not file_path:
Wei Shoulin's avatar
Wei Shoulin committed
23
                return Result.error(message="file_path is blank")
Wei Shoulin's avatar
C9    
Wei Shoulin committed
24
25
26
27
28
29
30
31
32
33
34
35
            if not os.path.exists(file_path):
                return Result.error(message="the file [%s] not existed" % (file_path, ))
            if not filename:
                filename = os.path.basename(file_path)
                conditions = {}
                conditions.update(kwargs)
                conditions["filename"] = filename
            with open(file_path, 'rb') as f:
                byte_stream = io.BytesIO(f.read())
                return write_stream_req("OtherDataServicer.Write", byte_stream, kwargs)
        except Exception as e:
            return Result.error(message="%s" % (e,))