Esempio n. 1
0
def get_single_group(model, group_spec, search_fn):
    '''
    Helper function.
    Resolve a string group_spec to a unique group for the given |search_fn|.
    Throw an error if zero or more than one group matches.
    '''
    if not group_spec:
        raise UsageError('Tried to expand empty group_spec!')
    if spec_util.UUID_REGEX.match(group_spec):
        groups = search_fn(model, uuid=group_spec)
        message = "uuid starting with '%s'" % (group_spec, )
    elif spec_util.UUID_PREFIX_REGEX.match(group_spec):
        groups = search_fn(model, uuid=LikeQuery(group_spec + '%'))
        message = "uuid starting with '%s'" % (group_spec, )
    else:
        spec_util.check_name(group_spec)
        groups = search_fn(model, name=group_spec)
        message = "name '%s'" % (group_spec, )
    if not groups:
        raise NotFoundError('Found no group with %s' % (message, ))
    elif len(groups) > 1:
        raise UsageError('Found multiple groups with %s:%s' %
                         (message, ''.join('\n  uuid=%s' % (group['uuid'], )
                                           for group in groups)))
    return groups[0]
Esempio n. 2
0
def get_worksheet_uuid(model, base_worksheet_uuid, worksheet_spec):
    """
    Resolve a string worksheet_spec to a unique worksheet uuid.
    If base_worksheet_uuid specified, then try to resolve worksheet_spec in the
    context of base_worksheet_uuid.
    """
    if not worksheet_spec:
        raise UsageError('Tried to expand empty worksheet_spec!')
    if spec_util.UUID_REGEX.match(worksheet_spec):
        return worksheet_spec

    if spec_util.UUID_PREFIX_REGEX.match(worksheet_spec):
        worksheets = model.batch_get_worksheets(fetch_items=False, uuid=LikeQuery(worksheet_spec + '%'),
                                                base_worksheet_uuid=base_worksheet_uuid)
        message = "uuid starting with '%s'" % (worksheet_spec,)
    else:
        spec_util.check_name(worksheet_spec)
        worksheets = model.batch_get_worksheets(fetch_items=False, name=worksheet_spec,
                                                base_worksheet_uuid=base_worksheet_uuid)
        message = "name '%s'" % (worksheet_spec,)

    if not worksheets:
        raise UsageError('No worksheet found with %s' % (message,))
    if len(worksheets) > 1:
        raise UsageError(
          'Found multiple worksheets with %s:%s' %
          (message, ''.join('\n  %s' % (worksheet,) for worksheet in worksheets))
        )

    return worksheets[0].uuid
Esempio n. 3
0
def get_single_group(model, group_spec, search_fn):
    '''
    Helper function.
    Resolve a string group_spec to a unique group for the given |search_fn|.
    Throw an error if zero or more than one group matches.
    '''
    if not group_spec:
        raise UsageError('Tried to expand empty group_spec!')
    if spec_util.UUID_REGEX.match(group_spec):
        groups = search_fn(model, uuid=group_spec)
        message = "uuid starting with '%s'" % (group_spec,)
    elif spec_util.UUID_PREFIX_REGEX.match(group_spec):
        groups = search_fn(model, uuid=LikeQuery(group_spec + '%'))
        message = "uuid starting with '%s'" % (group_spec,)
    else:
        spec_util.check_name(group_spec)
        groups = search_fn(model, name=group_spec)
        message = "name '%s'" % (group_spec,)
    if not groups:
        raise NotFoundError('Found no group with %s' % (message,))
    elif len(groups) > 1:
        raise UsageError(
          'Found multiple groups with %s:%s' %
          (message, ''.join('\n  uuid=%s' % (group['uuid'],) for group in groups))
        )
    return groups[0]
Esempio n. 4
0
def get_worksheet_uuid(model, base_worksheet_uuid, worksheet_spec):
    """
    Resolve a string worksheet_spec to a unique worksheet uuid.
    If base_worksheet_uuid specified, then try to resolve worksheet_spec in the
    context of base_worksheet_uuid.
    """
    if not worksheet_spec:
        raise UsageError('Tried to expand empty worksheet_spec!')
    if spec_util.UUID_REGEX.match(worksheet_spec):
        return worksheet_spec

    if spec_util.UUID_PREFIX_REGEX.match(worksheet_spec):
        worksheets = model.batch_get_worksheets(fetch_items=False, uuid=LikeQuery(worksheet_spec + '%'),
                                                base_worksheet_uuid=base_worksheet_uuid)
        message = "uuid starting with '%s'" % (worksheet_spec,)
    else:
        spec_util.check_name(worksheet_spec)
        worksheets = model.batch_get_worksheets(fetch_items=False, name=worksheet_spec,
                                                base_worksheet_uuid=base_worksheet_uuid)
        message = "name '%s'" % (worksheet_spec,)

    if not worksheets:
        raise NotFoundError('No worksheet found with %s' % (message,))
    if len(worksheets) > 1:
        raise UsageError(
          'Found multiple worksheets with %s:%s' %
          (message, ''.join('\n  %s' % (worksheet,) for worksheet in worksheets))
        )

    return worksheets[0].uuid
