Example #1
0
    def _process_relationships(self, fields):
        """Create any relationship if needed.

        :param dict fields: fields to be processed

        For each field that is a relationship or a list of relationships,
        instantiate those relationships and update the fields.

        Does not return anything, modify fields in place.
        """

        # For dictionaries, iterate over key, value and for lists iterate over
        # index, item
        if hasattr(fields, 'items'):
            field_iterator = _compat.iteritems(fields)
        else:
            field_iterator = enumerate(fields)

        for name, value in field_iterator:
            # One to one relationship
            if isinstance(value, RelationshipToken):
                fields[name] = self.get_relationship(value)

            # One to many relationship
            elif isinstance(value, (tuple, list)):
                for i, nested_value in enumerate(value):
                    if isinstance(nested_value, RelationshipToken):
                        fields[name][i] = (self.get_relationship(nested_value))
Example #2
0
    def extract_relationships(self):
        """Return all dependencies.

        :rtype generator:

        Yields ``(depends_on, attr_name)``.

        """

        # TODO: make this DRYer since it's mostly copied from
        # _process_relationships

        for dep in self.depend_on:
            yield dep, None

        # For dictionaries, iterate over key, value and for lists iterate over
        # index, item
        if hasattr(self.fields, 'items'):
            field_iterator = _compat.iteritems(self.fields)
        else:
            field_iterator = enumerate(self.fields)

        for name, value in field_iterator:
            # One to one relationship
            if isinstance(value, RelationshipToken):
                yield self.extract_rel_name(value)

            # One to many relationship
            elif isinstance(value, (tuple, list)):
                for i, nested_value in enumerate(value):
                    if isinstance(nested_value, RelationshipToken):
                        yield self.extract_rel_name(nested_value)
Example #3
0
def safe_iteritems(items):
    """Safely iterate over a dict or a list."""
    # For dictionaries, iterate over key, value and for lists iterate over
    # index, item
    if hasattr(items, 'items'):
        return _compat.iteritems(items)
    else:
        return enumerate(items)
Example #4
0
def safe_iteritems(items):
    """Safely iterate over a dict or a list."""
    # For dictionaries, iterate over key, value and for lists iterate over
    # index, item
    if hasattr(items, 'items'):
        return _compat.iteritems(items)
    else:
        return enumerate(items)
    def _load_fixtures(self, filenames, models_package=''):
        """Pre-load the fixtures.

        :param list or str filenames: files that hold the fixture data
        :param str models_package:
        """
        if isinstance(filenames, _compat.string_types):
            globbed_filenames = glob(filenames)
        else:
            globbed_filenames = list(
                chain.from_iterable(glob(f) for f in filenames)
            )

        if not globbed_filenames:
            raise IOError('File "%s" not found' % filenames)

        if len(globbed_filenames) == 1:
            content = load_file(filenames, self.use_unicode)
        else:
            content = {}

            for filename in globbed_filenames:
                namespace = self._get_namespace_from_filename(filename)
                content[namespace] = {
                    "objects": load_file(filename, self.use_unicode)
                }

        if content:
            for k, v in _compat.iteritems(content):

                if "objects" in v:
                    # It's a collection of fictures.
                    collection = self._handle_collection(
                        namespace=k,
                        definition=v,
                        objects=v["objects"],
                        models_package=models_package,
                    )
                    self.collection.add(k, collection)

                # Named fixtures
                else:
                    if "id" in v:
                        # Renaming id because it's a Python builtin function
                        v["id_"] = v["id"]
                        del v["id"]

                    fixture = Fixture(
                        key=k,
                        fixture_manager=self,
                        models_package=models_package,
                        **v)
                    self.collection.add(k, fixture)

        graph = self._check_cycle(self.collection)
        return graph
