示例#1
0
 def create_context(
         loader_source='aria.parser.loading.DefaultLoaderSource',
         reader_source='aria.parser.reading.DefaultReaderSource',
         presenter_source='aria.parser.presentation.DefaultPresenterSource',
         presenter=None,
         debug=False,
         cache=True,
         import_profile=None,
         adhoc_inputs=None,
         plugin_dir=None,
         validate_normative=None):
     context = ConsumptionContext()
     context.loading.loader_source = import_fullname(loader_source)()
     context.reading.reader_source = import_fullname(reader_source)()
     context.presentation.presenter_source = import_fullname(
         presenter_source)()
     context.presentation.presenter_class = import_fullname(presenter)
     context.presentation.threads = 1  # tests already run in maximum thread density
     context.presentation.cache = cache
     if import_profile is not None:
         context.presentation.configuration[
             'tosca.import_profile'] = import_profile
     if adhoc_inputs is not None:
         context.presentation.configuration[
             'tosca.adhoc_inputs'] = adhoc_inputs
     if validate_normative is not None:
         context.presentation.configuration[
             'validate_normative'] = validate_normative
     if plugin_dir:
         context.loading.prefixes = collections.StrictList([plugin_dir])
     context.presentation.print_exceptions = debug
     return context
示例#2
0
def create_context(
        uri,
        loader_source='aria.parser.loading.DefaultLoaderSource',
        reader_source='aria.parser.reading.DefaultReaderSource',
        presenter_source='aria.parser.presentation.DefaultPresenterSource',
        presenter=None,
        debug=False):
    context = ConsumptionContext()
    context.loading.loader_source = import_fullname(loader_source)()
    context.reading.reader_source = import_fullname(reader_source)()
    context.presentation.location = UriLocation(uri) if isinstance(
        uri, basestring) else uri
    context.presentation.presenter_source = import_fullname(presenter_source)()
    context.presentation.presenter_class = import_fullname(presenter)
    context.presentation.print_exceptions = debug
    return context
示例#3
0
def coerce_value(context,
                 presentation,
                 the_type,
                 entry_schema,
                 constraints,
                 value,
                 aspect=None):  # pylint: disable=too-many-return-statements
    """
    Returns the value after it's coerced to its type, reporting validation errors if it cannot be
    coerced.

    Supports both complex data types and primitives.

    Data types can use the ``coerce_value`` extension to hook their own specialized function.
    If the extension is present, we will delegate to that hook.
    """

    # TODO: should support models as well as presentations

    is_function, func = get_function(context, presentation, value)
    if is_function:
        return func

    if the_type is None:
        return value

    if the_type == None.__class__:
        if value is not None:
            context.validation.report(
                'field "%s" is of type "null" but has a non-null value: %s' %
                (presentation._name, safe_repr(value)),
                locator=presentation._locator,
                level=Issue.BETWEEN_FIELDS)
            return None

    # Delegate to 'coerce_value' extension
    if hasattr(the_type, '_get_extension'):
        coerce_value_fn_name = the_type._get_extension('coerce_value')
        if coerce_value_fn_name is not None:
            if value is None:
                return None
            coerce_value_fn = import_fullname(coerce_value_fn_name)
            return coerce_value_fn(context, presentation, the_type,
                                   entry_schema, constraints, value, aspect)

    if hasattr(the_type, '_coerce_value'):
        # Delegate to '_coerce_value' (likely a DataType instance)
        return the_type._coerce_value(context, presentation, entry_schema,
                                      constraints, value, aspect)

    # Coerce to primitive type
    return coerce_to_primitive(context, presentation, the_type, constraints,
                               value, aspect)
示例#4
0
def create_consumer(context, consumer_class_name):
    consumer = ConsumerChain(context, (Read, Validate))
    dumper = None
    if consumer_class_name == 'validate':
        dumper = None
    elif consumer_class_name == 'presentation':
        dumper = consumer.consumers[0]
    elif consumer_class_name == 'template':
        consumer.append(ServiceTemplate)
    elif consumer_class_name == 'types':
        consumer.append(ServiceTemplate, Types)
    elif consumer_class_name == 'instance':
        consumer.append(ServiceTemplate, Inputs, ServiceInstance)
    else:
        consumer.append(ServiceTemplate, Inputs, ServiceInstance)
        consumer.append(import_fullname(consumer_class_name))

    if dumper is None:
        # Default to last consumer
        dumper = consumer.consumers[-1]

    return consumer, dumper