Пример #1
0
def parse_metadata_form(bundle_subclass, form_result):
    '''
    Parse the result of a form template produced out request_missing_metadata.
    '''
    metadata_specs = bundle_subclass.get_user_defined_metadata()
    metadata_types = {spec.key: spec.type for spec in metadata_specs}
    result = {}
    for line in form_result:
        line = line.strip()
        if line != '' and not line.startswith('//'):
            if ':' not in line:
                # TODO: don't delete everything; go back to the editor and show the error message
                raise UsageError('Malformatted line (no colon): %s' % (line, ))
            (metadata_key, remainder) = line.split(':', 1)
            remainder = remainder.strip()
            if remainder == '':
                remainder = None

            if metadata_key not in metadata_types:
                raise UsageError('Unexpected metadata key: %s' %
                                 (metadata_key, ))
            metadata_type = metadata_types[metadata_key]

            if metadata_type == list:
                remainders = remainder.split() if remainder else []
                if any(unicode_util.contains_unicode(r) for r in remainders):
                    raise UsageError(
                        'Metadata cannot contain unicode: %s = %s' %
                        (metadata_key, remainder))
                result[metadata_key] = remainders
            elif metadata_type == str:
                if remainder is not None and unicode_util.contains_unicode(
                        remainder):
                    raise UsageError(
                        'Metadata cannot contain unicode: %s = %s' %
                        (metadata_key, remainder))
                result[metadata_key] = remainder
            else:
                try:
                    result[metadata_key] = metadata_type(
                        remainder) if remainder != None else None
                except:
                    raise UsageError('Invalid value %s for type %s' %
                                     (remainder, metadata_type))
    if 'name' not in result:
        raise UsageError('No name specified; aborting')
    return result
Пример #2
0
def validate_ascii(value):
    if isinstance(value, str):
        if contains_unicode(value):
            raise ValidationError('Unsupported character detected, use ascii characters')
    elif isinstance(value, list):
        for v in value:
            validate_ascii(v)
    elif isinstance(value, dict):
        for v in value.itervalues():
            validate_ascii(v)
Пример #3
0
def parse_metadata_form(bundle_subclass, form_result):
    '''
    Parse the result of a form template produced out request_missing_metadata.
    '''
    metadata_specs = bundle_subclass.get_user_defined_metadata()
    metadata_types = {spec.key: spec.type for spec in metadata_specs}
    result = {}
    for line in form_result:
        line = line.strip().encode('UTF-8')
        if line != '' and not line.startswith('//'):
            if ':' not in line:
                # TODO: don't delete everything; go back to the editor and show the error message
                raise UsageError('Malformatted line (no colon): %s' % (line,))
            (metadata_key, remainder) = line.split(':', 1)
            remainder = remainder.strip()
            if remainder == '':
                remainder = None

            if metadata_key not in metadata_types:
                raise UsageError('Unexpected metadata key: %s' % (metadata_key,))
            metadata_type = metadata_types[metadata_key]
            if metadata_type == list:
                result[metadata_key] = remainder.split() if remainder else []
                if any(unicode_util.contains_unicode(v) for v in result[metadata_key]):
                    raise UsageError(
                        'Metadata cannot contain unicode: %s = %s'
                        % (metadata_key, result[metadata_key])
                    )
            elif metadata_type == basestring:
                if remainder is not None and unicode_util.contains_unicode(remainder):
                    raise UsageError(
                        'Metadata cannot contain unicode: %s = %s' % (metadata_key, remainder)
                    )
                result[metadata_key] = remainder
            else:
                try:
                    result[metadata_key] = metadata_type(remainder) if remainder != None else None
                except:
                    raise UsageError('Invalid value %s for type %s' % (remainder, metadata_type))
    if 'name' not in result:
        raise UsageError('No name specified; aborting')
    return result
Пример #4
0
def fill_missing_metadata(bundle_subclass, args, initial_metadata):
    '''
    Return metadata for bundles by filling in the missing metadata with default values.
    args: Namespace object created from attributes parsed out of the command line. See
        `argparse` for more information
    '''
    # Fill in default values for all unsupplied metadata keys.
    new_initial_metadata = {}
    for spec in bundle_subclass.get_user_defined_metadata():
        new_initial_metadata[spec.key] = initial_metadata.get(spec.key)
        if not new_initial_metadata[spec.key]:
            default = MetadataDefaults.get_default(spec, bundle_subclass, args)
            new_initial_metadata[spec.key] = default
        final_value = new_initial_metadata[spec.key]
        is_unicode_string = isinstance(
            final_value, str) and unicode_util.contains_unicode(final_value)
        is_unicode_list = isinstance(final_value, list) and any(
            unicode_util.contains_unicode(v) for v in final_value)
        if is_unicode_string or is_unicode_list:
            raise UsageError('Metadata cannot contain unicode: %s = %s' %
                             (spec.key, final_value))

    return new_initial_metadata
Пример #5
0
def fill_missing_metadata(bundle_subclass, args, initial_metadata):
    '''
    Return metadata for bundles by filling in the missing metadata with default values.
    args: Namespace object created from attributes parsed out of the command line. See
        `argparse` for more information
    '''
    # Fill in default values for all unsupplied metadata keys.
    new_initial_metadata = {}
    for spec in bundle_subclass.get_user_defined_metadata():
        new_initial_metadata[spec.key] = initial_metadata.get(spec.key)
        if not new_initial_metadata[spec.key]:
            default = MetadataDefaults.get_default(spec, bundle_subclass, args)
            new_initial_metadata[spec.key] = default
        final_value = new_initial_metadata[spec.key]
        is_unicode_string = isinstance(final_value, basestring) and unicode_util.contains_unicode(
            final_value
        )
        is_unicode_list = isinstance(final_value, list) and any(
            unicode_util.contains_unicode(v) for v in final_value
        )
        if is_unicode_string or is_unicode_list:
            raise UsageError('Metadata cannot contain unicode: %s = %s' % (spec.key, final_value))

    return new_initial_metadata