data.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. import sys
  18. from flow_sdk.client.api.base import BaseFlowAPI
  19. from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
  20. from flow_sdk.utils import preprocess, start_cluster_standalone_job_server, get_project_base_directory, json_dumps
  21. class Data(BaseFlowAPI):
  22. def upload(self, config_data, verbose=0, drop=0):
  23. kwargs = locals()
  24. kwargs['drop'] = int(kwargs['drop'])
  25. kwargs['verbose'] = int(kwargs['verbose'])
  26. config_data, dsl_data = preprocess(**kwargs)
  27. if config_data.get('use_local_data', 1):
  28. file_name = config_data.get('file')
  29. if not os.path.isabs(file_name):
  30. file_name = os.path.join(get_project_base_directory(), file_name)
  31. if os.path.exists(file_name):
  32. with open(file_name, 'rb') as fp:
  33. data = MultipartEncoder(
  34. fields={'file': (os.path.basename(file_name), fp, 'application/octet-stream')}
  35. )
  36. tag = [0]
  37. def read_callback(monitor):
  38. if config_data.get('verbose') == 1:
  39. sys.stdout.write(
  40. "\r UPLOADING:{0}{1}".format("|" * (monitor.bytes_read * 100 // monitor.len),
  41. '%.2f%%' % (monitor.bytes_read * 100 // monitor.len)))
  42. sys.stdout.flush()
  43. if monitor.bytes_read / monitor.len == 1:
  44. tag[0] += 1
  45. if tag[0] == 2:
  46. sys.stdout.write('\n')
  47. data = MultipartEncoderMonitor(data, read_callback)
  48. return self._post(url='data/upload', data=data,
  49. params=json_dumps(config_data), headers={'Content-Type': data.content_type})
  50. else:
  51. raise Exception('The file is obtained from the fate flow client machine, but it does not exist, '
  52. 'please check the path: {}'.format(file_name))
  53. else:
  54. return self._post(url='data/upload', json=config_data)
  55. def download(self, config_data):
  56. kwargs = locals()
  57. config_data, dsl_data = preprocess(**kwargs)
  58. response = self._post(url='data/download', json=config_data)
  59. if response['retcode'] == 999:
  60. start_cluster_standalone_job_server()
  61. return self._post(url='data/download', json=config_data)
  62. return response
  63. def upload_history(self, limit=10, job_id=None):
  64. kwargs = locals()
  65. config_data, dsl_data = preprocess(**kwargs)
  66. response = self._post(url='data/upload/history', json=config_data)
  67. if response['retcode'] == 999:
  68. start_cluster_standalone_job_server()
  69. return self._post(url='data/upload/history', json=config_data)
  70. return response
  71. # TODO
  72. def download_history(self):
  73. pass