<commit_before>#!/usr/bin/env python3
# Software License Agreement (BSD License)
#
# Copyright (c) 2020, UFACTORY, Inc.
# All rights reserved.
#
# Author: Vinman <<EMAIL>> <<EMAIL>>
import json
import requests
import functools
from .xarm_api import XArmAPI


def _call_studio_api(*args, **kwargs):
    api_name = kwargs.pop('api_name', None)
    ip = kwargs.pop('ip', None)
    path = kwargs.pop('path', 'api')
    if ip and api_name:
        r = requests.post('http://{}:18333/{}'.format(ip, path), data=json.dumps({
            'cmd': api_name, 'args': args, 'kwargs': kwargs
        }), timeout=(5, None))
        if r.status_code == 200:
            res = r.json()
            if 'info' in res:
                return -1, res['info']
            return res['res']
        else:
            print('request failed, http_status_code={}'.format(r.status_code))
    else:
        print('ip or api_name is empty, ip={}, api_name={}'.format(ip, api_name))


class _RemoteXArm(object):
    def __init__(self, ip):
        self.ip = ip

    def __getattr__(self, item, *args, **kwargs):
        kwargs['ip'] = self.ip
        kwargs['api_name'] = item
        return functools.partial(_call_studio_api, *args, **kwargs)


class RemoteXArmAPI(XArmAPI):
    def __init__(self, ip):
        import warnings
        warnings.warn("don't use it for now, just for debugging")
        XArmAPI.__init__(self, do_not_open=True)
        self.__ip = ip
        self._arm = _RemoteXArm(ip)

    def delete_blockly_app(self, name):
        return _call_studio_api(None, 0, {
            'parentPath': name,
            'selectNode': {
                'type': 'file'
            }
        }, api_name='app_delete_item', path='cmd', ip=self.__ip)<commit_msg>add call_remote_sdk api<commit_after>#!/usr/bin/env python3
# Software License Agreement (BSD License)
#
# Copyright (c) 2020, UFACTORY, Inc.
# All rights reserved.
#
# Author: Vinman <<EMAIL>> <<EMAIL>>
import json
import requests
import functools
from .xarm_api import XArmAPI


def _call_studio_api(*args, **kwargs):
    api_name = kwargs.pop('api_name', None)
    ip = kwargs.pop('ip', None)
    path = kwargs.pop('path', 'api')
    if ip and api_name:
        r = requests.post('http://{}:18333/{}'.format(ip, path), data=json.dumps({
            'cmd': api_name, 'args': args, 'kwargs': kwargs
        }), timeout=(5, None))
        if r.status_code == 200:
            res = r.json()
            if 'info' in res:
                return -1, res['info']
            return res['res']
        else:
            print('request failed, http_status_code={}'.format(r.status_code))
    else:
        print('ip or api_name is empty, ip={}, api_name={}'.format(ip, api_name))


class _RemoteXArm(object):
    def __init__(self, ip, _arm):
        self.ip = ip
        self._arm = _arm

    def __getattr__(self, item, *args, **kwargs):
        kwargs['ip'] = self.ip
        kwargs['api_name'] = item
        attr = getattr(self._arm, item)
        remote_api = functools.partial(_call_studio_api, *args, **kwargs)
        return remote_api if callable(attr) else remote_api()


class RemoteXArmAPI(XArmAPI):
    def __init__(self, ip):
        import warnings
        warnings.warn("don't use it for now, just for debugging")
        XArmAPI.__init__(self, do_not_open=True)
        self.__ip = ip
        self.__old_arm = self._arm
        self._arm = _RemoteXArm(ip, self.__old_arm)

    def delete_blockly_app(self, name):
        return _call_studio_api(None, 0, {
            'parentPath': name,
            'selectNode': {
                'type': 'file'
            }
        }, api_name='app_delete_item', path='cmd', ip=self.__ip)
{'fraction_non_alphanumeric': 0.1425462459194777, 'fraction_numerical': 0.008705114254624592, 'mean_word_length': 2.5458052073288333, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1056441', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1198, 'n_words': 294}