Esempio n. 5
0
 def validate(self):
     '''
     Check a number of basic conditions that would indicate serious errors if
     they do not hold. Right now, validation only checks this worksheet's uuid
     and its name.
     '''
     spec_util.check_uuid(self.uuid)
     spec_util.check_name(self.name)
Esempio n. 6
0
 def validate(self):
     super(NamedBundle, self).validate()
     bundle_type = self.bundle_type.title()
     if not self.metadata.name:
         raise UsageError('%ss must have non-empty names' % (bundle_type,))
     spec_util.check_name(self.metadata.name)
     if not self.metadata.description:
         raise UsageError('%ss must have non-empty descriptions' % (bundle_type,))
Esempio n. 7
0
 def validate(self):
     '''
     Check a number of basic conditions that would indicate serious errors if
     they do not hold. Right now, validation only checks this worksheet's uuid
     and its name.
     '''
     spec_util.check_uuid(self.uuid)
     spec_util.check_name(self.name)
Esempio n. 8
0
 def validate(self):
     '''
     Check a number of basic conditions that would indicate serious errors if
     they do not hold. Right now, validation only checks this worksheet's uuid
     and its name.
     '''
     spec_util.check_uuid(self.uuid)
     spec_util.check_name(self.name)
     precondition(isinstance(self.owner_id, basestring), 'Invalid value: owner_id.')
     precondition(isinstance(self.user_defined, bool), 'Invalid value: user_defined.')
Esempio n. 9
0
 def validate(self):
     '''
     Check a number of basic conditions that would indicate serious errors if
     they do not hold. Right now, validation only checks this worksheet's uuid
     and its name.
     '''
     spec_util.check_uuid(self.uuid)
     spec_util.check_name(self.name)
     precondition(isinstance(self.owner_id, basestring),
                  'Invalid value: owner_id.')
     precondition(isinstance(self.user_defined, bool),
                  'Invalid value: user_defined.')
Esempio n. 10
0
def get_spec_uuid(model, bundle_spec):
    """
    Resolve a string bundle_spec to a unique bundle uuid.
    """
    if not bundle_spec:
        raise UsageError("Tried to expand empty bundle_spec!")
    if spec_util.UUID_REGEX.match(bundle_spec):
        return bundle_spec
    elif spec_util.UUID_PREFIX_REGEX.match(bundle_spec):
        bundles = model.batch_get_bundles(uuid=LikeQuery(bundle_spec + "%"))
        message = "uuid starting with '%s'" % (bundle_spec,)
    else:
        spec_util.check_name(bundle_spec)
        bundles = model.search_bundles(name=bundle_spec)
        message = "name '%s'" % (bundle_spec,)
    if not bundles:
        raise UsageError("No bundle found with %s" % (message,))
    elif len(bundles) > 1:
        raise UsageError(
            "Found multiple bundles with %s:%s" % (message, "".join("\n  %s" % (bundle,) for bundle in bundles))
        )
    return bundles[0].uuid
Esempio n. 11
0
def get_worksheet_uuid(model, worksheet_spec):
    '''
    Resolve a string worksheet_spec to a unique worksheet uuid.
    '''
    if not worksheet_spec:
        raise UsageError('Tried to expand empty worksheet_spec!')
    if spec_util.UUID_REGEX.match(worksheet_spec):
        return worksheet_spec
    elif spec_util.UUID_PREFIX_REGEX.match(worksheet_spec):
        worksheets = model.batch_get_worksheets(uuid=LikeQuery(worksheet_spec + '%'))
        message = "uuid starting with '%s'" % (worksheet_spec,)
    else:
        spec_util.check_name(worksheet_spec)
        worksheets = model.batch_get_worksheets(name=worksheet_spec)
        message = "name '%s'" % (worksheet_spec,)
    if not worksheets:
        raise UsageError('No worksheet found with %s' % (message,))
    elif len(worksheets) > 1:
        raise UsageError(
          'Found multiple worksheets with %s:%s' %
          (message, ''.join('\n  %s' % (worksheet,) for worksheet in worksheets))
        )
    return worksheets[0].uuid
Esempio n. 12
0
 def validate(self):
     super(NamedBundle, self).validate()
     bundle_type = self.bundle_type.title()
     if not self.metadata.name:
         raise UsageError('%ss must have non-empty names' % (bundle_type,))
     spec_util.check_name(self.metadata.name)