Example #6
0
    def _load_fixtures(self, filenames, models_package=''):
        """Pre-load the fixtures.

        :param list or str filenames: files that hold the fixture data
        :param str models_package:
        """
        if isinstance(filenames, _compat.string_types):
            globbed_filenames = glob(filenames)
        else:
            globbed_filenames = list(
                chain.from_iterable(glob(f) for f in filenames)
            )

        if not globbed_filenames:
            raise IOError('File "%s" not found' % filenames)

        if len(globbed_filenames) == 1:
            content = load_file(globbed_filenames[0], self.use_unicode)
        else:
            content = {}

            for filename in globbed_filenames:
                namespace = self._get_namespace_from_filename(filename)
                content[namespace] = {
                    "objects": load_file(filename, self.use_unicode)
                }

        if content:
            for k, v in _compat.iteritems(content):

                if "objects" in v:
                    # It's a collection of fictures.
                    collection = self._handle_collection(
                        namespace=k,
                        definition=v,
                        objects=v["objects"],
                        models_package=models_package,
                    )
                    self.collection.add(k, collection)

                # Named fixtures
                else:
                    if "id" in v:
                        # Renaming id because it's a Python builtin function
                        v["id_"] = v["id"]
                        del v["id"]

                    fixture = Fixture(
                        key=k,
                        fixture_manager=self,
                        models_package=models_package,
                        **v)
                    self.collection.add(k, fixture)

        graph = self._check_cycle(self.collection)
        return graph
Example #7
0
    def _load_fixtures(self, filenames):
        """Pre-load the fixtures.

        :param list or str filenames: files that hold the fixture data
        """

        if isinstance(filenames, _compat.string_types):
            globbed_filenames = glob(filenames)
        else:
            globbed_filenames = list(
                chain.from_iterable(glob(f) for f in filenames)
            )

        if len(globbed_filenames) == 1:
            content = load_file(filenames, self.use_unicode)
        else:
            content = {}

            for filename in globbed_filenames:
                namespace = self._get_namespace_from_filename(filename)
                content[namespace] = {
                    "objects": load_file(filename, self.use_unicode)
                }

        fixtures = {}
        for k, v in _compat.iteritems(content):

            if "objects" in v:
                # It's a collection of fictures.
                fixtures[k] = self._handle_collection(
                    namespace=k,
                    definition=v,
                    objects=v["objects"],
                )

            # Named fixtures
            else:
                if "id" in v:
                    # Renaming id because it's a Python builtin function
                    v["id_"] = v["id"]
                    del v["id"]

                fixtures[k] = Fixture(key=k, fixture_manager=self, **v)

        d = DepGraph()
        for fixture in fixtures.values():
            for dependency, _ in fixture.extract_relationships():
                d.add_edge(dependency, fixture.key)

        # This does nothing except raise an error if there's a cycle
        d.topo_sort()
        return fixtures, d
Example #8
0
def deep_update(source, overrides):
    """Update a nested dictionary or similar mapping.

    Modify ``source`` in place.
    """
    # http://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth  # noqa
    for key, value in _compat.iteritems(overrides):
        if isinstance(value, collections.Mapping) and value:
            returned = deep_update(source.get(key, {}), value)
            source[key] = returned
        else:
            source[key] = overrides[key]
    return source
Example #9
0
def deep_update(source, overrides):
    """Update a nested dictionary or similar mapping.

    Modify ``source`` in place.
    """
    # http://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth  # noqa
    for key, value in _compat.iteritems(overrides):
        if isinstance(value, collections.Mapping) and value:
            returned = deep_update(source.get(key, {}), value)
            source[key] = returned
        else:
            source[key] = overrides[key]
    return source
Example #10
0
    def _process_relationships(self, fields):
        """Create any relationship if needed.

        :param dict fields: fields to be processed

        For each field that is a relationship or a list of relationships,
        instantiate those relationships and update the fields.

        Does not return anything, modify fields in place.
        """
        # For dictionaries, iterate over key, value and for lists iterate over
        # index, item
        if hasattr(fields, 'items'):
            field_iterator = _compat.iteritems(fields)
        else:
            field_iterator = enumerate(fields)

        for name, value in field_iterator:
            fields[name] = self._process_field_relationships(value)
Example #11
0
    def _process_relationships(self, fields):
        """Create any relationship if needed.

        :param dict fields: fields to be processed

        For each field that is a relationship or a list of relationships,
        instantiate those relationships and update the fields.

        Does not return anything, modify fields in place.
        """
        # For dictionaries, iterate over key, value and for lists iterate over
        # index, item
        if hasattr(fields, 'items'):
            field_iterator = _compat.iteritems(fields)
        else:
            field_iterator = enumerate(fields)

        for name, value in field_iterator:
            fields[name] = self._process_field_relationships(value)
Example #12
0
def _sorted_iteritems(dct):
    """Iterate over the items in the dict in a deterministic fashion."""
    for k, v in sorted(_compat.iteritems(dct)):
        yield k, v
Example #13
0
def _sorted_iteritems(dct):
    """Iterate over the items in the dict in a deterministic fashion."""
    for k, v in sorted(_compat.iteritems(dct)):
        yield k, v