Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(6)  |  Call(5)  |  Derive(0)  |  Import(1)
Recover the 'top-level' references the given template requires.

:para template: This is a dict / templatewhich provides the items() method.

This should returning a list of (key, value) pairs.

Aliases are not resolved. This is purley the references mentioned in the
dict structure.

This does not scan internal / external references the template may provides.(more...)

        def what_is_required(template):
    """Recover the 'top-level' references the given template requires.

    :para template: This is a dict / templatewhich provides the items() method.

    This should returning a list of (key, value) pairs.

    Aliases are not resolved. This is purley the references mentioned in the
    dict structure.

    This does not scan internal / external references the template may provides.
    It only goes through the dict values checking for ref-attr or allinc. If the
    value found is a list, it will recurse look through it too.

    :returns: This is a dict whose keys are the references found.

    For example:

    .. code-block:: python

        >>> from boaconstructor import Template
        >>> from boaconstructor.utils import what_is_required
        >>>
        >>> test2 = Template(
        ...     'test2',
        ...     dict(
        ...         x='derivefrom.[trucks]',
        ...         items=[
        ...             dict(abc='derivefrom.[cars]'),
        ...         ],
        ...         host='test1.*',
        ...         stuff=[
        ...             'com.$.keep',
        ...             ["frank.*",]
        ...         ]
        ...     ),
        ... )
        >>>
        >>> result = what_is_required(test2)
        >>>
        >>> print result
        {'test1': 1, 'cars': 1, 'com': 1, 'trucks': 1, 'frank': 1}

    """
    required = {}

    def list_recurse(items):
        for item in items:
            result = parse_value(item)
            if result['found'] == 'refatt':
                required[result['reference']] = 1

            elif result['found'] == 'all':
                required[result['allfrom']] = 1

            elif result['found'] == 'derivefrom':
                required[result['derivefrom']] = 1

            else:
                if hasattr(item, '__iter__') and not hasattr(item, 'items'):
                    list_recurse(item)

                elif hasattr(item, 'items'):
                    # We need to render this dict like item:
                    #print "A. Here: <%s>" % pprint.pformat(item)
                    list_recurse(item.items())

    for top_level_ref, attr_or_ref in template.items():
        result = parse_value(attr_or_ref)

        if result['found'] == 'refatt':
            required[result['reference']] = 1

        elif result['found'] == 'all':
            required[result['allfrom']] = 1

        elif result['found'] == 'derivefrom':
            required[result['derivefrom']] = 1

        else:
            the_type = type(attr_or_ref)

            # Is this an iterable? If so we need to check each entry to
            # see if its a ref-attr or all-inc.
            if hasattr(attr_or_ref, '__iter__') and not hasattr(attr_or_ref, 'items'):
                list_recurse(attr_or_ref)

            elif hasattr(attr_or_ref, 'items'):
                # We need to render this dict like item:
                #print "B. Here: <%s>" % pprint.pformat(attr_or_ref)
                list_recurse(attr_or_ref.items())

    return required
        


src/b/o/boaconstructor-0.3.0/lib/boaconstructor/tests/testboaconstructor.py   boaconstructor(Download)
from boaconstructor import utils
from boaconstructor import Template
from boaconstructor.utils import what_is_required
 
 
        correct = {'common':1, 'something':1, 'bob': 1}
 
        result = what_is_required(test1)
 
        err_msg = """result != correct
        correct = {'test1':1, 'com':1, "frank":1}
 
        result = what_is_required(test2)
 
        err_msg = """result != correct
        correct = {'test1':1, 'com':1, "frank":1}
 
        result = what_is_required(test2)
 
        err_msg = """result != correct
        correct = {'test1':1, 'com':1, "frank":1}
 
        result = what_is_required(test2)
 
        err_msg = """result != correct