コード例 #1
0
ファイル: legacy.py プロジェクト: TuringWorks/codalab-cli
 def __init__(self):
     self.client = LocalBundleClient('local',
                                     local.bundle_store,
                                     local.model,
                                     local.download_manager,
                                     LocalUserAuthHandler(
                                         request.user, local.model),
                                     verbose=0)
コード例 #2
0
    def client(self, address, is_cli=True):
        '''
        Return a client given the address.  Note that this can either be called
        by the CLI (is_cli=True) or the server (is_cli=False).
        If called by the CLI, we don't need to authenticate.
        Cache the Client if necessary.
        '''
        if address in self.clients:
            return self.clients[address]
        # if local force mockauth or if locl server use correct auth
        if is_local_address(address):
            bundle_store = self.bundle_store()
            model = self.model()
            auth_handler = self.auth_handler(mock=is_cli)

            from codalab.client.local_bundle_client import LocalBundleClient
            client = LocalBundleClient(address, bundle_store, model,
                                       auth_handler, self.cli_verbose)
            self.clients[address] = client
            if is_cli:
                # Set current user
                access_token = self._authenticate(client)
                auth_handler.validate_token(access_token)
        else:
            from codalab.client.remote_bundle_client import RemoteBundleClient
            client = RemoteBundleClient(
                address, lambda a_client: self._authenticate(a_client),
                self.cli_verbose())
            self.clients[address] = client
            self._authenticate(client)
        return client
コード例 #3
0
 def setUpClass(cls):
     cls.test_root = path_util.normalize("~/.codalab_tests")
     path_util.make_directory(cls.test_root)
     cls.bundle_store = BundleStore(cls.test_root, [])
     cls.model = SQLiteModel(cls.test_root)
     cls.model.root_user_id = '0'
     users = [
         User('root', '0'),
         User('user1', '1'),
         User('user2', '2'),
         User('user4', '4')
     ]
     cls.auth_handler = MockAuthHandler(users)
     cls.client = LocalBundleClient('local',
                                    cls.bundle_store,
                                    cls.model,
                                    cls.auth_handler,
                                    verbose=1)
コード例 #4
0
 def setUpClass(cls):
     cls.test_root = path_util.normalize("~/.codalab_tests")
     path_util.make_directory(cls.test_root)
     cls.bundle_store = MultiDiskBundleStore(cls.test_root)
     cls.model = SQLiteModel(
         "sqlite:///{}".format(os.path.join(cls.test_root, 'bundle.db')),
         {})
     cls.model.root_user_id = '0'
     users = [
         User('root', '0'),
         User('user1', '1'),
         User('user2', '2'),
         User('user4', '4')
     ]
     cls.auth_handler = MockAuthHandler(users)
     cls.client = LocalBundleClient('local',
                                    cls.bundle_store,
                                    cls.model,
                                    None,
                                    cls.auth_handler,
                                    verbose=1)
コード例 #5
0
ファイル: legacy.py プロジェクト: see4c/codalab-cli
 def __init__(self):
     self.client = LocalBundleClient(
         'local', local.bundle_store, local.model,
         local.upload_manager, local.download_manager,
         LocalUserAuthHandler(request.user, local.model), verbose=0)
