How to use the codalab.client.json_api_client.JsonApiRelationship function in codalab

To help you get started, we’ve selected a few codalab examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github codalab / codalab-worksheets / codalab / lib / bundle_cli.py View on Github external
# Print direct numeric result
        if 'meta' in bundles:
            print >>self.stdout, bundles['meta']['result']
            return

        # Print table
        if len(bundles) > 0:
            self.print_bundle_info_list(bundles, uuid_only=args.uuid_only, print_ref=False)

        # Add the bundles to the current worksheet
        if args.append:
            client.create('worksheet-items', data=[
                {
                    'worksheet': JsonApiRelationship('worksheets', worksheet_uuid),
                    'bundle': JsonApiRelationship('bundles', bundle['uuid']),
                    'type': worksheet_util.TYPE_BUNDLE,
                }
                for bundle in bundles
            ])
            worksheet_info = client.fetch('worksheets', worksheet_uuid)
            print >>self.stdout, 'Added %d bundles to %s' % (len(bundles), self.worksheet_str(worksheet_info))

        return {
            'refs': self.create_reference_map('bundle', bundles)
        }
github codalab / codalab-worksheets / codalab / client / json_api_client.py View on Github external
def __init__(self):
        JsonApiRelationship.__init__(self, None, None)
github codalab / codalab-worksheets / scripts / competitiond.py View on Github external
def ensure_log_worksheet_private(self):
        """
        Ensure that the leaderboard worksheet is private, so that all bundles
        created on it are automatically private.
        """
        if self.config['make_predictions_public']:
            return

        # Get public group info
        public = self.client.fetch('groups', 'public')

        # Set permissions
        self.client.create(
            'worksheet-permissions',
            {
                'group': JsonApiRelationship('groups', public['id']),
                'worksheet': JsonApiRelationship('worksheets', self.config['log_worksheet_uuid']),
                'permission': 0,
            },
github codalab / codalab-worksheets / codalab / lib / bundle_cli.py View on Github external
})

        # Print direct numeric result
        if 'meta' in bundles:
            print >>self.stdout, bundles['meta']['result']
            return

        # Print table
        if len(bundles) > 0:
            self.print_bundle_info_list(bundles, uuid_only=args.uuid_only, print_ref=False)

        # Add the bundles to the current worksheet
        if args.append:
            client.create('worksheet-items', data=[
                {
                    'worksheet': JsonApiRelationship('worksheets', worksheet_uuid),
                    'bundle': JsonApiRelationship('bundles', bundle['uuid']),
                    'type': worksheet_util.TYPE_BUNDLE,
                }
                for bundle in bundles
            ])
            worksheet_info = client.fetch('worksheets', worksheet_uuid)
            print >>self.stdout, 'Added %d bundles to %s' % (len(bundles), self.worksheet_str(worksheet_info))

        return {
            'refs': self.create_reference_map('bundle', bundles)
        }
github codalab / codalab-worksheets / codalab / lib / bundle_cli.py View on Github external
# (although metadata could be different on source and destination).
        # TODO: sync the metadata.
        try:
            dest_client.fetch('bundles', source_bundle_uuid)
        except NotFoundError as e:
            bundle_exists = False
        else:
            bundle_exists = True

        # Bundle already exists, just need to add to worksheet if desired.
        if bundle_exists:
            if add_to_worksheet:
                dest_client.create('worksheet-items', data={
                    'type': worksheet_util.TYPE_BUNDLE,
                    'worksheet': JsonApiRelationship('worksheets', dest_worksheet_uuid),
                    'bundle': JsonApiRelationship('bundles', source_bundle_uuid)
                })
            return

        source_info = source_client.fetch('bundles', source_bundle_uuid)
        if source_info is None:
            print >>self.stdout, 'Unable to read bundle %s' % source_bundle_uuid
            return

        source_desc = self.simple_bundle_str(source_info)
        if source_info['state'] not in [State.READY, State.FAILED]:
            print >>self.stdout, 'Not copying %s because it has non-final state %s' % (source_desc, source_info['state'])
            return

        print >>self.stdout, "Copying %s..." % source_desc

        # Create the bundle, copying over metadata from the source bundle
