How to use the codalab.common.UsageError 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 / config / config_parser.py View on Github external
def client(self):
        client_class = self.config['client']['class']
        if client_class == 'LocalBundleClient':
            bundle_store = self.bundle_store()
            model = self.model()
            from codalab.client.local_bundle_client import LocalBundleClient
            return LocalBundleClient(bundle_store, model)
        elif client_class == 'RemoteBundleClient':
            address = self.config['client']['address']
            from codalab.client.remote_bundle_client import RemoteBundleClient
            return RemoteBundleClient(address)
        else:
            raise UsageError('Unexpected client class: %s' % (client_class,))
github codalab / codalab-worksheets / codalab / lib / bundle_cli.py View on Github external
'Append text items, bundles, or subworksheets to a worksheet (possibly on a different instance).',
            'Bundles that do not yet exist on the destination instance will be copied over.',
        ],
        arguments=(
            Commands.Argument('item_type', help='Type of item(s) to add {text, bundle, worksheet}.', choices=('text', 'bundle', 'worksheet'), metavar='item_type'),
            Commands.Argument('item_spec', help=ITEM_DESCRIPTION, nargs='+', completer=UnionCompleter(WorksheetsCompleter, BundlesCompleter)),
            Commands.Argument('dest_worksheet', help='Worksheet to which to add items (%s).' % WORKSHEET_SPEC_FORMAT, completer=WorksheetsCompleter),
            Commands.Argument('-d', '--copy-dependencies', help='If adding bundles, also add dependencies of the bundles.', action='store_true'),
        ),
    )
    def do_add_command(self, args):
        curr_client, curr_worksheet_uuid = self.manager.get_current_worksheet_uuid()
        dest_client, dest_worksheet_uuid = self.parse_client_worksheet_uuid(args.dest_worksheet)

        if args.item_type != 'bundle' and args.copy_dependencies:
            raise UsageError("-d/--copy_dependencies flag only applies when adding bundles.")

        if args.item_type == 'text':
            for item_spec in args.item_spec:
                if item_spec.startswith('%'):
                    dest_client.create('worksheet-items', data={
                        'type': worksheet_util.TYPE_DIRECTIVE,
                        'worksheet': JsonApiRelationship('worksheets', dest_worksheet_uuid),
                        'value': item_spec[1:].strip(),
                    })
                else:
                    dest_client.create('worksheet-items', data={
                        'type': worksheet_util.TYPE_MARKUP,
                        'worksheet': JsonApiRelationship('worksheets', dest_worksheet_uuid),
                        'value': item_spec,
                    })
github codalab / codalab-worksheets / codalab / lib / metadata_defaults.py View on Github external
if issubclass(bundle_subclass, UploadedBundle):
            items = []
            for path in args.path:
                absolute_path = path_util.normalize(path)
                items.append(os.path.basename(absolute_path))
            return spec_util.create_default_name(None, '-'.join(items))
        elif bundle_subclass is MakeBundle:
            if len(args.target_spec) == 1 and ':' not in args.target_spec[0]:  # direct link
                return os.path.basename(args.target_spec[0])
            else:  # multiple targets
                name = ' '.join(args.target_spec)
                return spec_util.create_default_name(bundle_subclass.BUNDLE_TYPE, str(name))
        elif bundle_subclass is RunBundle:
            return spec_util.create_default_name(bundle_subclass.BUNDLE_TYPE, args.command)
        else:
            raise UsageError("Unhandled class: %s" % bundle_subclass)
github codalab / codalab-worksheets / codalab / config / config_parser.py View on Github external
def model(self):
        model_class = self.config['model']['class']
        if model_class == 'MySQLModel':
            arguments = ('username', 'password', 'address', 'database')
            kwargs = {arg: self.config['model'][arg] for arg in arguments}
            from codalab.model.mysql_model import MySQLModel
            return MySQLModel(**kwargs)
        if model_class == 'SQLiteModel':
            home = self.home()
            from codalab.model.sqlite_model import SQLiteModel
            return SQLiteModel(home)
        else:
            raise UsageError('Unexpected model class: %s' % (model_class,))