コード例 #6
0
ファイル: legacy.py プロジェクト: see4c/codalab-cli
class BundleService(object):
    '''
    Adapts the LocalBundleClient for REST calls.
    '''

    def __init__(self):
        self.client = LocalBundleClient(
            'local', local.bundle_store, local.model,
            local.upload_manager, local.download_manager,
            LocalUserAuthHandler(request.user, local.model), verbose=0)

    def get_bundle_info(self, uuid):
        bundle_info = self.client.get_bundle_info(uuid, True, True, True)

        if bundle_info is None:
            return None
        # Set permissions
        bundle_info['edit_permission'] = (bundle_info['permission'] == GROUP_OBJECT_PERMISSION_ALL)
        # Format permissions into strings
        bundle_info['permission_str'] = permission_str(bundle_info['permission'])
        for group_permission in bundle_info['group_permissions']:
            group_permission['permission_str'] = permission_str(group_permission['permission'])

        metadata = bundle_info['metadata']

        cls = get_bundle_subclass(bundle_info['bundle_type'])
        for key, value in worksheet_util.get_formatted_metadata(cls, metadata):
            metadata[key] = value

        bundle_info['metadata'] = metadata
        bundle_info['editable_metadata_fields'] = worksheet_util.get_editable_metadata_fields(cls, metadata)

        return bundle_info

    def search_worksheets(self, keywords, worksheet_uuid=None):
        return self.client.search_worksheets(keywords)

    def get_worksheet_uuid(self, spec):
        # generic function sometimes get uuid already just return it.
        if spec_util.UUID_REGEX.match(spec):
            return spec
        else:
            return worksheet_util.get_worksheet_uuid(self.client, None, spec)

    def full_worksheet(self, uuid):
        """
        Return information about a worksheet. Calls
        - get_worksheet_info: get basic info
        - resolve_interpreted_items: get more information about a worksheet.
        In the future, for large worksheets, might want to break this up so
        that we can render something basic.
        """
        worksheet_info = self.client.get_worksheet_info(uuid, True, True)

        # Fetch items.
        worksheet_info['raw'] = worksheet_util.get_worksheet_lines(worksheet_info)

        # Set permissions
        worksheet_info['edit_permission'] = (worksheet_info['permission'] == GROUP_OBJECT_PERMISSION_ALL)
        # Check enable chat box
        worksheet_info['enable_chat'] = local.config.get('enable_chat', False)
        # Format permissions into strings
        worksheet_info['permission_str'] = permission_str(worksheet_info['permission'])
        for group_permission in worksheet_info['group_permissions']:
            group_permission['permission_str'] = permission_str(group_permission['permission'])

        # Go and fetch more information about the worksheet contents by
        # resolving the interpreted items.
        try:
            interpreted_items = worksheet_util.interpret_items(
                                worksheet_util.get_default_schemas(),
                                worksheet_info['items'])
        except UsageError, e:
            interpreted_items = {'items': []}
            worksheet_info['error'] = str(e)

        worksheet_info['items'] = self.client.resolve_interpreted_items(interpreted_items['items'])
        worksheet_info['raw_to_interpreted'] = interpreted_items['raw_to_interpreted']
        worksheet_info['interpreted_to_raw'] = interpreted_items['interpreted_to_raw']

        def decode_lines(interpreted):
            # interpreted is None or list of base64 encoded lines
            if interpreted is None:
                return formatting.contents_str(None)
            else:
                return map(base64.b64decode, interpreted)

        # Currently, only certain fields are base64 encoded.
        for item in worksheet_info['items']:
            if item['mode'] in ['html', 'contents']:
                item['interpreted'] = decode_lines(item['interpreted'])
            elif item['mode'] == 'table':
                for row_map in item['interpreted'][1]:
                    for k, v in row_map.iteritems():
                        if v is None:
                            row_map[k] = formatting.contents_str(v)
            if 'bundle_info' in item:
                infos = []
                if isinstance(item['bundle_info'], list):
                    infos = item['bundle_info']
                elif isinstance(item['bundle_info'], dict):
                    infos = [item['bundle_info']]
                for bundle_info in infos:
                    if bundle_info['bundle_type'] != PrivateBundle.BUNDLE_TYPE:
                        target_info = self.get_top_level_contents((bundle_info['uuid'], ''))
                        bundle_info['target_info'] = target_info
                    try:
                        if isinstance(bundle_info, dict):
                            worksheet_util.format_metadata(bundle_info.get('metadata'))
                    except Exception, e:
                        print e
                        import ipdb; ipdb.set_trace()