github codalab / codalab-worksheets / codalab / lib / bundle_util.py View on Github external
if not skip_prelude:
                                for item2 in prelude_items:
                                    # Create a copy of the item on the destination worksheet
                                    item2 = item2.copy()
                                    item2['worksheet'] = JsonApiRelationship(
                                        'worksheets', worksheet_uuid
                                    )
                                    client.create('worksheet-items', data=item2)

                            # Add the bundle item
                            client.create(
                                'worksheet-items',
                                data={
                                    'type': worksheet_util.TYPE_BUNDLE,
                                    'worksheet': JsonApiRelationship('worksheets', worksheet_uuid),
                                    'bundle': JsonApiRelationship('bundles', new_bundle_uuid),
                                },
                            )
                            new_bundle_uuids_added.add(new_bundle_uuid)
                            just_added = True

                if (item['type'] == worksheet_util.TYPE_MARKUP and item['value'] != '') or item[
                    'type'
                ] == worksheet_util.TYPE_DIRECTIVE:
                    prelude_items.append(item)  # Include in prelude
                else:
                    prelude_items = []  # Reset

        # Add the bundles that haven't been added yet
        for info, new_info in plan:
            new_bundle_uuid = new_info['uuid']
            if new_bundle_uuid not in new_bundle_uuids_added:
github codalab / codalab-worksheets / scripts / competitiond.py View on Github external
def _make_public_readable(self, bundle):
        """
        Make the given bundle readable to the public.
        """
        # Get public group info
        public = self.client.fetch('groups', 'public')

        # Set permissions
        self.client.create(
            'bundle-permissions',
            {
                'group': JsonApiRelationship('groups', public['id']),
                'bundle': JsonApiRelationship('bundles', bundle['id']),
                'permission': 1,
            },
github codalab / codalab-worksheets / codalab / lib / bundle_cli.py View on Github external
'uadd',
        help='Add a user to a group.',
        arguments=(
            Commands.Argument('user_spec', help='Username to add.'),
            Commands.Argument('group_spec', help='Group to add user to (%s).' % GROUP_SPEC_FORMAT, completer=GroupsCompleter),
            Commands.Argument('-a', '--admin', action='store_true', help='Give admin privileges to the user for the group.'),
        ),
    )
    def do_uadd_command(self, args):
        client = self.manager.current_client()

        user = client.fetch('users', args.user_spec)
        group = client.fetch('groups', args.group_spec)
        client.create_relationship('groups', group['id'],
                                   'admins' if args.admin else 'members',
                                   JsonApiRelationship('users', user['id']))

        print >>self.stdout, '%s in group %s as %s' % (
            user['user_name'],
            group['name'],
            'admin' if args.admin else 'member'
        )
github codalab / codalab-worksheets / codalab / lib / bundle_cli.py View on Github external
# Check if the bundle already exists on the destination, then don't copy it
        # (although metadata could be different on source and destination).
        # TODO: sync the metadata.
        try:
            dest_client.fetch('bundles', source_bundle_uuid)
        except NotFoundError as e:
            bundle_exists = False
        else:
            bundle_exists = True

        # Bundle already exists, just need to add to worksheet if desired.
        if bundle_exists:
            if add_to_worksheet:
                dest_client.create('worksheet-items', data={
                    'type': worksheet_util.TYPE_BUNDLE,
                    'worksheet': JsonApiRelationship('worksheets', dest_worksheet_uuid),
                    'bundle': JsonApiRelationship('bundles', source_bundle_uuid)
                })
            return

        source_info = source_client.fetch('bundles', source_bundle_uuid)
        if source_info is None:
            print >>self.stdout, 'Unable to read bundle %s' % source_bundle_uuid
            return

        source_desc = self.simple_bundle_str(source_info)
        if source_info['state'] not in [State.READY, State.FAILED]:
            print >>self.stdout, 'Not copying %s because it has non-final state %s' % (source_desc, source_info['state'])
            return

        print >>self.stdout, "Copying %s..." % source_desc