'Get the custom environment specified for the container and infer its
links.
It would be nice to do these two things separately but links are only
represented in the container\'s environment so both steps involve
inspecting the environment.
:param flocker.node._docker.Unit container: The container to inspect.
:return: A two-tuple of the container\'s links and environment.  Links
are given as a ``list`` of ``Link`` instances.  Environment is
given as a ``list`` of two-tuples giving the environment variable
name and value (as ``bytes``).'

def _environment_for_container(self, container):

    links = []
    environment = []
    if container.environment:
        environment_dict = container.environment.to_dict()
        for (label, value) in environment_dict.items():
            parts = label.rsplit('_', 4)
            try:
                (alias, pad_a, port, pad_b, pad_c) = parts
                local_port = int(port)
            except ValueError:
                parts = label.rsplit('_', 3)
                try:
                    (alias, pad_a, port, pad_b) = parts
                except ValueError:
                    environment.append((label, value))
                    continue
                if (not ((pad_a, pad_b) == ('PORT', 'TCP'))):
                    environment.append((label, value))
                continue
            if ((pad_a, pad_b, pad_c) == ('PORT', 'TCP', 'PORT')):
                links.append(Link(local_port=local_port, remote_port=int(value), alias=alias))
    return (links, environment)