コード例 #7
0
ファイル: legacy.py プロジェクト: see4c/codalab-cli
class BundleService(object):
    '''
    Adapts the LocalBundleClient for REST calls.
    '''
    def __init__(self):
        self.client = LocalBundleClient('local',
                                        local.bundle_store,
                                        local.model,
                                        local.upload_manager,
                                        local.download_manager,
                                        LocalUserAuthHandler(
                                            request.user, local.model),
                                        verbose=0)

    def get_bundle_info(self, uuid):
        bundle_info = self.client.get_bundle_info(uuid, True, True, True)

        if bundle_info is None:
            return None
        # Set permissions
        bundle_info['edit_permission'] = (
            bundle_info['permission'] == GROUP_OBJECT_PERMISSION_ALL)
        # Format permissions into strings
        bundle_info['permission_str'] = permission_str(
            bundle_info['permission'])
        for group_permission in bundle_info['group_permissions']:
            group_permission['permission_str'] = permission_str(
                group_permission['permission'])

        metadata = bundle_info['metadata']

        cls = get_bundle_subclass(bundle_info['bundle_type'])
        for key, value in worksheet_util.get_formatted_metadata(cls, metadata):
            metadata[key] = value

        bundle_info['metadata'] = metadata
        bundle_info[
            'editable_metadata_fields'] = worksheet_util.get_editable_metadata_fields(
                cls, metadata)

        return bundle_info

    def search_worksheets(self, keywords, worksheet_uuid=None):
        return self.client.search_worksheets(keywords)

    def get_worksheet_uuid(self, spec):
        # generic function sometimes get uuid already just return it.
        if spec_util.UUID_REGEX.match(spec):
            return spec
        else:
            return worksheet_util.get_worksheet_uuid(self.client, None, spec)

    def full_worksheet(self, uuid):
        """
        Return information about a worksheet. Calls
        - get_worksheet_info: get basic info
        - resolve_interpreted_items: get more information about a worksheet.
        In the future, for large worksheets, might want to break this up so
        that we can render something basic.
        """
        worksheet_info = self.client.get_worksheet_info(uuid, True, True)

        # Fetch items.
        worksheet_info['raw'] = worksheet_util.get_worksheet_lines(
            worksheet_info)

        # Set permissions
        worksheet_info['edit_permission'] = (
            worksheet_info['permission'] == GROUP_OBJECT_PERMISSION_ALL)
        # Check enable chat box
        worksheet_info['enable_chat'] = local.config.get('enable_chat', False)
        # Format permissions into strings
        worksheet_info['permission_str'] = permission_str(
            worksheet_info['permission'])
        for group_permission in worksheet_info['group_permissions']:
            group_permission['permission_str'] = permission_str(
                group_permission['permission'])

        # Go and fetch more information about the worksheet contents by
        # resolving the interpreted items.
        try:
            interpreted_items = worksheet_util.interpret_items(
                worksheet_util.get_default_schemas(), worksheet_info['items'])
        except UsageError, e:
            interpreted_items = {'items': []}
            worksheet_info['error'] = str(e)

        worksheet_info['items'] = self.client.resolve_interpreted_items(
            interpreted_items['items'])
        worksheet_info['raw_to_interpreted'] = interpreted_items[
            'raw_to_interpreted']
        worksheet_info['interpreted_to_raw'] = interpreted_items[
            'interpreted_to_raw']

        def decode_lines(interpreted):
            # interpreted is None or list of base64 encoded lines
            if interpreted is None:
                return formatting.contents_str(None)
            else:
                return map(base64.b64decode, interpreted)

        # Currently, only certain fields are base64 encoded.
        for item in worksheet_info['items']:
            if item['mode'] in ['html', 'contents']:
                item['interpreted'] = decode_lines(item['interpreted'])
            elif item['mode'] == 'table':
                for row_map in item['interpreted'][1]:
                    for k, v in row_map.iteritems():
                        if v is None:
                            row_map[k] = formatting.contents_str(v)
            if 'bundle_info' in item:
                infos = []
                if isinstance(item['bundle_info'], list):
                    infos = item['bundle_info']
                elif isinstance(item['bundle_info'], dict):
                    infos = [item['bundle_info']]
                for bundle_info in infos:
                    if bundle_info['bundle_type'] != PrivateBundle.BUNDLE_TYPE:
                        target_info = self.get_top_level_contents(
                            (bundle_info['uuid'], ''))
                        bundle_info['target_info'] = target_info
                    try:
                        if isinstance(bundle_info, dict):
                            worksheet_util.format_metadata(
                                bundle_info.get('metadata'))
                    except Exception, e:
                        print e
                        import ipdb
                        ipdb.set_trace()