<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def __parse_organizations(self, stream):
 """Parse GrimoireLab organizations. The GrimoireLab organizations format is a YAML element stored under the "organizations" key. The next example shows the structure of the document: - organizations: Bitergia: - bitergia.com - support.bitergia.com - biterg.io LibreSoft: - libresoft.es :param json: YAML object to parse :raises InvalidFormatError: raised when the format of the YAML is not valid. """

        if not stream:
            return

        yaml_file = self.__load_yml(stream)

        try:
            for element in yaml_file:
                name = self.__encode(element['organization'])

                if not name:
                    error = "Empty organization name"
                    msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error}
                    raise InvalidFormatError(cause=msg)

                o = Organization(name=name)

                if 'domains' in element:
                    if not isinstance(element['domains'], list):
                        error = "List of elements expected for organization %s" % name
                        msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error}
                        raise InvalidFormatError(cause=msg)
                    for dom in element['domains']:
                        if dom:
                            d = Domain(domain=dom, is_top_domain=False)
                            o.domains.append(d)
                        else:
                            error = "Empty domain name for organization %s" % name
                            msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error}
                            raise InvalidFormatError(cause=msg)

                self._organizations[name] = o
        except KeyError as e:
            error = "Attribute %s not found" % e.args
            msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error}
            raise InvalidFormatError(cause=msg)
        except TypeError as e:
            error = "%s" % e.args
            msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error}
            raise InvalidFormatError(cause=msg)