def get_bundle_uuid(model, user_id, worksheet_uuid, bundle_spec): """ Resolve a string bundle_spec to a bundle uuid. Types of specifications: - uuid: should be unique. - name[^[<index>]: there might be many uuids with this name. - ^[<index>], where index is the i-th (1-based) most recent element on the current worksheet. """ 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): bundle_uuids = model.get_bundle_uuids({'uuid': LikeQuery(bundle_spec + '%'), 'user_id': user_id}, max_results=2) if len(bundle_uuids) == 0: raise NotFoundError('uuid prefix %s doesn\'t match any bundles' % bundle_spec) elif len(bundle_uuids) == 1: return bundle_uuids[0] else: raise UsageError('uuid prefix %s more than one bundle' % bundle_spec) else: bundle_spec, reverse_index = _parse_relative_bundle_spec(bundle_spec) if bundle_spec: bundle_spec = bundle_spec.replace('.*', '%') # Convert regular expression syntax to SQL syntax if '%' in bundle_spec: bundle_spec_query = LikeQuery(bundle_spec) else: bundle_spec_query = bundle_spec else: bundle_spec_query = None # query results are ordered from newest to old bundle_uuids = model.get_bundle_uuids({ 'name': bundle_spec_query, 'worksheet_uuid': worksheet_uuid, 'user_id': user_id }, max_results=reverse_index) # Take the last bundle if reverse_index <= 0 or reverse_index > len(bundle_uuids): if bundle_spec is None: raise UsageError('%d bundles, index %d out of bounds' % (len(bundle_uuids), reverse_index)) elif len(bundle_uuids) == 0: raise NotFoundError('bundle spec %s doesn\'t match any bundles' % bundle_spec) else: raise UsageError('bundle spec %s matches %d bundles, index %d out of bounds' % (bundle_spec, len(bundle_uuids), reverse_index)) return bundle_uuids[reverse_index - 1]
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]
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