github codalab / codalab-worksheets / codalab / rest / bundles.py View on Github external
def delete_bundles(uuids, force, recursive, data_only, dry_run):
    """
    Delete the bundles specified by |uuids|.
    If |force|, allow deletion of bundles that have descendants or that appear across multiple worksheets.
    If |recursive|, add all bundles downstream too.
    If |data_only|, only remove from the bundle store, not the bundle metadata.
    """
    relevant_uuids = local.model.get_self_and_descendants(uuids, depth=sys.maxint)
    if not recursive:
        # If any descendants exist, then we only delete uuids if force = True.
        if (not force) and set(uuids) != set(relevant_uuids):
            relevant = local.model.batch_get_bundles(uuid=(set(relevant_uuids) - set(uuids)))
            raise UsageError(
                'Can\'t delete bundles %s because the following bundles depend on them:\n  %s'
                % (' '.join(uuids), '\n  '.join(bundle.simple_str() for bundle in relevant))
            )
        relevant_uuids = uuids
    check_bundles_have_all_permission(local.model, request.user, relevant_uuids)

    # Make sure we don't delete bundles which are active.
    states = local.model.get_bundle_states(uuids)
    logger.debug('delete states: %s', states)
    active_uuids = [uuid for (uuid, state) in states.items() if state in State.ACTIVE_STATES]
    logger.debug('delete actives: %s', active_uuids)
    if len(active_uuids) > 0:
        raise UsageError(
            'Can\'t delete bundles: %s. ' % (' '.join(active_uuids))
            + 'For run bundles, kill them first. '
            + 'Bundles stuck not running will eventually '
github codalab / codalab-worksheets / build / lib / codalab / lib / bundle_cli.py View on Github external
def do_list_command(self, argv, parser):
        parser.add_argument(
          '-a', '--all',
          action='store_true',
          help='list all bundles, not just this worksheet',
        )
        parser.add_argument(
          'worksheet_spec',
          help='identifier: [|] (default: current worksheet)',
          nargs='?',
        )
        args = parser.parse_args(argv)
        if args.all and args.worksheet_spec:
            raise UsageError("Can't use both --all and a worksheet spec!")
        source = ''
        if args.all:
            bundle_info_list = self.client.search()
        elif args.worksheet_spec:
            worksheet_info = self.client.worksheet_info(args.worksheet_spec)
            bundle_info_list = self.get_distinct_bundles(worksheet_info)
            source = ' from worksheet %s' % (worksheet_info['name'],)
        else:
            worksheet_info = self.get_current_worksheet_info()
            if not worksheet_info:
                bundle_info_list = self.client.search()
            else:
                bundle_info_list = self.get_distinct_bundles(worksheet_info)
                source = ' from worksheet %s' % (worksheet_info['name'],)
        if bundle_info_list:
            print 'Listing all bundles%s:\n' % (source,)
github codalab / codalab-worksheets / codalab / client / local_bundle_client.py View on Github external
def validate_user_metadata(self, bundle_subclass, metadata):
        '''
        Check that the user did not supply values for any auto-generated metadata.
        Raise a UsageError with the offending keys if they are.
        '''
        #legal_keys = set(spec.key for spec in bundle_subclass.get_user_defined_metadata())
        # Allow generated keys as well
        legal_keys = set(spec.key for spec in bundle_subclass.METADATA_SPECS)
        illegal_keys = [key for key in metadata if key not in legal_keys]
        if illegal_keys:
            raise UsageError('Illegal metadata keys: %s' % (', '.join(illegal_keys),))
github codalab / codalab-worksheets / codalab / rest / worksheets.py View on Github external
"""
    Set the worksheet to have items |new_items|.
    """
    worksheet_uuid = worksheet_info['uuid']
    last_item_id = worksheet_info['last_item_id']
    length = len(worksheet_info['items'])
    worksheet = local.model.get_worksheet(worksheet_uuid, fetch_items=False)
    check_worksheet_has_all_permission(local.model, request.user, worksheet)
    worksheet_util.check_worksheet_not_frozen(worksheet)
    try:
        if convert_items:
            new_items = [worksheet_util.convert_item_to_db(item) for item in new_items]
        local.model.update_worksheet_items(worksheet_uuid, last_item_id, length, new_items)
    except UsageError:
        # Turn the model error into a more readable one using the object.
        raise UsageError('%s was updated concurrently!' % (worksheet,))
github codalab / codalab-worksheets / codalab / rest / worksheets.py View on Github external
Ensure worksheet names are unique.
    Note: for simplicity, we are ensuring uniqueness across the system, even on
    worksheet names that the user may not have access to.
    """
    # If trying to set the name to a home worksheet, then it better be
    # user's home worksheet.
    if (
        spec_util.is_home_worksheet(name)
        and spec_util.home_worksheet(request.user.user_name) != name
    ):
        raise UsageError(
            'Cannot create %s because this is potentially the home worksheet of another user' % name
        )
    try:
        canonicalize.get_worksheet_uuid(local.model, request.user, None, name)
        raise UsageError('Worksheet with name %s already exists' % name)
    except NotFoundError:
        pass  # all good!
github codalab / codalab-competitions / codalab / apps / web / bundles.py View on Github external
def http_status_from_exception(self, ex):
            # This is brittle. See https://github.com/codalab/codalab/issues/345.
            if type(ex) == UsageError:
                return 404
            return 500